

{"id":990,"date":"2013-06-18T15:32:24","date_gmt":"2013-06-18T13:32:24","guid":{"rendered":"https:\/\/project.inria.fr\/plasma-lab\/?page_id=990"},"modified":"2013-06-26T11:45:29","modified_gmt":"2013-06-26T09:45:29","slug":"api-tutorial","status":"publish","type":"page","link":"https:\/\/project.inria.fr\/plasma-lab\/api-tutorial\/","title":{"rendered":"API tutorial"},"content":{"rendered":"<p>These examples come from our terminal interface. This interface uses the <strong>PLASMA Lab API as would an external tool<\/strong>. Using the PLASMA Lab API can be resumed in <strong>2 or 3 different steps<\/strong>:<\/p>\n<ul>\n<li><strong>Initialize a model<\/strong> and 0 to n <strong>requirement<\/strong><\/li>\n<\/ul>\n<p>If we want to run a simulation<\/p>\n<ul>\n<li>Launch and listen to the <strong>simulation manager<\/strong><\/li>\n<\/ul>\n<p>If we want to run an experiment<\/p>\n<ul>\n<li><strong>Initialize an algorithm<\/strong> scheduler<\/li>\n<li>Launch and listen to the <strong>experiment manager<\/strong><\/li>\n<\/ul>\n<hr \/>\n<h3>Create a model<\/h3>\n<p>To instantiate an AbstractModel object of our model we need to retrieve the corresponding factory. This is possible by knowing the identifier returned by the getId method of the factory or the model object. For instance, to use our RML simulator, the string is <em>fr.inria.plasmalab.rml<\/em>.<\/p>\n<p>Once we have the factory, we create a model using either a File or a String containing the model.<\/p>\n<pre lang=\"java\" line=\"1\">String modelFile = args[n];\r\nn++;\r\nString modelType = args[n];\r\n\/**\r\n* We create a new model by retrieving the appropriate model factory.\r\n**\/\r\nfor(AbstractModelFactory amf:controler.getAMFList()){\r\n\tif(amf.getId().equals(modelType)){\r\n\t\tmodel = amf.createAbstractModel(modelFile, new File(modelFile));\r\n\t\tbreak;\r\n\t}\r\n}<\/pre>\n<hr \/>\n<h3>Create a requirement<\/h3>\n<p>To instantiate an AbstractRequirement object of our requirement we need to retrieve the corresponding factory. This is possible by knowing the identifier returned by the getId method of the factory or the requirement object. For instance, to use our BLTL simulator, the string is <em>fr.inria.plasmalab.bltl<\/em>.<\/p>\n<p>Once we have the factory, we create a requirement using either a File or a String containing the requirement.<\/p>\n<pre lang=\"java\" line=\"1\">String propertyFile = args[n];\r\nn++;\r\nString propertyType = args[n];\t\t\t\t\t\r\n\/**\r\n * We create a new requirement by retrieving the appropriate requirement factory.\r\n **\/\r\nfor(AbstractRequirementFactory arf:controler.getARFList()){\r\n\tif(arf.getId().equals(propertyType)){\r\n\t\tAbstractRequirement r = arf.createAbstractRequirement(args[n-1], new File(propertyFile));\r\n\t\tarl.add(r);\r\n\t\tbreak;\r\n\t}\r\n}<\/pre>\n<hr \/>\n<h3>Retrieves an algorithm factory<\/h3>\n<p>To instantiate an algorithm, we have to retrieve the corresponding factory. This is possible by knowing the identifier returned by the getId method of the factory or the scheduler\/worker objects. For instance, to use our Chernoff algorithm, the string is <em>fr.inria.plasmalab.rml<\/em>.<\/p>\n<pre lang=\"java\" line=\"1\">\/**\r\n * Retrieves the appropriate algorithm factory.\r\n *\/\r\nfor(InterfaceAlgorithmFactory aaf2:controler.getAAFList()){\r\n\tif(aaf2.getId().equals(\"fr.inria.plasmalab.chernoff\")){\r\n\t\taaf = aaf2;\r\n\t\tbreak;\r\n\t}\r\n}<\/pre>\n<hr \/>\n<h3>Launch an experiment<\/h3>\n<p>Once we have a model, a property, and an algorithm factory we can set an experiment. Using the algorithm factory we instantiate a AlgorithmScheduler. An AlgorithmScheduler is the part of our algorithm which is runned locally.<\/p>\n<p>Then we instantiate the ExperimentationManager object and give it parameters such as AlgorithmScheduler, Model, Requirements.<\/p>\n<p>If we want to run the experimentation locally we then launch a service which will connect itself to the ExperimentationManager.<\/p>\n<pre lang=\"java\" line=\"1\">\/**\r\n* Initialize scheduler\r\n*\/\r\nInterfaceAlgorithmScheduler scheduler = aaf.createScheduler(model, arl, param);\r\n\/**\r\n* Get Experimentation manager\r\n*\/\r\nExperimentationManager exp = controler.getExpManager();\r\n\/**\r\n* Listen to the experimentation manager\r\n*\/\r\nexp.addExperimentationListener(new PlasmaLabTerminal(start));\r\n\/**\r\n* Launch the experiment\r\n*\/\r\nexp.setupAnExperiment(scheduler, model, arl, 8111, new ArrayList());\r\n\/**\r\n* Instantiate and connect a service\r\n*\/\r\nservice = new Service(\"localhost\", \"8111\", \"\/\", null);\r\nservice.connect();<\/pre>\n<hr \/>\n<h3>Run a simulation<\/h3>\n<pre lang=\"java\" line=\"1\">if(steps &gt;0){\r\n\tSimulationManager sim = controler.getSimManager();\r\n\tsim.newPath(model);\r\n\tfor(int i=0; i&lt;steps; i++){\r\n\t\tsim.simulate();\r\n\t}\r\n}<\/pre>\n<hr \/>\n<h3>Listen to the running experimentaiton<\/h3>\n<p>In order to receive progress <strong>notifications<\/strong> and <strong>results<\/strong> of the experiment, we need to implement the <strong>ExperimentationListener interface<\/strong>. An instance of our implementing class will then be added as a listener to the ExperimentationManager or SimulationManager.<\/p>\n<pre lang=\"java\" line=\"1\">import fr.inria.plasmalab.workflow.listener.ExperimentationListener;\r\npublic class PlasmaLabTerminal implements ExperimentationListener{\r\n\r\n\t@Override\r\n\tpublic void notifyAlgorithmStarted(String nodeURI) {}\r\n\r\n\t@Override\r\n\tpublic void notifyAlgorithmCompleted(String nodeURI) {}\r\n\r\n\t@Override\r\n\tpublic void notifyAlgorithmError(String nodeURI, String errorMessage) {}\r\n\r\n\t@Override\r\n\tpublic void notifyNewServiceConnected(String nodeURI) {}\r\n\r\n\t@Override\r\n\tpublic void notifyServiceDisconnected(String nodeURI) {}\r\n\r\n\t@Override\r\n\tpublic void publishResults(String nodeURI, List results) {}\r\n\r\n\t@Override\r\n\tpublic void notifyProgress(int percent) {}\r\n\r\n\t@Override\r\n\tpublic void notifyTimeRemaining(long milliseconds) {}\r\n}<\/pre>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>These examples come from our terminal interface. This interface uses the PLASMA Lab API as would an external tool. Using the PLASMA Lab API can be resumed in 2 or 3 different steps: Initialize a model and 0 to n requirement If we want to run a simulation Launch and\u2026<\/p>\n<p> <a class=\"continue-reading-link\" href=\"https:\/\/project.inria.fr\/plasma-lab\/api-tutorial\/\"><span>Continue reading<\/span><i class=\"crycon-right-dir\"><\/i><\/a> <\/p>\n","protected":false},"author":234,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-990","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/pages\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/users\/234"}],"replies":[{"embeddable":true,"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/comments?post=990"}],"version-history":[{"count":28,"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/pages\/990\/revisions"}],"predecessor-version":[{"id":1030,"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/pages\/990\/revisions\/1030"}],"wp:attachment":[{"href":"https:\/\/project.inria.fr\/plasma-lab\/wp-json\/wp\/v2\/media?parent=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}