package jcircus.newutil;
import java.util.List;
import java.util.Vector;
import javax.swing.Action;
import javax.swing.JOptionPane;
import jcircus.complementaryenvs.ChanDotFieldEnv;
import jcircus.parallelism.CopyProcessAnn;
import jcircus.parallelism.HiddenFromGUIInfo;
import net.sourceforge.czt.circus.ast.Action1;
import net.sourceforge.czt.circus.ast.Action2;
import net.sourceforge.czt.circus.ast.ActionList;
import net.sourceforge.czt.circus.ast.ActionPara;
import net.sourceforge.czt.circus.ast.AssignmentCommand;
import net.sourceforge.czt.circus.ast.AssignmentPairs;
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.ChannelDecl;
import net.sourceforge.czt.circus.ast.ChannelPara;
import net.sourceforge.czt.circus.ast.ChannelSet;
import net.sourceforge.czt.circus.ast.ChaosAction;
import net.sourceforge.czt.circus.ast.CircusAction;
import net.sourceforge.czt.circus.ast.CircusActionList;
import net.sourceforge.czt.circus.ast.CircusChannelSet;
import net.sourceforge.czt.circus.ast.CircusCommand;
import net.sourceforge.czt.circus.ast.CircusFieldList;
import net.sourceforge.czt.circus.ast.CircusProcess;
import net.sourceforge.czt.circus.ast.Communication;
import net.sourceforge.czt.circus.ast.DotField;
import net.sourceforge.czt.circus.ast.ExtChoiceAction;
import net.sourceforge.czt.circus.ast.ExtChoiceProcess;
import net.sourceforge.czt.circus.ast.Field;
import net.sourceforge.czt.circus.ast.GuardedAction;
import net.sourceforge.czt.circus.ast.HideAction;
import net.sourceforge.czt.circus.ast.HideProcess;
import net.sourceforge.czt.circus.ast.IfGuardedCommand;
import net.sourceforge.czt.circus.ast.InputField;
import net.sourceforge.czt.circus.ast.IntChoiceAction;
import net.sourceforge.czt.circus.ast.IntChoiceProcess;
import net.sourceforge.czt.circus.ast.InterleaveAction;
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.ParallelProcess;
import net.sourceforge.czt.circus.ast.ParamAction;
import net.sourceforge.czt.circus.ast.ParamProcess;
import net.sourceforge.czt.circus.ast.PrefixingAction;
import net.sourceforge.czt.circus.ast.ProcessPara;
import net.sourceforge.czt.circus.ast.RenameAction;
import net.sourceforge.czt.circus.ast.RenameProcess;
import net.sourceforge.czt.circus.ast.SeqAction;
import net.sourceforge.czt.circus.ast.SeqProcess;
import net.sourceforge.czt.circus.ast.SkipAction;
import net.sourceforge.czt.circus.ast.SpecStmtCommand;
import net.sourceforge.czt.circus.ast.StopAction;
import net.sourceforge.czt.circus.ast.VarDeclCommand;
import net.sourceforge.czt.z.ast.AndPred;
import net.sourceforge.czt.z.ast.ApplExpr;
import net.sourceforge.czt.z.ast.AxPara;
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.ImpliesExpr;
import net.sourceforge.czt.z.ast.MemPred;
import net.sourceforge.czt.z.ast.NameList;
import net.sourceforge.czt.z.ast.NarrSect;
import net.sourceforge.czt.z.ast.NumExpr;
import net.sourceforge.czt.z.ast.OrPred;
import net.sourceforge.czt.z.ast.Pred;
import net.sourceforge.czt.z.ast.RefExpr;
import net.sourceforge.czt.z.ast.SchExpr;
import net.sourceforge.czt.z.ast.SchText;
import net.sourceforge.czt.z.ast.SetExpr;
import net.sourceforge.czt.z.ast.Spec;
import net.sourceforge.czt.z.ast.TupleExpr;
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.z.ast.ZParaList;
import net.sourceforge.czt.z.ast.ZSchText;
import net.sourceforge.czt.z.ast.ZSect;
public class PrinterUtil {
public static String specAsString (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();
String str = "";
for (int i = 0; i < paras.size(); i++) {
if (paras.get(i) instanceof ChannelPara) {
str = str + channelParaAsString ((ChannelPara)paras.get(i)) + "\n";
}
else if (paras.get(i) instanceof ProcessPara) {
ProcessPara para = (ProcessPara) paras.get(i); //Aqui vai invocar os visitadores dos paragrafos que nao forem narrativos (com texto), ou de tags de Latex
String paraName = para.getZName().toString();
str = "\n" + str + processParaString (para, spec) + "\n";
}
}
str = str + "\n";
return str;
}
private static String channelParaAsString (ChannelPara channelPara) {
ZDeclList decls = channelPara.getZDeclList();
String declsstring = printZDeclList (decls);
return "Channel " + declsstring;
}
public static String printCalls (CircusAction action, CircusProcess process, Spec spec) {
Vector <String> callsvec = CallUtil.recursiveCalls (/*action, */process, spec);
String str = "Action: " + printAction (action, spec) + "\nCalls: ";
for (int i = 0; i < callsvec.size(); i++) {
str = str + callsvec.elementAt(i) + ", ";
}
return str;
}
public static void printCallsPane (CircusAction action, CircusProcess process, Spec spec) {
JOptionPane.showMessageDialog (null, printCalls (action, process, spec));
}
public static String printAxPara (AxPara axpara) {
String namelist = "NameList :: " + printZNameList (axpara.getZNameList());
String zschtext = "ZSchText :: " + printZSchText (axpara.getZSchText());
return "AxPara :: [" + namelist + " , " + zschtext + "]";
}
public static String printZSchText (ZSchText schtext) {
String decllist = "DeclList :: " + printZDeclList (schtext.getZDeclList());
String pred = "Predicate :: " + printPred (schtext.getPred());
return decllist + " , " + pred;
}
public static String printZNameList (ZNameList namelist) {
int size = namelist.size();
String str = "[";
for (int i = 0; i < size; i++) {
str = str + namelist.get (i);
if (i < size - 1)
str = str + ", ";
}
return str + "]";
}
public static String printBasicProcess (BasicProcess process, Spec spec) { //OBS: Este metodo pode ser aprimorado tbm para imprimir acoes de esquema e estados
String str = "";
str = str + "begin\n";
ZParaList paraList = process.getZParaList();
for (int i = 0; i < paraList.size(); i++) {
if (paraList.get(i) instanceof ActionPara) {
ActionPara para = (ActionPara) paraList.get(i);
str = str + " " + para.getName().toString() + " ^= " + printAction (para.getCircusAction(), spec) + "\n";
}
else if (paraList.get(i) instanceof AxPara) {
AxPara para = (AxPara) paraList.get(i);
str = str + " " + para.getName().toString() + printAxPara (para);
}
}
str = str + "end\n";
return str;
}
public static String printParamProcess (ParamProcess process) { //TODO
return "";
}
public static String processParaString (ProcessPara processPara, Spec spec) {
String str = "";
str = str + "process " + processPara.getName().toString() + " ^= " + printProcess (processPara.getCircusProcess(), spec);
return str;
}
public static void printProcessPara (ProcessPara processPara, Spec spec) {
System.out.println ("WWWW00WW :: " + processParaString (processPara, spec));
}
public static void printProcessPara (String paraname, CircusProcess process, Spec spec) {
System.out.println (paraname + " ^= " + PrinterUtil.printProcess(process, spec));
}
public static String printActionPara (ActionPara actionPara, Spec spec) {
return actionPara.getName().toString() + " ^= " + printAction (actionPara.getCircusAction(), spec) + "\n";
}
public static String printChannelSet (CircusProcess process, Spec spec) {
Vector <String> strChanSet = ChanSetUtil.channelSet(process, spec);
String str = "";
for (int i = 0; i < strChanSet.size(); i++) {
str = str + strChanSet.elementAt(i);
if (i < strChanSet.size() - 1) {
str = str + ", ";
}
}
return str;
}
public static String printChannelSet (CircusAction action, Spec spec) {
Vector <String> strChanSet = ChanSetUtil.channelSet(action, spec);
String str = "";
for (int i = 0; i < strChanSet.size(); i++) {
str = str + strChanSet.elementAt(i);
if (i < strChanSet.size() - 1) {
str = str + ", ";
}
}
return str;
}
public static String printCommunication (Communication comm) {
String str = "";
String channel = comm.getChannelExpr().getZName().toString();
CircusFieldList cfl = comm.getCircusFieldList();
int size = cfl.size();
str = channel;
for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
if (field instanceof InputField) {
str = str + "?" + ((InputField)field).getVariableZName().toString();
}
else if (field instanceof DotField && ChanDotFieldEnv.hasOutputFieldAnn (field)) {
str = str + "!" + printExpression (((DotField)field).getExpr());
}
else {
str = str + "." + printExpression (((DotField)field).getExpr());
}
}
return str;
}
public static String printPred (Pred pred) { //QUANDO INVOCADO, LEVA O MÉTODO printExpression a um loop infinito
if (pred instanceof MemPred) {
MemPred mp = (MemPred)pred;
Expr exprleft = mp.getLeftExpr();
Expr exprright = mp.getRightExpr();
if (mp.getMixfix()) {
return "(" + printExpression (((MemPred)pred).getLeftExpr()) + " = " + printExpression (((MemPred)pred).getRightExpr());
}
else {
return "(" + printExpression (((MemPred)pred).getLeftExpr()) + " OP " + printExpression (((MemPred)pred).getRightExpr());
}
}
else if (pred instanceof AndPred) {
AndPred ap = (AndPred)pred;
return printPred (ap.getLeftPred()) + " AND " + printPred (ap.getRightPred());
}
else if (pred instanceof OrPred) {
OrPred ap = (OrPred)pred;
return printPred (ap.getLeftPred()) + " OR " + printPred (ap.getRightPred());
}
else {
return "PREDICADO";
}
}
public static String printVarDecl (VarDecl decl) {
String str = "";
ZNameList nameList = decl.getZNameList();
int size = nameList.size();
str = str + printZNameList (nameList);
/*for (int i = 0; i < size; i++) {
str = str + nameList.get(i).toString();
if (i < size - 1)
str = str + ", ";
}*/
String expression = PrinterUtil.printExpression(decl.getExpr());
str = str + ": " + expression;
return str;
}
public static String printZDeclList (ZDeclList declList) {
String str = "";
int size = declList.size();
for (int i = 0; i < size; i++) {
Decl decl = declList.get(i);
if (decl instanceof VarDecl) {
str = str + "[" + printVarDecl ((VarDecl)decl) + "]";
if (i < size - 1)
str = str + ", ";
}
else if (decl instanceof ConstDecl) {
str = str + "[" + printConstDecl ((ConstDecl)decl) + "]";
if (i < size - 1)
str = str + ", ";
}
else if (decl instanceof ChannelDecl) {
str = str + "[" + printChannelDecl ((ChannelDecl)decl) + "]";
if (i < size - 1)
str = str + ", ";
}
}
return str;
}
public static String printChannelDecl (ChannelDecl decl) {
String channelliststring = printZNameList (decl.getZChannelNameList());
Expr expr = decl.getExpr();
String exprstring = printExpression (decl.getExpr());
return channelliststring + " : " + exprstring;
}
public static String printConstDecl (ConstDecl decl) {
return decl.getZName().toString() + " : " + printExpression (decl.getExpr());
}
public static String printAction (CircusAction action, Spec spec) {
//String str = "";
if (action instanceof ParamAction) {
return printZDeclList (((ParamAction)action).getZDeclList()) + " @ " + printAction (((ParamAction)action).getCircusAction(), spec);
}
else if (action instanceof ExtChoiceAction) {
return "(" + printAction (((ExtChoiceAction)action).getLeftAction(), spec) + " [] " + printAction (((ExtChoiceAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof IntChoiceAction) {
return "(" + printAction (((IntChoiceAction)action).getLeftAction(), spec) + " |~| " + printAction (((IntChoiceAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof SeqAction) {
return "(" + printAction (((SeqAction)action).getLeftAction(), spec) + " ; " + printAction (((SeqAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof ParallelAction) {
return "(" +
printAction (((ParallelAction)action).getLeftAction(), spec) +
" |{" + printChannelSet (action, spec) + "}| " +
printAction (((ParallelAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof InterleaveAction) {
return "(" + printAction (((InterleaveAction)action).getLeftAction(), spec) + " |{" + printChannelSet (action, spec) + "}| " + printAction (((InterleaveAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof PrefixingAction) {
String commName = ((PrefixingAction)action).getCommunication().getChannelExpr().getName().toString();
//return commName + " -> " + printAction (((PrefixingAction)action).getCircusAction());
return printCommunication (((PrefixingAction)action).getCommunication()) + " -> " + printAction (((PrefixingAction)action).getCircusAction(), spec);
}
else if (action instanceof GuardedAction) {
return "(" + /*"(PREDICADO)"*/ printPred (((GuardedAction)action).getPred()) + " & " + printAction (((GuardedAction)action).getCircusAction(), spec) + ")";
}
else if (action instanceof MuAction) {
return "u" + ((MuAction)action).getName().toString() + " @ " + printAction (((MuAction)action).getCircusAction(), spec);
}
else if (action instanceof HideAction) {
return "(" + printAction (((HideAction)action).getCircusAction(), spec) + ")";
}
else if (action instanceof SkipAction) {
return "SKIP";
}
else if (action instanceof StopAction) {
return "STOP";
}
else if (action instanceof ChaosAction) {
return "CHAOS";
}
else if (action instanceof CallAction) {
ZExprList callexprs = ((CallAction)action).getZExprList();
int size = callexprs.size();
String ret = ((CallAction)action).getZName().toString();
for (int i = 0; i < size; i++) {
if (i == 0) {
ret = ret + " ( ";
}
ret = ret + printExpression (callexprs.get(i));
if (i == size - 1) {
ret = ret + " )";
}
}
return ret;
//return ((CallAction)action).getZName().toString() + " ( " + printExpression ();
}
else if (action instanceof RenameAction) {
return printAction (((RenameAction)action).getCircusAction(), spec) + printAssignmentPairs (((RenameAction)action).getAssignmentPairs());
}
else if (action instanceof CircusCommand) {
if (action instanceof VarDeclCommand) {
VarDeclCommand vdc = (VarDeclCommand)action;
String printDeclList = "";
if (vdc != null) {
if (vdc.getDeclList() != null)
printDeclList = printZDeclList (vdc.getZDeclList());
}
String printAction = printAction (vdc.getCircusAction(), spec);
return "[" + printDeclList + "]" + printAction;
}
else if (action instanceof AssignmentCommand) {
String str = "";
AssignmentCommand as = (AssignmentCommand)action;
ZNameList names = as.getAssignmentPairs().getZLHS();
ZExprList exprs = as.getAssignmentPairs().getZRHS();
for (int i = 0; i < names.size(); i++) {
str = str + names.get(i).toString();
if (i < names.size() - 1) {
str = str + ", ";
}
}
str = str + " = ";
for (int i = 0; i < exprs.size(); i++) {
str = str + printExpression (exprs.get(i));
if (i < exprs.size() - 1) {
str = str + ", ";
}
}
return str;
}
else if (action instanceof IfGuardedCommand) {
IfGuardedCommand igc = (IfGuardedCommand)action;
CircusActionList actions = (CircusActionList) igc.getActionList();
String str = "if (PREDICADO) then ";
for (int i = 0; i < actions.size(); i++) {
str = str + printAction (actions.get(i), spec);
if (i < actions.size() - 2) {
str = str + " else if ";
}
else if (i < actions.size() - 1) {
str = str + " else ";
}
else {
str = str + " endif";
}
}
return str;
}
else {//if (action instanceof SpecStmtCommand) {
return "";
}
}
else {
return "";
}
}
private static String printSchExpr (SchExpr schexpr) {
String str = "SchExpr { " + printZSchText (schexpr.getZSchText()) + " }";
return str;
}
public static String printExpression (Expr expr) {
System.out.println ("-------------||||||||----------");
if (expr instanceof RefExpr) {
return ((RefExpr)expr).getZName().toString();
}
else if (expr instanceof NumExpr) {
return ((NumExpr)expr).getValue().toString();
}
else if (expr instanceof TupleExpr) {
String str = "";
TupleExpr expr2 = (TupleExpr)expr;
ZExprList exprs = expr2.getZExprList();
for (int i = 0; i < exprs.size(); i++) {
str = str + printExpression (exprs.get(i));
if (i < exprs.size() - 1)
str = str + "; ";
}
return str;
}
else if (expr instanceof ApplExpr) {
String str = "";
String left = printExpression (((ApplExpr) expr).getLeftExpr());
String right = printExpression (((ApplExpr) expr).getRightExpr());
str = str + "(" + left + " [" + right + "]" + ")";
return str;
}
else if (expr instanceof SetExpr) {
String str = "SetExpr {";
SetExpr setexpr = (SetExpr)expr;
ZExprList exprs = setexpr.getZExprList();
int size = exprs.size();
for (int i = 0; i < size; i++) {
str = str + exprs.get(i);
if (i < size - 1) {
str = str + ", ";
}
}
return str + "}";
}
else if (expr instanceof SchExpr) {
return printSchExpr ((SchExpr)expr);
}
else {
return "EXPR";
}
}
public static String printProcess (CircusProcess process, Spec spec) {
String str = "";
if (process instanceof ExtChoiceProcess) {
return "(" + printProcess (((ExtChoiceProcess)process).getLeftProcess(), spec) + " [] " + printProcess (((ExtChoiceProcess)process).getRightProcess(), spec) + ")";
}
else if (process instanceof IntChoiceProcess) {
return "(" + printProcess (((IntChoiceProcess)process).getLeftProcess(), spec) + " |~| " + printProcess (((IntChoiceProcess)process).getRightProcess(), spec) + ")";
}
else if (process instanceof SeqProcess) {
return "(" + printProcess (((SeqProcess)process).getLeftProcess(), spec) + " ; " + printProcess (((SeqProcess)process).getRightProcess(), spec) + ")";
}
else if (process instanceof ParProcess) {
if (process instanceof ParallelProcess) {
return
"(" + printProcess (((ParProcess)process).getLeftProcess(), spec) +
" |{" + printChannelSet (process, spec) + "}| "
+ printProcess (((ParProcess)process).getRightProcess(), spec) + ")";
}
else /*if (process instanceof InterleaveProcess)*/ {
return "(" + printProcess (((ParProcess)process).getLeftProcess(), spec) + " ||| " + printProcess (((ParProcess)process).getRightProcess(), spec) + ")";
}
}
else if (process instanceof HideProcess) {
return "(" + printProcess (((HideProcess)process).getCircusProcess(), spec) + ")";
}
else if (process instanceof CallProcess) {
return ((CallProcess)process).getCallExpr().getZName().toString();
}
else if (process instanceof RenameProcess) {
return printProcess (((RenameProcess)process).getCircusProcess(), spec) + printAssignmentPairs (((RenameProcess)process).getAssignmentPairs());
}
else if (process instanceof BasicProcess) {
return printBasicProcess ((BasicProcess)process, spec);
}
else if (process instanceof ParamProcess) {
ZDeclList decls = ((ParamProcess)process).getZDeclList();
return " (params) " + printBasicProcess (((ParamProcess)process).getCircusBasicProcess(), spec);//action.getClass().toString();
}
else {
return " Process to have its implementation exhibited ";
}
}
public static String printAssignmentPairs (AssignmentPairs ap) {
ZNameList lhs = (ZNameList) ap.getLHS();
ZNameList rhs = (ZNameList) ap.getRHS();
String str = "";
for (int i = 0; i < lhs.size(); i++) {
str = str + lhs.get(i).toString() + " <- " + rhs.get(i).toString();
if (i < lhs.size() - 1) {
str = str + ", ";
}
}
return str;
}
public static String printAnns4Action (CircusAction action, Spec spec) { //TODO ERRADO
String str = "";
if (action instanceof MuAction) {
MuAction act = (MuAction) action;
String name = act.getName().toString();
return "(mu" + name + " * " + printAnns4Action (act.getCircusAction(), spec) + ")";
}
/*else if (action instanceof CircusCommand) {
}*/
else if (action instanceof ExtChoiceAction) {
return "(" + printAnns4Action (((ExtChoiceAction)action).getLeftAction(), spec) + " [] " + printAnns4Action (((ExtChoiceAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof IntChoiceAction) {
return "(" + printAnns4Action (((IntChoiceAction)action).getLeftAction(), spec) + " |~| " + printAnns4Action (((IntChoiceAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof SeqAction) {
return "(" + printAnns4Action (((SeqAction)action).getLeftAction(), spec) + " ; " + printAnns4Action (((SeqAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof ParAction) {
return "(" + printAnns4Action (((ParAction)action).getLeftAction(), spec) + " || " + printAnns4Action (((ParAction)action).getRightAction(), spec) + ")";
}
else if (action instanceof PrefixingAction || action instanceof CallAction) {
return " " + action.getAnn(Integer.class) + " ";
}
else if (action instanceof GuardedAction) {
return "(" + printAction (((GuardedAction)action).getCircusAction(), spec) + ")";
}
else if (action instanceof HideAction) {
return "(" + printAction (((HideAction)action).getCircusAction(), spec) + ")";
}
else if (action instanceof SkipAction) {
return "SKIP";
}
else if (action instanceof ChaosAction) {
return "CHAOS";
}
else if (action instanceof CallAction) {
return ((CallAction)action).getName().toString();
}
else {
return "ACTION";
}
}
public static int countCallActions (CircusAction action, int counter) {
if (action instanceof CallAction) {
counter++;
return counter;
}
else if (action instanceof Action2) {
return countCallActions (((Action2)action).getLeftAction(), counter) + countCallActions (((Action2)action).getRightAction(), counter);
}
else if (action instanceof Action1) {
return countCallActions (((Action1)action).getCircusAction(), counter);
}
else if (action instanceof CircusCommand) {
if (action instanceof AssignmentCommand) {
return 0;
}
else if (action instanceof VarDeclCommand) {
return countCallActions (((VarDeclCommand)action).getCircusAction(), counter);
}
else if (action instanceof IfGuardedCommand) {
CircusActionList cal = ((IfGuardedCommand)action).getGuardedActionList();
int size = cal.size();
int ifguardedcounter = 0;
for (int i = 0; i < size; i++) {
CircusAction act = cal.get(i);
ifguardedcounter += countCallActions (act, counter);
}
return ifguardedcounter;
}
else {
ExceptionUtil.throwImplementationException("PrinterUtil.countCallActions", action.getClass());
return 0;
}
}
else if (action instanceof BasicAction) {
return 0;
}
else {
ExceptionUtil.throwImplementationException("PrinterUtil.countCallActions", action.getClass());
return 0;
}
}
}