package jcircus.newutil;
import java.util.Vector;
import jcircus.anns.MuCallAnn;
import jcircus.parallelism.Consumer;
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.BasicAction;
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.GuardedAction;
import net.sourceforge.czt.circus.ast.HideAction;
import net.sourceforge.czt.circus.ast.IfGuardedCommand;
import net.sourceforge.czt.circus.ast.InterleaveAction;
import net.sourceforge.czt.circus.ast.MuAction;
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.z.ast.Para;
import net.sourceforge.czt.z.ast.Spec;
import net.sourceforge.czt.z.ast.ZParaList;
public class ChannelUtil {
public static Vector <String> getVisibleChannels (CircusProcess process, Spec spec) {
if (process instanceof Process2) {
Vector <String> vec1 = getVisibleChannels (((Process2)process).getLeftProcess(), spec);
Vector <String> vec2 = getVisibleChannels (((Process2)process).getRightProcess(), spec);
vec1.addAll(vec2);
return vec1;
}
else if (process instanceof Process1) {
return getVisibleChannels (((Process1)process).getCircusProcess(), spec);
}
else if (process instanceof BasicProcess) {
BasicProcess bp = (BasicProcess) process;
ZParaList paras = bp.getZParaList();
Vector <String> visibleChannels = new Vector <String> ();
for (int i = 0; i < paras.size(); i++) {
Para para = paras.get(i);
if (para instanceof ActionPara) {
ActionPara actPara = (ActionPara)para;
visibleChannels.addAll (ChannelUtil.getVisibleChannels (actPara.getCircusAction(), process, actPara.getZName().toString(), spec));
}
}
return visibleChannels;
}
/*else if (action instanceof GuardedAction) {
return getVisibleChannels (((GuardedAction)action).getCircusAction(), process, actionParaName);
}
else if (action instanceof HideAction) {
return getVisibleChannels (((HideAction)action).getCircusAction(), process, actionParaName);
}*/
else if (process instanceof CallProcess) {
CircusProcess process2 = ProcessUtil.getContentOfCallProcess (((CallProcess)process), spec);
String newName = ((CallProcess)process).getCallExpr().getName().toString();
return getVisibleChannels (process2, spec);
}
else {
return new Vector <String> ();
}
}
public static Vector <String> getVisibleChannels (CircusAction action, CircusProcess process, String actionParaName, Spec spec) {
return ChannelUtil.getVisibleChannels (action, process, actionParaName, new Consumer (new Vector <String> ()), new Vector <String> (), spec);
}
public static Vector <String> getVisibleChannels (CircusAction action, CircusProcess process, String actionParaName, Vector <String> mucalls, Spec spec) {
return ChannelUtil.getVisibleChannels (action, process, actionParaName, new Consumer (new Vector <String> ()), mucalls, spec);
}
public static Vector <String> getVisibleChannels (CircusAction action, CircusProcess process, String actionParaName, Consumer c, Vector <String> mucalls, Spec spec) {
if (action instanceof PrefixingAction) {
String chan = ((PrefixingAction)action).getCommunication().getChannelExpr().getName().toString();
CircusAction ca = ((PrefixingAction)action).getCircusAction();
Vector <String> vec = getVisibleChannels (ca, process, actionParaName, c, mucalls, spec);
vec.add(chan);
return vec;
}
else if (action instanceof Action2) {
if (action instanceof InterleaveAction) {
System.out.println ();
}
Vector <String> vec1 = getVisibleChannels (((Action2)action).getLeftAction(), process, actionParaName, c, mucalls, spec);
Vector <String> vec2 = getVisibleChannels (((Action2)action).getRightAction(), process, actionParaName, c, mucalls, spec);
vec1.addAll(vec2);
return vec1;
}
else if (action instanceof GuardedAction) {
return getVisibleChannels (((GuardedAction)action).getCircusAction(), process, actionParaName, c, mucalls, spec);
}
else if (action instanceof HideAction) {
return getVisibleChannels (((HideAction)action).getCircusAction(), process, actionParaName, c, mucalls, spec);
}
else if (action instanceof MuAction) {
String muname = ((MuAction)action).getZName().toString();
mucalls.add(muname);
return getVisibleChannels (((MuAction)action).getCircusAction(), process, actionParaName, c, mucalls, spec);
}
else if (action instanceof Action1 && !(action instanceof PrefixingAction)) {
return getVisibleChannels (((Action1)action).getCircusAction(), process, actionParaName, c, mucalls, spec);
}
else if (action instanceof CallAction) {
//return new Vector <String> ();
String callname = ((CallAction)action).getZName().toString();
if (mucalls.contains(callname)) {
return new Vector <String> ();
}
CircusAction action2 = CallUtil.getContentOfCallAction (((CallAction)action), process, spec);
String newName = ((CallAction)action).getName().toString();
if (newName.equals(actionParaName)) {
return new Vector <String> ();
}
/*if (!(c.callStatus (((CallAction)action).getName().toString()) == Consumer.NORMALCALL)) {
return new Vector <String> ();
}*/
return getVisibleChannels (action2, process, newName, c, mucalls, spec);
}
else if (action instanceof GuardedAction) {
return getVisibleChannels (((GuardedAction)action).getCircusAction(), process, actionParaName, c, mucalls, spec);
}
else if (action instanceof CircusCommand) {
if (action instanceof AssignmentCommand) {
return new Vector <String> ();
}
else if (action instanceof IfGuardedCommand) {
Vector <String> allChannels = new Vector <String> ();
CircusActionList cal = ((IfGuardedCommand)action).getGuardedActionList();
int size = cal.size();
for (int i = 0; i < size; i++) {
CircusAction ca = cal.get(i);
allChannels.addAll(getVisibleChannels (ca, process, actionParaName, c, mucalls, spec));
}
return allChannels;
}
else if (action instanceof VarDeclCommand) {
return getVisibleChannels (((VarDeclCommand)action).getCircusAction(), process, actionParaName, c, mucalls, spec);
}
else {
ExceptionUtil.throwImplementationException("ChannelUtil.getVisibleChannels", action.getClass());
return new Vector <String> ();
}
}
else if (action instanceof BasicAction) {
return new Vector <String> ();
}
else {
ExceptionUtil.throwImplementationException("ChannelUtil.getVisibleChannels", action.getClass());
return new Vector <String> ();
}
}
public static Vector <String> getVisibleChannelsFromMainAction (ProcessPara para, Spec spec) {
return getVisibleChannelsFromMainAction (para.getCircusProcess(), para.getZName().toString(), spec);
}
public static Vector <String> getVisibleChannelsFromMainAction (CircusProcess process, String paraName, Spec spec)
{
if (process instanceof BasicProcess) {
return getVisibleChannels (((BasicProcess)process).getMainAction(), process, paraName, spec);
}
else if (process instanceof Process2) {
CircusProcess left = ((Process2)process).getLeftProcess();
CircusProcess right = ((Process2)process).getRightProcess();
Vector <String> vec = getVisibleChannelsFromMainAction (left, paraName, spec);
vec.addAll(getVisibleChannelsFromMainAction (right, paraName, spec));
return vec;
}
else if (process instanceof Process1) {
CircusProcess proc = ((Process1)process).getCircusProcess();
return getVisibleChannelsFromMainAction (proc, paraName, spec);
}
//TODO outros else if ()
else {
return new Vector <String> ();
//throw new NotYetImplementedException ("Method: getVisibleChannelsForMainAction... ");
}
}
}