API tutorial

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 listen to the simulation manager

If we want to run an experiment

  • Initialize an algorithm scheduler
  • Launch and listen to the experiment manager

Create a model

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 fr.inria.plasmalab.rml.

Once we have the factory, we create a model using either a File or a String containing the model.

1
2
3
4
5
6
7
8
9
10
11
12
String modelFile = args[n];
n++;
String modelType = args[n];
/**
* We create a new model by retrieving the appropriate model factory.
**/
for(AbstractModelFactory amf:controler.getAMFList()){
	if(amf.getId().equals(modelType)){
		model = amf.createAbstractModel(modelFile, new File(modelFile));
		break;
	}
}

Create a requirement

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 fr.inria.plasmalab.bltl.

Once we have the factory, we create a requirement using either a File or a String containing the requirement.

1
2
3
4
5
6
7
8
9
10
11
12
13
String propertyFile = args[n];
n++;
String propertyType = args[n];					
/**
 * We create a new requirement by retrieving the appropriate requirement factory.
 **/
for(AbstractRequirementFactory arf:controler.getARFList()){
	if(arf.getId().equals(propertyType)){
		AbstractRequirement r = arf.createAbstractRequirement(args[n-1], new File(propertyFile));
		arl.add(r);
		break;
	}
}

Retrieves an algorithm factory

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 fr.inria.plasmalab.rml.

1
2
3
4
5
6
7
8
9
/**
 * Retrieves the appropriate algorithm factory.
 */
for(InterfaceAlgorithmFactory aaf2:controler.getAAFList()){
	if(aaf2.getId().equals("fr.inria.plasmalab.chernoff")){
		aaf = aaf2;
		break;
	}
}

Launch an experiment

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.

Then we instantiate the ExperimentationManager object and give it parameters such as AlgorithmScheduler, Model, Requirements.

If we want to run the experimentation locally we then launch a service which will connect itself to the ExperimentationManager.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Initialize scheduler
*/
InterfaceAlgorithmScheduler scheduler = aaf.createScheduler(model, arl, param);
/**
* Get Experimentation manager
*/
ExperimentationManager exp = controler.getExpManager();
/**
* Listen to the experimentation manager
*/
exp.addExperimentationListener(new PlasmaLabTerminal(start));
/**
* Launch the experiment
*/
exp.setupAnExperiment(scheduler, model, arl, 8111, new ArrayList());
/**
* Instantiate and connect a service
*/
service = new Service("localhost", "8111", "/", null);
service.connect();

Run a simulation

1
2
3
4
5
6
7
if(steps >0){
	SimulationManager sim = controler.getSimManager();
	sim.newPath(model);
	for(int i=0; i<steps; i++){
		sim.simulate();
	}
}

Listen to the running experimentaiton

In order to receive progress notifications and results of the experiment, we need to implement the ExperimentationListener interface. An instance of our implementing class will then be added as a listener to the ExperimentationManager or SimulationManager.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import fr.inria.plasmalab.workflow.listener.ExperimentationListener;
public class PlasmaLabTerminal implements ExperimentationListener{
 
	@Override
	public void notifyAlgorithmStarted(String nodeURI) {}
 
	@Override
	public void notifyAlgorithmCompleted(String nodeURI) {}
 
	@Override
	public void notifyAlgorithmError(String nodeURI, String errorMessage) {}
 
	@Override
	public void notifyNewServiceConnected(String nodeURI) {}
 
	@Override
	public void notifyServiceDisconnected(String nodeURI) {}
 
	@Override
	public void publishResults(String nodeURI, List results) {}
 
	@Override
	public void notifyProgress(int percent) {}
 
	@Override
	public void notifyTimeRemaining(long milliseconds) {}
}

Comments are closed.