package jcircus.newutil;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
//import jcircus.complementaryenvs.ChanSetOfProcessIdSetsEnv;
import jcircus.complementaryenvs.ProcProcessParaEnv;
import jcircus.environment.ChanInfoEnv;
import jcircus.environment.ProcChanUseEnv;
import jcircus.util.Util;
import net.sourceforge.czt.circus.ast.Action2;
import net.sourceforge.czt.circus.ast.ActionPara;
import net.sourceforge.czt.circus.ast.BasicChannelSetExpr;
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.ChannelSetPara;
import net.sourceforge.czt.circus.ast.CircusAction;
import net.sourceforge.czt.circus.ast.CircusChannelSet;
import net.sourceforge.czt.circus.ast.CircusCommunicationList;
import net.sourceforge.czt.circus.ast.CircusProcess;
import net.sourceforge.czt.circus.ast.CommPattern;
import net.sourceforge.czt.circus.ast.CommUsage;
import net.sourceforge.czt.circus.ast.CommunicationList;
import net.sourceforge.czt.circus.ast.ExtChoiceProcess;
import net.sourceforge.czt.circus.ast.IntChoiceProcess;
import net.sourceforge.czt.circus.ast.ParAction;
import net.sourceforge.czt.circus.ast.ExtChoiceAction;
import net.sourceforge.czt.circus.ast.IntChoiceAction;
import net.sourceforge.czt.circus.ast.ParallelAction;
import net.sourceforge.czt.circus.ast.ParallelProcess;
import net.sourceforge.czt.circus.ast.ParamProcess;
import net.sourceforge.czt.circus.ast.PrefixingAction;
import net.sourceforge.czt.circus.ast.Communication;
import net.sourceforge.czt.circus.ast.InterleaveAction;
import net.sourceforge.czt.circus.ast.ProcessPara;
import net.sourceforge.czt.circus.util.Factory;
import net.sourceforge.czt.z.ast.Expr;
import net.sourceforge.czt.z.ast.Expr0N;
import net.sourceforge.czt.z.ast.NarrSect;
import net.sourceforge.czt.z.ast.Para;
import net.sourceforge.czt.z.ast.RefExpr;
import net.sourceforge.czt.z.ast.Spec;
import net.sourceforge.czt.z.ast.ZExprList;
import net.sourceforge.czt.z.ast.ZParaList;
import net.sourceforge.czt.z.ast.ZSect;
public class ChanSetUtil {
public static boolean isOnChannelSet (String channel, CircusAction action, Spec spec) {
if ((channelSet (action, spec)).contains(channel)) {
return true;
}
return false;
}
public static boolean isOnChannelSet (String channel, CircusProcess process, Spec spec) {
if ((channelSet(process, spec)).contains(channel)) {
return true;
}
return false;
}
private static Vector <String> getChannelNames (ChannelSetPara csp, ZParaList paralist) {
Vector <String> channelNames = new Vector <String> ();
CircusChannelSet ccs = (CircusChannelSet) csp.getChannelSet();
Expr expr = ccs.getExpr();
if (expr instanceof BasicChannelSetExpr) {
Vector <String> channelnames = new Vector <String> ();
CommunicationList commlist = (((BasicChannelSetExpr)expr).getCommunicationList());
int size = commlist.getChildren().length;
for (int i = 0; i < size; i++) {
channelnames.addElement (((Communication)commlist.getChildren() [i]).getChannelExpr().getZName().toString());
}
return channelnames;
}
else if (expr instanceof RefExpr) {
boolean aux = false;
Vector <String> channelnames = new Vector <String> ();
String name = ((RefExpr)expr).getZName().toString();
int size = paralist.size();
for (int i = 0; i < size; i++) {
Para para = paralist.get(i);
if (para instanceof ChannelSetPara) {
String paraname = ((ChannelSetPara)para).getZName().toString();
if (para.equals(paraname)) {
aux = true;
channelnames.addAll (getChannelNames ((ChannelSetPara)para, paralist));
}
}
}
if (!aux) {
ExceptionUtil.throwException("The channel set paragraph " + name + " was not found...");
}
return channelnames;
}
else {
ExceptionUtil.throwImplementationException("ChanSetUtil.getChannelNames", expr.getClass());
return new Vector <String> ();
}
//CommunicationList commList = ((BasicChannelSetExpr)((CircusChannelSet)(((ParallelAction)action).getChannelSet())).getExpr()).getCommunicationList();
//int commListLength = commList.getChildren().length;
//for (int i = 0; i < commListLength; i++) {
// vec.addElement (((Communication)commList.getChildren() [i]).getChannelExpr().getZName().toString());
//}
}
private static HashMap <String, Vector <String>> mapNameToChannelSet (Spec spec) {
HashMap <String, Vector <String>> map = new HashMap <String, Vector <String>> ();
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);
if (para instanceof ChannelSetPara) {
ChannelSetPara csp = (ChannelSetPara)para;
String csname = csp.getZName().toString();
map.put(csname, getChannelNames (csp, paras));
}
}
return map;
}
/*private static CommunicationList communications (Expr expr) {
if (expr instanceof BasicChannelSetExpr) {
}
}*/
public static Vector <String> channelSet (CircusAction action, Spec spec) {
Vector <String> vec = new Vector <String> ();
if (action instanceof ParallelAction) {
Expr channelexpr = ((CircusChannelSet)(((ParallelAction)action).getChannelSet())).getExpr();
if (channelexpr instanceof BasicChannelSetExpr) {
CommunicationList commList = ((BasicChannelSetExpr)channelexpr).getCommunicationList();
//CommunicationList commList = ((BasicChannelSetExpr)((CircusChannelSet)(((ParallelAction)action).getChannelSet())).getExpr()).getCommunicationList();
int commListLength = commList.getChildren().length;
for (int i = 0; i < commListLength; i++) {
vec.addElement (((Communication)commList.getChildren() [i]).getChannelExpr().getZName().toString());
}
return vec;
}
else if (channelexpr instanceof RefExpr) {
String name = ((RefExpr)channelexpr).getZName().toString();
HashMap <String, Vector <String>> map = mapNameToChannelSet (spec);
Vector <String> channelset = map.get(name);
return channelset;
}
else {
ExceptionUtil.throwImplementationException("ChanSetUtil.channelSet", channelexpr.getClass());
}
}
return vec;
}
/*public static void setActChannelSet (Vector <String> channelSet, CircusAction action) {
if (action instanceof ParallelAction) {
Factory f = new Factory ();
CircusChannelSet circusChannelSet = f.createCircusChannelSet();
BasicChannelSetExpr bcse = f.createBasicChannelSetExpr();
CircusCommunicationList cl = f.createCircusCommunicationList();
for (int i = 0; i < channelSet.size(); i++) {
Communication c = f.createCommunication(
f.createRefExpr(f.createZName(channelSet.elementAt(i))),
f.createCircusFieldList(),
CommUsage.Normal,//OBS: Aqui n�o necessariamente � normal... Pode ser generic tbm, mas para a gente, n�o far� diferen�a, pois o que interessa � o nome do canal //c.getCommUsage(),
CommPattern.Synch,//OBS: Idem para c�. � indiferente o CommPattern, s� interessa o nome do canal //c.getCommPattern(),
new BigInteger ("0"), //OBS: � irrelevante aqui o valor do BigInteger //c.getMultiSych(),
false //OBS: Irrelevante se � true ou false c.getIndexed()
);
cl.add(c);
}
bcse.setCommunicationList(cl);
circusChannelSet.setExpr(bcse);
((ParallelAction)action).setChannelSet (circusChannelSet);
}
}*/
public static Vector <String> channelSet (CircusProcess process, Spec spec) {
Vector <String> vec = new Vector <String> ();
if (process instanceof ParallelProcess) {
Expr channelexpr = ((CircusChannelSet)(((ParallelProcess)process).getChannelSet())).getExpr();
if (channelexpr instanceof BasicChannelSetExpr) {
CommunicationList commList = ((BasicChannelSetExpr)channelexpr).getCommunicationList();
//CommunicationList commList = ((BasicChannelSetExpr)((CircusChannelSet)(((ParallelAction)action).getChannelSet())).getExpr()).getCommunicationList();
int commListLength = commList.getChildren().length;
for (int i = 0; i < commListLength; i++) {
vec.addElement (((Communication)commList.getChildren() [i]).getChannelExpr().getZName().toString());
}
return vec;
}
else if (channelexpr instanceof RefExpr) {
String name = ((RefExpr)channelexpr).getZName().toString();
HashMap <String, Vector <String>> map = mapNameToChannelSet (spec);
Vector <String> channelset = map.get(name);
return channelset;
}
else {
ExceptionUtil.throwImplementationException("ChanSetUtil.channelSet", channelexpr.getClass());
}
}
return vec;
/*if (process instanceof ParallelProcess) {
CommunicationList commList = ((BasicChannelSetExpr)((CircusChannelSet)(((ParallelProcess)process).getChannelSet())).getExpr()).getCommunicationList();
int commListLength = commList.getChildren().length;
for (int i = 0; i < commListLength; i++) {
vec.addElement (((Communication)commList.getChildren() [i]).getChannelExpr().getZName().toString());
}
}*/
}
}