Skip to content
dmonsch edited this page Nov 10, 2019 · 4 revisions

Using the provided API for Java

The use of the offered API implementation is straightforward and prevents potential pitfalls. For example, it provides methods to prepare the PCM models for the simulation (building of the transitive closure and conversion of absolute paths to relative ones). Otherwise these tasks have to be performed manually, or you have to make sure in advance that the models are also valid on a different system. To be able to use the API for Java, you simply have to integrate the JAR (which is provided under Releases) into your application. Be sure to choose the correct version for your use case ("*-with-dependencies.jar" if you do not have any PCM related library included in your application, "*-slim.jar" if your application already consists of the PCM libraries).

Structure for using the API

Step 1 - Create the client

PCMHeadlessClient client = new PCMHeadlessClient("http://127.0.0.1:8080/");

Step 2 - Check if the backend is reachable

if (client.isReachable(3000)) {
    ...
}

3000 sets a timeout of 3 seconds in this case

Step 3 - Prepare the models

File allocationFile = new File("*.allocation");
File monitorRepositoryFile = new File("*.monitorrepository");
File repositoryFile = new File("*.repository");
File resourceEnvironmentFile = new File("*.resourceenvironment");
File systemFile = new File("*.system");
File usageFile = new File("*.usagemodel");

Step 4 - Prepare the simulation & set the models

SimulationClient sim = client.prepareSimulation();
sim.setAllocation(allocationFile);
sim.setRepository(repositoryFile);
sim.setSystem(systemFile);
sim.setUsageModel(usageFile);
sim.setResourceEnvironment(resourceEnvironmentFile);
sim.setMonitorRepository(monitorRepositoryFile);

Step 5 - Set the configuration for the simulation

sim.setSimulationConfig(HeadlessSimulationConfig.builder().type(ESimulationType.SIMULIZAR)
    .repetitions(10).experimentName("Simulation Name").build());

Step 6 - Create the transitive closure and push the data to the backend

sim.createTransitiveClosure(); // resolves all models that are referenced
sim.sync(); // with this call all parts needed for the simulation are transferred to the backend

Step 7 - Execute the simulation and use the results

sim.executeSimulation(res -> {
    // do something with the results
});

Usage of the MonitorRepositoryBuilderUtil

This class is often pretty helpful and allows you to quickly create a monitor repository.

MonitorRepositoryBuilderUtil monitorBuilder = new MonitorRepositoryBuilderUtil(repository, system, usage);
monitorBuilder.monitorExternalCalls().monitorUsageScenarios();
monitorBuilder.saveToFile(monitorRepositoryFile, new File("*"));

Example 1 - CoCoME

PCMHeadlessClient client = new PCMHeadlessClient("http://127.0.0.1:8080/");
if (client.isReachable(3000)) {
	System.out.println("Backend erreichbar.");

	File allocationFile = new File("examples/cocome/cocome.allocation");
	File monitorRepositoryFile = new File("examples/cocome/cocome_gen.monitorrepository");
	File repositoryFile = new File("examples/cocome/cocome.repository");
	File resourceEnvironmentFile = new File("examples/cocome/cocome.resourceenvironment");
	File systemFile = new File("examples/cocome/cocome.system");
	File usageFile = new File("examples/cocome/cocome.usagemodel");

	MonitorRepositoryBuilderUtil monitorBuilder = new MonitorRepositoryBuilderUtil(repositoryFile, systemFile,
			usageFile);
	monitorBuilder.monitorExternalCalls().monitorUsageScenarios();
	monitorBuilder.saveToFile(monitorRepositoryFile, new File("examples/cocome/cocome_gen.measuringpoint"));

	SimulationClient sim = client.prepareSimulation();
	sim.setAllocation(allocationFile);
	sim.setRepository(repositoryFile);
	sim.setSystem(systemFile);
	sim.setUsageModel(usageFile);
	sim.setResourceEnvironment(resourceEnvironmentFile);
	sim.setMonitorRepository(monitorRepositoryFile);

	sim.setSimulationConfig(HeadlessSimulationConfig.builder().type(ESimulationType.SIMULIZAR).repetitions(10)
			.experimentName("CoCoME Simulation").build());

	sim.createTransitiveClosure();
	sim.sync();

	long simStart = System.currentTimeMillis();
	boolean success = sim.executeSimulation(res -> {
		System.out.println(res.getValues().size());
		System.out.println("Simulation needed " + (System.currentTimeMillis() - simStart) + "ms");
	});
}

Example 2 - TeaStore

PCMHeadlessClient client = new PCMHeadlessClient("http://127.0.0.1:8080/");
if (client.isReachable(3000)) {
	System.out.println("Backend erreichbar.");

	File allocationFile = new File("examples/teastore/teastore.allocation");
	File repositoryFile = new File("examples/teastore/teastore.repository");
	File monitorRepositoryFile = new File("examples/teastore/teastore.monitorrepository");
	File resourceEnvironmentFile = new File("examples/teastore/teastore.resourceenvironment");
	File systemFile = new File("examples/teastore/teastore.system");
	File usageFile = new File("examples/teastore/teastore.usagemodel");

	SimulationClient sim = client.prepareSimulation();
	sim.setAllocation(allocationFile);
	sim.setRepository(repositoryFile);
	sim.setSystem(systemFile);
	sim.setUsageModel(usageFile);
	sim.setResourceEnvironment(resourceEnvironmentFile);
	sim.setMonitorRepository(monitorRepositoryFile);

	sim.setSimulationConfig(HeadlessSimulationConfig.builder().type(ESimulationType.SIMUCOM)
			.experimentName("TeaStore Simulation").maximumMeasurementCount(50000)
			.simulationTime(500000).repetitions(10).build());

	sim.createTransitiveClosure();
	sim.sync();

	long simStart = System.currentTimeMillis();
	boolean success = sim.executeSimulation(res -> {
		System.out.println(res.getValues().size());
		System.out.println("Simulation needed " + (System.currentTimeMillis() - simStart) + "ms");
	});
}

Using the REST interface directly

As an example, we implemented a small Python library that shows how to use the REST interface properly. However, it only supports a few functions and is intended more for demonstration purposes and not for use in production.

import requests

class HeadlessPCM:
    def __init__(self, url):
        self.url = url
        
    def isReachable(self):
        try:
            response = requests.get(self.url + "/rest/ping")
            return response.status_code == 200
        except:
            return False
        
    def prepareSimulation(self):
        return Simulation(self.url, requests.get(self.url + "/rest/prepare").text)
          
class Simulation:
    def __init__(self, url, id):
        self.url = url
        self.id = id
    
    def startSimulation(self):
        requests.get(self.url + "/rest/" + self.id + "/start")
        
    def setRepository(self, file):
        self.setFile("repository", file)
        
    def setSystem(self, file):
        self.setFile("system", file)
        
    def setUsageModel(self, file):
        self.setFile("usagemodel", file)
    
    def setAllocation(self, file):
        self.setFile("allocation", file)
        
    def setResourceEnvironment(self, file):
        self.setFile("resourceenv", file)
    
    def setMonitorRepository(self, file):
        self.setFile("monitor", file)
        
    def setAdditional(self, file):
        self.setFile("addit", file)
        
    def setFile(self, type, model):
        result = requests.post(self.url + "/rest/" + self.id + "/set/" + type, files = dict(file = open(model, 'rb')))

The use of this library could look like this:

client = HeadlessPCM("http://127.0.0.1:8080")
if (client.isReachable()):
    sim = client.prepareSimulation()
    
    sim.setRepository("examples/cocome/cocome.repository")
    sim.setSystem("examples/cocome/cocome.system")
    sim.setUsageModel("examples/cocome/cocome.usagemodel")
    sim.setAllocation("examples/cocome/cocome.allocation")
    sim.setResourceEnvironment("examples/cocome/cocome.resourceenvironment")
    sim.setMonitorRepository("examples/cocome/cocome_gen.monitorrepository")
    
    sim.setAdditional("examples/cocome/cocome_gen.measuringpoint")
    sim.setAdditional("examples/cocome/cocomeTypes.repository")
    
    sim.startSimulation()