package jcircus.complexcomms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.jcsp.lang.Guard;
import net.sourceforge.czt.util.Visitor;
import net.sourceforge.czt.z.ast.ConstDecl;
import net.sourceforge.czt.z.ast.Decl;
import net.sourceforge.czt.z.ast.Expr;
import net.sourceforge.czt.z.ast.Pred;
import net.sourceforge.czt.z.ast.ProdExpr;
import net.sourceforge.czt.z.ast.RefExpr;
import net.sourceforge.czt.z.ast.SetExpr;
import net.sourceforge.czt.z.ast.VarDecl;
import net.sourceforge.czt.z.ast.ZDeclList;
import net.sourceforge.czt.z.ast.ZExprList;
import net.sourceforge.czt.z.ast.ZNameList;
import net.sourceforge.czt.circus.ast.Action1;
import net.sourceforge.czt.circus.ast.Action2;
import net.sourceforge.czt.circus.ast.CircusAction;
import net.sourceforge.czt.circus.ast.Communication;
import net.sourceforge.czt.circus.ast.CircusFieldList;
import net.sourceforge.czt.circus.ast.DotField;
import net.sourceforge.czt.circus.ast.GuardedAction;
import net.sourceforge.czt.circus.ast.HideAction;
import net.sourceforge.czt.circus.ast.InputField;
import net.sourceforge.czt.circus.ast.ExtChoiceAction;
import net.sourceforge.czt.circus.ast.IntChoiceAction;
import net.sourceforge.czt.circus.ast.MuAction;
import net.sourceforge.czt.circus.ast.ParAction;
import net.sourceforge.czt.circus.ast.PrefixingAction;
import net.sourceforge.czt.circus.util.Factory;
import java.math.BigInteger;
import jcircus.newfrontendmethod.FrontEndAnn;
import jcircus.newutil.DeclUtil;
import jcircus.newutil.ExceptionUtil;
import jcircus.newutil.TypeUtil;
import jcircus.util.Constants;
import jcircus.util.Util;
import jcircus.visitor.TranslatorVisitor;
//public static String guardsCode (String chanName, Communication communication, int min, int max, Visitor c)
public class CCCode {
public static String inputDecoration (Communication communication) {
String str = "";
CircusFieldList cfl = communication.getCircusFieldList();
for (int i = 0; i < cfl.size(); i++) {
if (cfl.get(i) instanceof InputField) {
str = str + "true";
}
else {
str = str + "false";
}
if (i != cfl.size() - 1) {
str = str + " , ";
}
}
return str;
}
public static String commValues (Communication c, Visitor v) {
String str = "";
CircusFieldList cfl = c.getCircusFieldList();
int size = cfl.size();
for (int i = 0; i < size; i++) {
if (cfl.get(i) instanceof InputField) {
str = str + "-1";
}
else {
str = str + "(" + ((DotField)cfl.get(i)).getExpr().accept(v) + ").getValue()";
}
if (i != size - 1) {
str = str + " , ";
}
}
return str;
}
public static String varNames (Communication c, Visitor v) {
String str = "";
CircusFieldList cfl = c.getCircusFieldList();
int size = cfl.size();
for (int i = 0; i < size; i++) {
if (cfl.get(i) instanceof InputField) {
str = str + "\"" + ((InputField)cfl.get(i)).getVariableZName().toString() + "\"";
}
else {
str = str + "\"" + "none" + "\"";
}
if (i != size - 1) {
str = str + " , ";
}
}
return str;
}
public static String [] varNamesAsArray (Communication c, Visitor v) {
CircusFieldList cfl = c.getCircusFieldList();
int size = cfl.size();
String [] str = new String [size];
for (int i = 0; i < size; i++) {
str [i] = "";
if (cfl.get(i) instanceof InputField) {
str [i] = ((InputField)cfl.get(i)).getVariableZName().toString();
}
else {
str [i] = "none";
}
}
return str;
}
/*private static String declarationsCode (int indent, String [] varNames, int label, String nameComplement, Vector <String> putNames, String typename) {
String str = "";
for (int i = 0; i < varNames.length; i++) {
if (!varNames[i].equals("none") && !putNames.contains(varNames [i])) {
str = str + ident (indent) + "CircusInteger " + varNames [i] + " = new CircusInteger (0);" + "\n";
putNames.addElement(varNames [i]);
System.out.print ("");
}
}
return str;
}*/
private static String [] getTypesOfFields (Expr expr) {
Factory f = new Factory ();
if (expr instanceof RefExpr || expr instanceof SetExpr) {
String typename = ((RefExpr)expr).getZName().toString();
String [] types = new String [1] ;
types [0] = typename;
return types;
}
else if (expr instanceof ProdExpr) {
ZExprList exprs = ((ProdExpr)expr).getZExprList();
int size = exprs.size();
String [] types = new String [size];
for (int i = 0; i < size; i++) {
types [i] = ((RefExpr)exprs.get(i)).getZName().toString();
}
return types;
}
else {
ExceptionUtil.throwImplementationException("getTypesOfFields", expr.getClass());
return new String [0];
}
}
public static String assignmentsAndDeclarationCode (int indent, String [] varNames, int label, String nameComplement, Expr expr) {
//Ex:
//int x = vfe.get("x");
//int y = vfe.get("y");
//
String [] typenames = getTypesOfFields (expr);
String str = "";
for (int i = 0; i < varNames.length; i++) {
if (!varNames[i].equals("none") /*&& !putNames.contains(varNames [i])*/) {
//str = str + ident (indent) + "final CircusInteger " + varNames [i] + " = new CircusInteger ((Integer)((HashMap) ccMaps_" + nameComplement + label + ".mapFromSelectToVarEnv.get (select_" + nameComplement + label + ")).get (\"" + varNames [i] + "\"));\n";
String auxtypename = typenames [i].contains("â„•")? "CircusInteger" : typenames [i];
str = str + ident (indent) + "final " + auxtypename + " " + varNames [i] + " = new " + auxtypename + " ((Integer)((HashMap) ccMaps_" + nameComplement + label + ".mapFromSelectToVarEnv.get (select_" + nameComplement + label + ")).get (\"" + varNames [i] + "\"));\n";
str = str + ident (indent) + "System.out.println (\"" + varNames[i] + " = \" + " + varNames [i] + ");\n";
}
}
return str;
}
public static String assignmentsCode (int indent, String [] varNames, int label, String nameComplement) {
//Ex:
//int x = vfe.get("x");
//int y = vfe.get("y");
//
String str = "";
for (int i = 0; i < varNames.length; i++) {
if (!varNames[i].equals("none")) {
str = str + ident (indent) + "/*CircusInteger*/ " + varNames [i] + " = new CircusInteger ((Integer)((HashMap) ccMaps_" + nameComplement + label + ".mapFromSelectToVarEnv.get (select_" + nameComplement + label + ")).get (\"" + varNames [i] + "\"));\n";
str = str + ident (indent) + "System.out.println (\"" + varNames[i] + " = \" + " + varNames [i] + ");\n";
}
}
return str;
}
public static String ident (int n) {
String str = "";
for (int i = 0; i < n; i++) {
str = str + " ";
}
return str;
}
public static boolean isComplexCommCase (ExtChoiceAction eca) {
PrefixingAction leftPrefixingAction, rightPrefixingAction;
boolean b = false;
if (eca.getLeftAction() instanceof PrefixingAction && eca.getRightAction() instanceof PrefixingAction) {
leftPrefixingAction = (PrefixingAction)eca.getLeftAction();
rightPrefixingAction = (PrefixingAction)eca.getRightAction();
Communication fstComm = leftPrefixingAction.getCommunication();
Communication sndComm = rightPrefixingAction.getCommunication();
if (fstComm.getCircusFieldList().size() > 1 && fstComm.getCircusFieldList().size() > 1) {
b = true;
}
}
return b;
}
public static boolean isComplexCommCase (IntChoiceAction eca) {
PrefixingAction leftPrefixingAction, rightPrefixingAction;
boolean b = false;
if (eca.getLeftAction() instanceof PrefixingAction && eca.getRightAction() instanceof PrefixingAction) {
leftPrefixingAction = (PrefixingAction)eca.getLeftAction();
rightPrefixingAction = (PrefixingAction)eca.getRightAction();
Communication fstComm = leftPrefixingAction.getCommunication();
Communication sndComm = rightPrefixingAction.getCommunication();
if (fstComm.getCircusFieldList().size() > 1 && fstComm.getCircusFieldList().size() > 1) {
b = true;
}
}
return b;
}
public static boolean isComplexCommCase (ParAction eca) {
PrefixingAction leftPrefixingAction, rightPrefixingAction;
boolean b = false;
if (eca.getLeftAction() instanceof PrefixingAction && eca.getRightAction() instanceof PrefixingAction) {
leftPrefixingAction = (PrefixingAction)eca.getLeftAction();
rightPrefixingAction = (PrefixingAction)eca.getRightAction();
Communication fstComm = leftPrefixingAction.getCommunication();
Communication sndComm = rightPrefixingAction.getCommunication();
if (fstComm.getCircusFieldList().size() > 1 && fstComm.getCircusFieldList().size() > 1) {
b = true;
}
}
return b;
}
public static String complexCommsSingleCommunicationCode (
Communication comm,
CircusAction prefixedAction,
Visitor c,
int min, int max,
Channel2TypeEnvironment env,
String paper, String src, String projectDir, String specName,
String procName,
int counter,
Vector <CCMapsGenerator> ccMapsGens,
HashMap <String, String> vars
) {
HashMap <String, String> singlecommvars = new HashMap <String, String> (vars);
String channelName = comm.getChannelExpr().getName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(channelName));
String code = "";
CCMapsGenerator ccmg = new CCMapsGenerator (paper, src, projectDir, specName, procName, counter, "comm_");
ccmg.concatCode (
" public HashMap mapFromSelectToVarEnv = new HashMap ();\n" +
" public HashMap mapLeftOrRight = new HashMap ();\n" +
" public CCMaps_comm_" + procName + "_" + counter + " () {\n" +
" makeEnvChannels ();\n" +
" makeOtherEnvs ();\n" +
" }\n" +
CCUtil.makeEnvChannelsCode (channelName, env.get(channelName)) +
" public void makeOtherEnvs () {\n" +
" " + CCUtil.envsCode (channelName, comm, mam.min, mam.max, c, 0, 0, true, env.get(channelName)).str +
" }\n" +
CCUtil.indexesbutfirstcode() +
CCUtil.makeSyncOnOtherEndsButFirstCode(channelName, comm, mam.min, mam.max, c, env.get(channelName), counter, env)
);
ccMapsGens.add(ccmg);
code = code +
"class SingleCommBranch" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (singlecommvars) +
" public SingleCommBranch" + counter + "(" + paramChoiceVariables (singlecommvars) + ") {\n" +
assignChoiceVariables (singlecommvars) +
" }\n" +
" public void run () {\n" +
" int select_comm_" + counter + ";\n" +
" abs_" + comm.getChannelExpr().getName().toString() + " = new " + "Abs_" + comm.getChannelExpr().getName().toString() + "();\n" +
" CCMaps_comm_" + procName + "_" + counter + " ccMaps_comm_" + counter + " = new CCMaps_comm_" + procName + "_" + counter + " ();\n" +
" select_comm_" + counter + " = (new Alternative (";
code = code + "\n" +
" new Guard []\n " +
" {\n" +
" \n" +
//" " + "\n" +
CCUtil.guardsCode(comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, env.get(comm.getChannelExpr().getName().toString()), counter) +
" }" +
" )).select();\n" +
" GPSE.resetEnds (" + channelName + ", " + "new int [] {" + TypeUtil.getTypeSizes(channelName, env) + "});\n" +
assignmentsAndDeclarationCode (3, varNamesAsArray (comm, c), counter, "comm_", env.getExpr(channelName));
code = code + prefixedAction.accept(c);
code = code +
" }\n" +
"}\n";
code = code +
"SingleCommBranch" + counter +
" singlecommbranch" + counter + " = new SingleCommBranch" + counter +
" (" + paramCallExtChoiceVariables (singlecommvars) + ");\n";
code = code +
"class FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (singlecommvars) +
" public FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " (" + paramChoiceVariables (singlecommvars) + ") {\n" +
assignChoiceVariables (singlecommvars) +
" }\n" +
" public void run () {\n";
code = code +
" CCMaps_comm_" + procName + "_" + counter +
".syncOnOtherEndsButFirst_" +
comm.getChannelExpr().getName().toString() +
"(" +
comm.getChannelExpr().getName().toString() + ", " +
"abs_" + comm.getChannelExpr().getName().toString() +
CCUtil.complementarCommParams (false, comm, env) + ", " +
FrontEndAnn.getFrontEndAnn(comm).toString() +
");\n" +
" }\n" +
"};\n";
code = code +
"FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " " +
"frontendsbutfirstcommbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " = " +
" new FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter +
" (" + paramCallExtChoiceVariables (singlecommvars) + ");\n";
code = code + "(new Parallel (new CSProcess [] {singlecommbranch" + counter + ", " + "frontendsbutfirstcommbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + "})).run();\n";
code = code + updateVariableValuesCode (singlecommvars, "singlecommbranch" + counter);
System.out.println ("*****************************\nCurrent time milis 2: " + System.currentTimeMillis());
return code;
}
public static String complexCommsPrefixingActionCode (
PrefixingAction pa,
Visitor c,
int min, int max,
Channel2TypeEnvironment env,
String paper,
String src,
String projectDir,
String specName,
String procName,
int counter,
Vector <CCMapsGenerator> ccMapsGens,
HashMap <String, String> vars
)
{
Communication comm = pa.getCommunication();
String channelName = comm.getChannelExpr().getZName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(comm.getChannelExpr().getName().toString()));
String code = "";
HashMap <String, String> prefactvars = new HashMap <String, String> (vars);
CCMapsGenerator ccmg = new CCMapsGenerator (paper, src, projectDir, specName, procName, counter, "");
ccmg.concatCode (
" public HashMap mapFromSelectToVarEnv = new HashMap ();\n" +
" public HashMap mapLeftOrRight = new HashMap ();\n" +
" public CCMaps_" + procName + "_" + counter + " () {\n" +
" makeEnvChannels ();\n" +
" makeOtherEnvs ();\n" +
" }\n" +
CCUtil.makeEnvChannelsCode (comm.getChannelExpr().getName().toString(), env.get(comm.getChannelExpr().getName().toString())) +
" public void makeOtherEnvs () {\n" +
" " + CCUtil.envsCode (comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, 0, 0, true, env.get(comm.getChannelExpr().getName().toString())).str +
" }\n" +
CCUtil.indexesbutfirstcode() +
CCUtil.makeSyncOnOtherEndsButFirstCode(comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, env.get(comm.getChannelExpr().getName().toString()), counter, env)
);
ccMapsGens.add(ccmg);
code = code +
"class PrefActBranch" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (vars) +
" public PrefActBranch" + counter + "(" + paramChoiceVariables (vars) + ") {\n" +
assignChoiceVariables (vars) +
" }\n" +
" public void run () {\n" +
" int select_" + counter + ";\n" +
" abs_" + comm.getChannelExpr().getName().toString() + " = new " + "Abs_" + comm.getChannelExpr().getName().toString() + "();\n" +
" CCMaps_" + procName + "_" + counter + " ccMaps_" + counter + " = new CCMaps_" + procName + "_" + counter + " ();\n" +
" select_" + counter + " = (new Alternative (";
code = code + "\n" +
" new Guard []\n " +
" {\n" +
" \n" +
CCUtil.guardsCode(comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, env.get(comm.getChannelExpr().getName().toString()), counter) +
" }" +
" )).select();\n" +
" GPSE.resetEnds (" + channelName + ", " + "new int [] {" + TypeUtil.getTypeSizes(channelName, env) + "});\n" +
assignmentsAndDeclarationCode (3, varNamesAsArray (comm, c), counter, "", env.getExpr(channelName));
code = code + pa.getCircusAction().accept(c);
code = code +
" }\n" +
"}\n";
code = code +
"PrefActBranch" + counter +
" prefactbranch" + counter + " = new PrefActBranch" + counter +
" (" + paramCallExtChoiceVariables (prefactvars) + ");\n";
code = code +
"class FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (prefactvars) +
" public FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " (" + paramChoiceVariables (prefactvars) + ") {\n" +
assignChoiceVariables (prefactvars) +
" }\n" +
" public void run () {\n";
code = code +
" CCMaps_" + procName + "_" + counter +
".syncOnOtherEndsButFirst_" +
comm.getChannelExpr().getName().toString() +
"(" +
comm.getChannelExpr().getName().toString() + ", " +
"abs_" + comm.getChannelExpr().getName().toString() +
CCUtil.complementarCommParams (false, comm, env) + ", " +
FrontEndAnn.getFrontEndAnn(comm).toString() +
");\n" +
" }\n" +
"};\n";
code = code +
"FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " " +
"frontendsbutfirstbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " = " +
" new FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter +
" (" + paramCallExtChoiceVariables (prefactvars) + ");\n";
code = code + "(new Parallel (new CSProcess [] {prefactbranch" + counter + ", " + "frontendsbutfirstbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + "})).run()";
System.out.println ("*****************************\nCurrent time milis 2: " + System.currentTimeMillis());
return code;
}
public static String [] getChannelNames (Communication [] c) { //TODO NAO TESTADO, MAS PROVAVELMENTE FUNCIONA, POIS EH TRIVIAL
String [] chanNames = new String [c.length];
for (int i = 0; i < chanNames.length; i++) {
chanNames [i] = c [i].getChannelExpr().getName().toString();
}
return chanNames;
}
public static List <Communication> getCommunications (CircusAction action) { //TODO NAO TESTADO
List <Communication> comms = new ArrayList <Communication> ();
if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
comms.addAll(getCommunications (left));
comms.addAll(getCommunications (right));
}
else if (action instanceof Action1) {
if (action instanceof GuardedAction || action instanceof HideAction || action instanceof MuAction /*|| TODO outras acoes unarias*/) {
comms.addAll(getCommunications (((Action1)action).getCircusAction()));
}
else if (action instanceof PrefixingAction) {
comms.add(((PrefixingAction)action).getCommunication());
}
}
return comms;
}
public static List <CircusAction> getPrefixedActions (CircusAction action) { //TODO NAO TESTADO
List <CircusAction> comms = new ArrayList <CircusAction> ();
if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
comms.addAll(getPrefixedActions (left));
comms.addAll(getPrefixedActions (right));
}
else if (action instanceof Action1) {
if (action instanceof GuardedAction || action instanceof HideAction || action instanceof MuAction /*|| TODO outras a��es un�rias*/) {
comms.addAll(getPrefixedActions (((Action1)action).getCircusAction()));
}
else if (action instanceof PrefixingAction) {
comms.add(((PrefixingAction)action).getCircusAction());
}
}
return comms;
}
public static List <CircusAction> getBranchedActions (CircusAction action) { //TODO NAO TESTADO
List <CircusAction> comms = new ArrayList <CircusAction> ();
if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
comms.addAll(getBranchedActions (left));
comms.addAll(getBranchedActions (right));
}
else if (action instanceof Action1) {
if (/*action instanceof GuardedAction ||*/ action instanceof HideAction || action instanceof MuAction /*|| TODO outras a��es un�rias*/) {
comms.addAll(getBranchedActions (((Action1)action).getCircusAction()));
}
else if (action instanceof PrefixingAction) {
comms.add(((PrefixingAction)action));
}
else if (action instanceof GuardedAction) {
comms.add(((GuardedAction)action));
}
}
return comms;
}
public static List <Pred> getPredicates (List <CircusAction> actions) {
List <Pred> predicates = new ArrayList <Pred> ();
Factory f = new Factory ();
for (int i = 0; i < actions.size(); i++) {
if (!(actions.get(i) instanceof GuardedAction)) {
predicates.add(f.createTruePred());
}
else {
GuardedAction ga = (GuardedAction)actions.get(i);
predicates.add(ga.getPred());
}
}
return predicates;
}
public static boolean isTranslatable (ExtChoiceAction eca) {
List <CircusAction> actions = getBranchedActions(eca);
List <Pred> predicates = getPredicates (actions);
CircusAction [] actionsArray = actionsToArray (actions);
boolean aux = true;
for (int i = 0; i < actionsArray.length; i++) {
if (!(actionsArray [i] instanceof PrefixingAction || actionsArray [i] instanceof GuardedAction))
aux = false;
}
return aux;
}
public static CircusAction [] actionsToArray (List <CircusAction> actions) {
CircusAction [] actionsArray = new CircusAction [actions.size()];
for (int i = 0; i < actions.size(); i++) {
actionsArray [i] = actions.get(i);
}
return actionsArray;
}
public static Communication [] commsToArray (List <Communication> comms) {
Communication [] commsArray = new Communication [comms.size()];
for (int i = 0; i < comms.size(); i++) {
commsArray [i] = comms.get(i);
}
return commsArray;
}
//countGuards (String chanName, Communication communication, int [] min, int [] max, Visitor c, Vector <Vector <BigInteger>> valuesSet, int label)
private static String booleansArrayCode (List <Pred> predicates, List <Communication> comms, Visitor v, Channel2TypeEnvironment env/*Vector <Vector <BigInteger>> valuesSet*/, int label) {
String strpredicates = "";
for (int i = 0; i < predicates.size(); i++) {
String chanName = comms.get(i).getChannelExpr().getZName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(chanName));
int numberOfGuards = CCUtil.countGuards
(chanName, comms.get(i), mam.min, mam.max, v, env.get(chanName), label);
for (int j = 0; j < numberOfGuards; j++) {
strpredicates += predicates.get(i).accept (v);
if (j < numberOfGuards - 1)
strpredicates += ", ";
}
if (i < predicates.size() - 1)
strpredicates += ", ";
}
String strfinal = "final boolean [] b_" + label + " = new boolean [] {" + strpredicates + "};";
return strfinal;
}
private Vector <String> getChannelNames (List <Communication> comms) {
Vector <String> names = new Vector <String> ();
int size = comms.size();
for (int i = 0; i < size; i++) {
names.addElement(comms.get(i).getChannelExpr().getZName().toString());
}
return names;
}
private static String declareExtChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + " " + var2type.get((String)keys [i]) + " " + ((String)keys [i]) + ";\n";
}
return str;
}
private static String assignChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + " this." + ((String)keys [i]) + " = " + ((String)keys [i]) + ";\n";
}
return str;
}
private static String paramChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + var2type.get((String)keys [i]) + " " + ((String)keys [i]);
if (i < keys.length - 1)
str = str + ", ";
}
return str;
}
private static String paramCallExtChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + ((String)keys [i]);
if (i < keys.length - 1)
str = str + ", ";
}
return str;
}
private static String updateVariableValuesCode (HashMap <String, String> vars, String classname) {
int size = vars.keySet().toArray().length;
String strreturn = "";
for (int i = 0; i < size; i++) {
String varname = (String) vars.keySet().toArray() [i];
strreturn = varname + " = " + classname + "." + varname + ";\n";
}
return strreturn;
}
public static String complexCommsExtChoiceCode (
ExtChoiceAction eca,
TranslatorVisitor c,
int min,
int max,
Channel2TypeEnvironment env,
String paper,
String src,
String projectDir,
String specName,
String procName,
int counter,
Vector <CCMapsGenerator> ccMapsGens,
HashMap <String, String> vars
) { //OK
HashMap <String, String> extchoicevars = new HashMap <String, String> (vars);
PrefixingAction leftPrefixingAction, rightPrefixingAction;
String code = "";
List <CircusAction> actionsWithoutPrefixing = getPrefixedActions(eca);
List <CircusAction> branchedActions = getBranchedActions (eca);
List <Pred> predicates = getPredicates (branchedActions);
CircusAction [] actionsArray = actionsToArray (actionsWithoutPrefixing);
List <Communication> comms = getCommunications (eca);
Communication [] commsArray = commsToArray (comms);
String [] channelNames = getChannelNames (commsArray);
CCMapsGenerator ccmg = new CCMapsGenerator (paper, src, projectDir, specName, procName, counter, "");
String concatCode = "";
concatCode =
" public HashMap mapFromSelectToVarEnv = new HashMap ();\n" +
" public HashMap mapLeftOrRight = new HashMap ();\n" +
" public CCMaps_" + procName + "_" + counter + " () {\n";
for (int i = 0; i < commsArray.length; i++) {
concatCode += " makeEnvChannels" + i + " ();\n";
concatCode += " makeOtherEnvs" + i + " ();\n";
}
concatCode += " }\n" +
CCUtil.makeEnvChannelsCode (channelNames, env);
int initCounterSelect = 0;
Vector <String> seenchans = new Vector <String> ();
for (int i = 0; i < commsArray.length; i++) {
String channame = commsArray [i].getChannelExpr().getZName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(channelNames [i]));
StringAndCounter sac = CCUtil.envsCode (channelNames [i],
commsArray [i], mam.min, mam.max, c, i, initCounterSelect, true, env.get(channelNames [i]));
concatCode +=
" public void makeOtherEnvs" + i + " () {\n" +
" " + sac.str +
" }\n";
if (!seenchans.contains(channame)) {
concatCode += CCUtil.makeSyncOnOtherEndsButFirstCode(comms.get(i).getChannelExpr().getName().toString(), comms.get (i), mam.min, mam.max, c, env.get(comms.get(i).getChannelExpr().getName().toString()), counter, env);
initCounterSelect += sac.counter;
seenchans.addElement(channame);
System.out.print ("");
}
}
concatCode += CCUtil.indexesbutfirstcode();
ccmg.concatCode(concatCode);
ccMapsGens.add(ccmg);
code =
//"int select_" + counter + ";\n" +
"final CCMaps_" + procName + "_" + counter + " ccMaps_" + counter
+ " = new CCMaps_" + procName + "_" + counter + " ();\n"
;
for (int i = 0; i < comms.size(); i++) {
code = code +
" abs_" + comms.get(i).getChannelExpr().getName().toString()
+ " = new " + "Abs_" + comms.get(i).getChannelExpr().getName().toString() + "();\n"
;
}
code += "\n" + booleansArrayCode (predicates, comms, c, env, counter);
code +=
"class ExtBranch" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (extchoicevars) +
" public ExtBranch" + counter + " (" + paramChoiceVariables (extchoicevars) + ") {\n" +
assignChoiceVariables (extchoicevars) +
" }\n" +
" public void run () {\n" +
" int select_" + counter + " = (new Alternative (" + "\n" +
" new Guard [] {\n";
for (int i = 0; i < channelNames.length; i++) {
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(channelNames [i]));
code = code +
"\n"
+ CCUtil.guardsCode(channelNames [i], commsArray [i], mam.min, mam.max, c, env.get(channelNames [i]), counter) + "\n";
}
code = code +
"\n })).select(" + "b_" + counter + ");\n";
for (int i = 0; i < channelNames.length; i++) {
code = code +
" GPSE.resetEnds (" + channelNames [i] + ", " + "new int [] {" + TypeUtil.getTypeSizes(channelNames [i], env) + "});\n";
}
Vector <String> putNames = new Vector <String> ();
//c.extChoiceCaseSwitch = true;
code = code +
" switch (((Integer)ccMaps_" + counter + ".mapLeftOrRight.get(select_" + counter + ")).intValue()) {\n";
for (int i = 0; i < commsArray.length; i++) {
code = code +
" case " + i + ": {\n" +
assignmentsAndDeclarationCode (3, varNamesAsArray (commsArray [i], c), counter, "", env.getExpr(commsArray [i].getChannelExpr().getZName().toString())) +
" " + actionsArray [i].accept(c) + "\n" +
" break;\n" +
" }\n";
}
//c.extChoiceCaseSwitch = false;
code = code +
" }\n" +
" }\n" +
"};\n";
if (counter == 60) {
System.out.print ("");
}
code = code +
"ExtBranch" + counter +
" extbranch" + counter + " = new ExtBranch" + counter +
" (" + paramCallExtChoiceVariables (extchoicevars) + ");\n";
seenchans = new Vector <String> ();
for (int i = 0; i < commsArray.length; i++) {
String channame = commsArray [i].getChannelExpr().getZName().toString();
if (!seenchans.contains(channame)) {
code = code +
"class FrontEndsButFirstBranch_" + channame + "_" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (extchoicevars) +
" public FrontEndsButFirstBranch_" + channame + "_" + counter + " (" + paramChoiceVariables (extchoicevars) + ") {\n" +
assignChoiceVariables (extchoicevars) +
" }\n" +
" public void run () {\n";
code = code +
" CCMaps_" + procName + "_" + counter +
".syncOnOtherEndsButFirst_" +
commsArray [i].getChannelExpr().getName().toString() +
"(" +
commsArray [i].getChannelExpr().getName().toString() + ", " +
"abs_" + commsArray [i].getChannelExpr().getName().toString() +
CCUtil.complementarCommParams (false, comms.get(i), env) + ", " +
FrontEndAnn.getFrontEndAnn(comms.get(i)).toString() +
");\n" +
" }\n" +
"};\n";
code = code +
"FrontEndsButFirstBranch_" + channame + "_" + counter + " " +
"frontendsbutfirstbranch_" + channame + "_" + counter + " = " +
" new FrontEndsButFirstBranch_" + channame + "_" + counter +
" (" + paramCallExtChoiceVariables (extchoicevars) + ");\n";
seenchans.addElement(channame);
}
}
code = code +
"(new Parallel (new CSProcess [] {extbranch" + counter + ", ";
seenchans = new Vector <String> ();
for (int i = 0; i < commsArray.length; i++) {
String channame = commsArray [i].getChannelExpr().getZName().toString();
if (!seenchans.contains(channame)) {
code = code + "frontendsbutfirstbranch_" + channame + "_" + counter;
seenchans.addElement(channame);
if (i < commsArray.length - 1)
code = code + ", ";
}
}
code = code + "})).run();\n";
code = code + updateVariableValuesCode (extchoicevars, "extbranch" + counter);
return code;
}
}