import mecsyco.core.agent.EventMAgent; import mecsyco.core.coupling.CentralizedEventCouplingArtifact; import mecsyco.core.exception.CausalityException; import mecsyco.core.operation.time.AdditionTimeOperation; import mecsyco.core.operation.time.DivisionTimeOperation; import mecsyco.core.operation.time.ExponentTimeOperation; import mecsyco.core.operation.time.MutliplicationTimeOperation; public class LauncherWithOperations { public final static double maxSimulationTime = 10; public static void main(String args[]) { /**********************************/ /**** AGENTS & MODEL ARTEFACTS ****/ /**********************************/ // First agent with first model (model1) EventMAgent agent1 = new EventMAgent("Name1",maxSimulationTime); MyModel1Artefact ModelArtefact1 = new MyModel1Artefact(); agent1.setModelArtefact(ModelArtefact1); // Second agent with second model (model2) EventMAgent agent2 = new EventMAgent("Name2",maxSimulationTime); MyModel2Artefact ModelArtefact2 = new MyModel2Artefact(); agent2.setModelArtefact(ModelArtefact2); /****************************/ /**** COUPLING ARTEFACTS ****/ /****************************/ // Model1 Model2 // .--------------. .--------------. // | .---. .---. .---. .---. | // | | y |------| y |<-------| Y |------| Y | | // | '---' '---' '---' '---' | // | | | | // | .---. .---. .---. .---. | // | | X |------| X |------->| x |------| x | | // | '---' '---' '---' '---' | // '--------------' '--------------' // // "y" and "X" are model1's state variables (typed Double) // "Y" and "x" are model2's state variables (typed Double) // We consider that the port names correspond to the state variable with which they are linked // During the simulation, X and Y are exchanged and models states are modified, // with model1.y = model2.Y and model2.x = model1.X CentralizedEventCouplingArtifact couplingFrom1To2 = new CentralizedEventCouplingArtifact(); CentralizedEventCouplingArtifact couplingFrom2To1 = new CentralizedEventCouplingArtifact(); // Agent1 will update "y" with the value received from couplingFrom2To1 (input events) // Agent2 will update "x" with the value received from couplingFrom1To2 (input events) agent1.addInputCouplingArtifact(couplingFrom2To1, "y"); agent2.addInputCouplingArtifact(couplingFrom1To2, "x"); // Agent1 will send "X" to couplingFrom1To2 (output events) // Agent2 will send "Y" to couplingFrom2To1 (output events) agent1.addOutputCouplingArtifact(couplingFrom1To2, "X"); agent2.addOutputCouplingArtifact(couplingFrom2To1, "Y"); /*******************************/ /********* Operations **********/ /*******************************/ /* * On the link from X to x, we will put the freshly made DataOperation * We assume here that the operation do not need parameters */ //create the operation DataOperationTemplate EventOpe=new DataOperationTemplate(); //add it to the link couplingFrom1To2.addEventOperation(EventOpe); /* * On the link from Y to y, we will put the freshly made TimeOperation * (we assume here that the operation do not need parameters) * and all operation to our disposal */ //create the operations TimeOperationTemplate TimeOpe1=new TimeOperationTemplate(); AdditionTimeOperation TimeOpe2=new AdditionTimeOperation(1); DivisionTimeOperation TimeOpe3=new DivisionTimeOperation(2); ExponentTimeOperation TimeOpe4=new ExponentTimeOperation(3); MutliplicationTimeOperation TimeOpe5=new MutliplicationTimeOperation(4); //add it to the link couplingFrom2To1.addTimeOperation(TimeOpe1); couplingFrom2To1.addTimeOperation(TimeOpe2); couplingFrom2To1.addTimeOperation(TimeOpe3); couplingFrom2To1.addTimeOperation(TimeOpe4); couplingFrom2To1.addTimeOperation(TimeOpe5); couplingFrom2To1.addTimeOperation(TimeOpe1); /* * the timestamp send by Model2 will then be: * manipulate by the freshly made operation * adding 1 to the previous operation * divide the result by 2 * multiply it by 10^3 * multiply the latest result by 4 * then manipulate the final result, once again, by the freshly made operation */ /*******************************/ /**** MODELS INITIALIZATION ****/ /*******************************/ // Start the simulation softwares associated to model1 and model2 // This is not systematically necessary, it depends on the simulation software used agent1.startModelSoftware(); agent2.startModelSoftware(); // Initialize model1 and model2 parameters // e.g. time discretization or constants // This is not systematically necessary, it depends on the model String [] args_model1 = { "0.001" }; String [] args_model2 = { "0.01" }; agent1.setModelParameters(args_model1); agent2.setModelParameters(args_model2); /***************************************/ /**** CO-SIMULATION INIT & STARTING ****/ /***************************************/ try { // Co-initialization with first exchanges // This is necessary only when the model initial states are co-dependent agent1.coInitialize(); agent2.coInitialize(); // Start the co-simulation agent1.start(); agent2.start(); // This should never happen } catch (CausalityException e) { e.printStackTrace(); } } }