diff --git a/src/main/java/avatartranslator/toturtle/AVATAR2TURTLE.java b/src/main/java/avatartranslator/toturtle/AVATAR2TURTLE.java index 6c814559f5b5468567aa9bf7173da33f410ed4ed..2209c39c926970848f343ae9a9490ea376017041 100644 --- a/src/main/java/avatartranslator/toturtle/AVATAR2TURTLE.java +++ b/src/main/java/avatartranslator/toturtle/AVATAR2TURTLE.java @@ -1,61 +1,58 @@ -/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille - * - * ludovic.apvrille AT enst.fr - * - * This software is a computer program whose purpose is to allow the - * edition of TURTLE analysis, design and deployment diagrams, to - * allow the generation of RT-LOTOS or Java code from this diagram, - * and at last to allow the analysis of formal validation traces - * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP - * from INRIA Rhone-Alpes. - * - * This software is governed by the CeCILL license under French law and - * abiding by the rules of distribution of free software. You can use, - * modify and/ or redistribute the software under the terms of the CeCILL - * license as circulated by CEA, CNRS and INRIA at the following URL - * "http://www.cecill.info". - * - * As a counterpart to the access to the source code and rights to copy, - * modify and redistribute granted by the license, users are provided only - * with a limited warranty and the software's author, the holder of the - * economic rights, and the successive licensors have only limited - * liability. - * - * In this respect, the user's attention is drawn to the risks associated - * with loading, using, modifying and/or developing or reproducing the - * software by the user in light of its specific status of free software, - * that may mean that it is complicated to manipulate, and that also - * therefore means that it is reserved for developers and experienced - * professionals having in-depth computer knowledge. Users are therefore - * encouraged to load and test the software's suitability as regards their - * requirements in conditions enabling the security of their systems and/or - * data to be ensured and, more generally, to use and operate it in the - * same conditions as regards security. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL license and that you accept its terms. - */ - - - +/**Copyright or or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille +* +* ludovic.apvrille AT enst.fr +* +* This software is a computer program whose purpose is to allow the +* edition of TURTLE analysis, design and deployment diagrams, to +* allow the generation of RT-LOTOS or Java code from this diagram, +* and at last to allow the analysis of formal validation traces +* obtained from external tools, e.g. RTL from LAAS-CNRS and CADP +* from INRIA Rhone-Alpes. +* +* This software is governed by the CeCILL license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL +* license as circulated by CEA, CNRS and INRIA at the following URL +* "http://www.cecill.info". +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited +* liability. +* +* In this respect, the user's attention is drawn to the risks associated +* with loading, using, modifying and/or developing or reproducing the +* software by the user in light of its specific status of free software, +* that may mean that it is complicated to manipulate, and that also +* therefore means that it is reserved for developers and experienced +* professionals having in-depth computer knowledge. Users are therefore +* encouraged to load and test the software's suitability as regards their +* requirements in conditions enabling the security of their systems and/or +* data to be ensured and, more generally, to use and operate it in the +* same conditions as regards security. +* +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL license and that you accept its terms. +* +* /** +* Class AVATAR2TURTLE +* Creation: 21/05/2010 +* @version 1.0 21/05/2010 +* @author Ludovic APVRILLE +* @see +*/ package avatartranslator.toturtle; -import avatartranslator.AvatarBlock; -import avatartranslator.AvatarSpecification; -import translator.ActivityDiagram; -import translator.TClass; -import translator.TURTLEModeling; +import java.util.*; -import java.util.Vector; +import avatartranslator.*; +import myutil.*; +import translator.*; +import ui.*; -/** -* Class AVATAR2TURTLE -* Creation: 21/05/2010 -* @version 1.0 21/05/2010 -* @author Ludovic APVRILLE - */ public class AVATAR2TURTLE { //private static int gateId; @@ -75,7 +72,7 @@ public class AVATAR2TURTLE { public TURTLEModeling generateTURTLEModeling() { - // + //System.out.println("generate TM"); //tmlmodeling.removeAllRandomSequences(); spec.removeCompositeStates(); spec.removeLibraryFunctionCalls (); @@ -85,17 +82,17 @@ public class AVATAR2TURTLE { // Create TClasses -> same name as TML tasks //nbClass = 0; - // + //System.out.println("Tclasses"); createTClasses(); - // + //System.out.println("Channels"); /*createChannelTClasses(); - // + //System.out.println("Events"); createEventTClasses(); - // + //System.out.println("Requests"); createRequestTClasses(); - // + //System.out.println("AD of tclasses"); createADOfTClasses(); - // + //System.out.println("All done");*/ return tm; } @@ -223,9 +220,9 @@ public class AVATAR2TURTLE { for(int i=0; i<nbClass; i++) { t = tm.getTClassAtIndex(i); - // + //System.out.println("Create AD"); createADOfTClass(t, (TMLTask)(tmlmodeling.getTasks().get(i))); - // + //System.out.println("End create AD"); } } @@ -234,15 +231,15 @@ public class AVATAR2TURTLE { Vector newElements = new Vector(); // elements of AD Vector baseElements = new Vector(); // elements of basic task - // + //System.out.println("Making AD of " + tclass.getName()); translateAD(newElements, baseElements, tclass, task, task.getActivityDiagram().getFirst(), null, null); // DANGER: if task may be requested, the AD must be modified!!!! - // + //System.out.println("task requested?"); if (task.isRequested()) { setADRequested(tclass, task); } - // + //System.out.println("end task requested?"); setGatesToTask(tclass, task); } @@ -284,7 +281,7 @@ public class AVATAR2TURTLE { // START STATE - // + //System.out.println("Call to TMLE=" + tmle.toString()); try { if (tmle instanceof TMLStartState) { @@ -331,20 +328,20 @@ public class AVATAR2TURTLE { // CHOICE } else if (tmle instanceof TMLChoice) { - // + //System.out.println("TML Choice!"); tmlchoice = (TMLChoice)tmle; adchoice = new ADChoice(); newElements.add(adchoice); baseElements.add(tmle); tclass.getActivityDiagram().add(adchoice); - // + //System.out.println("Get guards nb=" + tmlchoice.getNbGuard()); //String guard = ""; if (tmlchoice.getNbGuard() !=0 ) { int index1 = tmlchoice.getElseGuard(), index2 = tmlchoice.getAfterGuard(); if (index2 != -1) { - // + //System.out.println("Managing after"); adj = new ADJunction(); adc1 = translateAD(newElements, baseElements, tclass, task, tmle.getNextElement(index2), adchoice, adj); tclass.getActivityDiagram().add(adj); @@ -353,8 +350,8 @@ public class AVATAR2TURTLE { } for(i=0; i<tmlchoice.getNbGuard(); i++) { - // - // + //System.out.println("Get guards i=" + i); + //System.out.println("ADjunc=" + adjunc); if (i==index1) { action = modifyString(tmlchoice.getValueOfElse()); } else { @@ -367,9 +364,9 @@ public class AVATAR2TURTLE { } adc1 = translateAD(newElements, baseElements, tclass, task, tmle.getNextElement(i), adchoice, adj); if (adc1 == null) { - // + //System.out.println("Null adc1"); } else { - // + //System.out.println("adc1 = " +adc1); } g = tclass.addNewGateIfApplicable("branching"); adag = new ADActionStateWithGate(g); @@ -379,7 +376,7 @@ public class AVATAR2TURTLE { adchoice.addGuard(action); adchoice.addNext(adag); } - // + //System.out.println("Return adchoice ..."); return adchoice; } else { return endOfActivity(newElements, baseElements, tclass, adjunc); @@ -510,10 +507,10 @@ public class AVATAR2TURTLE { } else if (tmle instanceof TMLForLoop) { tmlforloop = (TMLForLoop)tmle; action = modifyString(tmlforloop.getInit()); - // + //System.out.println("FOR action = " + action); parameter = null; if ((action.length() == 0) || ((parameter = paramAnalyzer(action, tclass)) != null)) { - // + //System.out.println("parameter1 ok"); if (action.length() != 0) { adacparam1 = new ADActionStateWithParam(parameter); adacparam1.setActionValue(getActionValueParam(action, tclass)); @@ -524,7 +521,7 @@ public class AVATAR2TURTLE { action = modifyString(tmlforloop.getIncrement()); parameter = null; if ((action.length() == 0) || ((parameter = paramAnalyzer(action, tclass)) != null)) { - // + //System.out.println("New loop"); if (action.length() != 0) { adacparam2 = new ADActionStateWithParam(parameter); adacparam2.setActionValue(getActionValueParam(action, tclass)); @@ -600,7 +597,7 @@ public class AVATAR2TURTLE { // TML Sequence } else if (tmle instanceof TMLSequence) { - // + //System.out.println("TML sequence !"); tmlseq = (TMLSequence)tmle; if (tmlseq.getNbNext() == 0) { @@ -760,10 +757,10 @@ public class AVATAR2TURTLE { TClass tcl = tm.getTClassWithName(nameRequest + tmlreq.getRequest().getName()); //g1 = tcl.getGateByName("sendReq"); //int index = tmlreq.getRequest().getOriginTasks().indexOf(task); - // + //System.out.println("task=" + task.getName() + " index=" + index); //g1 = (Gate)(((TClassRequest)tcl).getGatesWrite().get(index)); g1 = (Gate)(((TClassRequest)tcl).getGateWrite(task.getName())); - // + //System.out.println("task=" + task.getName() + " index=" + index + "gate=" + g.getName()); tm.addSynchroRelation(tclass, g, tcl, g1); adacparam2 = null; @@ -947,7 +944,7 @@ public class AVATAR2TURTLE { } } } catch (Exception e) { - + System.out.println("Exception in AD diagram analysis -> " + e.getMessage()); return null; } @@ -993,11 +990,11 @@ public class AVATAR2TURTLE { adag.setActionValue(action); // Search for all adcomponents which next is a stop ... Replace this next to a next to the first adjunction - // + //System.out.println("Remove all elements .."); try { tm.removeAllElement(Class.forName("translator.ADStop"), adj, tclass.getActivityDiagram()); } catch (ClassNotFoundException cnfe ) {} - // + //System.out.println("All elements removed ..."); tclass.getActivityDiagram().add(adag); tclass.getActivityDiagram().add(adj); @@ -1062,9 +1059,9 @@ public class AVATAR2TURTLE { //g1 = tcl.getGateByName("sendReq"); index = request.getOriginTasks().indexOf(task); if (index != -1) { - // + //System.out.println("task=" + task.getName() + " index=" + index); g1 = (Gate)(((TClassRequest)tcl).getGatesWrite().get(index)); - // + //System.out.println("task=" + task.getName() + " index=" + index + "gate=" + g.getName()); tm.addSynchroRelation(tclass, g, tcl, g1); } } @@ -1149,7 +1146,7 @@ public class AVATAR2TURTLE { tmla = (TMLAttribute)(iterator.next()); switch (tmla.type.getType()) { case TMLType.NATURAL: - // + //System.out.println("Adding nat attribute:" + modifyString(tmla.name)); tcl.addNewParamIfApplicable(modifyString(tmla.name), "nat", modifyString(tmla.initialValue)); break; default: diff --git a/src/main/java/dseengine/DSEMappingSimulationResults.java b/src/main/java/dseengine/DSEMappingSimulationResults.java index 0ad68db105552a73dbbf633cdf4d371a3d4cd6ae..2f9dafeb50e2d1640ed6a120ec5123d8e0f981a4 100755 --- a/src/main/java/dseengine/DSEMappingSimulationResults.java +++ b/src/main/java/dseengine/DSEMappingSimulationResults.java @@ -42,6 +42,7 @@ package dseengine; import myutil.Conversion; +import myutil.TraceManager; import tmltranslator.TMLMapping; import java.util.Vector; diff --git a/src/main/java/translator/TURTLEModeling.java b/src/main/java/translator/TURTLEModeling.java index 1345ebe0a8f050310092ef83171c77bfa360bd52..ea8584e8912578b816617c5ce21fb1dab7908e50 100755 --- a/src/main/java/translator/TURTLEModeling.java +++ b/src/main/java/translator/TURTLEModeling.java @@ -1049,7 +1049,7 @@ public class TURTLEModeling { if ((adc instanceof ADParallel) || (adc instanceof ADPreempt) || (adc instanceof ADSequence) || (adc instanceof ADJunction)) { if (adc instanceof ADParallel) { - if (debug) + } if ((adc.getNbNext() > 1) && ((adc instanceof ADParallel) || (adc instanceof ADSequence))) { @@ -1071,20 +1071,20 @@ public class TURTLEModeling { } if ((adc.getNbNext() == 1) && (ad.getNbComponentLeadingTo(adc) == 1)) { - if (debug) + adc1 = ad.getFirstComponentLeadingTo(adc); - // + if (adc1 != null) { adc1.updateNext(adc, adc.getNext(0)); - if (debug) + ad.removeElement(adc); return removeAllUselessComponent(ad, debug, specialChoices); } } else if (adc instanceof ADJunction) { // Junction looping on itself? - if (debug) + if (adc.getNext(0) == adc) { @@ -1098,9 +1098,7 @@ public class TURTLEModeling { // Two junctions are set after the other, and the number of leading components to them may fit on only one junction. adc1 = adc.getNext(0); if (adc1 instanceof ADJunction) { - if (debug) { - - } + if ((ad.getNbComponentLeadingTo(adc) + ad.getNbComponentLeadingTo(adc1)) < 5) { // The first junction is kept, and the second is removed @@ -1135,9 +1133,7 @@ public class TURTLEModeling { } - if (debug) { - - } + // Choice with the same next components and the same guard if (adc.getNbNext() > 1) { for(j=0; j<adc.getNbNext()-1; j++) { @@ -1146,14 +1142,9 @@ public class TURTLEModeling { adc2 = adc.getNext(k); if ((adc1 == adc2) && (adcc.getGuard(j).compareTo(adcc.getGuard(k)) ==0)){ - if (debug) { - - - } + adcc.removeNext(adc2); - if (debug) { - - } + return removeAllUselessComponent(ad, debug, specialChoices); } } @@ -1662,15 +1653,10 @@ public class TURTLEModeling { for(int i=0; i<tclass.size(); i++) { t = tclass.elementAt(i); - if (debug) { - - - } + removeAllUselessComponent(t.getActivityDiagram(), debug); - if (debug) { - - } + } } @@ -1679,14 +1665,9 @@ public class TURTLEModeling { for(int i=0; i<tclass.size(); i++) { t = tclass.elementAt(i); - if (debug) { - - - } + removeAllUselessComponent(t.getActivityDiagram(), debug, specialChoices); - if (debug) { - - } + } } @@ -2119,7 +2100,7 @@ public class TURTLEModeling { System.exit(0); } ad.add(adcclone); - if (debug) + if (i == (path.size()-1)) { adcclone.removeNext(ad2); @@ -2136,7 +2117,7 @@ public class TURTLEModeling { adtmp.addNext(adcclone); } } else { - if (debug) + if (path.size() > 1) { ad2.removeNext((ADComponent)(path.elementAt(1))); @@ -2144,7 +2125,7 @@ public class TURTLEModeling { ad2.removeNext((ADComponent)(path.elementAt(0))); } } - if (debug) + //ad.print(); return true;*/ diff --git a/src/main/java/translator/tojava/TURTLE2Java.java b/src/main/java/translator/tojava/TURTLE2Java.java index db5bfeb6325d734d3ee37a189013a666e742cdd9..24a28d95593fd338105ce2a77cb09f905d72b9ae 100755 --- a/src/main/java/translator/tojava/TURTLE2Java.java +++ b/src/main/java/translator/tojava/TURTLE2Java.java @@ -1,1538 +1,1575 @@ -/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille - * - * ludovic.apvrille AT enst.fr - * - * This software is a computer program whose purpose is to allow the - * edition of TURTLE analysis, design and deployment diagrams, to - * allow the generation of RT-LOTOS or Java code from this diagram, - * and at last to allow the analysis of formal validation traces - * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP - * from INRIA Rhone-Alpes. - * - * This software is governed by the CeCILL license under French law and - * abiding by the rules of distribution of free software. You can use, - * modify and/ or redistribute the software under the terms of the CeCILL - * license as circulated by CEA, CNRS and INRIA at the following URL - * "http://www.cecill.info". - * - * As a counterpart to the access to the source code and rights to copy, - * modify and redistribute granted by the license, users are provided only - * with a limited warranty and the software's author, the holder of the - * economic rights, and the successive licensors have only limited - * liability. - * - * In this respect, the user's attention is drawn to the risks associated - * with loading, using, modifying and/or developing or reproducing the - * software by the user in light of its specific status of free software, - * that may mean that it is complicated to manipulate, and that also - * therefore means that it is reserved for developers and experienced - * professionals having in-depth computer knowledge. Users are therefore - * encouraged to load and test the software's suitability as regards their - * requirements in conditions enabling the security of their systems and/or - * data to be ensured and, more generally, to use and operate it in the - * same conditions as regards security. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL license and that you accept its terms. - */ - - - - -package translator.tojava; - -import ddtranslator.TClassLinkNode; -import myutil.Conversion; -import myutil.FileException; -import translator.*; -import translator.JKeyword; - -import java.util.LinkedList; -import java.util.ListIterator; -import java.util.Vector; - -/** - * Class TURTLE2Java - * Creation: 03/03/2005 - * @version 1.0 03/03/2005 - * @author Ludovic APVRILLE - */ -public class TURTLE2Java { - - //private static int gateId; - private int idSeq = 0; - private int idPar = 0; - - private TURTLEModeling tm; - private LinkedList<JavaClass> javaClasses; - private MasterGateManager mgm; - private Vector<MainClass> mainclasses; - //private MainClass mainclass; - private Vector<ComponentId> components; - private String header; - - private long millis; - private long nanos; - - private boolean debug; - private boolean longforint; - - public static String DECL_CODE_01 = "public class "; - public static String DECL_CODE_02 = " extends JTToolThread {"; - public static String DECL_CODE_03 = "public void startSequence() {"; - public static String DECL_CODE_04 = "public void startPreemptionTasks() {"; - public static String OP_SEQ = "startingSequence()"; - public static String OP_PRE = "startPreemptionTasks()"; - public static String TH_EXCEPTION = "throws PreemptionException"; - public static String ATTR_DEC = "__"; - public static final String JGATE = "JGate"; - - public static final String T__STATE = "t__state"; - public static final String T__GO = "t__go"; - public static final String OP_STATE = "opForState"; - - public static final String MAIN_CLASS = "MainClass_"; - - public static final int NO_PROTOCOL = 0; - public static final int UDP = 1; - public static final int TCP = 2; - public static final int RMI = 3; - - public TURTLE2Java(TURTLEModeling _tm, long _millis, long _nanos, String _header) { - tm = _tm; - millis = _millis; - nanos = _nanos; - longforint = false; - header = _header; - components = new Vector<>(); - mainclasses = new Vector<>(); - } - - public void saveJavaClasses(String path) throws FileException { - for (JavaClass jc: this.javaClasses) { - jc.saveAsFileIn(path); - } - - saveAsFileInMainClasses(path); - } - - public String getMainListFiles(String path) { - String s = ""; - MainClass tmpc; - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - s += path + tmpc.getName() + ".java "; - } - return s; - } - - public void setLongSelected(boolean b) { - longforint = b; - } - - public void printJavaClasses() { - for (JavaClass jc: this.javaClasses) { - - } - - printMainClasses(); - } - - public void generateJava(boolean _debug) { - //int i; - debug = _debug; - - // Preprocessing - tm.makeRTLOTOSName(); - tm.translateWatchdogs(); - tm.translateInvocationIntoSynchronization(); - tm.translateActionStatesWithMultipleParams(); - mgm = new MasterGateManager(tm, false); - mgm.sort(); - - javaClasses = new LinkedList<>(); - - // Creating classes & attributes & operations - generateConstantClass(); - generateMainClasses(); - generateBasicCodeMainClasses(); - //mainclass = new MainClass("MainClass"); - //mainclass.generateBasicCode(); - - generateJavaClasses1(); - generateJavaClasses2(); - - // Generate mainclass starting code - generateJGateCreation(); - generateJGateSynchronisation(); - generateTClassStarting(); - generateOperationCodeMainClasses(); - - - - // Post computing - MasterGateManager.reinitNameRestriction(); - - } - - private void generateJavaClasses1() { - TClass t; - int i; - JavaClass jc; - - for(i=0; i<tm.classNb(); i++) { - t = tm.getTClassAtIndex(i); - if (!(t instanceof TClassLinkNode)) { - jc = new JavaClass(t.getName(), t.isActive()); - jc.setPackageName(t.getPackageName()); - jc.setUserHeader(header); - jc.setDeclarationCode(DECL_CODE_01 + jc.getJavaName() + DECL_CODE_02); - javaClasses.add(jc); - generateBasicAttributes(t, jc); - // Prepare parallels at activity diagram level -> new gates - prepareParallelOperators(t, t.getActivityDiagram()); - generateGateAttributes(t, jc); - jc.generateAttributeDeclaration(); - jc.generateGateDeclaration(); - jc.generateJTToolAttributes(); - jc.generateConstructor(); - } - } - } - - private void generateJavaClasses2() { - TClass t; - int i; - JavaClass jc; - - for(i=0; i<tm.classNb(); i++) { - t = tm.getTClassAtIndex(i); - jc = foundJClass(t.getName()); - if (jc != null) { - generateExternalPreemption(t, jc); - // - generateOperations(t, jc); - // - } - } - } - - /* It does not verify that attributes are not keywords of the Java language */ - public void generateBasicAttributes(TClass t, JavaClass jc) { - int i; - Param p; - JAttribute ja; - - Vector params = t.getParamList(); - - for(i=0; i<params.size(); i++) { - p = (Param)(params.elementAt(i)); - ja = new JAttribute(p, longforint); - - /*if (JKeyword.isAKeyword(ja.getJavaName())) { - ja.setJavaName(ja.getJavaName() + "__0"); - } */ - jc.addAttribute(ja); - } - } - - /* It does not verify that gates are not keywords of the Java language */ - public void generateGateAttributes(TClass t, JavaClass jc) { - int i; - Gate g; - JGate jg; - - Vector gates = t.getGateList(); - - for(i=0; i<gates.size(); i++) { - g = (Gate)(gates.elementAt(i)); - jg = new JGate(g.getName(), g.isInternal(), g.getProtocolJava(), g.getLocalPortJava(), g.getDestPortJava(), g.getDestHostJava(), g.getLocalHostJava()); - jc.addGate(jg); - } - } - - public void generateExternalPreemption(TClass t, JavaClass jc) { - int i; - JavaClass jc1, jc2; - Relation r; - //TClass t1; - - LinkedList<JavaClass> tclasses = new LinkedList<>(); - - for(i=0; i<tm.relationNb(); i++) { - r = tm.getRelationAtIndex(i); - - if ((r.type == Relation.PRE) && (r.t1 == t)) { - // - jc1 = foundJClass(r.t2.getName()); - // - tclasses.add(jc1); - } - } - - if (tclasses.size() == 0) { - return; - } - - // - - // Create the necessary operation - jc.addStartingPreemptionCode(translator.JKeyword.INDENT + DECL_CODE_04 + "\n"); - - // Create all new threads - jc.addStartingPreemptionCode(generateJGateCreation(tclasses)); - jc.addStartingPreemptionCode(generateTClassCreation(tclasses, false)); - - - ListIterator iterator = tclasses.listIterator(); - ListIterator iterator1; - while(iterator.hasNext()) { - jc1 = (JavaClass)(iterator.next()); - jc.addStartingPreemptionCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(this);\n"); - iterator1 = tclasses.listIterator(); - while(iterator1.hasNext()) { - jc2 = (JavaClass)(iterator1.next()); - if (jc2 != jc1) { - jc.addStartingPreemptionCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(" + jc2.getJavaName().toLowerCase() + ");\n"); - } - } - - } - jc.addStartingPreemptionCode(generateTClassStarting(tclasses, false, false)); - jc.addStartingPreemptionCode(translator.JKeyword.INDENT + translator.JKeyword.STOP_CODE_N); - - } - - private void generateOperations(TClass t, JavaClass jc) { - addRunOperation(jc); - generateStandardOperations(t, jc); - generateRunOperation(jc); - jc.generateAllOperations(); - } - - private void addRunOperation(JavaClass jc) { - JOperation jo = new JOperation("runMe"); - jo.addCode(translator.JKeyword.INDENT + translator.JKeyword.PUBLIC + " void " + jo.name + " () " + TH_EXCEPTION + " " + translator.JKeyword.START_CODE + "\n"); - jc.addOperation(jo); - } - - private void generateStandardOperations(TClass t, JavaClass jc) { - ActivityDiagram ad = t.getActivityDiagram(); - ADComponent adc = ad.getStartState(); - - JOperation jo = makeNewJOperation(jc); - jo.addStandardCode(); - - generateStandardOperationsRec(t, jc, ad, adc.getNext(0), adc, jo, 2, true); - } - - private JOperation makeNewJOperation(JavaClass jc) { - JOperation jo = new JOperation(TURTLE2Java.OP_STATE + (jc.getOperationNb()-1)); - jc.addOperation(jo); - jo.addCode(jc.getDeclarationOfOp(jc.getOperationNb() - 1)); - return jo; - } - - private void generateStandardOperationsRec(TClass t, JavaClass jc, ActivityDiagram ad, ADComponent adc, ADComponent last, JOperation jo, int dec, boolean endNeeded) { - - if (adc instanceof ADActionStateWithGate) { - ADActionStateWithGate adaswg = (ADActionStateWithGate)adc; - indent(jo, dec); - if (debug) { - jo.addCode(" - } - // Call on gate! - manageCallOnGate(t, jc, ad, adc, jo,dec, jc.foundJGate(adaswg.getGate().getName()), adaswg.getActionValue(), null, null, endNeeded); - // Next component - //generateStandardOperationsRec(t, jc, adc.getNext(0), jo, dec, endNeeded); - } else if (adc instanceof ADActionStateWithParam) { - ADActionStateWithParam adaswp = (ADActionStateWithParam) adc; - indent(jo, dec); - // - // - jo.addCode(makeJavaExpression(adaswp.brutToString()) + translator.JKeyword.END_OP_N); - generateStandardOperationsRec(t, jc, ad, adc.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADChoice) { - ADChoice adch = (ADChoice) adc; - makeChoiceCode(t, jc, ad, adch, jo, dec, endNeeded); - } else if (adc instanceof ADDelay) { - ADDelay add = (ADDelay) adc; - makeDelayCode(add.getValue(), jo, dec); - generateStandardOperationsRec(t, jc, ad, add.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADJunction) { - ADJunction adj = (ADJunction) adc; - makeJunctionCode(t, jc, ad, adj, jo, dec, endNeeded); - } else if (adc instanceof ADLatency) { - ADLatency adl = (ADLatency) adc; - makeLatencyCode(adl.getValue(), jo, dec); - generateStandardOperationsRec(t, jc, ad, adl.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADTimeInterval) { - ADTimeInterval adti = (ADTimeInterval) adc; - makeDelayCode(adti.getMinValue(), jo, dec); - makeLatencyCode("(" + adti.getMaxValue() + ")-(" +adti.getMinValue()+ ")", jo, dec); - generateStandardOperationsRec(t, jc, ad, adti.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADTLO) { - ADTLO adtlo = (ADTLO)(adc); - indent(jo, dec); - if (debug) - jo.addCode(" - manageCallOnGate(t, jc, ad, adc, jo,dec, jc.foundJGate(adtlo.getGate().getName()), adtlo.getAction(), adtlo.getLatency(), adtlo.getDelay(), endNeeded); - //generateStandardOperationsRec(t, jc, adtlo.getNext(0), jo, dec, endNeeded); - } else if (adc instanceof ADStop) { - makeEndCode(jo, dec, endNeeded); - } else if (adc instanceof ADSequence) { - makeSequenceCode(t, jc, ad, (ADSequence)adc, jo, dec, endNeeded); - } else if (adc instanceof ADParallel) { - makeParallelCode(t, jc, ad, (ADParallel)adc, last, jo, dec, endNeeded); - } else if (adc instanceof ADPreempt) { - makePreemptionCode(t, jc, ad, (ADPreempt)adc, last, jo, dec, endNeeded); - } else { - - if (endNeeded) { - makeEndCode(jo, dec, true); - } - } - } - - private void indent(JOperation jo, int d) { - while(d>0) { - jo.addCode(translator.JKeyword.INDENT); - d --; - } - } - - private String addThread(String name, int dec) { - String s = ""; - while(dec>0) { - s += translator.JKeyword.INDENT; - dec --; - } - s+="internalThreads.add(" + name + ");\n"; - return s; - } - - - private void generateRunOperation(JavaClass jc) { - int i; - JOperation run0 = jc.getOperationAt(0); - //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "try " + JKeyword.START_CODE_N); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + "while(" + TURTLE2Java.T__GO + " == true)" + translator.JKeyword.START_CODE_N); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + "switch(" + TURTLE2Java.T__STATE + ")" + translator.JKeyword.START_CODE_N); - - for(i=1; i<jc.getOperationNb(); i++) { - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT); - run0.addCode("case " + (i-1) + ":\n"); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + " "); - run0.addCode(jc.getCallToOp(i)); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + " "); - run0.addCode("break" + translator.JKeyword.END_OP_N); - } - - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT); - run0.addCode("default:\n"); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + " "); - run0.addCode(TURTLE2Java.T__GO + " = false" + translator.JKeyword.END_OP_N); - - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.STOP_CODE_N); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + translator.JKeyword.STOP_CODE_N); - //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + OP_SEQ + JKeyword.END_OP_N); - //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "} catch (PreemptionException pe) {}\n"); - run0.addCode(translator.JKeyword.INDENT + translator.JKeyword.STOP_CODE); - } - - private void generateJGateCreation() { - generateJGateCreationMainClasses(); - //addGateCodeMainClass(generateJGateCreation(javaClasses)); - } - - private String generateJGateCreation(LinkedList<JavaClass> toTakeIntoAccountJC) { - int j; - JGate jg; - String s = ""; - - for (JavaClass jc: this.javaClasses) { - if (toTakeIntoAccountJC.contains(jc)) { - for(j=0; j<jc.getGateNb(); j++) { - jg = jc.getGateAt(j); - jg.setJName(jc.getJavaName() + "__" + jg.getName()); - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + "JGate " + jg.getJName() + " = new JGate(\"" + jg.getJName() + "\")" + translator.JKeyword.END_OP + "\n"; - if (jg.hasAProtocol()) { - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg.getJName() + ".setProtocol(" + jg.getProtocol() + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg.getJName() + ".setLocalPort(" + jg.getLocalPort() + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg.getJName() + ".setDestPort(" + jg.getDestPort() + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg.getJName() + ".setDestHost(\"" + jg.getDestHost() + "\")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg.getJName() + ".setLocalHost(\"" + jg.getLocalHost() + "\")" + translator.JKeyword.END_OP + "\n"; - } - } - } - } - - return s; - } - - private void generateJGateSynchronisation() { - generateJGateSynchronisationMainClasses(); - } - - private String generateJGateSynchronisation(LinkedList<JavaClass> toTakeIntoAccountJC) { - // Assume that all invocation operations have been removed - //TClass t; - Relation r; - int i, j; - JavaClass jc2; - JGate jg1, jg2; - int id; - //Gate g; - String s = ""; - - MainClass tmpc; - - if (toTakeIntoAccountJC.size() == 0) { - return ""; - } - - tmpc=getMainClassOf(toTakeIntoAccountJC.get(0)); - - for(i=0; i<tm.relationNb(); i++) { - r = tm.getRelationAtIndex(i); - if (r.type == Relation.SYN) { - JavaClass jc1 = foundJClass(r.t1.getName()); - jc2 = foundJClass(r.t2.getName()); - if ((jc1 != null) && (jc2 != null)) { - if (toTakeIntoAccountJC.contains(jc1) && toTakeIntoAccountJC.contains(jc2)) { - for(j=0; j<r.gatesOfT1.size(); j++) { - - jg1 = jc1.foundJGate(r.gatesOfT1.elementAt(j).getName()); - jg2 = jc2.foundJGate(r.gatesOfT2.elementAt(j).getName()); - // - if ((jg1 != null) && (jg2 != null)) { - // - id = tmpc.getUniqueGateId(); - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + "JMasterGate " + "mgate__" + id + " = new JMasterGate()" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg1.getJName() + ".setLeft()" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg2.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg2.getJName() + ".setRight()" + translator.JKeyword.END_OP + "\n"; - } - } - } - } - } - } - - // Gates of parallel operators in activity diagrams - //Vector v; - String name, nameSearched; - - tmpc.addSynchroCode(translator.JKeyword.INDENT + translator.JKeyword.INDENT + "/* Parallel operators of activity diagrams */\n"); - //JavaClass jc; - - for (JavaClass jc1: this.javaClasses) { - if (toTakeIntoAccountJC.contains(jc1)) { - for(i=0; i<jc1.getGateNb(); i++) { - jg1 = jc1.getGateAt(i); - name = jg1.getName(); - if (name.startsWith("par__master")) { - nameSearched = "par__" + name.substring(11, name.length()); - jg2 = jc1.foundJGate(nameSearched); - if (jg2 != null) { - id = getMainClassOf(jc1).getUniqueGateId(); - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + "JMasterGate " + "mgate__" + id + " = new JMasterGate()" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg1.getJName() + ".setLeft()" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg2.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + translator.JKeyword.END_OP + "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jg2.getJName() + ".setRight()" + translator.JKeyword.END_OP + "\n"; - } - } - } - } - } - - return s; - } - - private String generateExternalSequence(LinkedList<JavaClass> toTakeIntoAccount, boolean onlyActiveClasses) { - String s = ""; - LinkedList<JavaClass> ll; - LinkedList<JavaClass> one; - - s += "\n"; - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + "/* Sequence operator */ "; - // Sequence code - s+= "\n"; - - for (JavaClass jc: this.javaClasses) { - if (toTakeIntoAccount.contains(jc)) { - ll = listClassesStartingAt(jc.getJavaName()); - - if ((ll != null) && (ll.size() > 0)) { - jc.addStartingSequenceCode(translator.JKeyword.INDENT + DECL_CODE_03 + "\n"); - jc.addStartingSequenceCode(generateJGateCreation(ll)); - jc.addStartingSequenceCode(generateJGateSynchronisation(ll)); - jc.addStartingSequenceCode(generateTClassCreation(ll, false)); - jc.addStartingSequenceCode(generateCodeStartingSeq(ll)); - jc.addStartingSequenceCode(generateTClassStarting(ll, false, false)); - jc.addStartingSequenceCode(translator.JKeyword.INDENT + translator.JKeyword.STOP_CODE_N); - if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { - one = new LinkedList<>(); - one.add(jc); - s+= generateCodeStartingSeq(one);//JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; - } - } - } - } - return s; - } - - private String generateCodeStartingSeq(LinkedList<JavaClass> ll) { - String s = ""; - - for (JavaClass jc: ll) { - s+= translator.JKeyword.INDENT + translator.JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + translator.JKeyword.END_OP + "\n"; - } - return s; - } - - private LinkedList<JavaClass> listClassesStartingAt(String name) { - //TClass t1, t2; - Relation r; - JavaClass jc; - LinkedList<JavaClass> ll = new LinkedList<>(); - for(int i=0; i<tm.relationNb(); i++) { - r = tm.getRelationAtIndex(i); - if (r.type == Relation.SEQ) { - - if(r.t1.getName().equals(name)) { - jc = foundJClass(r.t2.getName()); - if (jc != null) { - ll.add(jc); - } - } - } - } - return ll; - } - - private void generateTClassStarting() { - generateTClassStartingMainClasses(); - //mainclass.addStartingCode(generateTClassCreation(javaClasses, true)); - //mainclass.addStartingCode(generateExternalPreemption(javaClasses, true)); - //mainclass.addStartingCode(generateExternalSequence(javaClasses, true)); - //mainclass.addStartingCode(generateTClassStarting(javaClasses, true, true)); - } - - private String generateTClassCreation(LinkedList<JavaClass> toTakeIntoAccount, boolean onlyActiveClasses) { - String s = ""; - - for (JavaClass jc: this.javaClasses) { - if (toTakeIntoAccount.contains(jc)) { - if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { - s += jc.getCreationCode(jc.getJavaName().toLowerCase()) + "\n"; - } - } - } - - return s; - } - - - private String generateTClassStarting(LinkedList<JavaClass> toTakeIntoAccount, boolean onlyActiveClasses, boolean internal) { - String s = ""; - for (JavaClass jc: this.javaClasses) { - if (toTakeIntoAccount.contains(jc)) { - if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { - if (internal) { - s += addThread(jc.getJavaName().toLowerCase(), 2); - } - s += translator.JKeyword.INDENT + translator.JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".start()" + translator.JKeyword.END_OP + "\n"; - } - } - } - - return s; - } - - - private JavaClass foundJClass(String name) { - for (JavaClass jc: this.javaClasses) { - if (jc.getTURTLEName().equals(name)) { - return jc; - } - } - - return null; - } - - private void makeSynchroSchemes(TClass t, JOperation jo, int dec, JGate jg, String value) { - int i; - boolean sending; - boolean nat; - - indent(jo, dec); - jo.addCode("/* Synchro on " + jg.getName() + " with action = " + value + " */\n"); - - indent(jo, dec); - //jo.addCode("__sss.removeAllElements();\n"); - - jo.addCode("__sss = new SynchroSchemes();\n"); - - // Pre syncro - for(i=0; i<tm.getNbOfSynchroItems(value); i++) { - // Sending - indent(jo, dec); - sending = tm.isSendingSynchro(value, i); - if (sending) { - jo.addCode("__ss = new SynchroScheme(true);\n"); - } else { - jo.addCode("__ss = new SynchroScheme(false);\n"); - } - - // Type and value - indent(jo, dec); - - nat = tm.isNaturalSynchro(t, value, i); - if (nat) { - jo.addCode("__ss.type = 0;\n"); - } else { - jo.addCode("__ss.type = 1;\n"); - } - - if (sending) { - indent(jo, dec); - if (nat) { - jo.addCode("__ss.valueNat = (int)(" + tm.getSynchroValueAt(value, i) + ");\n"); - } else { - jo.addCode("__ss.valueBool = " + tm.getSynchroValueAt(value, i) + ";\n"); - } - } - - indent(jo, dec); - jo.addCode("__sss.add(__ss);\n"); - } - } - - private void manageCallOnGate(TClass t, JavaClass jc, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JGate jg, String value, String latency, String delay, boolean endNeeded) { - //int i; - - if (adc.getPreJavaCode() != null) { - addUserCode(jo, dec, adc.getPreJavaCode()); - } - - - if ((delay != null) && (delay.length() == 0)) { - delay = null; - } - - if ((latency != null) && (latency.length() == 0)) { - latency = null; - } - - - if ((delay != null) && (latency != null)) { - makeLatencyCode(latency, jo, dec); - delay = "(" + delay + ") - (" +latency + ")"; - } - - if (delay != null) { - delay = " ( " + millis + " * (" + delay + ")) + (" + nanos + " * (" + delay + " / 100000))"; - } - - // Removing all elements from the Vector - makeSynchroSchemes(t, jo, dec, jg, value); - - - // Synchro - - jo.addCode("\n"); - //indent(jo, dec); - /*if (delay == null) { - jo.addCode("__sss = " + jg.getName() + ".synchro(__sss, this);\n"); - } else { - jo.addCode("__sss = " + jg.getName() + ".synchro(__sss," + delay + ", this);\n"); - } - */ - if (delay != null) { - indent(jo, dec); - jo.addCode("__sss.maxDelay = " + delay + ";\n"); - } - - indent(jo, dec); - jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); - indent(jo,dec); - jo.addCode("__ssss = new SynchroSchemes[1];\n"); - indent(jo,dec); - jo.addCode("__ssss[0] = __sss;\n"); - indent(jo,dec); - jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n"); - - indent(jo, dec); - if (debug) - jo.addCode(" - - if (delay != null) { - indent(jo, dec); - jo.addCode("if (__sss == null) {\n"); - dec ++; - - if (adc.getNext(1) == null) { - makeEndCode(jo, dec, endNeeded); - } else { - generateStandardOperationsRec(t, jc, ad, adc.getNext(1), adc, jo, dec, false); - } - - dec --; - indent(jo, dec); - jo.addCode("} else {\n"); - dec ++; - makePostSynchro(t, jc, ad, adc, jo, dec, jg, value, false); - dec --; - indent(jo, dec); - jo.addCode("}\n"); - - if (endNeeded) { - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - } else { - makePostSynchro(t, jc, ad, adc, jo, dec, jg, value, endNeeded); - } - } - - private void makePostSynchro(TClass t, JavaClass jc, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JGate jg, String value, boolean endNeeded) { - int i; - boolean sending, nat; - - // Post synchro - for(i=0; i<tm.getNbOfSynchroItems(value); i++) { - - sending = tm.isSendingSynchro(value, i); - if (!sending) { - //Receiving data - indent(jo, dec); - jo.addCode("__ss = __sss.synchroSchemeAt(" + i + ");\n"); - - nat = tm.isNaturalSynchro(t, value, i); - indent(jo, dec); - if (nat) { - jo.addCode(tm.getShortSynchroValueAt(value, i) + " = __ss.valueNat;\n"); - } else { - jo.addCode(tm.getShortSynchroValueAt(value, i) + " = __ss.valueBool;\n"); - } - } - } - - jo.addCode("\n"); - - if (adc.getPostJavaCode() != null) { - addUserCode(jo, dec, adc.getPostJavaCode()); - } - - generateStandardOperationsRec(t, jc, ad, adc.getNext(0), adc, jo, dec, endNeeded); - } - - public void addUserCode(JOperation jo, int dec, String code) { - jo.addNonFormattedCode(dec, "/* User Code */\n" + code + "\n/* End of User Code */\n"); - } - - public void makeEndCode(JOperation jo, int dec, boolean endNeeded) { - indent(jo, dec); - jo.addCode(TURTLE2Java.T__GO + " = false" + translator.JKeyword.END_OP_N); - if (endNeeded) { - indent(jo, dec-1); - jo.addCode(translator.JKeyword.STOP_CODE_N); - } - } - - public void makeChoiceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADChoice adch, JOperation jo, int dec, boolean endNeeded) { - int nbG = adch.getNbGuard(); - int i; - String guard; - JGate jg; - ADActionStateWithGate adag; - //long delay; - String delays; - - if (nbG < 1) { - makeEndCode(jo, dec, endNeeded); - return; - } - - // Commentary - indent(jo, dec); - jo.addCode("/* Managing choice */\n"); - - // building array - indent(jo, dec); - jo.addCode("__bchoice = new boolean[" + nbG + "];\n"); - - for(i=0; i<nbG; i++) { - indent(jo, dec); - if (!(adch.isGuarded(i))) { - jo.addCode("__bchoice[" + i + "] = true;\n"); - } else { - guard = adch.getGuard(i); - if (guard == null) { - jo.addCode("__bchoice[" + i + "] = false;\n"); - } else { - guard = makeJavaGuard(guard); - jo.addCode("__bchoice[" + i + "] = " + guard + ";\n"); - } - } - } - - - if (adch.isSpecialChoice(false)) { - /*indent(jo, dec); - jo.addCode("__bchoice__name = new JGate[" + nbG + "];\n"); - indent(jo, dec); - jo.addCode("__bchoice__mindelay = new long[" + nbG + "];\n"); - indent(jo, dec); - jo.addCode("__bchoice__maxdelay = new long[" + nbG + "];\n");*/ - indent(jo, dec); - jo.addCode("__bchoice__synchro = new SynchroSchemes[" + nbG + "];\n"); - - - for(i=0; i<nbG; i++) { - adag = adch.getADActionStateWithGate(i); - jg = jc.foundJGate(adag.getGate().getName()); - if (adag.getPreJavaCode() != null) { - addUserCode(jo, dec, adag.getPreJavaCode()); - } - - makeSynchroSchemes(t, jo, dec, jg, adag.getActionValue()); - - indent(jo, dec); - jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); - indent(jo, dec); - delays = adch.getMinDelay(i); - if (delays.compareTo("-1") != 0) { - delays = " ( " + millis + " * (" + delays + ")) + (" + nanos + " * (" + delays + " / 100000))"; - } - jo.addCode("__sss.minDelay =" + delays + ";\n"); - indent(jo, dec); - delays = adch.getMaxDelay(i); - if (delays.compareTo("-1") != 0) { - delays = " ( " + millis + " * (" + delays + ")) + (" + nanos + " * (" + delays + " / 100000))"; - } - jo.addCode("__sss.maxDelay =" + delays + ";\n"); - - indent(jo, dec); - jo.addCode("__bchoice__synchro[" + i + "] = __sss;\n"); - - } - - // new scheme - indent(jo, dec); - jo.addCode("/* Special choice */\n\n"); - - indent(jo, dec); - jo.addCode("__nchoice = LibLogicalOperator.nbOfTrue(__bchoice);\n"); - - if (debug) { - indent(jo, dec); - jo.addCode(" - } - indent(jo,dec); - jo.addCode("__ssss = new SynchroSchemes[__nchoice];\n"); - - indent(jo, dec); - jo.addCode("__cpt1 = 0; __cpt2 = 0;\n"); - indent(jo, dec); - jo.addCode("while(__cpt2 < __nchoice) {\n"); - dec ++; - indent(jo, dec); - jo.addCode("if (__bchoice[__cpt1] == true) {\n"); - dec ++; - - indent(jo, dec); - jo.addCode("__ssss[__cpt2] = __bchoice__synchro[__cpt1];\n"); - indent(jo, dec); - jo.addCode("__cpt2++;\n"); - - dec --; - indent(jo, dec); - jo.addCode("}\n"); - indent(jo, dec); - jo.addCode("__cpt1 ++;\n"); - - dec --; - indent(jo, dec); - jo.addCode("}\n"); - - indent(jo,dec); - jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n\n"); - - // After choice has been made - // Determine the nchoice number .. - - indent(jo,dec); - jo.addCode("__cpt1 = 0;\n"); - indent(jo,dec); - jo.addCode("while(__cpt1 < __nchoice) {\n"); - dec ++; - indent(jo,dec); - jo.addCode("if (__bchoice__synchro[__cpt1] == __sss) break;\n"); - indent(jo,dec); - jo.addCode("__cpt1 ++;\n"); - dec--; - indent(jo,dec); - jo.addCode("}\n\n"); - - // switching a path - indent(jo, dec); - jo.addCode("switch(__cpt1) {\n"); - - dec ++; - for(i=0; i<nbG; i++) { - indent(jo, dec); - jo.addCode("case " + i + ":\n"); - dec ++; - - if (debug) { - indent(jo, dec); - jo.addCode(" - } - // Get values from synchro - adag = adch.getADActionStateWithGate(i); - jg = jc.foundJGate(adag.getGate().getName()); - - makePostSynchro(t, jc, ad, adag, jo, dec, jg, adag.getActionValue(), false); - - //generateStandardOperationsRec(t, jc, ad, adch.getADActionStateWithGate(i).getNext(0), adch.getADActionStateWithGate(i), jo, dec, false); - indent(jo, dec); - jo.addCode("break;\n"); - dec --; - } - dec --; - indent(jo, dec); - jo.addCode("}\n"); - - - if (endNeeded) { - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - - } else { - // old scheme - - - // Deciding of a path - indent(jo, dec); - jo.addCode("__nchoice = LibLogicalOperator.makeChoice(__bchoice);\n"); - indent(jo, dec); - if (debug) - jo.addCode(" - - // switching a path - indent(jo, dec); - jo.addCode("switch(__nchoice) {\n"); - - dec ++; - for(i=0; i<nbG; i++) { - indent(jo, dec); - jo.addCode("case " + i + ":\n"); - dec ++; - indent(jo, dec); - if (debug) - jo.addCode(" - generateStandardOperationsRec(t, jc, ad, adch.getNext(i), adch, jo, dec, false); - indent(jo, dec); - jo.addCode("break;\n"); - dec --; - } - - dec --; - indent(jo, dec); - jo.addCode("}\n\n"); - - if (endNeeded) { - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - } - } - - public void makeDelayCode(String expr, JOperation jo, int dec) { - expr = makeJavaExpression(expr); - - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("/* Deterministic delay of " + expr + "*/\n"); - indent(jo, dec); - jo.addCode("LibTemporalOperator.waitFor( (" + expr + ") * " + millis + ", (" + expr + ") *" + nanos + ");\n"); - } - - public void makeLatencyCode(String expr, JOperation jo, int dec) { - expr = makeJavaExpression(expr); - - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("/* Non deterministic delay of " + expr + "*/\n"); - indent(jo, dec); - jo.addCode("LibTemporalOperator.waitForAtMost( (" + expr + ") * " + millis + ", (" + expr + ") *" + nanos + ");\n"); - } - - public void makeJunctionCode(TClass t, JavaClass jc, ActivityDiagram ad, ADJunction adj, JOperation jo, int dec, boolean endNeeded) { - int numOp; - JOperation jo1; - boolean makeOp = false; - ComponentId cid; - - cid = getComponentId(adj); - - if (cid != null) { - numOp = cid.id; - jo1 = jc.getOperationAt(numOp-1); - } else { - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - numOp = jc.getOperationNb() - 1; - cid = new ComponentId(adj, numOp); - components.add(cid); - makeOp = true; - } - indent(jo, dec); - jo.addCode("t__state = " + (numOp-1) + ";\n"); - dec --; - if (endNeeded) { - indent(jo, dec); - jo.addCode("}\n"); - } - - if (makeOp) { - generateStandardOperationsRec(t, jc, ad, adj.getNext(0), adj, jo1, 2, true); - } - } - - public void makeSequenceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADSequence adseq, JOperation jo, int dec, boolean endNeeded) { - // One thread is created to execute each sequenced operation - JOperation jo1; - String name; - int i; - int currentJc; - - if (adseq.getNbNext() > 1) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("/* Sequence operator */\n"); - indent(jo, dec); - jo.addCode("\n"); - for(i=0; i<adseq.getNbNext()-1; i++) { - currentJc = jc.getOperationNb() - 1; - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - generateStandardOperationsRec(t, jc, ad, adseq.getNext(i), adseq, jo1, dec, true); - //indent(jo, dec); - name = jc.getJavaName().toLowerCase() + "__" + idSeq; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, currentJc, dec) + "\n"); - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".setState(" + currentJc +")" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(name + ".start()" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode("try {\n"); - dec ++; - indent(jo, dec); - jo.addCode(name+ ".join();\n"); - dec --; - indent(jo, dec); - jo.addCode("} catch (InterruptedException ie) { - idSeq ++; - indent(jo, dec); - jo.addCode("\n"); - } - generateStandardOperationsRec(t, jc, ad, adseq.getNext(adseq.getNbNext()-1), adseq, jo, dec, endNeeded); - } else { - generateStandardOperationsRec(t, jc, ad, adseq.getNext(0), adseq, jo, dec, endNeeded); - } - } - - public void makeParallelCode(TClass t, JavaClass jc, ActivityDiagram ad, ADParallel adpar, ADComponent last, JOperation jo, int dec, boolean endNeeded) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("/* Parallel operator */\n"); - - int i; - ComponentId cid = getComponentId(adpar); - - if (ad.getNbComponentLeadingTo(adpar) > 1) { - // The first one must synchronize with all the others - if (ad.getFirstComponentLeadingTo(adpar) == last) { - for(i=1; i<ad.getNbComponentLeadingTo(adpar); i++) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("__sss = new SynchroSchemes();\n"); - indent(jo, dec); - jo.addCode("__sss = par__master" + cid.id + "__" + i + ".synchro(__sss, this);\n"); - } - } else { - // The others synchronize with the first one and exit - int id = ad.getIndexOfComponentLeadingTo(adpar, last); - indent(jo, dec); - jo.addCode("__sss = new SynchroSchemes();\n"); - indent(jo, dec); - jo.addCode("__sss = par__" + cid.id + "__" + id + ".synchro(__sss, this);\n"); - makeEndCode(jo, dec, endNeeded); - return; - } - - } - - JOperation jo1; - String name; - int idop; - - if (adpar.getNbNext() > 1) { - // We assume that there are no synchronization - boolean b = true;//adpar.isAValidMotif(t); - - if ((adpar.nbGate() == 0) ||(adpar.getNbNext() > 2) || (!b)) { - indent(jo, dec); - jo.addCode("\n"); - - indent(jo, dec); - jo.addCode("\n"); - for(i=1; i<adpar.getNbNext(); i++) { - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - idop = jc.getOperationNb() - 2; - // - generateStandardOperationsRec(t, jc, ad, adpar.getNext(i), adpar, jo1, 2, true); - //indent(jo, dec); - name = jc.getJavaName().toLowerCase() + "__" + idPar; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, idop, dec) + "\n"); - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".setState(" + idop +")" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(name + ".start()" + translator.JKeyword.END_OP + "\n"); - idPar ++; - indent(jo, dec); - jo.addCode("\n"); - } - generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); - } else { - - // Synchronization - // adpar : nb next == 2 - // Creation of new synchronization gates - Vector v = adpar.getGateList(); - Gate g; - JGate jg1; - JGate jg2; - int id = 0; - - for(i=0; i<v.size(); i++) { - g = (Gate)(v.elementAt(i)); - jg1 = jc.foundJGate(g.getName()); - if (jg1 != null) { - id = getMainClassOf(jc).getUniqueGateId(); - indent(jo, dec); - jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); - indent(jo, dec); - jo.addCode("JMasterGate " + "mgate__" + id + " = new JMasterGate()" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setLeft()" + translator.JKeyword.END_OP + "\n"); - } - } - - // Preparing second process - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - idop = jc.getOperationNb() - 2; - generateStandardOperationsRec(t, jc, ad, adpar.getNext(1), adpar, jo1, dec, true); - //indent(jo, dec); - name = jc.getJavaName().toLowerCase() + "__" + idPar; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, jc.getOperationNb() - 2, dec) + "\n"); - - // First process - int idp = id - v.size() + 1; - for(i=0; i<v.size(); i++) { - g = (Gate)(v.elementAt(i)); - jg1 = jc.foundJGate(g.getName()); - if (jg1 != null) { - indent(jo, dec); - jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + (idp + i) + ")" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setRight()" + translator.JKeyword.END_OP + "\n"); - } - } - - // Starting second process - - - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".setState(" + idop +")" + translator.JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(name + ".start()" + translator.JKeyword.END_OP + "\n"); - idPar ++; - indent(jo, dec); - jo.addCode("\n"); - - - // Starting of one new process - generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); - } - } else { - generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); - } - - } - - public void makePreemptionCode(TClass t, JavaClass jc, ActivityDiagram ad, ADPreempt adpre, ADComponent last, JOperation jo, int dec, boolean endNeeded) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("/* Preemption operator */\n"); - - int i; - if (adpre.getNbNext() < 2) { - // no real preemption - generateStandardOperationsRec(t, jc, ad, adpre.getNext(0), adpre, jo, dec, endNeeded); - return; - } - - JOperation jo1; - String name = ""; - int idop; - // One thread is started per outside next - for(i=0; i<adpre.getNbNext(); i++) { - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - idop = jc.getOperationNb() - 2; - generateStandardOperationsRec(t, jc, ad, adpre.getNext(i), adpre, jo1, 2, true); - name = jc.getJavaName().toLowerCase() + "__" + idPar; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, idop, dec) + "\n"); - - if (i>0) { - indent(jo, dec); - jo.addCode(name + ".setToPreempt(" + jc.getJavaName().toLowerCase() + "__" + (idPar - 1) + ");\n"); - } - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); - idPar ++; - indent(jo, dec); - jo.addCode("\n"); - } - makeEndCode(jo, dec, true); - - } - - public String makeJavaGuard(String guard) { - guard = guard.replace('[', '('); - guard = guard.replace(']', ')'); - - return makeJavaExpression(guard); - } - - public String makeJavaExpression(String guard) { - // min, max, div, mod, divs, not, or, and - // div mod divs are not currently supported - - // min -> Math.min - guard = Conversion.replaceOp(guard, "min", "Math.min"); - - // max -> Math.max - guard = Conversion.replaceOp(guard, "max", "Math.max"); - - // not -> ! - guard = Conversion.replaceOp(guard, "not", "!"); - - // or -> || - guard = Conversion.replaceOp(guard, "or", "||"); - - // and -> && - guard = Conversion.replaceOp(guard, "and", "&&"); - - return guard; - } - - private ComponentId getComponentId(ADComponent adc) { - ComponentId cid; - - for(int i=0; i<components.size(); i++) { - cid = components.elementAt(i); - if (cid.adc == adc) { - return cid; - } - } - - return null; - } - - private void prepareParallelOperators(TClass t, ActivityDiagram ad) { - ADComponent adc; - int idPar = 0; - ComponentId cid; - int i, j; - //Gate g; - - for(i=0; i<ad.size(); i++) { - adc = ad.elementAt(i); - if (adc instanceof ADParallel) { - cid = new ComponentId(adc, idPar); - - for(j=1; j<ad.getNbComponentLeadingTo(adc); j++) { - t.addNewGateIfApplicable("par__" + idPar + "__" + j); - t.addNewGateIfApplicable("par__master" + idPar + "__" + j); - } - - components.add(cid); - - idPar ++; - } - } - } - - private void generateMainClasses() { - MainClass tmpc; - TClass t; - for(int i=0; i<tm.classNb(); i++) { - t = tm.getTClassAtIndex(i); - if (!(t instanceof TClassLinkNode)) { - tmpc = foundMainClassByPackageName(t.getPackageName()); - if (tmpc == null) { - tmpc = new MainClass(MAIN_CLASS + t.getPackageName()); - mainclasses.add(tmpc); - - } - } - } - } - - private void generateConstantClass() { - JavaClass jc; - jc = new JavaClass("Constant", false); - jc.addAttributeCode("\tpublic static int MILLIS = " + millis + ";\n"); - jc.addAttributeCode("\tpublic static int NANOS = " + nanos + ";\n"); - jc.setDeclarationCode(DECL_CODE_01 + jc.getJavaName() + "{\n"); - javaClasses.add(jc); - - } - - private void generateBasicCodeMainClasses() { - MainClass tmpc; - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - tmpc.generateBasicCode(); - } - } - - private void generateOperationCodeMainClasses() { - MainClass tmpc; - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - tmpc.generateOperationCode(); - } - } - - private MainClass foundMainClassByPackageName(String packageName) { - MainClass tmpc; - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - if (tmpc.getName().compareTo(MAIN_CLASS + packageName) == 0) { - return tmpc; - } - } - return null; - } - - private void saveAsFileInMainClasses(String path) throws FileException { - MainClass tmpc; - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - tmpc.saveAsFileIn(path); - } - } - - private LinkedList<JavaClass> listJavaClassesSamePackageName(MainClass tmpc) { - LinkedList<JavaClass> ll = new LinkedList<>(); - - for (JavaClass jc: this.javaClasses) { - if (tmpc.getName().compareTo(MAIN_CLASS+jc.getPackageName()) == 0) { - ll.add(jc); - } - } - return ll; - } - - private void generateJGateCreationMainClasses() { - MainClass tmpc; - LinkedList<JavaClass> ll; - - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - - // we consider all classes of the package of tmpc - ll = listJavaClassesSamePackageName(tmpc); - - if (ll.size() > 0) { - tmpc.addGateCode(generateJGateCreation(ll)); - } - } - } - - private void generateJGateSynchronisationMainClasses() { - MainClass tmpc; - LinkedList<JavaClass> ll; - - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - - // we consider all classes of the package of tmpc - ll = listJavaClassesSamePackageName(tmpc); - - if (ll.size() > 0) { - tmpc.addSynchroCode(generateJGateSynchronisation(ll)); - } - } - } - //mainclass.addSynchroCode(generateJGateSynchronisation(javaClasses)); - - private void generateTClassStartingMainClasses() { - MainClass tmpc; - LinkedList<JavaClass> ll; - - for(int i=0; i<mainclasses.size(); i++) { - tmpc = mainclasses.elementAt(i); - ll = listJavaClassesSamePackageName(tmpc); - - if (ll.size() > 0) { - tmpc.addStartingCode(generateTClassCreation(ll, true)); - //tmpc.addStartingCode(generateExternalPreemption(ll, true)); - tmpc.addStartingCode(generateExternalSequence(ll, true)); - tmpc.addStartingCode(generateTClassStarting(ll, true, true)); - } - } - } - - - private MainClass getMainClassOf(JavaClass jc) { - return foundMainClassByPackageName(jc.getPackageName()); - } - - private void printMainClasses() { - MainClass tmpc; - for(int i=0; i<tm.classNb(); i++) { - tmpc = mainclasses.elementAt(i); - - } - } - +/**Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille + * + * ludovic.apvrille AT enst.fr + * + * This software is a computer program whose purpose is to allow the + * edition of TURTLE analysis, design and deployment diagrams, to + * allow the generation of RT-LOTOS or Java code from this diagram, + * and at last to allow the analysis of formal validation traces + * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP + * from INRIA Rhone-Alpes. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + * + * /** + * Class TURTLE2Java + * Creation: 03/03/2005 + * @version 1.0 03/03/2005 + * @author Ludovic APVRILLE + * @see + */ + +package translator.tojava; + +import java.util.*; + +import ddtranslator.*; +import myutil.*; +import translator.*; + +public class TURTLE2Java { + + //private static int gateId; + private int idSeq = 0; + private int idPar = 0; + + private TURTLEModeling tm; + private LinkedList javaClasses; + private MasterGateManager mgm; + private Vector mainclasses; + //private MainClass mainclass; + private Vector components; + private String header; + + private long millis; + private long nanos; + + private boolean debug; + private boolean longforint; + + public static String DECL_CODE_01 = "public class "; + public static String DECL_CODE_02 = " extends JTToolThread {"; + public static String DECL_CODE_03 = "public void startSequence() {"; + public static String DECL_CODE_04 = "public void startPreemptionTasks() {"; + public static String OP_SEQ = "startingSequence()"; + public static String OP_PRE = "startPreemptionTasks()"; + public static String TH_EXCEPTION = "throws PreemptionException"; + public static String ATTR_DEC = "__"; + public static final String JGATE = "JGate"; + + public static final String T__STATE = "t__state"; + public static final String T__GO = "t__go"; + public static final String OP_STATE = "opForState"; + + public static final String MAIN_CLASS = "MainClass_"; + + public static final int NO_PROTOCOL = 0; + public static final int UDP = 1; + public static final int TCP = 2; + public static final int RMI = 3; + + public TURTLE2Java(TURTLEModeling _tm, long _millis, long _nanos, String _header) { + tm = _tm; + millis = _millis; + nanos = _nanos; + longforint = false; + header = _header; + components = new Vector(); + mainclasses = new Vector(); + } + + public void saveJavaClasses(String path) throws FileException { + ListIterator iterator = javaClasses.listIterator(); + JavaClass jc; + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + jc.saveAsFileIn(path); + } + + saveAsFileInMainClasses(path); + } + + public String getMainListFiles(String path) { + String s = ""; + MainClass tmpc; + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + s += path + tmpc.getName() + ".java "; + } + return s; + } + + public void setLongSelected(boolean b) { + longforint = b; + } + + public void printJavaClasses() { + ListIterator iterator = javaClasses.listIterator(); + JavaClass jc; + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + TraceManager.addDev(jc.getJavaName() + ":\n" + jc.toString() + "\n\n"); + } + + printMainClasses(); + //TraceManager.addDev(mainclass.getName() + ":\n" + mainclass.toString() + "\n\n"); + + } + + public void generateJava(boolean _debug) { + //int i; + debug = _debug; + + // Preprocessing + tm.makeRTLOTOSName(); + tm.translateWatchdogs(); + tm.translateInvocationIntoSynchronization(); + tm.translateActionStatesWithMultipleParams(); + mgm = new MasterGateManager(tm, false); + mgm.sort(); + + javaClasses = new LinkedList(); + + // Creating classes & attributes & operations + generateConstantClass(); + generateMainClasses(); + generateBasicCodeMainClasses(); + //mainclass = new MainClass("MainClass"); + //mainclass.generateBasicCode(); + + generateJavaClasses1(); + generateJavaClasses2(); + + // Generate mainclass starting code + generateJGateCreation(); + generateJGateSynchronisation(); + generateTClassStarting(); + generateOperationCodeMainClasses(); + + + + // Post computing + MasterGateManager.reinitNameRestriction(); + + } + + private void generateJavaClasses1() { + TClass t; + int i; + JavaClass jc; + + for(i=0; i<tm.classNb(); i++) { + t = tm.getTClassAtIndex(i); + if (!(t instanceof TClassLinkNode)) { + jc = new JavaClass(t.getName(), t.isActive()); + jc.setPackageName(t.getPackageName()); + jc.setUserHeader(header); + jc.setDeclarationCode(DECL_CODE_01 + jc.getJavaName() + DECL_CODE_02); + javaClasses.add(jc); + generateBasicAttributes(t, jc); + // Prepare parallels at activity diagram level -> new gates + prepareParallelOperators(t, t.getActivityDiagram()); + generateGateAttributes(t, jc); + jc.generateAttributeDeclaration(); + jc.generateGateDeclaration(); + jc.generateJTToolAttributes(); + jc.generateConstructor(); + } + } + } + + private void generateJavaClasses2() { + TClass t; + int i; + JavaClass jc; + + for(i=0; i<tm.classNb(); i++) { + t = tm.getTClassAtIndex(i); + jc = foundJClass(t.getName()); + if (jc != null) { + generateExternalPreemption(t, jc); + //TraceManager.addDev("Generate operations for " + jc.getJavaName()); + generateOperations(t, jc); + //TraceManager.addDev("Done"); + } + } + } + + /* It does not verify that attributes are not keywords of the Java language */ + public void generateBasicAttributes(TClass t, JavaClass jc) { + int i; + Param p; + JAttribute ja; + + Vector params = t.getParamList(); + + for(i=0; i<params.size(); i++) { + p = (Param)(params.elementAt(i)); + ja = new JAttribute(p, longforint); + + /*if (JKeyword.isAKeyword(ja.getJavaName())) { + ja.setJavaName(ja.getJavaName() + "__0"); + } */ + jc.addAttribute(ja); + } + } + + /* It does not verify that gates are not keywords of the Java language */ + public void generateGateAttributes(TClass t, JavaClass jc) { + int i; + Gate g; + JGate jg; + + Vector gates = t.getGateList(); + + for(i=0; i<gates.size(); i++) { + g = (Gate)(gates.elementAt(i)); + jg = new JGate(g.getName(), g.isInternal(), g.getProtocolJava(), g.getLocalPortJava(), g.getDestPortJava(), g.getDestHostJava(), g.getLocalHostJava()); + jc.addGate(jg); + } + } + + public void generateExternalPreemption(TClass t, JavaClass jc) { + int i; + JavaClass jc1, jc2; + Relation r; + //TClass t1; + + LinkedList tclasses = new LinkedList(); + + for(i=0; i<tm.relationNb(); i++) { + r = tm.getRelationAtIndex(i); + TraceManager.addDev("t = " + t.getName() + " Relation=" + r); + if ((r.type == Relation.PRE) && (r.t1 == t)) { + //TraceManager.addDev("Preemption from t to " + r.t2.getName()); + jc1 = foundJClass(r.t2.getName()); + //TraceManager.addDev("jc1 = " + jc1.getJavaName()); + tclasses.add(jc1); + } + } + + if (tclasses.size() == 0) { + return; + } + + //TraceManager.addDev("Size=" + tclasses.size()); + + // Create the necessary operation + jc.addStartingPreemptionCode(JKeyword.INDENT + DECL_CODE_04 + "\n"); + + // Create all new threads + jc.addStartingPreemptionCode(generateJGateCreation(tclasses)); + jc.addStartingPreemptionCode(generateTClassCreation(tclasses, false)); + + + ListIterator iterator = tclasses.listIterator(); + ListIterator iterator1; + while(iterator.hasNext()) { + jc1 = (JavaClass)(iterator.next()); + jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(this);\n"); + iterator1 = tclasses.listIterator(); + while(iterator1.hasNext()) { + jc2 = (JavaClass)(iterator1.next()); + if (jc2 != jc1) { + jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(" + jc2.getJavaName().toLowerCase() + ");\n"); + } + } + + } + jc.addStartingPreemptionCode(generateTClassStarting(tclasses, false, false)); + jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.STOP_CODE_N); + + } + + private void generateOperations(TClass t, JavaClass jc) { + addRunOperation(jc); + generateStandardOperations(t, jc); + generateRunOperation(jc); + jc.generateAllOperations(); + } + + private void addRunOperation(JavaClass jc) { + JOperation jo = new JOperation("runMe"); + jo.addCode(JKeyword.INDENT + JKeyword.PUBLIC + " void " + jo.name + " () " + TH_EXCEPTION + " " + JKeyword.START_CODE + "\n"); + jc.addOperation(jo); + } + + private void generateStandardOperations(TClass t, JavaClass jc) { + ActivityDiagram ad = t.getActivityDiagram(); + ADComponent adc = ad.getStartState(); + + JOperation jo = makeNewJOperation(jc); + jo.addStandardCode(); + + generateStandardOperationsRec(t, jc, ad, adc.getNext(0), adc, jo, 2, true); + } + + private JOperation makeNewJOperation(JavaClass jc) { + JOperation jo = new JOperation(TURTLE2Java.OP_STATE + (jc.getOperationNb()-1)); + jc.addOperation(jo); + jo.addCode(jc.getDeclarationOfOp(jc.getOperationNb() - 1)); + return jo; + } + + private void generateStandardOperationsRec(TClass t, JavaClass jc, ActivityDiagram ad, ADComponent adc, ADComponent last, JOperation jo, int dec, boolean endNeeded) { + TraceManager.addDev("Generating std op rec " + jo.getName()); + if (adc instanceof ADActionStateWithGate) { + ADActionStateWithGate adaswg = (ADActionStateWithGate)adc; + indent(jo, dec); + if (debug) { + jo.addCode("System.out.println(\"Call on " + adaswg.getGate().getName() + " value=" + adaswg.getActionValue() + " from " + jc.getJavaName() + "\");\n"); + } + // Call on gate! + manageCallOnGate(t, jc, ad, adc, jo,dec, jc.foundJGate(adaswg.getGate().getName()), adaswg.getActionValue(), null, null, endNeeded); + // Next component + //generateStandardOperationsRec(t, jc, adc.getNext(0), jo, dec, endNeeded); + } else if (adc instanceof ADActionStateWithParam) { + ADActionStateWithParam adaswp = (ADActionStateWithParam) adc; + indent(jo, dec); + //TraceManager.addDev("java expr: " + adaswp.brutToString()); + //TraceManager.addDev(" Modified java expr: " + makeJavaExpression(adaswp.brutToString())); + jo.addCode(makeJavaExpression(adaswp.brutToString()) + JKeyword.END_OP_N); + generateStandardOperationsRec(t, jc, ad, adc.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADChoice) { + ADChoice adch = (ADChoice) adc; + makeChoiceCode(t, jc, ad, adch, jo, dec, endNeeded); + } else if (adc instanceof ADDelay) { + ADDelay add = (ADDelay) adc; + makeDelayCode(add.getValue(), jo, dec); + generateStandardOperationsRec(t, jc, ad, add.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADJunction) { + ADJunction adj = (ADJunction) adc; + makeJunctionCode(t, jc, ad, adj, jo, dec, endNeeded); + } else if (adc instanceof ADLatency) { + ADLatency adl = (ADLatency) adc; + makeLatencyCode(adl.getValue(), jo, dec); + generateStandardOperationsRec(t, jc, ad, adl.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADTimeInterval) { + ADTimeInterval adti = (ADTimeInterval) adc; + makeDelayCode(adti.getMinValue(), jo, dec); + makeLatencyCode("(" + adti.getMaxValue() + ")-(" +adti.getMinValue()+ ")", jo, dec); + generateStandardOperationsRec(t, jc, ad, adti.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADTLO) { + ADTLO adtlo = (ADTLO)(adc); + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\"Limited call on " + adtlo.getGate().getName() + " value=" + adtlo.getAction() + " from " + jc.getJavaName() + "\");\n"); + manageCallOnGate(t, jc, ad, adc, jo,dec, jc.foundJGate(adtlo.getGate().getName()), adtlo.getAction(), adtlo.getLatency(), adtlo.getDelay(), endNeeded); + //generateStandardOperationsRec(t, jc, adtlo.getNext(0), jo, dec, endNeeded); + } else if (adc instanceof ADStop) { + makeEndCode(jo, dec, endNeeded); + } else if (adc instanceof ADSequence) { + makeSequenceCode(t, jc, ad, (ADSequence)adc, jo, dec, endNeeded); + } else if (adc instanceof ADParallel) { + makeParallelCode(t, jc, ad, (ADParallel)adc, last, jo, dec, endNeeded); + } else if (adc instanceof ADPreempt) { + makePreemptionCode(t, jc, ad, (ADPreempt)adc, last, jo, dec, endNeeded); + } else { + TraceManager.addDev("Operator not supported: " + adc.toString()); + if (endNeeded) { + makeEndCode(jo, dec, true); + } + } + } + + private void indent(JOperation jo, int d) { + while(d>0) { + jo.addCode(JKeyword.INDENT); + d --; + } + } + + private String addThread(String name, int dec) { + String s = ""; + while(dec>0) { + s += JKeyword.INDENT; + dec --; + } + s+="internalThreads.add(" + name + ");\n"; + return s; + } + + + private void generateRunOperation(JavaClass jc) { + int i; + JOperation run0 = jc.getOperationAt(0); + //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "try " + JKeyword.START_CODE_N); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "while(" + TURTLE2Java.T__GO + " == true)" + JKeyword.START_CODE_N); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + "switch(" + TURTLE2Java.T__STATE + ")" + JKeyword.START_CODE_N); + + for(i=1; i<jc.getOperationNb(); i++) { + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT); + run0.addCode("case " + (i-1) + ":\n"); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); + run0.addCode(jc.getCallToOp(i)); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); + run0.addCode("break" + JKeyword.END_OP_N); + } + + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT); + run0.addCode("default:\n"); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); + run0.addCode(TURTLE2Java.T__GO + " = false" + JKeyword.END_OP_N); + + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.STOP_CODE_N); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.STOP_CODE_N); + //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + OP_SEQ + JKeyword.END_OP_N); + //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "} catch (PreemptionException pe) {}\n"); + run0.addCode(JKeyword.INDENT + JKeyword.STOP_CODE); + } + + private void generateJGateCreation() { + generateJGateCreationMainClasses(); + //addGateCodeMainClass(generateJGateCreation(javaClasses)); + } + + private String generateJGateCreation(LinkedList toTakeIntoAccountJC) { + JavaClass jc; + int j; + JGate jg; + String s = ""; + + ListIterator iterator = javaClasses.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (toTakeIntoAccountJC.contains(jc)) { + for(j=0; j<jc.getGateNb(); j++) { + jg = jc.getGateAt(j); + jg.setJName(jc.getJavaName() + "__" + jg.getName()); + s += JKeyword.INDENT + JKeyword.INDENT + "JGate " + jg.getJName() + " = new JGate(\"" + jg.getJName() + "\")" + JKeyword.END_OP + "\n"; + if (jg.hasAProtocol()) { + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setProtocol(" + jg.getProtocol() + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setLocalPort(" + jg.getLocalPort() + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setDestPort(" + jg.getDestPort() + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setDestHost(\"" + jg.getDestHost() + "\")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setLocalHost(\"" + jg.getLocalHost() + "\")" + JKeyword.END_OP + "\n"; + } + } + } + } + + return s; + } + + private void generateJGateSynchronisation() { + generateJGateSynchronisationMainClasses(); + //mainclass.addSynchroCode(generateJGateSynchronisation(javaClasses)); + } + + private String generateJGateSynchronisation(LinkedList toTakeIntoAccountJC) { + // Assume that all invocation operations have been removed + //TClass t; + Relation r; + int i, j; + JavaClass jc1, jc2; + JGate jg1, jg2; + int id; + //Gate g; + String s = ""; + + MainClass tmpc; + + if (toTakeIntoAccountJC.size() == 0) { + return ""; + } + + tmpc=getMainClassOf((JavaClass)(toTakeIntoAccountJC.get(0))); + + for(i=0; i<tm.relationNb(); i++) { + r = tm.getRelationAtIndex(i); + if (r.type == Relation.SYN) { + jc1 = foundJClass(r.t1.getName()); + jc2 = foundJClass(r.t2.getName()); + if ((jc1 != null) && (jc2 != null)) { + if (toTakeIntoAccountJC.contains(jc1) && toTakeIntoAccountJC.contains(jc2)) { + for(j=0; j<r.gatesOfT1.size(); j++) { + TraceManager.addDev("Gates 1)" + ((Gate)(r.gatesOfT1.elementAt(j))).getName() + " 2:" + ((Gate)(r.gatesOfT2.elementAt(j))).getName()); + jg1 = jc1.foundJGate(((Gate)(r.gatesOfT1.elementAt(j))).getName()); + jg2 = jc2.foundJGate(((Gate)(r.gatesOfT2.elementAt(j))).getName()); + //TraceManager.addDev("foundJGate"); + if ((jg1 != null) && (jg2 != null)) { + //TraceManager.addDev("master"); + id = tmpc.getUniqueGateId(); + s += JKeyword.INDENT + JKeyword.INDENT + "JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setRight()" + JKeyword.END_OP + "\n"; + } + } + } + } + } + } + + // Gates of parallel operators in activity diagrams + //Vector v; + String name, nameSearched; + + tmpc.addSynchroCode(JKeyword.INDENT + JKeyword.INDENT + "/* Parallel operators of activity diagrams */\n"); + ListIterator iterator = javaClasses.listIterator(); + //JavaClass jc; + + while(iterator.hasNext()) { + jc1 = (JavaClass)(iterator.next()); + if (toTakeIntoAccountJC.contains(jc1)) { + for(i=0; i<jc1.getGateNb(); i++) { + jg1 = jc1.getGateAt(i); + name = jg1.getName(); + if (name.startsWith("par__master")) { + nameSearched = "par__" + name.substring(11, name.length()); + jg2 = jc1.foundJGate(nameSearched); + if (jg2 != null) { + id = getMainClassOf(jc1).getUniqueGateId(); + s += JKeyword.INDENT + JKeyword.INDENT + "JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setRight()" + JKeyword.END_OP + "\n"; + } + } + } + } + } + + return s; + } + + private String generateExternalSequence(LinkedList toTakeIntoAccount, boolean onlyActiveClasses) { + JavaClass jc; + String s = ""; + LinkedList ll; + LinkedList one; + + s += "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + "/* Sequence operator */ "; + // Sequence code + s+= "\n"; + + + ListIterator iterator = javaClasses.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (toTakeIntoAccount.contains(jc)) { + ll = listClassesStartingAt(jc.getJavaName()); + TraceManager.addDev("Getting list for" + jc.getJavaName()); + if ((ll != null) && (ll.size() > 0)) { + jc.addStartingSequenceCode(JKeyword.INDENT + DECL_CODE_03 + "\n"); + jc.addStartingSequenceCode(generateJGateCreation(ll)); + jc.addStartingSequenceCode(generateJGateSynchronisation(ll)); + jc.addStartingSequenceCode(generateTClassCreation(ll, false)); + jc.addStartingSequenceCode(generateCodeStartingSeq(ll)); + jc.addStartingSequenceCode(generateTClassStarting(ll, false, false)); + jc.addStartingSequenceCode(JKeyword.INDENT + JKeyword.STOP_CODE_N); + if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { + one = new LinkedList(); + one.add(jc); + s+= generateCodeStartingSeq(one);//JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; + } + } + } + } + return s; + } + + private String generateCodeStartingSeq(LinkedList ll) { + String s = ""; + JavaClass jc; + ListIterator iterator = ll.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + s+= JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; + } + return s; + } + + private LinkedList listClassesStartingAt(String name) { + //TClass t1, t2; + Relation r; + JavaClass jc; + LinkedList ll = new LinkedList(); + for(int i=0; i<tm.relationNb(); i++) { + r = tm.getRelationAtIndex(i); + if (r.type == Relation.SEQ) { + TraceManager.addDev("Found one seq relation"); + if(r.t1.getName().equals(name)) { + jc = foundJClass(r.t2.getName()); + if (jc != null) { + ll.add(jc); + } + } + } + } + return ll; + } + + private void generateTClassStarting() { + generateTClassStartingMainClasses(); + //mainclass.addStartingCode(generateTClassCreation(javaClasses, true)); + //mainclass.addStartingCode(generateExternalPreemption(javaClasses, true)); + //mainclass.addStartingCode(generateExternalSequence(javaClasses, true)); + //mainclass.addStartingCode(generateTClassStarting(javaClasses, true, true)); + } + + private String generateTClassCreation(LinkedList toTakeIntoAccount, boolean onlyActiveClasses) { + JavaClass jc; + String s = ""; + + ListIterator iterator = javaClasses.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (toTakeIntoAccount.contains(jc)) { + if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { + s += jc.getCreationCode(jc.getJavaName().toLowerCase()) + "\n"; + } + } + } + + return s; + } + + + private String generateTClassStarting(LinkedList toTakeIntoAccount, boolean onlyActiveClasses, boolean internal) { + JavaClass jc; + String s = ""; + ListIterator iterator = javaClasses.listIterator(); + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (toTakeIntoAccount.contains(jc)) { + if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { + if (internal) { + s += addThread(jc.getJavaName().toLowerCase(), 2); + } + s += JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".start()" + JKeyword.END_OP + "\n"; + } + } + } + + return s; + } + + + private JavaClass foundJClass(String name) { + JavaClass jc; + ListIterator iterator = javaClasses.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (jc.getTURTLEName().equals(name)) { + return jc; + } + } + + return null; + } + + private void makeSynchroSchemes(TClass t, JOperation jo, int dec, JGate jg, String value) { + int i; + boolean sending; + boolean nat; + + indent(jo, dec); + jo.addCode("/* Synchro on " + jg.getName() + " with action = " + value + " */\n"); + + indent(jo, dec); + //jo.addCode("__sss.removeAllElements();\n"); + + jo.addCode("__sss = new SynchroSchemes();\n"); + + // Pre syncro + for(i=0; i<tm.getNbOfSynchroItems(value); i++) { + // Sending + indent(jo, dec); + sending = tm.isSendingSynchro(value, i); + if (sending) { + jo.addCode("__ss = new SynchroScheme(true);\n"); + } else { + jo.addCode("__ss = new SynchroScheme(false);\n"); + } + + // Type and value + indent(jo, dec); + + nat = tm.isNaturalSynchro(t, value, i); + if (nat) { + jo.addCode("__ss.type = 0;\n"); + } else { + jo.addCode("__ss.type = 1;\n"); + } + + if (sending) { + indent(jo, dec); + if (nat) { + jo.addCode("__ss.valueNat = (int)(" + tm.getSynchroValueAt(value, i) + ");\n"); + } else { + jo.addCode("__ss.valueBool = " + tm.getSynchroValueAt(value, i) + ";\n"); + } + } + + indent(jo, dec); + jo.addCode("__sss.add(__ss);\n"); + } + } + + private void manageCallOnGate(TClass t, JavaClass jc, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JGate jg, String value, String latency, String delay, boolean endNeeded) { + //int i; + + if (adc.getPreJavaCode() != null) { + addUserCode(jo, dec, adc.getPreJavaCode()); + } + + + if ((delay != null) && (delay.length() == 0)) { + delay = null; + } + + if ((latency != null) && (latency.length() == 0)) { + latency = null; + } + + + if ((delay != null) && (latency != null)) { + makeLatencyCode(latency, jo, dec); + delay = "(" + delay + ") - (" +latency + ")"; + } + + if (delay != null) { + delay = " ( " + millis + " * (" + delay + ")) + (" + nanos + " * (" + delay + " / 100000))"; + } + + // Removing all elements from the Vector + makeSynchroSchemes(t, jo, dec, jg, value); + + + // Synchro + + jo.addCode("\n"); + //indent(jo, dec); + /*if (delay == null) { + jo.addCode("__sss = " + jg.getName() + ".synchro(__sss, this);\n"); + } else { + jo.addCode("__sss = " + jg.getName() + ".synchro(__sss," + delay + ", this);\n"); + } + */ + if (delay != null) { + indent(jo, dec); + jo.addCode("__sss.maxDelay = " + delay + ";\n"); + } + + indent(jo, dec); + jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); + indent(jo,dec); + jo.addCode("__ssss = new SynchroSchemes[1];\n"); + indent(jo,dec); + jo.addCode("__ssss[0] = __sss;\n"); + indent(jo,dec); + jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n"); + + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\"Call terminated for" + jc.getJavaName() + "\");\n"); + + if (delay != null) { + indent(jo, dec); + jo.addCode("if (__sss == null) {\n"); + dec ++; + + if (adc.getNext(1) == null) { + makeEndCode(jo, dec, endNeeded); + } else { + generateStandardOperationsRec(t, jc, ad, adc.getNext(1), adc, jo, dec, false); + } + + dec --; + indent(jo, dec); + jo.addCode("} else {\n"); + dec ++; + makePostSynchro(t, jc, ad, adc, jo, dec, jg, value, false); + dec --; + indent(jo, dec); + jo.addCode("}\n"); + + if (endNeeded) { + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + } else { + makePostSynchro(t, jc, ad, adc, jo, dec, jg, value, endNeeded); + } + } + + private void makePostSynchro(TClass t, JavaClass jc, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JGate jg, String value, boolean endNeeded) { + int i; + boolean sending, nat; + + // Post synchro + for(i=0; i<tm.getNbOfSynchroItems(value); i++) { + + sending = tm.isSendingSynchro(value, i); + if (!sending) { + //Receiving data + indent(jo, dec); + jo.addCode("__ss = __sss.synchroSchemeAt(" + i + ");\n"); + + nat = tm.isNaturalSynchro(t, value, i); + indent(jo, dec); + if (nat) { + jo.addCode(tm.getShortSynchroValueAt(value, i) + " = __ss.valueNat;\n"); + } else { + jo.addCode(tm.getShortSynchroValueAt(value, i) + " = __ss.valueBool;\n"); + } + } + } + + jo.addCode("\n"); + + if (adc.getPostJavaCode() != null) { + addUserCode(jo, dec, adc.getPostJavaCode()); + } + + generateStandardOperationsRec(t, jc, ad, adc.getNext(0), adc, jo, dec, endNeeded); + } + + public void addUserCode(JOperation jo, int dec, String code) { + jo.addNonFormattedCode(dec, "/* User Code */\n" + code + "\n/* End of User Code */\n"); + } + + public void makeEndCode(JOperation jo, int dec, boolean endNeeded) { + indent(jo, dec); + jo.addCode(TURTLE2Java.T__GO + " = false" + JKeyword.END_OP_N); + if (endNeeded) { + indent(jo, dec-1); + jo.addCode(JKeyword.STOP_CODE_N); + } + } + + public void makeChoiceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADChoice adch, JOperation jo, int dec, boolean endNeeded) { + int nbG = adch.getNbGuard(); + int i; + String guard; + JGate jg; + ADActionStateWithGate adag; + //long delay; + String delays; + + if (nbG < 1) { + makeEndCode(jo, dec, endNeeded); + return; + } + + // Commentary + indent(jo, dec); + jo.addCode("/* Managing choice */\n"); + + // building array + indent(jo, dec); + jo.addCode("__bchoice = new boolean[" + nbG + "];\n"); + + for(i=0; i<nbG; i++) { + indent(jo, dec); + if (!(adch.isGuarded(i))) { + jo.addCode("__bchoice[" + i + "] = true;\n"); + } else { + guard = adch.getGuard(i); + if (guard == null) { + jo.addCode("__bchoice[" + i + "] = false;\n"); + } else { + guard = makeJavaGuard(guard); + jo.addCode("__bchoice[" + i + "] = " + guard + ";\n"); + } + } + } + + + if (adch.isSpecialChoice(false)) { + /*indent(jo, dec); + jo.addCode("__bchoice__name = new JGate[" + nbG + "];\n"); + indent(jo, dec); + jo.addCode("__bchoice__mindelay = new long[" + nbG + "];\n"); + indent(jo, dec); + jo.addCode("__bchoice__maxdelay = new long[" + nbG + "];\n");*/ + indent(jo, dec); + jo.addCode("__bchoice__synchro = new SynchroSchemes[" + nbG + "];\n"); + + + for(i=0; i<nbG; i++) { + adag = adch.getADActionStateWithGate(i); + jg = jc.foundJGate(adag.getGate().getName()); + if (adag.getPreJavaCode() != null) { + addUserCode(jo, dec, adag.getPreJavaCode()); + } + + makeSynchroSchemes(t, jo, dec, jg, adag.getActionValue()); + + indent(jo, dec); + jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); + indent(jo, dec); + delays = adch.getMinDelay(i); + if (delays.compareTo("-1") != 0) { + delays = " ( " + millis + " * (" + delays + ")) + (" + nanos + " * (" + delays + " / 100000))"; + } + jo.addCode("__sss.minDelay =" + delays + ";\n"); + indent(jo, dec); + delays = adch.getMaxDelay(i); + if (delays.compareTo("-1") != 0) { + delays = " ( " + millis + " * (" + delays + ")) + (" + nanos + " * (" + delays + " / 100000))"; + } + jo.addCode("__sss.maxDelay =" + delays + ";\n"); + + indent(jo, dec); + jo.addCode("__bchoice__synchro[" + i + "] = __sss;\n"); + + } + + // new scheme + indent(jo, dec); + jo.addCode("/* Special choice */\n\n"); + + indent(jo, dec); + jo.addCode("__nchoice = LibLogicalOperator.nbOfTrue(__bchoice);\n"); + + if (debug) { + indent(jo, dec); + jo.addCode("System.out.println(\" __nchoice=\" + __nchoice + \" \");\n"); + } + indent(jo,dec); + jo.addCode("__ssss = new SynchroSchemes[__nchoice];\n"); + + indent(jo, dec); + jo.addCode("__cpt1 = 0; __cpt2 = 0;\n"); + indent(jo, dec); + jo.addCode("while(__cpt2 < __nchoice) {\n"); + dec ++; + indent(jo, dec); + jo.addCode("if (__bchoice[__cpt1] == true) {\n"); + dec ++; + + indent(jo, dec); + jo.addCode("__ssss[__cpt2] = __bchoice__synchro[__cpt1];\n"); + indent(jo, dec); + jo.addCode("__cpt2++;\n"); + + dec --; + indent(jo, dec); + jo.addCode("}\n"); + indent(jo, dec); + jo.addCode("__cpt1 ++;\n"); + + dec --; + indent(jo, dec); + jo.addCode("}\n"); + + indent(jo,dec); + jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n\n"); + + // After choice has been made + // Determine the nchoice number .. + + indent(jo,dec); + jo.addCode("__cpt1 = 0;\n"); + indent(jo,dec); + jo.addCode("while(__cpt1 < __nchoice) {\n"); + dec ++; + indent(jo,dec); + jo.addCode("if (__bchoice__synchro[__cpt1] == __sss) break;\n"); + indent(jo,dec); + jo.addCode("__cpt1 ++;\n"); + dec--; + indent(jo,dec); + jo.addCode("}\n\n"); + + // switching a path + indent(jo, dec); + jo.addCode("switch(__cpt1) {\n"); + + dec ++; + for(i=0; i<nbG; i++) { + indent(jo, dec); + jo.addCode("case " + i + ":\n"); + dec ++; + + if (debug) { + indent(jo, dec); + jo.addCode("System.out.println(\"Going to branch " + i + "\");\n"); + } + // Get values from synchro + adag = adch.getADActionStateWithGate(i); + jg = jc.foundJGate(adag.getGate().getName()); + + makePostSynchro(t, jc, ad, adag, jo, dec, jg, adag.getActionValue(), false); + + //generateStandardOperationsRec(t, jc, ad, adch.getADActionStateWithGate(i).getNext(0), adch.getADActionStateWithGate(i), jo, dec, false); + indent(jo, dec); + jo.addCode("break;\n"); + dec --; + } + dec --; + indent(jo, dec); + jo.addCode("}\n"); + + + if (endNeeded) { + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + + } else { + // old scheme + + + // Deciding of a path + indent(jo, dec); + jo.addCode("__nchoice = LibLogicalOperator.makeChoice(__bchoice);\n"); + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\" __nchoice=\" + __nchoice + \" \");\n"); + + // switching a path + indent(jo, dec); + jo.addCode("switch(__nchoice) {\n"); + + dec ++; + for(i=0; i<nbG; i++) { + indent(jo, dec); + jo.addCode("case " + i + ":\n"); + dec ++; + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\"Guard #" + i + "\");\n"); + generateStandardOperationsRec(t, jc, ad, adch.getNext(i), adch, jo, dec, false); + indent(jo, dec); + jo.addCode("break;\n"); + dec --; + } + + dec --; + indent(jo, dec); + jo.addCode("}\n\n"); + + if (endNeeded) { + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + } + } + + public void makeDelayCode(String expr, JOperation jo, int dec) { + expr = makeJavaExpression(expr); + + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("/* Deterministic delay of " + expr + "*/\n"); + indent(jo, dec); + jo.addCode("LibTemporalOperator.waitFor( (" + expr + ") * " + millis + ", (" + expr + ") *" + nanos + ");\n"); + } + + public void makeLatencyCode(String expr, JOperation jo, int dec) { + expr = makeJavaExpression(expr); + + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("/* Non deterministic delay of " + expr + "*/\n"); + indent(jo, dec); + jo.addCode("LibTemporalOperator.waitForAtMost( (" + expr + ") * " + millis + ", (" + expr + ") *" + nanos + ");\n"); + } + + public void makeJunctionCode(TClass t, JavaClass jc, ActivityDiagram ad, ADJunction adj, JOperation jo, int dec, boolean endNeeded) { + int numOp; + JOperation jo1; + boolean makeOp = false; + ComponentId cid; + + cid = getComponentId(adj); + + if (cid != null) { + numOp = cid.id; + jo1 = jc.getOperationAt(numOp-1); + } else { + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + numOp = jc.getOperationNb() - 1; + cid = new ComponentId(adj, numOp); + components.add(cid); + makeOp = true; + } + indent(jo, dec); + jo.addCode("t__state = " + (numOp-1) + ";\n"); + dec --; + if (endNeeded) { + indent(jo, dec); + jo.addCode("}\n"); + } + + if (makeOp) { + generateStandardOperationsRec(t, jc, ad, adj.getNext(0), adj, jo1, 2, true); + } + } + + public void makeSequenceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADSequence adseq, JOperation jo, int dec, boolean endNeeded) { + // One thread is created to execute each sequenced operation + JOperation jo1; + String name; + int i; + int currentJc; + + if (adseq.getNbNext() > 1) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("/* Sequence operator */\n"); + indent(jo, dec); + jo.addCode("\n"); + for(i=0; i<adseq.getNbNext()-1; i++) { + currentJc = jc.getOperationNb() - 1; + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + generateStandardOperationsRec(t, jc, ad, adseq.getNext(i), adseq, jo1, dec, true); + //indent(jo, dec); + name = jc.getJavaName().toLowerCase() + "__" + idSeq; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, currentJc, dec) + "\n"); + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".setState(" + currentJc +")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode("try {\n"); + dec ++; + indent(jo, dec); + jo.addCode(name+ ".join();\n"); + dec --; + indent(jo, dec); + jo.addCode("} catch (InterruptedException ie) {System.out.println(\"Interrupted\");}\n"); + idSeq ++; + indent(jo, dec); + jo.addCode("\n"); + } + generateStandardOperationsRec(t, jc, ad, adseq.getNext(adseq.getNbNext()-1), adseq, jo, dec, endNeeded); + } else { + generateStandardOperationsRec(t, jc, ad, adseq.getNext(0), adseq, jo, dec, endNeeded); + } + } + + public void makeParallelCode(TClass t, JavaClass jc, ActivityDiagram ad, ADParallel adpar, ADComponent last, JOperation jo, int dec, boolean endNeeded) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("/* Parallel operator */\n"); + + int i; + ComponentId cid = getComponentId(adpar); + + if (ad.getNbComponentLeadingTo(adpar) > 1) { + // The first one must synchronize with all the others + if (ad.getFirstComponentLeadingTo(adpar) == last) { + for(i=1; i<ad.getNbComponentLeadingTo(adpar); i++) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("__sss = new SynchroSchemes();\n"); + indent(jo, dec); + jo.addCode("__sss = par__master" + cid.id + "__" + i + ".synchro(__sss, this);\n"); + } + } else { + // The others synchronize with the first one and exit + int id = ad.getIndexOfComponentLeadingTo(adpar, last); + indent(jo, dec); + jo.addCode("__sss = new SynchroSchemes();\n"); + indent(jo, dec); + jo.addCode("__sss = par__" + cid.id + "__" + id + ".synchro(__sss, this);\n"); + makeEndCode(jo, dec, endNeeded); + return; + } + + } + + JOperation jo1; + String name; + int idop; + + if (adpar.getNbNext() > 1) { + // We assume that there are no synchronization + boolean b = true;//adpar.isAValidMotif(t); + TraceManager.addDev("Nb of gates = " + adpar.nbGate() + " valueGate=" + adpar.getValueGate()); + if ((adpar.nbGate() == 0) ||(adpar.getNbNext() > 2) || (!b)) { + indent(jo, dec); + jo.addCode("\n"); + + indent(jo, dec); + jo.addCode("\n"); + for(i=1; i<adpar.getNbNext(); i++) { + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + idop = jc.getOperationNb() - 2; + //TraceManager.addDev("Parallel with next = " + adpar.getNext(i).toString()); + generateStandardOperationsRec(t, jc, ad, adpar.getNext(i), adpar, jo1, 2, true); + //indent(jo, dec); + name = jc.getJavaName().toLowerCase() + "__" + idPar; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, idop, dec) + "\n"); + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".setState(" + idop +")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + idPar ++; + indent(jo, dec); + jo.addCode("\n"); + } + generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); + } else { + TraceManager.addDev("Parallel - synchro"); + // Synchronization + // adpar : nb next == 2 + // Creation of new synchronization gates + Vector v = adpar.getGateList(); + Gate g; + JGate jg1; + JGate jg2; + int id = 0; + + for(i=0; i<v.size(); i++) { + g = (Gate)(v.elementAt(i)); + jg1 = jc.foundJGate(g.getName()); + if (jg1 != null) { + id = getMainClassOf(jc).getUniqueGateId(); + indent(jo, dec); + jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); + indent(jo, dec); + jo.addCode("JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"); + } + } + + // Preparing second process + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + idop = jc.getOperationNb() - 2; + generateStandardOperationsRec(t, jc, ad, adpar.getNext(1), adpar, jo1, dec, true); + //indent(jo, dec); + name = jc.getJavaName().toLowerCase() + "__" + idPar; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, jc.getOperationNb() - 2, dec) + "\n"); + + // First process + int idp = id - v.size() + 1; + for(i=0; i<v.size(); i++) { + g = (Gate)(v.elementAt(i)); + jg1 = jc.foundJGate(g.getName()); + if (jg1 != null) { + indent(jo, dec); + jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + (idp + i) + ")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setRight()" + JKeyword.END_OP + "\n"); + } + } + + // Starting second process + + + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".setState(" + idop +")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + idPar ++; + indent(jo, dec); + jo.addCode("\n"); + + + // Starting of one new process + generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); + } + } else { + generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); + } + + } + + public void makePreemptionCode(TClass t, JavaClass jc, ActivityDiagram ad, ADPreempt adpre, ADComponent last, JOperation jo, int dec, boolean endNeeded) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("/* Preemption operator */\n"); + + int i; + if (adpre.getNbNext() < 2) { + // no real preemption + generateStandardOperationsRec(t, jc, ad, adpre.getNext(0), adpre, jo, dec, endNeeded); + return; + } + + JOperation jo1; + String name = ""; + int idop; + // One thread is started per outside next + for(i=0; i<adpre.getNbNext(); i++) { + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + idop = jc.getOperationNb() - 2; + generateStandardOperationsRec(t, jc, ad, adpre.getNext(i), adpre, jo1, 2, true); + name = jc.getJavaName().toLowerCase() + "__" + idPar; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, idop, dec) + "\n"); + + if (i>0) { + indent(jo, dec); + jo.addCode(name + ".setToPreempt(" + jc.getJavaName().toLowerCase() + "__" + (idPar - 1) + ");\n"); + } + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + idPar ++; + indent(jo, dec); + jo.addCode("\n"); + } + makeEndCode(jo, dec, true); + + } + + public String makeJavaGuard(String guard) { + guard = guard.replace('[', '('); + guard = guard.replace(']', ')'); + + return makeJavaExpression(guard); + } + + public String makeJavaExpression(String guard) { + // min, max, div, mod, divs, not, or, and + // div mod divs are not currently supported + + // min -> Math.min + guard = Conversion.replaceOp(guard, "min", "Math.min"); + + // max -> Math.max + guard = Conversion.replaceOp(guard, "max", "Math.max"); + + // not -> ! + guard = Conversion.replaceOp(guard, "not", "!"); + + // or -> || + guard = Conversion.replaceOp(guard, "or", "||"); + + // and -> && + guard = Conversion.replaceOp(guard, "and", "&&"); + + return guard; + } + + private ComponentId getComponentId(ADComponent adc) { + ComponentId cid; + + for(int i=0; i<components.size(); i++) { + cid = (ComponentId)(components.elementAt(i)); + if (cid.adc == adc) { + return cid; + } + } + + return null; + } + + private void prepareParallelOperators(TClass t, ActivityDiagram ad) { + ADComponent adc; + int idPar = 0; + ComponentId cid; + int i, j; + //Gate g; + + for(i=0; i<ad.size(); i++) { + adc = (ADComponent)(ad.elementAt(i)); + if (adc instanceof ADParallel) { + cid = new ComponentId(adc, idPar); + + for(j=1; j<ad.getNbComponentLeadingTo(adc); j++) { + t.addNewGateIfApplicable("par__" + idPar + "__" + j); + t.addNewGateIfApplicable("par__master" + idPar + "__" + j); + } + + components.add(cid); + + idPar ++; + } + } + } + + private void generateMainClasses() { + MainClass tmpc; + TClass t; + for(int i=0; i<tm.classNb(); i++) { + t = tm.getTClassAtIndex(i); + if (!(t instanceof TClassLinkNode)) { + tmpc = foundMainClassByPackageName(t.getPackageName()); + if (tmpc == null) { + tmpc = new MainClass(MAIN_CLASS + t.getPackageName()); + mainclasses.add(tmpc); + TraceManager.addDev("Adding mainclass :" + MAIN_CLASS + t.getPackageName()); + } + } + } + } + + private void generateConstantClass() { + JavaClass jc; + jc = new JavaClass("Constant", false); + jc.addAttributeCode("\tpublic static int MILLIS = " + millis + ";\n"); + jc.addAttributeCode("\tpublic static int NANOS = " + nanos + ";\n"); + jc.setDeclarationCode(DECL_CODE_01 + jc.getJavaName() + "{\n"); + javaClasses.add(jc); + + } + + private void generateBasicCodeMainClasses() { + MainClass tmpc; + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + tmpc.generateBasicCode(); + } + } + + private void generateOperationCodeMainClasses() { + MainClass tmpc; + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + tmpc.generateOperationCode(); + } + } + + private MainClass foundMainClassByPackageName(String packageName) { + MainClass tmpc; + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + if (tmpc.getName().compareTo(MAIN_CLASS + packageName) == 0) { + return tmpc; + } + } + return null; + } + + private void saveAsFileInMainClasses(String path) throws FileException { + MainClass tmpc; + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + tmpc.saveAsFileIn(path); + } + } + + private LinkedList listJavaClassesSamePackageName(MainClass tmpc) { + LinkedList ll = new LinkedList(); + ListIterator iterator1 = javaClasses.listIterator(); + JavaClass jc; + + while(iterator1.hasNext()) { + jc = (JavaClass)(iterator1.next()); + if (tmpc.getName().compareTo(MAIN_CLASS+jc.getPackageName()) == 0) { + ll.add(jc); + } + } + return ll; + } + + private void generateJGateCreationMainClasses() { + MainClass tmpc; + LinkedList ll; + ListIterator iterator1; + + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + + // we consider all classes of the package of tmpc + ll = listJavaClassesSamePackageName(tmpc); + + if (ll.size() > 0) { + tmpc.addGateCode(generateJGateCreation(ll)); + } + } + } + + private void generateJGateSynchronisationMainClasses() { + MainClass tmpc; + LinkedList ll; + ListIterator iterator1; + JavaClass jc; + + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + + // we consider all classes of the package of tmpc + ll = listJavaClassesSamePackageName(tmpc); + + if (ll.size() > 0) { + tmpc.addSynchroCode(generateJGateSynchronisation(ll)); + } + } + } + //mainclass.addSynchroCode(generateJGateSynchronisation(javaClasses)); + + private void generateTClassStartingMainClasses() { + MainClass tmpc; + LinkedList ll; + ListIterator iterator1; + + for(int i=0; i<mainclasses.size(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + ll = listJavaClassesSamePackageName(tmpc); + + if (ll.size() > 0) { + tmpc.addStartingCode(generateTClassCreation(ll, true)); + //tmpc.addStartingCode(generateExternalPreemption(ll, true)); + tmpc.addStartingCode(generateExternalSequence(ll, true)); + tmpc.addStartingCode(generateTClassStarting(ll, true, true)); + } + } + } + + + private MainClass getMainClassOf(JavaClass jc) { + return foundMainClassByPackageName(jc.getPackageName()); + } + + private void printMainClasses() { + MainClass tmpc; + for(int i=0; i<tm.classNb(); i++) { + tmpc = (MainClass)(mainclasses.elementAt(i)); + TraceManager.addDev(tmpc.getName() + ":\n" + tmpc.toString() + "\n\n"); + } + } + } \ No newline at end of file diff --git a/src/main/java/translator/tosimujava/TURTLE2SimuJava.java b/src/main/java/translator/tosimujava/TURTLE2SimuJava.java index 8c3e499e02199fd036fa176d2574f601d99e0048..64047da295ffea8cf883ad5c6a50e3ebd2139089 100755 --- a/src/main/java/translator/tosimujava/TURTLE2SimuJava.java +++ b/src/main/java/translator/tosimujava/TURTLE2SimuJava.java @@ -1,1424 +1,1438 @@ -/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille - * - * ludovic.apvrille AT enst.fr - * - * This software is a computer program whose purpose is to allow the - * edition of TURTLE analysis, design and deployment diagrams, to - * allow the generation of RT-LOTOS or Java code from this diagram, - * and at last to allow the analysis of formal validation traces - * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP - * from INRIA Rhone-Alpes. - * - * This software is governed by the CeCILL license under French law and - * abiding by the rules of distribution of free software. You can use, - * modify and/ or redistribute the software under the terms of the CeCILL - * license as circulated by CEA, CNRS and INRIA at the following URL - * "http://www.cecill.info". - * - * As a counterpart to the access to the source code and rights to copy, - * modify and redistribute granted by the license, users are provided only - * with a limited warranty and the software's author, the holder of the - * economic rights, and the successive licensors have only limited - * liability. - * - * In this respect, the user's attention is drawn to the risks associated - * with loading, using, modifying and/or developing or reproducing the - * software by the user in light of its specific status of free software, - * that may mean that it is complicated to manipulate, and that also - * therefore means that it is reserved for developers and experienced - * professionals having in-depth computer knowledge. Users are therefore - * encouraged to load and test the software's suitability as regards their - * requirements in conditions enabling the security of their systems and/or - * data to be ensured and, more generally, to use and operate it in the - * same conditions as regards security. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL license and that you accept its terms. - */ - - - - -package translator.tosimujava; - -import myutil.Conversion; -import myutil.FileException; -import translator.*; -import translator.tojava.ComponentId; -import translator.tojava.JAttribute; -import translator.JKeyword; -import translator.tojava.JOperation; - -import java.util.LinkedList; -import java.util.Vector; - -/** - * Class TURTLE2SimuJava - * Creation: 19/06/2006 - * @version 1.0 19/06/2006 - * @author Ludovic APVRILLE - */ -public class TURTLE2SimuJava { - - //private static int gateId; - //private int idSeq = 0; - //private int idPar = 0; - - private TURTLEModeling tm; - private LinkedList<TJavaProcess> processes; - private MasterGateManager mgm; - private MainClassSimu mainClass; - //private MainClass mainclass; - private Vector<ComponentId> components; - - private boolean debug; - private boolean longforint; - - public static String DECL_CODE_01 = "public class "; - public static String DECL_CODE_02 = " extends TProcess {"; - public static String DECL_CODE_03 = "public void startSequence() {"; - public static String DECL_CODE_04 = "public void startPreemptionTasks() {"; - public static String OP_SEQ = "startingSequence()"; - public static String OP_PRE = "startPreemptionTasks()"; - public static String OP_DEC = "execute__"; - public static String TH_EXCEPTION = "throws PreemptionException"; - public static String EXIT = "jse.exitRequest();\n"; - public static String ATTR_DEC = "__"; - public static final String JGATE = "Gate"; - - public static final String MAIN_CLASS = "MainClass"; - - public static final int NO_PROTOCOL = 0; - public static final int UDP = 1; - public static final int TCP = 2; - public static final int RMI = 3; - - public TURTLE2SimuJava(TURTLEModeling _tm) { - tm = _tm; - longforint = false; - components = new Vector<>(); - } - - public void saveJavaClasses(String path) throws FileException { - for (TJavaProcess tjc: this.processes) { - tjc.saveAsFileIn(path); - } - - saveAsFileInMainClass(path); - } - - public String getMainListFiles(String path) { - return path + mainClass.getName() + ".java "; - } - - public void setLongSelected(boolean b) { - longforint = b; - } - - public void printJavaClasses() { - for (TJavaProcess tjp: this.processes) { - - } - - printMainClass(); - } - - public void generateSimulationCode(boolean _debug) { - //int i; - debug = _debug; - - // Preprocessing - tm.makeRTLOTOSName(); - tm.translateWatchdogs(); - tm.translateInvocationIntoSynchronization(); - tm.translateActionStatesWithMultipleParams(); - mgm = new MasterGateManager(tm, false); - mgm.sort(); - - processes = new LinkedList<>(); - - // Creating classes & attributes & operations - generateMainClass(); - generateBasicCodeMainClass(); - //mainclass = new MainClass("MainClass"); - //mainclass.generateBasicCode(); - - generateJavaClasses1(); - generateJavaClasses2(); - - // Generate mainclass starting code - generateJGateCreation(); - generateJGateSynchronisation(); - generateProcessStarting(); - generateOperationCodeMainClass(); - - - - // Post computing - MasterGateManager.reinitNameRestriction(); - - } - - private void generateJavaClasses1() { - TClass t; - int i; - TJavaProcess tjp; - - for(i=0; i<tm.classNb(); i++) { - t = tm.getTClassAtIndex(i); - tjp = new TJavaProcess(t.getName(), t.isActive()); - tjp.setDeclarationCode(DECL_CODE_01 + tjp.getJavaName() + DECL_CODE_02); - processes.add(tjp); - generateBasicAttributes(t, tjp); - // Prepare parallels at activity diagram level -> new gates - //prepareParallelOperators(t, t.getActivityDiagram()); - generateGateAttributes(t, tjp); - tjp.generateAttributeDeclaration(); - tjp.generateGateDeclaration(); - tjp.generateJTToolAttributes(); - tjp.generateConstructor(); - generateCloneOperation(tjp); - } - } - - private void generateJavaClasses2() { - TClass t; - int i; - TJavaProcess tjp; - - for(i=0; i<tm.classNb(); i++) { - t = tm.getTClassAtIndex(i); - tjp = foundTJProcess(t.getName()); - if (tjp != null) { - //generateExternalPreemption(t, jc); - // - generateOperations(t, tjp); - // - } - } - } - - /* It does not verify that attributes are not keywords of the Java language */ - public void generateBasicAttributes(TClass t, TJavaProcess tjp) { - int i; - Param p; - JAttribute ja; - - Vector params = t.getParamList(); - - for(i=0; i<params.size(); i++) { - p = (Param)(params.elementAt(i)); - ja = new JAttribute(p, longforint); - tjp.addAttribute(ja); - } - } - - /* It does not verify that gates are not keywords of the Java language */ - public void generateGateAttributes(TClass t, TJavaProcess tjp) { - int i; - Gate g; - JSimuGate jsg; - - Vector gates = t.getGateList(); - - for(i=0; i<gates.size(); i++) { - g = (Gate)(gates.elementAt(i)); - jsg = new JSimuGate(g.getName(), g.isInternal()); - tjp.addGate(jsg); - } - } - - /*public void generateExternalPreemption(TClass t, JavaClass jc) { - int i; - JavaClass jc1, jc2; - Relation r; - TClass t1; - - LinkedList tclasses = new LinkedList(); - - for(i=0; i<tm.relationNb(); i++) { - r = tm.getRelationAtIndex(i); - - if ((r.type == Relation.PRE) && (r.t1 == t)) { - // - jc1 = foundJClass(r.t2.getName()); - // - tclasses.add(jc1); - } - } - - if (tclasses.size() == 0) { - return; - } - - // - - // Create the necessary operation - jc.addStartingPreemptionCode(JKeyword.INDENT + DECL_CODE_04 + "\n"); - - // Create all new threads - jc.addStartingPreemptionCode(generateJGateCreation(tclasses)); - jc.addStartingPreemptionCode(generateTClassCreation(tclasses, false)); - - - ListIterator iterator = tclasses.listIterator(); - ListIterator iterator1; - while(iterator.hasNext()) { - jc1 = (JavaClass)(iterator.next()); - jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(this);\n"); - iterator1 = tclasses.listIterator(); - while(iterator1.hasNext()) { - jc2 = (JavaClass)(iterator1.next()); - if (jc2 != jc1) { - jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(" + jc2.getJavaName().toLowerCase() + ");\n"); - } - } - - } - jc.addStartingPreemptionCode(generateTClassStarting(tclasses, false, false)); - jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.STOP_CODE_N); - - }*/ - - private void generateOperations(TClass t, TJavaProcess tjp) { - generateStandardOperations(t, tjp); - tjp.generateAllOperations(); - } - - private void generateStandardOperations(TClass t, TJavaProcess tjp) { - ActivityDiagram ad = t.getActivityDiagram(); - ADComponent adc = ad.getStartState(); - - JOperation jo = makeNewJOperation(tjp); - jo.addStandardCode(); - - generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, 2, true); - } - - private JOperation newAndfinishJO(TJavaProcess tjp, JOperation jo, int dec) { - finishJO(jo, dec, tjp.getOperationNb()); - return makeNewJOperation(tjp); - } - - private void finishJO(JOperation jo, int dec, int nb) { - indent(jo, dec); - jo.addCode("functionId=" + nb + ";\n"); - dec--; - indent(jo, dec); - jo.addCode("}\n"); - } - - private void basicFinishJO(JOperation jo, int dec) { - dec--; - indent(jo, dec); - jo.addCode("}\n"); - } - - private JOperation makeNewJOperation(TJavaProcess tjp) { - JOperation jo = new JOperation(TURTLE2SimuJava.OP_DEC + (tjp.getOperationNb())); - jo.nb = tjp.getOperationNb(); - tjp.addOperation(jo); - jo.addCode(tjp.getDeclarationOfOp(tjp.getOperationNb())); - if (debug) { - jo.addCode(" - } - return jo; - } - - private void generateStandardOperationsRec(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADComponent adc, ADComponent last, JOperation jo, int dec, boolean endNeeded) { - - if (adc instanceof ADActionStateWithGate) { - ADActionStateWithGate adaswg = (ADActionStateWithGate)adc; - indent(jo, dec); - if (debug) { - jo.addCode(" - } - // Call on gate! - manageCallOnGate(t, tjp, ad, adc, jo,dec, tjp.foundJGate(adaswg.getGate().getName()), adaswg.getActionValue(), null, null, endNeeded); - // Next component - //generateStandardOperationsRec(t, jc, adc.getNext(0), jo, dec, endNeeded); - } else if (adc instanceof ADActionStateWithParam) { - ADActionStateWithParam adaswp = (ADActionStateWithParam) adc; - indent(jo, dec); - // - // - jo.addCode(makeJavaExpression(adaswp.brutToString()) + JKeyword.END_OP_N); - generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, dec, endNeeded); - } /*else if (adc instanceof ADChoice) { - ADChoice adch = (ADChoice) adc; - makeChoiceCode(t, tjp, ad, adch, jo, dec, endNeeded); - } */else if (adc instanceof ADDelay) { - ADDelay add = (ADDelay) adc; - makeIntervalCode(add.getValue(), add.getValue(), jo, dec); - jo = newAndfinishJO(tjp, jo, dec); - generateStandardOperationsRec(t, tjp, ad, add.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADJunction) { - ADJunction adj = (ADJunction) adc; - makeJunctionCode(t, tjp, ad, adj, jo, dec, endNeeded); - } else if (adc instanceof ADLatency) { - ADLatency adl = (ADLatency) adc; - makeIntervalCode("0", adl.getValue(), jo, dec); - jo = newAndfinishJO(tjp, jo, dec); - generateStandardOperationsRec(t, tjp, ad, adl.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADTimeInterval) { - ADTimeInterval adti = (ADTimeInterval) adc; - makeIntervalCode(adti.getMinValue(), adti.getMaxValue(), jo, dec); - jo = newAndfinishJO(tjp, jo, dec); - generateStandardOperationsRec(t, tjp, ad, adti.getNext(0), adc, jo, dec, endNeeded); - } else if (adc instanceof ADTLO) { - ADTLO adtlo = (ADTLO)(adc); - indent(jo, dec); - if (debug) - jo.addCode(" - manageCallOnGate(t, tjp, ad, adc, jo,dec, tjp.foundJGate(adtlo.getGate().getName()), adtlo.getAction(), adtlo.getLatency(), adtlo.getDelay(), endNeeded); - //generateStandardOperationsRec(t, jc, adtlo.getNext(0), jo, dec, endNeeded); - } else if (adc instanceof ADStop) { - makeEndCode(jo, dec, endNeeded); - }/*else if (adc instanceof ADSequence) { - makeSequenceCode(t, tjp, ad, (ADSequence)adc, jo, dec, endNeeded); - } */else if (adc instanceof ADParallel) { - makeParallelCode(t, tjp, ad, (ADParallel)adc, last, jo, dec, endNeeded); - } /*else if (adc instanceof ADPreempt) { - makePreemptionCode(t, tjp, ad, (ADPreempt)adc, last, jo, dec, endNeeded); - } else { - - if (endNeeded) { - makeEndCode(jo, dec, true); - } - }*/ - } - - private void indent(JOperation jo, int d) { - while(d>0) { - jo.addCode(JKeyword.INDENT); - d --; - } - } - - /*private String addThread(String name, int dec) { - String s = ""; - while(dec>0) { - s += JKeyword.INDENT; - dec --; - } - s+="internalThreads.add(" + name + ");\n"; - return s; - }*/ - - - /*private void generateRunOperation(JavaClass jc) { - int i; - JOperation run0 = jc.getOperationAt(0); - //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "try " + JKeyword.START_CODE_N); - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "while(" + TURTLE2Java.T__GO + " == true)" + JKeyword.START_CODE_N); - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + "switch(" + TURTLE2Java.T__STATE + ")" + JKeyword.START_CODE_N); - - for(i=1; i<jc.getOperationNb(); i++) { - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT); - run0.addCode("case " + (i-1) + ":\n"); - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); - run0.addCode(jc.getCallToOp(i)); - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); - run0.addCode("break" + JKeyword.END_OP_N); - } - - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT); - run0.addCode("default:\n"); - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); - run0.addCode(TURTLE2Java.T__GO + " = false" + JKeyword.END_OP_N); - - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.STOP_CODE_N); - run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.STOP_CODE_N); - //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + OP_SEQ + JKeyword.END_OP_N); - //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "} catch (PreemptionException pe) {}\n"); - run0.addCode(JKeyword.INDENT + JKeyword.STOP_CODE); - }*/ - - private void generateJGateCreation() { - generateJGateCreationMainClass(); - //addGateCodeMainClass(generateJGateCreation(javaClasses)); - } - - private String generateJGateCreation(LinkedList<TJavaProcess> toTakeIntoAccountJC) { - int j; - JSimuGate jg; - String s = ""; - - for (TJavaProcess tjp: this.processes) { - if (toTakeIntoAccountJC.contains(tjp)) { - for(j=0; j<tjp.getGateNb(); j++) { - jg = tjp.getGateAt(j); - jg.setJName(tjp.getJavaName() + "__" + jg.getName()); - s += JKeyword.INDENT + JKeyword.INDENT + "Gate " + jg.getJName() + " = new Gate(\"" + jg.getJName() + "\")" + JKeyword.END_OP + "\n"; - /*if (jg.hasAProtocol()) { - s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setProtocol(" + jg.getProtocol() + ")" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setLocalPort(" + jg.getLocalPort() + ")" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setDestPort(" + jg.getDestPort() + ")" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setDestHost(\"" + jg.getDestHost() + "\")" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setLocalHost(\"" + jg.getLocalHost() + "\")" + JKeyword.END_OP + "\n"; - }*/ - } - } - } - - return s; - } - - private void generateJGateSynchronisation() { - generateJGateSynchronisationMainClass(); - //mainclass.addSynchroCode(generateJGateSynchronisation(javaClasses)); - } - - private String generateJGateSynchronisation(LinkedList<TJavaProcess> toTakeIntoAccountJC) { - // Assume that all invocation operations have been removed - //TClass t; - Relation r; - int i, j; - TJavaProcess tjp1, tjp2; - JSimuGate jg1, jg2; - //Gate g; - String s = ""; - //MainClassSimu tmpc; - - if (toTakeIntoAccountJC.size() == 0) { - return ""; - } - - //tmpc=mainClass; - - for(i=0; i<tm.relationNb(); i++) { - r = tm.getRelationAtIndex(i); - if (r.type == Relation.SYN) { - tjp1 = foundTJProcess(r.t1.getName()); - tjp2 = foundTJProcess(r.t2.getName()); - if ((tjp1 != null) && (tjp2 != null)) { - if (toTakeIntoAccountJC.contains(tjp1) && toTakeIntoAccountJC.contains(tjp2)) { - for(j=0; j<r.gatesOfT1.size(); j++) { - - jg1 = tjp1.foundJGate(r.gatesOfT1.elementAt(j).getName()); - jg2 = tjp2.foundJGate(r.gatesOfT2.elementAt(j).getName()); - // - if ((jg1 != null) && (jg2 != null)) { - s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".synchroGate = " + jg2.getJName() + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".synchroGate = " + jg1.getJName() + JKeyword.END_OP + "\n"; - } - } - } - } - } - } - - // Gates of parallel operators in activity diagrams - //Vector v; - //String name, nameSearched;*/ - - //tmpc.addSynchroCode(JKeyword.INDENT + JKeyword.INDENT + "/* Parallel operators of activity diagrams */\n"); - /*ListIterator iterator = javaClasses.listIterator(); - JavaClass jc; - - while(iterator.hasNext()) { - jc1 = (JavaClass)(iterator.next()); - if (toTakeIntoAccountJC.contains(jc1)) { - for(i=0; i<jc1.getGateNb(); i++) { - jg1 = jc1.getGateAt(i); - name = jg1.getName(); - if (name.startsWith("par__master")) { - nameSearched = "par__" + name.substring(11, name.length()); - jg2 = jc1.foundJGate(nameSearched); - if (jg2 != null) { - id = getMainClassOf(jc1).getUniqueGateId(); - s += JKeyword.INDENT + JKeyword.INDENT + "JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; - s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setRight()" + JKeyword.END_OP + "\n"; - } - } - } - } - }*/ - - return s; - } - - /*private String generateExternalSequence(LinkedList toTakeIntoAccount, boolean onlyActiveClasses) { - JavaClass jc; - String s = ""; - LinkedList ll; - LinkedList one; - - s += "\n"; - // Sequence code - s+= "\n"; - - - ListIterator iterator = javaClasses.listIterator(); - - while(iterator.hasNext()) { - jc = (JavaClass)(iterator.next()); - if (toTakeIntoAccount.contains(jc)) { - ll = listClassesStartingAt(jc.getJavaName()); - - if ((ll != null) && (ll.size() > 0)) { - jc.addStartingSequenceCode(JKeyword.INDENT + DECL_CODE_03 + "\n"); - jc.addStartingSequenceCode(generateJGateCreation(ll)); - jc.addStartingSequenceCode(generateJGateSynchronisation(ll)); - jc.addStartingSequenceCode(generateTClassCreation(ll, false)); - jc.addStartingSequenceCode(generateCodeStartingSeq(ll)); - jc.addStartingSequenceCode(generateTClassStarting(ll, false, false)); - jc.addStartingSequenceCode(JKeyword.INDENT + JKeyword.STOP_CODE_N); - if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { - one = new LinkedList(); - one.add(jc); - s+= generateCodeStartingSeq(one);//JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; - } - } - } - } - return s; - }*/ - - /*private String generateCodeStartingSeq(LinkedList ll) { - String s = ""; - JavaClass jc; - ListIterator iterator = ll.listIterator(); - - while(iterator.hasNext()) { - jc = (JavaClass)(iterator.next()); - s+= JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; - } - return s; - }*/ - - /*private LinkedList listClassesStartingAt(String name) { - TClass t1, t2; - Relation r; - JavaClass jc; - LinkedList ll = new LinkedList(); - for(int i=0; i<tm.relationNb(); i++) { - r = tm.getRelationAtIndex(i); - if (r.type == Relation.SEQ) { - - if(r.t1.getName().equals(name)) { - jc = foundJClass(r.t2.getName()); - if (jc != null) { - ll.add(jc); - } - } - } - } - return ll; - }*/ - - private void generateProcessStarting() { - generateProcessStartingMainClass(); - } - - private String generateProcessCreation(LinkedList<TJavaProcess> toTakeIntoAccount, boolean onlyActiveClasses) { - String s = ""; - - for (TJavaProcess tjc: this.processes) { - if (toTakeIntoAccount.contains(tjc)) { - if ((!onlyActiveClasses) || (onlyActiveClasses && tjc.isActive())) { - s += tjc.getCreationCode(tjc.getJavaName().toLowerCase()) + "\n"; - } - } - } - - return s; - } - - - /*private String generateProcessStarting(LinkedList toTakeIntoAccount, boolean onlyActiveClasses, boolean internal) { - JavaClass jc; - String s = ""; - ListIterator iterator = javaClasses.listIterator(); - while(iterator.hasNext()) { - jc = (JavaClass)(iterator.next()); - if (toTakeIntoAccount.contains(jc)) { - if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { - if (internal) { - s += addThread(jc.getJavaName().toLowerCase(), 2); - } - s += JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".start()" + JKeyword.END_OP + "\n"; - } - } - } - - return s; - }*/ - - - private TJavaProcess foundTJProcess(String name) { - for (TJavaProcess tjp: this.processes) { - if (tjp.getTURTLEName().equals(name)) { - return tjp; - } - } - - return null; - } - - private void makeSynchroSchemes(TClass t, JOperation jo, int dec, JSimuGate jg, String value) { - int i; - boolean sending; - boolean nat; - - indent(jo, dec); - jo.addCode("//Synchro on " + jg.getName() + " with action = " + value + " \n"); - - - indent(jo, dec); - jo.addCode("sizeSynchros = " + tm.getNbOfSynchroItems(value) + ";\n"); - - // Pre syncro - for(i=0; i<tm.getNbOfSynchroItems(value); i++) { - // Sending - indent(jo, dec); - sending = tm.isSendingSynchro(value, i); - if (sending) { - jo.addCode("synchros[" + i + "].sending = true;\n"); - } else { - jo.addCode("synchros[" + i + "].sending = false;\n"); - } - - // Type and value - indent(jo, dec); - - nat = tm.isNaturalSynchro(t, value, i); - if (nat) { - jo.addCode("synchros[" + i + "].type = 0;\n"); - } else { - jo.addCode("synchros[" + i + "].type = 1;\n"); - } - - if (sending) { - indent(jo, dec); - if (nat) { - jo.addCode("synchros[" + i + "].valueNat = (int)(" + tm.getSynchroValueAt(value, i) + ");\n"); - } else { - jo.addCode("synchros[" + i + "].valueBool = " + tm.getSynchroValueAt(value, i) + ";\n"); - } - } - } - } - - private void manageCallOnGate(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JSimuGate jg, String value, String latency, String delay, boolean endNeeded) { - - if (adc.getPreJavaCode() != null) { - addUserCode(jo, dec, adc.getPreJavaCode()); - } - - - if ((delay != null) && (delay.length() == 0)) { - delay = null; - } - - if ((latency != null) && (latency.length() == 0)) { - latency = null; - } - - if ((delay != null) && (latency == null)) { - latency = "0"; - } - - - if ((delay != null) && (latency != null)) { - JOperation jo1 = makeNewJOperation(tjp); - makePostSynchro(t, tjp, ad, adc, jo1, dec, jg, value, endNeeded); - generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo1, dec, endNeeded); - JOperation jo2 = makeNewJOperation(tjp); - generateStandardOperationsRec(t, tjp, ad, adc.getNext(1), adc, jo2, dec, endNeeded); - makeSynchroSchemes(t, jo, dec, jg, value); - indent(jo, dec); - jo.addCode("jse.synchroTimeLimitedRequest(" + jg.getName() + ", " + latency + ", " + delay + " ," + jo1.nb + "," + jo2.nb + ");\n"); - finishJO(jo, dec, jo1.nb); - } else { - makeSynchroSchemes(t, jo, dec, jg, value); - indent(jo, dec); - jo.addCode("jse.synchroRequest(" + jg.getName() + ");\n"); - jo = newAndfinishJO(tjp, jo, dec); - makePostSynchro(t, tjp, ad, adc, jo, dec, jg, value, endNeeded); - generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, dec, endNeeded); - } - - - /*// Removing all elements from the Vector - makeSynchroSchemes(t, jo, dec, jg, value); - - - // Synchro - - jo.addCode("\n"); - //indent(jo, dec); - - if (delay != null) { - indent(jo, dec); - jo.addCode("__sss.maxDelay = " + delay + ";\n"); - } - - indent(jo, dec); - jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); - indent(jo,dec); - jo.addCode("__ssss = new SynchroSchemes[1];\n"); - indent(jo,dec); - jo.addCode("__ssss[0] = __sss;\n"); - indent(jo,dec); - jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n"); - - indent(jo, dec); - if (debug) - jo.addCode(" - - if (delay != null) { - indent(jo, dec); - jo.addCode("if (__sss == null) {\n"); - dec ++; - - if (adc.getNext(1) == null) { - makeEndCode(jo, dec, endNeeded); - } else { - generateStandardOperationsRec(t, tjp, ad, adc.getNext(1), adc, jo, dec, false); - } - - dec --; - indent(jo, dec); - jo.addCode("} else {\n"); - dec ++; - makePostSynchro(t, tjp, ad, adc, jo, dec, jg, value, false); - dec --; - indent(jo, dec); - jo.addCode("}\n"); - - if (endNeeded) { - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - } else { - makePostSynchro(t, tjp, ad, adc, jo, dec, jg, value, endNeeded); - }*/ - } - - private void makePostSynchro(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JSimuGate jg, String value, boolean endNeeded) { - int i; - boolean sending, nat; - - // Post synchro - for(i=0; i<tm.getNbOfSynchroItems(value); i++) { - sending = tm.isSendingSynchro(value, i); - if (!sending) { - nat = tm.isNaturalSynchro(t, value, i); - indent(jo, dec); - if (nat) { - jo.addCode(tm.getShortSynchroValueAt(value, i) + " = synchros[" + i + "].valueNat;\n"); - } else { - jo.addCode(tm.getShortSynchroValueAt(value, i) + " = synchros[" + i + "].valueBool;\n"); - } - } - } - - jo.addCode("\n"); - - if (adc.getPostJavaCode() != null) { - addUserCode(jo, dec, adc.getPostJavaCode()); - } - - //generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, dec, endNeeded); - } - - public void addUserCode(JOperation jo, int dec, String code) { - jo.addNonFormattedCode(dec, "/* User Code */\n" + code + "\n/* End of User Code */\n"); - } - - public void makeEndCode(JOperation jo, int dec, boolean endNeeded) { - indent(jo, dec); - //jo.addCode(TURTLE2Java.T__GO + " = false" + JKeyword.END_OP_N); - if (endNeeded) { - indent(jo, dec); - jo.addCode(TURTLE2SimuJava.EXIT); - dec --; - indent(jo, dec); - jo.addCode("}"); - } - } - - /*public void makeChoiceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADChoice adch, JOperation jo, int dec, boolean endNeeded) { - int nbG = adch.getNbGuard(); - int i; - String guard; - JGate jg; - ADActionStateWithGate adag; - long delay; - String delays; - - if (nbG < 1) { - makeEndCode(jo, dec, endNeeded); - return; - } - - // Commentary - indent(jo, dec); - jo.addCode("//Managing choice\n"); - - // building array - indent(jo, dec); - jo.addCode("__bchoice = new boolean[" + nbG + "];\n"); - - for(i=0; i<nbG; i++) { - indent(jo, dec); - if (!(adch.isGuarded(i))) { - jo.addCode("__bchoice[" + i + "] = true;\n"); - } else { - guard = adch.getGuard(i); - if (guard == null) { - jo.addCode("__bchoice[" + i + "] = false;\n"); - } else { - guard = makeJavaGuard(guard); - jo.addCode("__bchoice[" + i + "] = " + guard + ";\n"); - } - } - } - - - if (adch.isSpecialChoice()) { - indent(jo, dec); - jo.addCode("__bchoice__synchro = new SynchroSchemes[" + nbG + "];\n"); - - - for(i=0; i<nbG; i++) { - adag = adch.getADActionStateWithGate(i); - jg = jc.foundJGate(adag.getGate().getName()); - if (adag.getPreJavaCode() != null) { - addUserCode(jo, dec, adag.getPreJavaCode()); - } - - makeSynchroSchemes(t, jo, dec, jg, adag.getActionValue()); - - indent(jo, dec); - jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); - indent(jo, dec); - delays = adch.getMinDelay(i); - - jo.addCode("__sss.minDelay =" + delays + ";\n"); - indent(jo, dec); - delays = adch.getMaxDelay(i); - - jo.addCode("__sss.maxDelay =" + delays + ";\n"); - - indent(jo, dec); - jo.addCode("__bchoice__synchro[" + i + "] = __sss;\n"); - - } - - // new scheme - indent(jo, dec); - jo.addCode("//Special choice \n\n"); - - indent(jo, dec); - jo.addCode("__nchoice = LibLogicalOperator.nbOfTrue(__bchoice);\n"); - - if (debug) { - indent(jo, dec); - jo.addCode(" - } - indent(jo,dec); - jo.addCode("__ssss = new SynchroSchemes[__nchoice];\n"); - - indent(jo, dec); - jo.addCode("__cpt1 = 0; __cpt2 = 0;\n"); - indent(jo, dec); - jo.addCode("while(__cpt2 < __nchoice) {\n"); - dec ++; - indent(jo, dec); - jo.addCode("if (__bchoice[__cpt1] == true) {\n"); - dec ++; - - indent(jo, dec); - jo.addCode("__ssss[__cpt2] = __bchoice__synchro[__cpt1];\n"); - indent(jo, dec); - jo.addCode("__cpt2++;\n"); - - dec --; - indent(jo, dec); - jo.addCode("}\n"); - indent(jo, dec); - jo.addCode("__cpt1 ++;\n"); - - dec --; - indent(jo, dec); - jo.addCode("}\n"); - - indent(jo,dec); - jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n\n"); - - // After choice has been made - // Determine the nchoice number .. - - indent(jo,dec); - jo.addCode("__cpt1 = 0;\n"); - indent(jo,dec); - jo.addCode("while(__cpt1 < __nchoice) {\n"); - dec ++; - indent(jo,dec); - jo.addCode("if (__bchoice__synchro[__cpt1] == __sss) break;\n"); - indent(jo,dec); - jo.addCode("__cpt1 ++;\n"); - dec--; - indent(jo,dec); - jo.addCode("}\n\n"); - - // switching a path - indent(jo, dec); - jo.addCode("switch(__cpt1) {\n"); - - dec ++; - for(i=0; i<nbG; i++) { - indent(jo, dec); - jo.addCode("case " + i + ":\n"); - dec ++; - - if (debug) { - indent(jo, dec); - jo.addCode(" - } - // Get values from synchro - adag = adch.getADActionStateWithGate(i); - jg = jc.foundJGate(adag.getGate().getName()); - - makePostSynchro(t, jc, ad, adag, jo, dec, jg, adag.getActionValue(), false); - - //generateStandardOperationsRec(t, jc, ad, adch.getADActionStateWithGate(i).getNext(0), adch.getADActionStateWithGate(i), jo, dec, false); - indent(jo, dec); - jo.addCode("break;\n"); - dec --; - } - dec --; - indent(jo, dec); - jo.addCode("}\n"); - - - if (endNeeded) { - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - - } else { - // old scheme - - - // Deciding of a path - indent(jo, dec); - jo.addCode("__nchoice = LibLogicalOperator.makeChoice(__bchoice);\n"); - indent(jo, dec); - if (debug) - jo.addCode(" - - // switching a path - indent(jo, dec); - jo.addCode("switch(__nchoice) {\n"); - - dec ++; - for(i=0; i<nbG; i++) { - indent(jo, dec); - jo.addCode("case " + i + ":\n"); - dec ++; - indent(jo, dec); - if (debug) - jo.addCode(" - generateStandardOperationsRec(t, jc, ad, adch.getNext(i), adch, jo, dec, false); - indent(jo, dec); - jo.addCode("break;\n"); - dec --; - } - - dec --; - indent(jo, dec); - jo.addCode("}\n\n"); - - if (endNeeded) { - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - } - }*/ - - public void makeIntervalCode(String minExpr, String maxExpr, JOperation jo, int dec) { - minExpr = makeJavaExpression(minExpr); - maxExpr = makeJavaExpression(maxExpr); - - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("jse.addDelay(" + minExpr + ", " + maxExpr + ");\n"); - } - - /*public void makeLatencyCode(String expr, JOperation jo, int dec) { - expr = makeJavaExpression(expr); - - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("//Non deterministic delay of " + expr + "\n"); - indent(jo, dec); - //jo.addCode("LibTemporalOperator.waitForAtMost( (" + expr + ") * " + millis + ", (" + expr + ") *" + nanos + ");\n"); - }*/ - - public void makeJunctionCode(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADJunction adj, JOperation jo, int dec, boolean endNeeded) { - int numOp; - JOperation jo1; - boolean makeOp = false; - ComponentId cid; - - cid = getComponentId(adj); - - if (cid != null) { - numOp = cid.id; - jo1 = tjp.getOperationAt(numOp-1); - } else { - jo1 = makeNewJOperation(tjp); - jo1.addStandardCode(); - numOp = tjp.getOperationNb() - 1; - cid = new ComponentId(adj, numOp); - components.add(cid); - makeOp = true; - } - - if (endNeeded) { - indent(jo, dec); - jo.addCode(TURTLE2SimuJava.OP_DEC + numOp + "();\n"); - dec --; - indent(jo, dec); - jo.addCode("}\n"); - } - - if (makeOp) { - generateStandardOperationsRec(t, tjp, ad, adj.getNext(0), adj, jo1, 2, true); - } - } - - /*public void makeSequenceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADSequence adseq, JOperation jo, int dec, boolean endNeeded) { - // One thread is created to execute each sequenced operation - JOperation jo1; - String name; - int i; - - if (adseq.getNbNext() > 1) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("// Sequence operator \n"); - indent(jo, dec); - jo.addCode("\n"); - for(i=0; i<adseq.getNbNext()-1; i++) { - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - generateStandardOperationsRec(t, jc, ad, adseq.getNext(i), adseq, jo1, dec, true); - //indent(jo, dec); - name = jc.getJavaName().toLowerCase() + "__" + idSeq; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, jc.getOperationNb() - 2, dec) + "\n"); - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode("try {\n"); - dec ++; - indent(jo, dec); - jo.addCode(name+ ".join();\n"); - dec --; - indent(jo, dec); - jo.addCode("} catch (InterruptedException ie) { - idSeq ++; - indent(jo, dec); - jo.addCode("\n"); - } - generateStandardOperationsRec(t, jc, ad, adseq.getNext(adseq.getNbNext()-1), adseq, jo, dec, endNeeded); - } else { - generateStandardOperationsRec(t, jc, ad, adseq.getNext(0), adseq, jo, dec, endNeeded); - } - }*/ - - public void makeParallelCode(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADParallel adpar, ADComponent last, JOperation jo, int dec, boolean endNeeded) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("// Parallel operator \n"); - - int i; - //ComponentId cid = getComponentId(adpar); - - /*if (ad.getNbComponentLeadingTo(adpar) > 1) { - // The first one must synchronize with all the others - if (ad.getFirstComponentLeadingTo(adpar) == last) { - for(i=1; i<ad.getNbComponentLeadingTo(adpar); i++) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("__sss = new SynchroSchemes();\n"); - indent(jo, dec); - jo.addCode("__sss = par__master" + cid.id + "__" + i + ".synchro(__sss, this);\n"); - } - } else { - // The others synchronize with the first one and exit - int id = ad.getIndexOfComponentLeadingTo(adpar, last); - indent(jo, dec); - jo.addCode("__sss = new SynchroSchemes();\n"); - indent(jo, dec); - jo.addCode("__sss = par__" + cid.id + "__" + id + ".synchro(__sss, this);\n"); - makeEndCode(jo, dec, endNeeded); - return; - } - - }*/ - - JOperation jo1 = null; - //String name; - int idop; - - if (adpar.getNbNext() > 1) { - // We assume that there is no synchronization - boolean b = true;//adpar.isAValidMotif(t); - - if ((adpar.nbGate() == 0) ||(adpar.getNbNext() > 2) || (!b)) { - indent(jo, dec); - jo.addCode("int []tab = new int[" + adpar.getNbNext() + "];\n"); - for(i=0; i<adpar.getNbNext(); i++) { - jo1 = makeNewJOperation(tjp); - jo1.addStandardCode(); - idop = tjp.getOperationNb() - 1; - indent(jo, dec); - jo.addCode("tab[" + i + "] = " + idop + ";\n"); - // - generateStandardOperationsRec(t, tjp, ad, adpar.getNext(i), adpar, jo1, 2, true); - } - indent(jo, dec); - jo.addCode("jse.addParallelProcess(tab);\n"); - basicFinishJO(jo, dec); - } else { - - // Synchronization - // adpar : nb next == 2 - // Creation of new synchronization gates - /*Vector v = adpar.getGateList(); - Gate g; - JGate jg1; - JGate jg2; - int id = 0; - - for(i=0; i<v.size(); i++) { - g = (Gate)(v.elementAt(i)); - jg1 = jc.foundJGate(g.getName()); - if (jg1 != null) { - id = getMainClassOf(jc).getUniqueGateId(); - indent(jo, dec); - jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); - indent(jo, dec); - jo.addCode("JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"); - } - } - - // Preparing second process - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - idop = jc.getOperationNb() - 2; - generateStandardOperationsRec(t, jc, ad, adpar.getNext(1), adpar, jo1, dec, true); - //indent(jo, dec); - name = jc.getJavaName().toLowerCase() + "__" + idPar; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, jc.getOperationNb() - 2, dec) + "\n"); - - // First process - int idp = id - v.size() + 1; - for(i=0; i<v.size(); i++) { - g = (Gate)(v.elementAt(i)); - jg1 = jc.foundJGate(g.getName()); - if (jg1 != null) { - indent(jo, dec); - jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + (idp + i) + ")" + JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(jg1.getJName() + ".setRight()" + JKeyword.END_OP + "\n"); - } - } - - // Starting second process - - - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".setState(" + idop +")" + JKeyword.END_OP + "\n"); - indent(jo, dec); - jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); - idPar ++; - indent(jo, dec); - jo.addCode("\n"); - - - // Starting of one new process - generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); - **/ - } - } else { - generateStandardOperationsRec(t, tjp, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); - } - - } - - /*public void makePreemptionCode(TClass t, JavaClass jc, ActivityDiagram ad, ADPreempt adpre, ADComponent last, JOperation jo, int dec, boolean endNeeded) { - indent(jo, dec); - jo.addCode("\n"); - indent(jo, dec); - jo.addCode("// Preemption operator \n"); - - int i; - if (adpre.getNbNext() < 2) { - // no real preemption - generateStandardOperationsRec(t, jc, ad, adpre.getNext(0), adpre, jo, dec, endNeeded); - return; - } - - JOperation jo1; - String name = ""; - int idop; - // One thread is started per outside next - for(i=0; i<adpre.getNbNext(); i++) { - jo1 = makeNewJOperation(jc); - jo1.addStandardCode(); - idop = jc.getOperationNb() - 2; - generateStandardOperationsRec(t, jc, ad, adpre.getNext(i), adpre, jo1, 2, true); - name = jc.getJavaName().toLowerCase() + "__" + idPar; - jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, idop, dec) + "\n"); - - if (i>0) { - indent(jo, dec); - jo.addCode(name + ".setToPreempt(" + jc.getJavaName().toLowerCase() + "__" + (idPar - 1) + ");\n"); - } - jo.addCode(addThread(name, dec)); - indent(jo, dec); - jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); - idPar ++; - indent(jo, dec); - jo.addCode("\n"); - } - makeEndCode(jo, dec, true); - - }*/ - - public String makeJavaGuard(String guard) { - guard = guard.replace('[', '('); - guard = guard.replace(']', ')'); - - return makeJavaExpression(guard); - } - - public String makeJavaExpression(String guard) { - // min, max, div, mod, divs, not, or, and - // div mod divs are not currently supported - - // min -> Math.min - guard = Conversion.replaceOp(guard, "min", "Math.min"); - - // max -> Math.max - guard = Conversion.replaceOp(guard, "max", "Math.max"); - - // not -> ! - guard = Conversion.replaceOp(guard, "not", "!"); - - // or -> || - guard = Conversion.replaceOp(guard, "or", "||"); - - // and -> && - guard = Conversion.replaceOp(guard, "and", "&&"); - - return guard; - } - - private ComponentId getComponentId(ADComponent adc) { - ComponentId cid; - - for(int i=0; i<components.size(); i++) { - cid = components.elementAt(i); - if (cid.adc == adc) { - return cid; - } - } - - return null; - } - - /*private void prepareParallelOperators(TClass t, ActivityDiagram ad) { - ADComponent adc; - int idPar = 0; - ComponentId cid; - int i, j; - Gate g; - - for(i=0; i<ad.size(); i++) { - adc = (ADComponent)(ad.elementAt(i)); - if (adc instanceof ADParallel) { - cid = new ComponentId(adc, idPar); - - for(j=1; j<ad.getNbComponentLeadingTo(adc); j++) { - t.addNewGateIfApplicable("par__" + idPar + "__" + j); - t.addNewGateIfApplicable("par__master" + idPar + "__" + j); - } - - components.add(cid); - - idPar ++; - } - } - }*/ - - private void generateMainClass() { - mainClass = new MainClassSimu(MAIN_CLASS); - - } - - private void generateBasicCodeMainClass() { - mainClass.generateBasicCode(); - } - - private void generateOperationCodeMainClass() { - mainClass.generateOperationCode(); - } - - private void saveAsFileInMainClass(String path) throws FileException { - mainClass.saveAsFileIn(path); - } - - - private void generateJGateCreationMainClass() { - mainClass.addGateCode(generateJGateCreation(processes)); - } - - private void generateJGateSynchronisationMainClass() { - mainClass.addSynchroCode(generateJGateSynchronisation(processes)); - } - - private void generateProcessStartingMainClass() { - mainClass.addStartingCode(generateProcessCreation(processes, true)); - } - - private void generateCloneOperation(TJavaProcess tjp) { - String code = "\n\n public TProcess makeClone() {\n"; - code += tjp.getCloneCodeWithSpecialState("__tp", tjp.getJavaName(), 2); - code +=" setClone(__tp);\n"; - code +=" return __tp;\n"; - code +=" }\n\n"; - tjp.addOperationCode(code); - } - - - - private void printMainClass() { - - } - +/**Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille + * + * ludovic.apvrille AT enst.fr + * + * This software is a computer program whose purpose is to allow the + * edition of TURTLE analysis, design and deployment diagrams, to + * allow the generation of RT-LOTOS or Java code from this diagram, + * and at last to allow the analysis of formal validation traces + * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP + * from INRIA Rhone-Alpes. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + * + * /** + * Class TURTLE2SimuJava + * Creation: 19/06/2006 + * @version 1.0 19/06/2006 + * @author Ludovic APVRILLE + * @see + */ + +package translator.tosimujava; + +import java.util.*; + +//import ddtranslator.*; +import myutil.*; +import translator.*; +import translator.tojava.*; + +public class TURTLE2SimuJava { + + //private static int gateId; + //private int idSeq = 0; + //private int idPar = 0; + + private TURTLEModeling tm; + private LinkedList processes; + private MasterGateManager mgm; + private MainClassSimu mainClass; + //private MainClass mainclass; + private Vector components; + + private boolean debug; + private boolean longforint; + + public static String DECL_CODE_01 = "public class "; + public static String DECL_CODE_02 = " extends TProcess {"; + public static String DECL_CODE_03 = "public void startSequence() {"; + public static String DECL_CODE_04 = "public void startPreemptionTasks() {"; + public static String OP_SEQ = "startingSequence()"; + public static String OP_PRE = "startPreemptionTasks()"; + public static String OP_DEC = "execute__"; + public static String TH_EXCEPTION = "throws PreemptionException"; + public static String EXIT = "jse.exitRequest();\n"; + public static String ATTR_DEC = "__"; + public static final String JGATE = "Gate"; + + public static final String MAIN_CLASS = "MainClass"; + + public static final int NO_PROTOCOL = 0; + public static final int UDP = 1; + public static final int TCP = 2; + public static final int RMI = 3; + + public TURTLE2SimuJava(TURTLEModeling _tm) { + tm = _tm; + longforint = false; + components = new Vector(); + } + + public void saveJavaClasses(String path) throws FileException { + ListIterator iterator = processes.listIterator(); + TJavaProcess tjc; + + while(iterator.hasNext()) { + tjc = (TJavaProcess)(iterator.next()); + tjc.saveAsFileIn(path); + } + + saveAsFileInMainClass(path); + } + + public String getMainListFiles(String path) { + return path + mainClass.getName() + ".java "; + } + + public void setLongSelected(boolean b) { + longforint = b; + } + + public void printJavaClasses() { + ListIterator iterator = processes.listIterator(); + TJavaProcess tjp; + + while(iterator.hasNext()) { + tjp = (TJavaProcess)(iterator.next()); + TraceManager.addDev(tjp.getJavaName() + ":\n" + tjp.toString() + "\n\n"); + } + + printMainClass(); + } + + public void generateSimulationCode(boolean _debug) { + //int i; + debug = _debug; + + // Preprocessing + tm.makeRTLOTOSName(); + tm.translateWatchdogs(); + tm.translateInvocationIntoSynchronization(); + tm.translateActionStatesWithMultipleParams(); + mgm = new MasterGateManager(tm, false); + mgm.sort(); + + processes = new LinkedList(); + + // Creating classes & attributes & operations + generateMainClass(); + generateBasicCodeMainClass(); + //mainclass = new MainClass("MainClass"); + //mainclass.generateBasicCode(); + + generateJavaClasses1(); + generateJavaClasses2(); + + // Generate mainclass starting code + generateJGateCreation(); + generateJGateSynchronisation(); + generateProcessStarting(); + generateOperationCodeMainClass(); + + + + // Post computing + MasterGateManager.reinitNameRestriction(); + + } + + private void generateJavaClasses1() { + TClass t; + int i; + TJavaProcess tjp; + + for(i=0; i<tm.classNb(); i++) { + t = tm.getTClassAtIndex(i); + tjp = new TJavaProcess(t.getName(), t.isActive()); + tjp.setDeclarationCode(DECL_CODE_01 + tjp.getJavaName() + DECL_CODE_02); + processes.add(tjp); + generateBasicAttributes(t, tjp); + // Prepare parallels at activity diagram level -> new gates + //prepareParallelOperators(t, t.getActivityDiagram()); + generateGateAttributes(t, tjp); + tjp.generateAttributeDeclaration(); + tjp.generateGateDeclaration(); + tjp.generateJTToolAttributes(); + tjp.generateConstructor(); + generateCloneOperation(tjp); + } + } + + private void generateJavaClasses2() { + TClass t; + int i; + TJavaProcess tjp; + + for(i=0; i<tm.classNb(); i++) { + t = tm.getTClassAtIndex(i); + tjp = foundTJProcess(t.getName()); + if (tjp != null) { + //generateExternalPreemption(t, jc); + //TraceManager.addDev("Generate operations for " + jc.getJavaName()); + generateOperations(t, tjp); + //TraceManager.addDev("Done"); + } + } + } + + /* It does not verify that attributes are not keywords of the Java language */ + public void generateBasicAttributes(TClass t, TJavaProcess tjp) { + int i; + Param p; + JAttribute ja; + + Vector params = t.getParamList(); + + for(i=0; i<params.size(); i++) { + p = (Param)(params.elementAt(i)); + ja = new JAttribute(p, longforint); + tjp.addAttribute(ja); + } + } + + /* It does not verify that gates are not keywords of the Java language */ + public void generateGateAttributes(TClass t, TJavaProcess tjp) { + int i; + Gate g; + JSimuGate jsg; + + Vector gates = t.getGateList(); + + for(i=0; i<gates.size(); i++) { + g = (Gate)(gates.elementAt(i)); + jsg = new JSimuGate(g.getName(), g.isInternal()); + tjp.addGate(jsg); + } + } + + /*public void generateExternalPreemption(TClass t, JavaClass jc) { + int i; + JavaClass jc1, jc2; + Relation r; + TClass t1; + + LinkedList tclasses = new LinkedList(); + + for(i=0; i<tm.relationNb(); i++) { + r = tm.getRelationAtIndex(i); + TraceManager.addDev("t = " + t.getName() + " Relation=" + r); + if ((r.type == Relation.PRE) && (r.t1 == t)) { + //TraceManager.addDev("Preemption from t to " + r.t2.getName()); + jc1 = foundJClass(r.t2.getName()); + //TraceManager.addDev("jc1 = " + jc1.getJavaName()); + tclasses.add(jc1); + } + } + + if (tclasses.size() == 0) { + return; + } + + //TraceManager.addDev("Size=" + tclasses.size()); + + // Create the necessary operation + jc.addStartingPreemptionCode(JKeyword.INDENT + DECL_CODE_04 + "\n"); + + // Create all new threads + jc.addStartingPreemptionCode(generateJGateCreation(tclasses)); + jc.addStartingPreemptionCode(generateTClassCreation(tclasses, false)); + + + ListIterator iterator = tclasses.listIterator(); + ListIterator iterator1; + while(iterator.hasNext()) { + jc1 = (JavaClass)(iterator.next()); + jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(this);\n"); + iterator1 = tclasses.listIterator(); + while(iterator1.hasNext()) { + jc2 = (JavaClass)(iterator1.next()); + if (jc2 != jc1) { + jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.INDENT + jc1.getJavaName().toLowerCase() + ".setToPreempt(" + jc2.getJavaName().toLowerCase() + ");\n"); + } + } + + } + jc.addStartingPreemptionCode(generateTClassStarting(tclasses, false, false)); + jc.addStartingPreemptionCode(JKeyword.INDENT + JKeyword.STOP_CODE_N); + + }*/ + + private void generateOperations(TClass t, TJavaProcess tjp) { + generateStandardOperations(t, tjp); + tjp.generateAllOperations(); + } + + private void generateStandardOperations(TClass t, TJavaProcess tjp) { + ActivityDiagram ad = t.getActivityDiagram(); + ADComponent adc = ad.getStartState(); + + JOperation jo = makeNewJOperation(tjp); + jo.addStandardCode(); + + generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, 2, true); + } + + private JOperation newAndfinishJO(TJavaProcess tjp, JOperation jo, int dec) { + finishJO(jo, dec, tjp.getOperationNb()); + return makeNewJOperation(tjp); + } + + private void finishJO(JOperation jo, int dec, int nb) { + indent(jo, dec); + jo.addCode("functionId=" + nb + ";\n"); + dec--; + indent(jo, dec); + jo.addCode("}\n"); + } + + private void basicFinishJO(JOperation jo, int dec) { + dec--; + indent(jo, dec); + jo.addCode("}\n"); + } + + private JOperation makeNewJOperation(TJavaProcess tjp) { + JOperation jo = new JOperation(TURTLE2SimuJava.OP_DEC + (tjp.getOperationNb())); + jo.nb = tjp.getOperationNb(); + tjp.addOperation(jo); + jo.addCode(tjp.getDeclarationOfOp(tjp.getOperationNb())); + if (debug) { + jo.addCode("System.out.println(getName() + \" / id=\" + id + \"executes :\" + " + jo.nb + ");\n"); + } + return jo; + } + + private void generateStandardOperationsRec(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADComponent adc, ADComponent last, JOperation jo, int dec, boolean endNeeded) { + TraceManager.addDev("Generating std op rec " + jo.getName()); + if (adc instanceof ADActionStateWithGate) { + ADActionStateWithGate adaswg = (ADActionStateWithGate)adc; + indent(jo, dec); + if (debug) { + jo.addCode("System.out.println(\"Call on " + adaswg.getGate().getName() + " value=" + adaswg.getActionValue() + " from " + tjp.getJavaName() + "\");\n"); + } + // Call on gate! + manageCallOnGate(t, tjp, ad, adc, jo,dec, tjp.foundJGate(adaswg.getGate().getName()), adaswg.getActionValue(), null, null, endNeeded); + // Next component + //generateStandardOperationsRec(t, jc, adc.getNext(0), jo, dec, endNeeded); + } else if (adc instanceof ADActionStateWithParam) { + ADActionStateWithParam adaswp = (ADActionStateWithParam) adc; + indent(jo, dec); + //TraceManager.addDev("java expr: " + adaswp.brutToString()); + //TraceManager.addDev(" Modified java expr: " + makeJavaExpression(adaswp.brutToString())); + jo.addCode(makeJavaExpression(adaswp.brutToString()) + JKeyword.END_OP_N); + generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, dec, endNeeded); + } /*else if (adc instanceof ADChoice) { + ADChoice adch = (ADChoice) adc; + makeChoiceCode(t, tjp, ad, adch, jo, dec, endNeeded); + } */else if (adc instanceof ADDelay) { + ADDelay add = (ADDelay) adc; + makeIntervalCode(add.getValue(), add.getValue(), jo, dec); + jo = newAndfinishJO(tjp, jo, dec); + generateStandardOperationsRec(t, tjp, ad, add.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADJunction) { + ADJunction adj = (ADJunction) adc; + makeJunctionCode(t, tjp, ad, adj, jo, dec, endNeeded); + } else if (adc instanceof ADLatency) { + ADLatency adl = (ADLatency) adc; + makeIntervalCode("0", adl.getValue(), jo, dec); + jo = newAndfinishJO(tjp, jo, dec); + generateStandardOperationsRec(t, tjp, ad, adl.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADTimeInterval) { + ADTimeInterval adti = (ADTimeInterval) adc; + makeIntervalCode(adti.getMinValue(), adti.getMaxValue(), jo, dec); + jo = newAndfinishJO(tjp, jo, dec); + generateStandardOperationsRec(t, tjp, ad, adti.getNext(0), adc, jo, dec, endNeeded); + } else if (adc instanceof ADTLO) { + ADTLO adtlo = (ADTLO)(adc); + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\"Limited call on " + adtlo.getGate().getName() + " value=" + adtlo.getAction() + " from " + tjp.getJavaName() + "\");\n"); + manageCallOnGate(t, tjp, ad, adc, jo,dec, tjp.foundJGate(adtlo.getGate().getName()), adtlo.getAction(), adtlo.getLatency(), adtlo.getDelay(), endNeeded); + //generateStandardOperationsRec(t, jc, adtlo.getNext(0), jo, dec, endNeeded); + } else if (adc instanceof ADStop) { + makeEndCode(jo, dec, endNeeded); + }/*else if (adc instanceof ADSequence) { + makeSequenceCode(t, tjp, ad, (ADSequence)adc, jo, dec, endNeeded); + } */else if (adc instanceof ADParallel) { + makeParallelCode(t, tjp, ad, (ADParallel)adc, last, jo, dec, endNeeded); + } /*else if (adc instanceof ADPreempt) { + makePreemptionCode(t, tjp, ad, (ADPreempt)adc, last, jo, dec, endNeeded); + } else { + TraceManager.addDev("Operator not supported: " + adc.toString()); + if (endNeeded) { + makeEndCode(jo, dec, true); + } + }*/ + } + + private void indent(JOperation jo, int d) { + while(d>0) { + jo.addCode(JKeyword.INDENT); + d --; + } + } + + /*private String addThread(String name, int dec) { + String s = ""; + while(dec>0) { + s += JKeyword.INDENT; + dec --; + } + s+="internalThreads.add(" + name + ");\n"; + return s; + }*/ + + + /*private void generateRunOperation(JavaClass jc) { + int i; + JOperation run0 = jc.getOperationAt(0); + //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "try " + JKeyword.START_CODE_N); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "while(" + TURTLE2Java.T__GO + " == true)" + JKeyword.START_CODE_N); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + "switch(" + TURTLE2Java.T__STATE + ")" + JKeyword.START_CODE_N); + + for(i=1; i<jc.getOperationNb(); i++) { + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT); + run0.addCode("case " + (i-1) + ":\n"); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); + run0.addCode(jc.getCallToOp(i)); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); + run0.addCode("break" + JKeyword.END_OP_N); + } + + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT); + run0.addCode("default:\n"); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + " "); + run0.addCode(TURTLE2Java.T__GO + " = false" + JKeyword.END_OP_N); + + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.INDENT + JKeyword.STOP_CODE_N); + run0.addCode(JKeyword.INDENT + JKeyword.INDENT + JKeyword.STOP_CODE_N); + //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + OP_SEQ + JKeyword.END_OP_N); + //run0.addCode(JKeyword.INDENT + JKeyword.INDENT + "} catch (PreemptionException pe) {}\n"); + run0.addCode(JKeyword.INDENT + JKeyword.STOP_CODE); + }*/ + + private void generateJGateCreation() { + generateJGateCreationMainClass(); + //addGateCodeMainClass(generateJGateCreation(javaClasses)); + } + + private String generateJGateCreation(LinkedList toTakeIntoAccountJC) { + TJavaProcess tjp; + int j; + JSimuGate jg; + String s = ""; + + ListIterator iterator = processes.listIterator(); + + while(iterator.hasNext()) { + tjp = (TJavaProcess)(iterator.next()); + if (toTakeIntoAccountJC.contains(tjp)) { + for(j=0; j<tjp.getGateNb(); j++) { + jg = tjp.getGateAt(j); + jg.setJName(tjp.getJavaName() + "__" + jg.getName()); + s += JKeyword.INDENT + JKeyword.INDENT + "Gate " + jg.getJName() + " = new Gate(\"" + jg.getJName() + "\")" + JKeyword.END_OP + "\n"; + /*if (jg.hasAProtocol()) { + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setProtocol(" + jg.getProtocol() + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setLocalPort(" + jg.getLocalPort() + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setDestPort(" + jg.getDestPort() + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setDestHost(\"" + jg.getDestHost() + "\")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg.getJName() + ".setLocalHost(\"" + jg.getLocalHost() + "\")" + JKeyword.END_OP + "\n"; + }*/ + } + } + } + + return s; + } + + private void generateJGateSynchronisation() { + generateJGateSynchronisationMainClass(); + //mainclass.addSynchroCode(generateJGateSynchronisation(javaClasses)); + } + + private String generateJGateSynchronisation(LinkedList toTakeIntoAccountJC) { + // Assume that all invocation operations have been removed + //TClass t; + Relation r; + int i, j; + TJavaProcess tjp1, tjp2; + JSimuGate jg1, jg2; + //Gate g; + String s = ""; + //MainClassSimu tmpc; + + if (toTakeIntoAccountJC.size() == 0) { + return ""; + } + + //tmpc=mainClass; + + for(i=0; i<tm.relationNb(); i++) { + r = tm.getRelationAtIndex(i); + if (r.type == Relation.SYN) { + tjp1 = foundTJProcess(r.t1.getName()); + tjp2 = foundTJProcess(r.t2.getName()); + if ((tjp1 != null) && (tjp2 != null)) { + if (toTakeIntoAccountJC.contains(tjp1) && toTakeIntoAccountJC.contains(tjp2)) { + for(j=0; j<r.gatesOfT1.size(); j++) { + TraceManager.addDev("Gates 1)" + ((Gate)(r.gatesOfT1.elementAt(j))).getName() + " 2:" + ((Gate)(r.gatesOfT2.elementAt(j))).getName()); + jg1 = tjp1.foundJGate(((Gate)(r.gatesOfT1.elementAt(j))).getName()); + jg2 = tjp2.foundJGate(((Gate)(r.gatesOfT2.elementAt(j))).getName()); + //TraceManager.addDev("foundJGate"); + if ((jg1 != null) && (jg2 != null)) { + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".synchroGate = " + jg2.getJName() + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".synchroGate = " + jg1.getJName() + JKeyword.END_OP + "\n"; + } + } + } + } + } + } + + // Gates of parallel operators in activity diagrams + //Vector v; + //String name, nameSearched;*/ + + //tmpc.addSynchroCode(JKeyword.INDENT + JKeyword.INDENT + "/* Parallel operators of activity diagrams */\n"); + /*ListIterator iterator = javaClasses.listIterator(); + JavaClass jc; + + while(iterator.hasNext()) { + jc1 = (JavaClass)(iterator.next()); + if (toTakeIntoAccountJC.contains(jc1)) { + for(i=0; i<jc1.getGateNb(); i++) { + jg1 = jc1.getGateAt(i); + name = jg1.getName(); + if (name.startsWith("par__master")) { + nameSearched = "par__" + name.substring(11, name.length()); + jg2 = jc1.foundJGate(nameSearched); + if (jg2 != null) { + id = getMainClassOf(jc1).getUniqueGateId(); + s += JKeyword.INDENT + JKeyword.INDENT + "JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"; + s += JKeyword.INDENT + JKeyword.INDENT + jg2.getJName() + ".setRight()" + JKeyword.END_OP + "\n"; + } + } + } + } + }*/ + + return s; + } + + /*private String generateExternalSequence(LinkedList toTakeIntoAccount, boolean onlyActiveClasses) { + JavaClass jc; + String s = ""; + LinkedList ll; + LinkedList one; + + s += "\n"; + // Sequence code + s+= "\n"; + + + ListIterator iterator = javaClasses.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (toTakeIntoAccount.contains(jc)) { + ll = listClassesStartingAt(jc.getJavaName()); + TraceManager.addDev("Getting list for" + jc.getJavaName()); + if ((ll != null) && (ll.size() > 0)) { + jc.addStartingSequenceCode(JKeyword.INDENT + DECL_CODE_03 + "\n"); + jc.addStartingSequenceCode(generateJGateCreation(ll)); + jc.addStartingSequenceCode(generateJGateSynchronisation(ll)); + jc.addStartingSequenceCode(generateTClassCreation(ll, false)); + jc.addStartingSequenceCode(generateCodeStartingSeq(ll)); + jc.addStartingSequenceCode(generateTClassStarting(ll, false, false)); + jc.addStartingSequenceCode(JKeyword.INDENT + JKeyword.STOP_CODE_N); + if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { + one = new LinkedList(); + one.add(jc); + s+= generateCodeStartingSeq(one);//JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; + } + } + } + } + return s; + }*/ + + /*private String generateCodeStartingSeq(LinkedList ll) { + String s = ""; + JavaClass jc; + ListIterator iterator = ll.listIterator(); + + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + s+= JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".setStartingSequence(true)" + JKeyword.END_OP + "\n"; + } + return s; + }*/ + + /*private LinkedList listClassesStartingAt(String name) { + TClass t1, t2; + Relation r; + JavaClass jc; + LinkedList ll = new LinkedList(); + for(int i=0; i<tm.relationNb(); i++) { + r = tm.getRelationAtIndex(i); + if (r.type == Relation.SEQ) { + TraceManager.addDev("Found one seq relation"); + if(r.t1.getName().equals(name)) { + jc = foundJClass(r.t2.getName()); + if (jc != null) { + ll.add(jc); + } + } + } + } + return ll; + }*/ + + private void generateProcessStarting() { + generateProcessStartingMainClass(); + } + + private String generateProcessCreation(LinkedList toTakeIntoAccount, boolean onlyActiveClasses) { + TJavaProcess tjc; + String s = ""; + + ListIterator iterator = processes.listIterator(); + + while(iterator.hasNext()) { + tjc = (TJavaProcess)(iterator.next()); + if (toTakeIntoAccount.contains(tjc)) { + if ((!onlyActiveClasses) || (onlyActiveClasses && tjc.isActive())) { + s += tjc.getCreationCode(tjc.getJavaName().toLowerCase()) + "\n"; + } + } + } + + return s; + } + + + /*private String generateProcessStarting(LinkedList toTakeIntoAccount, boolean onlyActiveClasses, boolean internal) { + JavaClass jc; + String s = ""; + ListIterator iterator = javaClasses.listIterator(); + while(iterator.hasNext()) { + jc = (JavaClass)(iterator.next()); + if (toTakeIntoAccount.contains(jc)) { + if ((!onlyActiveClasses) || (onlyActiveClasses && jc.isActive())) { + if (internal) { + s += addThread(jc.getJavaName().toLowerCase(), 2); + } + s += JKeyword.INDENT + JKeyword.INDENT + jc.getJavaName().toLowerCase() + ".start()" + JKeyword.END_OP + "\n"; + } + } + } + + return s; + }*/ + + + private TJavaProcess foundTJProcess(String name) { + TJavaProcess tjp; + ListIterator iterator = processes.listIterator(); + + while(iterator.hasNext()) { + tjp = (TJavaProcess)(iterator.next()); + if (tjp.getTURTLEName().equals(name)) { + return tjp; + } + } + + return null; + } + + private void makeSynchroSchemes(TClass t, JOperation jo, int dec, JSimuGate jg, String value) { + int i; + boolean sending; + boolean nat; + + indent(jo, dec); + jo.addCode("//Synchro on " + jg.getName() + " with action = " + value + " \n"); + + + indent(jo, dec); + jo.addCode("sizeSynchros = " + tm.getNbOfSynchroItems(value) + ";\n"); + + // Pre syncro + for(i=0; i<tm.getNbOfSynchroItems(value); i++) { + // Sending + indent(jo, dec); + sending = tm.isSendingSynchro(value, i); + if (sending) { + jo.addCode("synchros[" + i + "].sending = true;\n"); + } else { + jo.addCode("synchros[" + i + "].sending = false;\n"); + } + + // Type and value + indent(jo, dec); + + nat = tm.isNaturalSynchro(t, value, i); + if (nat) { + jo.addCode("synchros[" + i + "].type = 0;\n"); + } else { + jo.addCode("synchros[" + i + "].type = 1;\n"); + } + + if (sending) { + indent(jo, dec); + if (nat) { + jo.addCode("synchros[" + i + "].valueNat = (int)(" + tm.getSynchroValueAt(value, i) + ");\n"); + } else { + jo.addCode("synchros[" + i + "].valueBool = " + tm.getSynchroValueAt(value, i) + ";\n"); + } + } + } + } + + private void manageCallOnGate(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JSimuGate jg, String value, String latency, String delay, boolean endNeeded) { + + if (adc.getPreJavaCode() != null) { + addUserCode(jo, dec, adc.getPreJavaCode()); + } + + + if ((delay != null) && (delay.length() == 0)) { + delay = null; + } + + if ((latency != null) && (latency.length() == 0)) { + latency = null; + } + + if ((delay != null) && (latency == null)) { + latency = "0"; + } + + + if ((delay != null) && (latency != null)) { + JOperation jo1 = makeNewJOperation(tjp); + makePostSynchro(t, tjp, ad, adc, jo1, dec, jg, value, endNeeded); + generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo1, dec, endNeeded); + JOperation jo2 = makeNewJOperation(tjp); + generateStandardOperationsRec(t, tjp, ad, adc.getNext(1), adc, jo2, dec, endNeeded); + makeSynchroSchemes(t, jo, dec, jg, value); + indent(jo, dec); + jo.addCode("jse.synchroTimeLimitedRequest(" + jg.getName() + ", " + latency + ", " + delay + " ," + jo1.nb + "," + jo2.nb + ");\n"); + finishJO(jo, dec, jo1.nb); + } else { + makeSynchroSchemes(t, jo, dec, jg, value); + indent(jo, dec); + jo.addCode("jse.synchroRequest(" + jg.getName() + ");\n"); + jo = newAndfinishJO(tjp, jo, dec); + makePostSynchro(t, tjp, ad, adc, jo, dec, jg, value, endNeeded); + generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, dec, endNeeded); + } + + + /*// Removing all elements from the Vector + makeSynchroSchemes(t, jo, dec, jg, value); + + + // Synchro + + jo.addCode("\n"); + //indent(jo, dec); + + if (delay != null) { + indent(jo, dec); + jo.addCode("__sss.maxDelay = " + delay + ";\n"); + } + + indent(jo, dec); + jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); + indent(jo,dec); + jo.addCode("__ssss = new SynchroSchemes[1];\n"); + indent(jo,dec); + jo.addCode("__ssss[0] = __sss;\n"); + indent(jo,dec); + jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n"); + + indent(jo, dec); + if (debug) + jo.addCode("TraceManager.addDev(\"Call terminated for" + jc.getJavaName() + "\");\n"); + + if (delay != null) { + indent(jo, dec); + jo.addCode("if (__sss == null) {\n"); + dec ++; + + if (adc.getNext(1) == null) { + makeEndCode(jo, dec, endNeeded); + } else { + generateStandardOperationsRec(t, tjp, ad, adc.getNext(1), adc, jo, dec, false); + } + + dec --; + indent(jo, dec); + jo.addCode("} else {\n"); + dec ++; + makePostSynchro(t, tjp, ad, adc, jo, dec, jg, value, false); + dec --; + indent(jo, dec); + jo.addCode("}\n"); + + if (endNeeded) { + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + } else { + makePostSynchro(t, tjp, ad, adc, jo, dec, jg, value, endNeeded); + }*/ + } + + private void makePostSynchro(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADComponent adc, JOperation jo, int dec, JSimuGate jg, String value, boolean endNeeded) { + int i; + boolean sending, nat; + + // Post synchro + for(i=0; i<tm.getNbOfSynchroItems(value); i++) { + sending = tm.isSendingSynchro(value, i); + if (!sending) { + nat = tm.isNaturalSynchro(t, value, i); + indent(jo, dec); + if (nat) { + jo.addCode(tm.getShortSynchroValueAt(value, i) + " = synchros[" + i + "].valueNat;\n"); + } else { + jo.addCode(tm.getShortSynchroValueAt(value, i) + " = synchros[" + i + "].valueBool;\n"); + } + } + } + + jo.addCode("\n"); + + if (adc.getPostJavaCode() != null) { + addUserCode(jo, dec, adc.getPostJavaCode()); + } + + //generateStandardOperationsRec(t, tjp, ad, adc.getNext(0), adc, jo, dec, endNeeded); + } + + public void addUserCode(JOperation jo, int dec, String code) { + jo.addNonFormattedCode(dec, "/* User Code */\n" + code + "\n/* End of User Code */\n"); + } + + public void makeEndCode(JOperation jo, int dec, boolean endNeeded) { + indent(jo, dec); + //jo.addCode(TURTLE2Java.T__GO + " = false" + JKeyword.END_OP_N); + if (endNeeded) { + indent(jo, dec); + jo.addCode(TURTLE2SimuJava.EXIT); + dec --; + indent(jo, dec); + jo.addCode("}"); + } + } + + /*public void makeChoiceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADChoice adch, JOperation jo, int dec, boolean endNeeded) { + int nbG = adch.getNbGuard(); + int i; + String guard; + JGate jg; + ADActionStateWithGate adag; + long delay; + String delays; + + if (nbG < 1) { + makeEndCode(jo, dec, endNeeded); + return; + } + + // Commentary + indent(jo, dec); + jo.addCode("//Managing choice\n"); + + // building array + indent(jo, dec); + jo.addCode("__bchoice = new boolean[" + nbG + "];\n"); + + for(i=0; i<nbG; i++) { + indent(jo, dec); + if (!(adch.isGuarded(i))) { + jo.addCode("__bchoice[" + i + "] = true;\n"); + } else { + guard = adch.getGuard(i); + if (guard == null) { + jo.addCode("__bchoice[" + i + "] = false;\n"); + } else { + guard = makeJavaGuard(guard); + jo.addCode("__bchoice[" + i + "] = " + guard + ";\n"); + } + } + } + + + if (adch.isSpecialChoice()) { + indent(jo, dec); + jo.addCode("__bchoice__synchro = new SynchroSchemes[" + nbG + "];\n"); + + + for(i=0; i<nbG; i++) { + adag = adch.getADActionStateWithGate(i); + jg = jc.foundJGate(adag.getGate().getName()); + if (adag.getPreJavaCode() != null) { + addUserCode(jo, dec, adag.getPreJavaCode()); + } + + makeSynchroSchemes(t, jo, dec, jg, adag.getActionValue()); + + indent(jo, dec); + jo.addCode("__sss.jgate = " + jg.getName() + ";\n"); + indent(jo, dec); + delays = adch.getMinDelay(i); + + jo.addCode("__sss.minDelay =" + delays + ";\n"); + indent(jo, dec); + delays = adch.getMaxDelay(i); + + jo.addCode("__sss.maxDelay =" + delays + ";\n"); + + indent(jo, dec); + jo.addCode("__bchoice__synchro[" + i + "] = __sss;\n"); + + } + + // new scheme + indent(jo, dec); + jo.addCode("//Special choice \n\n"); + + indent(jo, dec); + jo.addCode("__nchoice = LibLogicalOperator.nbOfTrue(__bchoice);\n"); + + if (debug) { + indent(jo, dec); + jo.addCode("System.out.println(\" __nchoice=\" + __nchoice + \" \");\n"); + } + indent(jo,dec); + jo.addCode("__ssss = new SynchroSchemes[__nchoice];\n"); + + indent(jo, dec); + jo.addCode("__cpt1 = 0; __cpt2 = 0;\n"); + indent(jo, dec); + jo.addCode("while(__cpt2 < __nchoice) {\n"); + dec ++; + indent(jo, dec); + jo.addCode("if (__bchoice[__cpt1] == true) {\n"); + dec ++; + + indent(jo, dec); + jo.addCode("__ssss[__cpt2] = __bchoice__synchro[__cpt1];\n"); + indent(jo, dec); + jo.addCode("__cpt2++;\n"); + + dec --; + indent(jo, dec); + jo.addCode("}\n"); + indent(jo, dec); + jo.addCode("__cpt1 ++;\n"); + + dec --; + indent(jo, dec); + jo.addCode("}\n"); + + indent(jo,dec); + jo.addCode("__sss = MasterSynchro.master.synchro(__ssss, this);\n\n"); + + // After choice has been made + // Determine the nchoice number .. + + indent(jo,dec); + jo.addCode("__cpt1 = 0;\n"); + indent(jo,dec); + jo.addCode("while(__cpt1 < __nchoice) {\n"); + dec ++; + indent(jo,dec); + jo.addCode("if (__bchoice__synchro[__cpt1] == __sss) break;\n"); + indent(jo,dec); + jo.addCode("__cpt1 ++;\n"); + dec--; + indent(jo,dec); + jo.addCode("}\n\n"); + + // switching a path + indent(jo, dec); + jo.addCode("switch(__cpt1) {\n"); + + dec ++; + for(i=0; i<nbG; i++) { + indent(jo, dec); + jo.addCode("case " + i + ":\n"); + dec ++; + + if (debug) { + indent(jo, dec); + jo.addCode("System.out.println(\"Going to branch " + i + "\");\n"); + } + // Get values from synchro + adag = adch.getADActionStateWithGate(i); + jg = jc.foundJGate(adag.getGate().getName()); + + makePostSynchro(t, jc, ad, adag, jo, dec, jg, adag.getActionValue(), false); + + //generateStandardOperationsRec(t, jc, ad, adch.getADActionStateWithGate(i).getNext(0), adch.getADActionStateWithGate(i), jo, dec, false); + indent(jo, dec); + jo.addCode("break;\n"); + dec --; + } + dec --; + indent(jo, dec); + jo.addCode("}\n"); + + + if (endNeeded) { + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + + } else { + // old scheme + + + // Deciding of a path + indent(jo, dec); + jo.addCode("__nchoice = LibLogicalOperator.makeChoice(__bchoice);\n"); + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\" __nchoice=\" + __nchoice + \" \");\n"); + + // switching a path + indent(jo, dec); + jo.addCode("switch(__nchoice) {\n"); + + dec ++; + for(i=0; i<nbG; i++) { + indent(jo, dec); + jo.addCode("case " + i + ":\n"); + dec ++; + indent(jo, dec); + if (debug) + jo.addCode("System.out.println(\"Guard #" + i + "\");\n"); + generateStandardOperationsRec(t, jc, ad, adch.getNext(i), adch, jo, dec, false); + indent(jo, dec); + jo.addCode("break;\n"); + dec --; + } + + dec --; + indent(jo, dec); + jo.addCode("}\n\n"); + + if (endNeeded) { + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + } + }*/ + + public void makeIntervalCode(String minExpr, String maxExpr, JOperation jo, int dec) { + minExpr = makeJavaExpression(minExpr); + maxExpr = makeJavaExpression(maxExpr); + + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("jse.addDelay(" + minExpr + ", " + maxExpr + ");\n"); + } + + /*public void makeLatencyCode(String expr, JOperation jo, int dec) { + expr = makeJavaExpression(expr); + + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("//Non deterministic delay of " + expr + "\n"); + indent(jo, dec); + //jo.addCode("LibTemporalOperator.waitForAtMost( (" + expr + ") * " + millis + ", (" + expr + ") *" + nanos + ");\n"); + }*/ + + public void makeJunctionCode(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADJunction adj, JOperation jo, int dec, boolean endNeeded) { + int numOp; + JOperation jo1; + boolean makeOp = false; + ComponentId cid; + + cid = getComponentId(adj); + + if (cid != null) { + numOp = cid.id; + jo1 = tjp.getOperationAt(numOp-1); + } else { + jo1 = makeNewJOperation(tjp); + jo1.addStandardCode(); + numOp = tjp.getOperationNb() - 1; + cid = new ComponentId(adj, numOp); + components.add(cid); + makeOp = true; + } + + if (endNeeded) { + indent(jo, dec); + jo.addCode(TURTLE2SimuJava.OP_DEC + numOp + "();\n"); + dec --; + indent(jo, dec); + jo.addCode("}\n"); + } + + if (makeOp) { + generateStandardOperationsRec(t, tjp, ad, adj.getNext(0), adj, jo1, 2, true); + } + } + + /*public void makeSequenceCode(TClass t, JavaClass jc, ActivityDiagram ad, ADSequence adseq, JOperation jo, int dec, boolean endNeeded) { + // One thread is created to execute each sequenced operation + JOperation jo1; + String name; + int i; + + if (adseq.getNbNext() > 1) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("// Sequence operator \n"); + indent(jo, dec); + jo.addCode("\n"); + for(i=0; i<adseq.getNbNext()-1; i++) { + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + generateStandardOperationsRec(t, jc, ad, adseq.getNext(i), adseq, jo1, dec, true); + //indent(jo, dec); + name = jc.getJavaName().toLowerCase() + "__" + idSeq; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, jc.getOperationNb() - 2, dec) + "\n"); + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode("try {\n"); + dec ++; + indent(jo, dec); + jo.addCode(name+ ".join();\n"); + dec --; + indent(jo, dec); + jo.addCode("} catch (InterruptedException ie) {TraceManager.addDev(\"Interrupted\");}\n"); + idSeq ++; + indent(jo, dec); + jo.addCode("\n"); + } + generateStandardOperationsRec(t, jc, ad, adseq.getNext(adseq.getNbNext()-1), adseq, jo, dec, endNeeded); + } else { + generateStandardOperationsRec(t, jc, ad, adseq.getNext(0), adseq, jo, dec, endNeeded); + } + }*/ + + public void makeParallelCode(TClass t, TJavaProcess tjp, ActivityDiagram ad, ADParallel adpar, ADComponent last, JOperation jo, int dec, boolean endNeeded) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("// Parallel operator \n"); + + int i; + //ComponentId cid = getComponentId(adpar); + + /*if (ad.getNbComponentLeadingTo(adpar) > 1) { + // The first one must synchronize with all the others + if (ad.getFirstComponentLeadingTo(adpar) == last) { + for(i=1; i<ad.getNbComponentLeadingTo(adpar); i++) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("__sss = new SynchroSchemes();\n"); + indent(jo, dec); + jo.addCode("__sss = par__master" + cid.id + "__" + i + ".synchro(__sss, this);\n"); + } + } else { + // The others synchronize with the first one and exit + int id = ad.getIndexOfComponentLeadingTo(adpar, last); + indent(jo, dec); + jo.addCode("__sss = new SynchroSchemes();\n"); + indent(jo, dec); + jo.addCode("__sss = par__" + cid.id + "__" + id + ".synchro(__sss, this);\n"); + makeEndCode(jo, dec, endNeeded); + return; + } + + }*/ + + JOperation jo1 = null; + //String name; + int idop; + + if (adpar.getNbNext() > 1) { + // We assume that there is no synchronization + boolean b = true;//adpar.isAValidMotif(t); + TraceManager.addDev("Nb of gates = " + adpar.nbGate() + " valueGate=" + adpar.getValueGate()); + if ((adpar.nbGate() == 0) ||(adpar.getNbNext() > 2) || (!b)) { + indent(jo, dec); + jo.addCode("int []tab = new int[" + adpar.getNbNext() + "];\n"); + for(i=0; i<adpar.getNbNext(); i++) { + jo1 = makeNewJOperation(tjp); + jo1.addStandardCode(); + idop = tjp.getOperationNb() - 1; + indent(jo, dec); + jo.addCode("tab[" + i + "] = " + idop + ";\n"); + //TraceManager.addDev("Parallel with next = " + adpar.getNext(i).toString()); + generateStandardOperationsRec(t, tjp, ad, adpar.getNext(i), adpar, jo1, 2, true); + } + indent(jo, dec); + jo.addCode("jse.addParallelProcess(tab);\n"); + basicFinishJO(jo, dec); + } else { + TraceManager.addDev("Parallel - synchro"); + // Synchronization + // adpar : nb next == 2 + // Creation of new synchronization gates + /*Vector v = adpar.getGateList(); + Gate g; + JGate jg1; + JGate jg2; + int id = 0; + + for(i=0; i<v.size(); i++) { + g = (Gate)(v.elementAt(i)); + jg1 = jc.foundJGate(g.getName()); + if (jg1 != null) { + id = getMainClassOf(jc).getUniqueGateId(); + indent(jo, dec); + jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); + indent(jo, dec); + jo.addCode("JMasterGate " + "mgate__" + id + " = new JMasterGate()" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + id + ")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setLeft()" + JKeyword.END_OP + "\n"); + } + } + + // Preparing second process + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + idop = jc.getOperationNb() - 2; + generateStandardOperationsRec(t, jc, ad, adpar.getNext(1), adpar, jo1, dec, true); + //indent(jo, dec); + name = jc.getJavaName().toLowerCase() + "__" + idPar; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, jc.getOperationNb() - 2, dec) + "\n"); + + // First process + int idp = id - v.size() + 1; + for(i=0; i<v.size(); i++) { + g = (Gate)(v.elementAt(i)); + jg1 = jc.foundJGate(g.getName()); + if (jg1 != null) { + indent(jo, dec); + jo.addCode(jg1.getJName() + "= new JGate(\"" + jg1.getName()+ "\");\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setMasterGate(" + "mgate__" + (idp + i) + ")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(jg1.getJName() + ".setRight()" + JKeyword.END_OP + "\n"); + } + } + + // Starting second process + + + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".setState(" + idop +")" + JKeyword.END_OP + "\n"); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + idPar ++; + indent(jo, dec); + jo.addCode("\n"); + + + // Starting of one new process + generateStandardOperationsRec(t, jc, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); + **/ + } + } else { + generateStandardOperationsRec(t, tjp, ad, adpar.getNext(0), adpar, jo, dec, endNeeded); + } + + } + + /*public void makePreemptionCode(TClass t, JavaClass jc, ActivityDiagram ad, ADPreempt adpre, ADComponent last, JOperation jo, int dec, boolean endNeeded) { + indent(jo, dec); + jo.addCode("\n"); + indent(jo, dec); + jo.addCode("// Preemption operator \n"); + + int i; + if (adpre.getNbNext() < 2) { + // no real preemption + generateStandardOperationsRec(t, jc, ad, adpre.getNext(0), adpre, jo, dec, endNeeded); + return; + } + + JOperation jo1; + String name = ""; + int idop; + // One thread is started per outside next + for(i=0; i<adpre.getNbNext(); i++) { + jo1 = makeNewJOperation(jc); + jo1.addStandardCode(); + idop = jc.getOperationNb() - 2; + generateStandardOperationsRec(t, jc, ad, adpre.getNext(i), adpre, jo1, 2, true); + name = jc.getJavaName().toLowerCase() + "__" + idPar; + jo.addCode(jc.getCreationCodeWithSpecialStateCurrentValues(name, idop, dec) + "\n"); + + if (i>0) { + indent(jo, dec); + jo.addCode(name + ".setToPreempt(" + jc.getJavaName().toLowerCase() + "__" + (idPar - 1) + ");\n"); + } + jo.addCode(addThread(name, dec)); + indent(jo, dec); + jo.addCode(name + ".start()" + JKeyword.END_OP + "\n"); + idPar ++; + indent(jo, dec); + jo.addCode("\n"); + } + makeEndCode(jo, dec, true); + + }*/ + + public String makeJavaGuard(String guard) { + guard = guard.replace('[', '('); + guard = guard.replace(']', ')'); + + return makeJavaExpression(guard); + } + + public String makeJavaExpression(String guard) { + // min, max, div, mod, divs, not, or, and + // div mod divs are not currently supported + + // min -> Math.min + guard = Conversion.replaceOp(guard, "min", "Math.min"); + + // max -> Math.max + guard = Conversion.replaceOp(guard, "max", "Math.max"); + + // not -> ! + guard = Conversion.replaceOp(guard, "not", "!"); + + // or -> || + guard = Conversion.replaceOp(guard, "or", "||"); + + // and -> && + guard = Conversion.replaceOp(guard, "and", "&&"); + + return guard; + } + + private ComponentId getComponentId(ADComponent adc) { + ComponentId cid; + + for(int i=0; i<components.size(); i++) { + cid = (ComponentId)(components.elementAt(i)); + if (cid.adc == adc) { + return cid; + } + } + + return null; + } + + /*private void prepareParallelOperators(TClass t, ActivityDiagram ad) { + ADComponent adc; + int idPar = 0; + ComponentId cid; + int i, j; + Gate g; + + for(i=0; i<ad.size(); i++) { + adc = (ADComponent)(ad.elementAt(i)); + if (adc instanceof ADParallel) { + cid = new ComponentId(adc, idPar); + + for(j=1; j<ad.getNbComponentLeadingTo(adc); j++) { + t.addNewGateIfApplicable("par__" + idPar + "__" + j); + t.addNewGateIfApplicable("par__master" + idPar + "__" + j); + } + + components.add(cid); + + idPar ++; + } + } + }*/ + + private void generateMainClass() { + mainClass = new MainClassSimu(MAIN_CLASS); + TraceManager.addDev("Adding mainclass :" + MAIN_CLASS); + } + + private void generateBasicCodeMainClass() { + mainClass.generateBasicCode(); + } + + private void generateOperationCodeMainClass() { + mainClass.generateOperationCode(); + } + + private void saveAsFileInMainClass(String path) throws FileException { + mainClass.saveAsFileIn(path); + } + + + private void generateJGateCreationMainClass() { + mainClass.addGateCode(generateJGateCreation(processes)); + } + + private void generateJGateSynchronisationMainClass() { + mainClass.addSynchroCode(generateJGateSynchronisation(processes)); + } + + private void generateProcessStartingMainClass() { + mainClass.addStartingCode(generateProcessCreation(processes, true)); + } + + private void generateCloneOperation(TJavaProcess tjp) { + String code = "\n\n public TProcess makeClone() {\n"; + code += tjp.getCloneCodeWithSpecialState("__tp", tjp.getJavaName(), 2); + code +=" setClone(__tp);\n"; + code +=" return __tp;\n"; + code +=" }\n\n"; + tjp.addOperationCode(code); + } + + + + private void printMainClass() { + TraceManager.addDev(mainClass.getName() + ":\n" + mainClass.toString() + "\n\n"); + } + } \ No newline at end of file