package jcircus.newfrontendmethod; import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Vector; import javax.swing.JOptionPane; import jcircus.anns.MuCallAnn; import jcircus.complementaryenvs.ActStackVarEnv; import jcircus.complementaryenvs.PId2PNameEnv; import jcircus.newutil.CallUtil; import jcircus.newutil.ChanSetUtil; import jcircus.newutil.ExceptionUtil; import jcircus.newutil.PrinterUtil; import jcircus.newutil.ProcessUtil; import jcircus.parallelism.Consumer; import jcircus.parallelism.ParallelismTestBuilders; import jcircus.parallelism.ParallelismUpdater; import net.sourceforge.czt.base.util.UnmarshalException; import net.sourceforge.czt.circus.ast.Action1; import net.sourceforge.czt.circus.ast.Action2; import net.sourceforge.czt.circus.ast.ActionPara; import net.sourceforge.czt.circus.ast.AssignmentCommand; import net.sourceforge.czt.circus.ast.BasicProcess; import net.sourceforge.czt.circus.ast.CallAction; import net.sourceforge.czt.circus.ast.CallProcess; import net.sourceforge.czt.circus.ast.CircusAction; import net.sourceforge.czt.circus.ast.CircusActionList; import net.sourceforge.czt.circus.ast.CircusCommand; import net.sourceforge.czt.circus.ast.CircusProcess; import net.sourceforge.czt.circus.ast.CommPattern; import net.sourceforge.czt.circus.ast.Communication; import net.sourceforge.czt.circus.ast.ExtChoiceAction; import net.sourceforge.czt.circus.ast.GuardedAction; import net.sourceforge.czt.circus.ast.IfGuardedCommand; 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.ParProcess; import net.sourceforge.czt.circus.ast.ParallelAction; import net.sourceforge.czt.circus.ast.ParamProcess; import net.sourceforge.czt.circus.ast.PrefixingAction; import net.sourceforge.czt.circus.ast.Process1; import net.sourceforge.czt.circus.ast.Process2; import net.sourceforge.czt.circus.ast.ProcessPara; import net.sourceforge.czt.circus.ast.VarDeclCommand; import net.sourceforge.czt.circus.util.Factory; import net.sourceforge.czt.parser.oz.ParseUtils; import net.sourceforge.czt.parser.util.ParseException; import net.sourceforge.czt.session.FileSource; import net.sourceforge.czt.session.SectionManager; import net.sourceforge.czt.z.ast.NarrSect; import net.sourceforge.czt.z.ast.Para; import net.sourceforge.czt.z.ast.Spec; import net.sourceforge.czt.z.ast.ZParaList; import net.sourceforge.czt.z.ast.ParaList; import net.sourceforge.czt.z.ast.ZSect; //Esta classe percorrerá a AST da especificação atrás de comunicações e sincronizações, //adicionando assinaturas FrontEndAnn a cada comunicação que aparecer, com um inteiro diferente public class FrontEndUpdater { HashMap procFrontEndCounterEnv = new HashMap (); HashMap procFrontEndMapEnv = new HashMap (); public HashMap getProcFrontEndCounterEnv () { return this.procFrontEndCounterEnv; } /*public void mountFrontEndCounterEnv (Spec spec) { }*/ public String strfrontends (String procName, String chanName, int startValue) { if (procName.equals("$$GUI$$")) return "new int [] {0}"; FrontEndCounter fec = procFrontEndCounterEnv.get(procName); Integer nfes = new Integer (0); if (fec != null) { if (fec.get(chanName) != null) { nfes = fec.get(chanName); } } String str = "new int [" /*+ nfes*/ + "] {"; for (int i = 0; i < nfes; i++) { int is = i + startValue; str = str + (new Integer (is)).toString(); if (i < nfes - 1) { str = str + ", "; } } //startValue = nfes + 1; return str + "}"; } public void updateSpec (Spec spec) { ZSect zSect; if (spec.getSect().get(0) instanceof NarrSect) zSect = (ZSect) spec.getSect().get(1); else zSect = (ZSect) spec.getSect().get(0); ZParaList paras = (ZParaList) zSect.getParaList(); int size = paras.size (); for (int i = 0; i < size; i++) { Para para = paras.get(i); System.out.print (""); if (para instanceof ProcessPara) { ProcessPara processPara = (ProcessPara) para; String procName = processPara.getZName().toString(); buildProcFrontEndCounterEnv (processPara, spec); } } buildProcFrontEndMappingEnv (); for (int i = 0; i < size; i++) { Para para = paras.get(i); System.out.print (""); if (para instanceof ProcessPara) { FrontEndCounter init = FrontEndCounter.createOneCounter (getFrontEndCounterKeys ((ProcessPara)para, spec)); ProcessPara processPara = (ProcessPara) para; updateProcessPara (processPara, spec, init); System.out.print(""); } } System.out.print (""); } private Object [] getFrontEndCounterKeys (ProcessPara para, Spec spec) { return getFrontEndCounterKeys (para.getCircusProcess(), spec); } private Object [] getFrontEndCounterKeys (CircusProcess process, Spec spec) { if (process instanceof BasicProcess) { CircusAction mainaction = ((BasicProcess)process).getMainAction(); FrontEndCounter fec = mountFrontEndCounter (mainaction, process, spec); return fec.getMap().keySet().toArray(); } else if (process instanceof ParamProcess) { return getFrontEndCounterKeys (((ParamProcess)process).getCircusProcess(), spec); } else { return new Object [] {}; } } private void updateProcessPara (ProcessPara processPara, Spec spec, FrontEndCounter init) { CircusProcess process = processPara.getCircusProcess(); String procName = processPara.getName().toString(); FrontEndMapping fem = this.procFrontEndMapEnv.get(procName); updateCircusProcess (process/*, fem*/, init, spec); } public void buildProcFrontEndCounterEnv (ProcessPara processPara, Spec spec) { CircusProcess process = processPara.getCircusProcess(); String processName = processPara.getZName().toString(); this.procFrontEndCounterEnv.put(processName, mountFrontEndCounter (process, spec)); } private Vector createVector (int x, int y) { Vector vec = new Vector (); for (int i = x; i <= y; i++) { vec.addElement(new Integer (i)); } return vec; } private FrontEndCounter one (FrontEndCounter fec) { Object [] keys = fec.getMap().keySet().toArray(); FrontEndCounter fec2 = new FrontEndCounter (); for (int i = 0; i < keys.length; i++) { fec2.put((String)keys [i], 1); //It has to start from one because the zero front-end is for the GUI } return fec2; } private FrontEndMapping constructFrontEndMapping (FrontEndCounter fec1, FrontEndCounter fec2) { Object [] keys1 = fec1.getMap().keySet().toArray(); FrontEndMapping fem = new FrontEndMapping (); for (int i = 0; i < keys1.length; i++) { if (fec2.getMap().containsKey((String)keys1 [i])) { Integer x1 = fec1.get((String)keys1 [i]); Integer x2 = fec2.get((String)keys1 [i]); fem.put((String)keys1 [i], createVector (x1, x2/* - 1*/)); } } return fem; } public void buildProcFrontEndMappingEnv () { Object [] keys = this.procFrontEndCounterEnv.keySet().toArray(); for (int i = 0; i < keys.length; i++) { FrontEndCounter fecaux = this.procFrontEndCounterEnv.get((String)keys [i]); FrontEndCounter fecprevious = (i == 0? one (fecaux) : this.procFrontEndCounterEnv.get((String)keys [i - 1]) ); this.procFrontEndMapEnv.put((String) keys [i], constructFrontEndMapping (fecprevious, fecaux)); } } public static FrontEndMapping mountFrontEndMapping (CircusAction action, CircusProcess process, FrontEndCounter fec, FrontEndCounter init, Vector mucalls, Spec spec) { if (action instanceof Action2) { Vector channelSet = ChanSetUtil.channelSet(action, spec); CircusAction left = ((Action2)action).getLeftAction(); CircusAction right = ((Action2)action).getRightAction(); FrontEndCounter fecleft = mountFrontEndCounter (left, process, spec); FrontEndCounter initleft = new FrontEndCounter (init); FrontEndMapping femleft = mountFrontEndMapping (left, process, fecleft, initleft, mucalls, spec); FrontEndCounter fecright = mountFrontEndCounter (right, process, spec); FrontEndCounter initright = FrontEndCounter.mergeFrontEndCounters (init, fecleft, channelSet); FrontEndMapping femright; //if (action instanceof ParAction) femright = mountFrontEndMapping (right, process, fecright, initright, mucalls, spec); //else // femright = mountFrontEndMapping (right, process, fecleft, initleft, mucalls, spec); FrontEndMapping femparallel = FrontEndMapping.mergeFrontEndMappings (femleft, femright, channelSet); if (channelSet.size() > 2) { System.out.print (""); } return femparallel; } else if (action instanceof PrefixingAction) { Communication c = ((PrefixingAction)action).getCommunication(); CircusAction paction = ((PrefixingAction)action).getCircusAction(); String channel = c.getChannelExpr().getName().toString(); Integer fecounter = fec.get(channel); Integer feinit = init.get(channel); Vector vec = FrontEndMapping.build(fecounter, feinit); FrontEndMapping fem = mountFrontEndMapping (paction, process, fec, init, mucalls, spec);//new FrontEndMapping (); fem.put(channel, vec); c.getAnns().add(new FrontEndAnn (vec)); return fem; } else if (action instanceof CallAction) { String callname = ((CallAction)action).getZName().toString(); if (!mucalls.contains(callname)) return mountFrontEndMapping (CallUtil.getContentOfCallAction((CallAction)action, process, spec), process, fec, init, mucalls, spec); return new FrontEndMapping (); } else if (action instanceof MuAction) { String muname = ((MuAction)action).getZName().toString(); mucalls.addElement(muname); return mountFrontEndMapping (((MuAction)action).getCircusAction(), process, fec, init, mucalls, spec); } else if (action instanceof Action1 && !(action instanceof PrefixingAction) && !(action instanceof CallAction)) { return mountFrontEndMapping (((Action1)action).getCircusAction(), process, fec, init, mucalls, spec); } else if (action instanceof CircusCommand) { if (action instanceof AssignmentCommand) { //nothing to do... } else if (action instanceof VarDeclCommand) { return mountFrontEndMapping (((VarDeclCommand)action).getCircusAction(), process, fec, init, mucalls, spec); } else if (action instanceof IfGuardedCommand) { FrontEndMapping femaux = new FrontEndMapping (); FrontEndCounter aux = init; IfGuardedCommand igc = (IfGuardedCommand)action; CircusActionList cal = igc.getGuardedActionList(); for (int i = 0; i < cal.size(); i++) { GuardedAction ga = (GuardedAction) cal.get(i); FrontEndMapping fem2 = mountFrontEndMapping (ga.getCircusAction(), process, fec, aux, mucalls, spec); femaux.getMap().putAll(fem2.getMap()); } return femaux; } else { ExceptionUtil.throwImplementationException("FrontEndUpdater.frontEndMapping", action.getClass()); return new FrontEndMapping (); } } else /*if (action instanceof BasicAction)*/{ return new FrontEndMapping (); } return new FrontEndMapping (); } public void updateCircusProcess (CircusProcess process, FrontEndCounter init, Spec spec) { if (process instanceof BasicProcess) { BasicProcess basic = (BasicProcess)process; CircusAction mainaction = basic.getMainAction(); updateCircusAction (mainaction, process, /*fem,*/ init, spec); System.out.print (""); } else if (process instanceof ParamProcess) { updateCircusProcess (((ParamProcess) process).getCircusBasicProcess(), /*fem*/init, spec); } else if (process instanceof Process1) { updateCircusProcess (((Process1)process).getCircusProcess (), /*fem*/init, spec); } else if (process instanceof Process2) { updateCircusProcess (((Process2)process).getLeftProcess (), /*fem*/init, spec); updateCircusProcess (((Process2)process).getRightProcess (), /*fem*/init, spec); } else if (process instanceof CallProcess) { updateCircusProcess (ProcessUtil.getExpandedContentOfProcess((CallProcess)process, spec), init, spec); } else { /*anything else?*/ } } public void updateCircusAction (CircusAction action, CircusProcess process, /*FrontEndMapping fem,*/ FrontEndCounter init, Spec spec) { FrontEndCounter fec = mountFrontEndCounter (action, process, spec); FrontEndMapping fem = mountFrontEndMapping (action, process, fec, init, new Vector (), spec); //JOptionPane.showMessageDialog (null, FrontEndPrinter.frontEndContentAsString (action)); init = new FrontEndCounter (fec); //init = FrontEndCounter.sumFrontEnds(init, fec, new Vector ()); System.out.print (""); } public static Vector subvector (int x, int y, Vector vec) { Vector subvec = new Vector (); for (int i = x; i <= y; i++) { subvec.addElement (vec.elementAt(i)); } return subvec; } public static FrontEndCounter mountFrontEndCounter (CircusProcess process, Spec spec) { if (process instanceof Process2) { CircusProcess left = ((Process2)process).getLeftProcess(); CircusProcess right = ((Process2)process).getRightProcess(); FrontEndCounter femleft = mountFrontEndCounter (left, spec); FrontEndCounter femright = mountFrontEndCounter (right, spec); Vector channelSet = ChanSetUtil.channelSet(process, spec); FrontEndCounter femparallel = FrontEndCounter.mergeFrontEndCounters(femleft, femright, channelSet); return femparallel; } else if (process instanceof CallProcess) { return mountFrontEndCounter (ProcessUtil.getExpandedContentOfProcess((CallProcess)process, spec), spec); } else if (process instanceof Process1) { return mountFrontEndCounter (((Process1)process).getCircusProcess(), spec); } else if (process instanceof BasicProcess) { BasicProcess basicProcess = (BasicProcess)process; ZParaList paras = basicProcess.getZParaList(); CircusAction mainAction = basicProcess.getMainAction(); System.out.println (PrinterUtil.printAction(mainAction, spec)); Vector mucalls = CallUtil.muCalls (mainAction, process, spec); CallUtil.addMuCallAnns (mainAction, process, spec, mucalls); return mountFrontEndCounter (mainAction, process, spec); } else { return new FrontEndCounter (); } } public static FrontEndCounter mountFrontEndCounter (CircusAction action, CircusProcess process, Spec spec) { return mountFrontEndCounter (action, process, new Consumer (new Vector ()), spec); } public static FrontEndCounter mountFrontEndCounter (CircusAction action, CircusProcess process, Consumer consumer, Spec spec) { if (action instanceof Action2) { CircusAction left = ((Action2)action).getLeftAction(); CircusAction right = ((Action2)action).getRightAction(); FrontEndCounter femleft = mountFrontEndCounter (left, process, consumer, spec); FrontEndCounter femright = mountFrontEndCounter (right, process, consumer, spec); Vector channelSet = ChanSetUtil.channelSet (action, spec); if (action instanceof ParallelAction) { System.out.print (""); } FrontEndCounter femparallel = FrontEndCounter.mergeFrontEndCounters(femleft, femright, channelSet); return femparallel; } else if (action instanceof PrefixingAction) { Communication c = ((PrefixingAction)action).getCommunication(); CircusAction paction = ((PrefixingAction)action).getCircusAction(); String channel = c.getChannelExpr().getName().toString(); FrontEndCounter fem = mountFrontEndCounter (paction, process, consumer, spec);//new FrontEndCounter (); fem.put(channel); return fem; } else if (action instanceof CallAction) { //Aqui já se supõe que todo o conteúdo das ações que precisaram de expansão foram expandidos /*Vector mucalls = CallUtil.muCalls (action, process, spec); CallUtil.addMuCallAnns (action, process, spec, mucalls); if (consumer.callStatus (((CallAction)action).getName().toString()) == Consumer.NORMALCALL && action.getAnn(MuCallAnn.class) == null) { return mountFrontEndCounter (CallUtil.getContentOfCallAction((CallAction)action, process, spec), process, consumer, spec); }*/ return new FrontEndCounter (); } else if (action instanceof Action1 && !(action instanceof PrefixingAction) && !(action instanceof CallAction)) { return mountFrontEndCounter (((Action1)action).getCircusAction(), process, consumer, spec); } else if (action instanceof CircusCommand) { if (action instanceof AssignmentCommand) { return new FrontEndCounter (); } else if (action instanceof VarDeclCommand) { return mountFrontEndCounter (((VarDeclCommand)action).getCircusAction(), process, consumer, spec); } else if (action instanceof IfGuardedCommand) { FrontEndCounter fec = new FrontEndCounter (); IfGuardedCommand igc = (IfGuardedCommand)action; CircusActionList cal = igc.getGuardedActionList(); for (int i = 0; i < cal.size(); i++) { GuardedAction ga = (GuardedAction) cal.get(i); FrontEndCounter fec2 = mountFrontEndCounter (ga.getCircusAction(), process, consumer, spec); fec.getMap().putAll(fec2.getMap()); } return fec; } else { return new FrontEndCounter (); } } else /*if (action instanceof BasicAction)*/{ return new FrontEndCounter (); } } public static void main (String args []) { Factory f = new Factory (); Spec spec = f.createSpec(); ZSect zSect = f.createZSect(); BasicProcess process = f.createBasicProcess (); ExtChoiceAction eca = ParallelismTestBuilders.buildLargerExtChoiceAction( new String [][] { new String [] {"a", "b", "c"}, new String [] {"d", "e", "f"} } ); IntChoiceAction ica = ParallelismTestBuilders.buildLargerIntChoiceAction( new String [][] { new String [] {"a", "b", "c"}, new String [] {"a", "e", "f"} } ); ParallelAction pa = f.createParallelAction (); pa.setLeftAction(ica); pa.setRightAction(eca); //FrontEndCounter fem = mountFrontEndCounter (pa, f.createBasicProcess()); System.out.print(""); } } /* FileSource source = new FileSource ("C:\\Users\\sam\\Softwares\\workspaceGalileo\\circus\\src\\jcircus\\fe.tex"); Spec spec = null; try { spec = (Spec) ParseUtils.parse(source, new SectionManager ("circus")); System.out.println ("Parseou"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnmarshalException e) { // TODO Auto-generated catch block e.printStackTrace(); } ZSect zSect; if (spec.getSect().get(0) instanceof NarrSect) zSect = (ZSect) spec.getSect().get(1); else zSect = (ZSect) spec.getSect().get(0); ZParaList paras = zSect.getZParaList(); for (int i = 0; i < paras.size(); i++) { Para para = paras.get(i); if (para instanceof ProcessPara) { ProcessPara ppara = (ProcessPara)para; CircusProcess process = ppara.getCircusProcess(); FrontEndMap fem = mountFrontEndMap (process, spec); System.out.print(""); } } * */