ScopeUtil.java 13.2 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
package jcircus.newutil;

import java.util.List;
import java.util.Vector;

import jcircus.anns.MuCallAnn;

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.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.CircusAction;
import net.sourceforge.czt.circus.ast.CircusActionList;
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.DotField;
import net.sourceforge.czt.circus.ast.Field;
import net.sourceforge.czt.circus.ast.GuardedAction;
import net.sourceforge.czt.circus.ast.IfGuardedCommand;
import net.sourceforge.czt.circus.ast.InputField;
import net.sourceforge.czt.circus.ast.MuAction;
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.Process1;
import net.sourceforge.czt.circus.ast.Process2;
import net.sourceforge.czt.circus.ast.VarDeclCommand;
import net.sourceforge.czt.z.ast.AndPred;
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.FalsePred;
import net.sourceforge.czt.z.ast.InclDecl;
import net.sourceforge.czt.z.ast.MemPred;
import net.sourceforge.czt.z.ast.NegPred;
import net.sourceforge.czt.z.ast.OrPred;
import net.sourceforge.czt.z.ast.Para;
import net.sourceforge.czt.z.ast.Pred;
import net.sourceforge.czt.z.ast.Spec;
import net.sourceforge.czt.z.ast.TruePred;
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.ZName;
import net.sourceforge.czt.z.ast.ZNameList;
import net.sourceforge.czt.z.ast.ZParaList;
import net.sourceforge.czt.z.ast.ZSchText;

/**Author: Samuel Barrocas (created in 13/06/2013)
* It works as an auxiliary util for determining if a variable is on the scope of a process...
*
* */

public class ScopeUtil {
private static boolean variableIsInsideExpression (String var, Expr expr, Vector <String> inputFieldVariables) {
if (inputFieldVariables.contains(var)) {
return false;
}
return ExprUtil.exprContainsName(expr, var);
}
private static boolean variableIsInsideZExprList (String var, ZExprList exprlist, Vector <String> inputFieldVariables) {
int size = exprlist.size();
boolean aux = false;
for (int i = 0; i < size; i++) {
aux = aux || variableIsInsideExpression (var, exprlist.get(i), inputFieldVariables);
}
return aux;
}
private static boolean variableIsInsideZName (String var, ZName name) {
if (name.toString().equals(var))
return true;
return false;
}
private static boolean variableIsInsideZNameList (String var, ZNameList namelist) {
int size = namelist.size();
boolean aux = false;
for (int i = 0; i < size; i++) {
ZName name = (ZName) namelist.get(i);
aux = aux || variableIsInsideZName (var, name);
}
return aux;
}
private static boolean variableIsInsideDecl (String var, Decl decl, Vector <String> inputFieldVariables) {
if (decl instanceof VarDecl) {
ZNameList namelist = ((VarDecl)decl).getZNameList();
Expr expr = ((VarDecl)decl).getExpr();
return variableIsInsideZNameList (var, namelist) || variableIsInsideExpression (var, expr, inputFieldVariables);
}
else if (decl instanceof ConstDecl) {
Expr expr = ((ConstDecl)decl).getExpr();
ZName name = ((ConstDecl)decl).getZName();
return variableIsInsideZName (var, name) || variableIsInsideExpression (var, expr, inputFieldVariables);
}
else if (decl instanceof InclDecl) {
return variableIsInsideExpression (var, ((InclDecl)decl).getExpr(), inputFieldVariables);
}
//else if () {} //mais algum??
else {
ExceptionUtil.throwException ("ScopeUtil.variableIsInsideDecl method: there is no translation to this Decl declaration...");
return false;
}
}
private static boolean variableIsInsideZDeclList (String var, ZDeclList decllist, Vector <String> inputFieldVariables) {
int size = decllist.size();
boolean aux = false;
for (int i = 0; i < size; i++) {
aux = aux || variableIsInsideDecl (var, decllist.get(i), inputFieldVariables);
}
return aux;
}
private static boolean variableIsInsidePred (String var, Pred pred, Vector <String> inputFieldVariables) {
if (pred instanceof MemPred) {
boolean left = variableIsInsideExpression (var, ((MemPred)pred).getLeftExpr(), inputFieldVariables);
boolean right = variableIsInsideExpression (var, ((MemPred)pred).getRightExpr(), inputFieldVariables);
return left || right;
}
else if (pred instanceof OrPred) {
Pred left = ((OrPred)pred).getLeftPred();
Pred right = ((OrPred)pred).getRightPred();
return
variableIsInsidePred (var, left, inputFieldVariables)
|| variableIsInsidePred (var, right, inputFieldVariables);
}
else if (pred instanceof AndPred) {
Pred left = ((AndPred)pred).getLeftPred();
Pred right = ((AndPred)pred).getRightPred();
return
variableIsInsidePred (var, left, inputFieldVariables)
|| variableIsInsidePred (var, right, inputFieldVariables);
}
else if (pred instanceof TruePred || pred instanceof FalsePred) {
return false;
}
else if (pred instanceof NegPred) {
return variableIsInsidePred (var, ((NegPred)pred).getPred(), inputFieldVariables);
}
else {
ExceptionUtil.throwException("ScopeUtil.variableIsInsidePred: Does not accept the predicate " + pred.getClass() + ". Why not implementing it for this method? ;)");
return false;
}
}
public static boolean variableIsInsideAction (String var, CircusAction action, BasicProcess process, Spec spec) {
return variableIsInsideAction (var, action, process, new Vector <String> (), new Vector <String> (), spec);
}
private static boolean variableIsInsideAction (String var, CircusAction action, BasicProcess process, Vector <String> calls, Vector <String> inputFieldVariables, Spec spec) {
if (action instanceof CircusCommand) {
return variableIsInsideCommand (var, (CircusCommand)action, process, calls, inputFieldVariables, spec);
}
else if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
return
variableIsInsideAction (var, left, process, calls, inputFieldVariables, spec)
|| variableIsInsideAction (var, right, process, calls, inputFieldVariables, spec);
}
/*else if (action instanceof Action1) {
CircusAction getaction = ((Action1)action).getCircusAction();
return variableIsInsideAction (var, getaction, process, calls);
}*/
else if (action instanceof GuardedAction) {
Pred pred = ((GuardedAction)action).getPred();
boolean isonpred = variableIsInsidePred (var, pred, inputFieldVariables);
boolean isonaction = variableIsInsideAction (var, ((GuardedAction)action).getCircusAction(), process, calls, inputFieldVariables, spec);
return isonpred || isonaction;
}
else if (action instanceof ParamAction) {
CircusAction circusaction = ((ParamAction)action).getCircusAction();
ZDeclList decls = ((ParamAction)action).getZDeclList();
return
variableIsInsideAction (var, circusaction, process, calls, inputFieldVariables, spec)
&& !variableIsInsideZDeclList (var, decls, inputFieldVariables)
;
}
else if (action instanceof PrefixingAction) {
CircusFieldList cfl = ((PrefixingAction)action).getCommunication().getCircusFieldList();
CircusAction prefixed = ((PrefixingAction)action).getCircusAction ();
int cflsize = cfl.size();
boolean aux = false;
for (int i = 0; i < cflsize; i++) {
Field field = cfl.get(i);
if (field instanceof InputField) {
aux = false;
ZName name = ((InputField)field).getVariableZName();
inputFieldVariables.addElement(name.toString());
//aux = aux || variableIsInsideZName (var, name);
}
else if (field instanceof DotField) {
Expr expr = ((DotField)field).getExpr();
aux = aux || variableIsInsideExpression (var, expr, inputFieldVariables);
}
}
return aux || variableIsInsideAction (var, prefixed, process, calls, inputFieldVariables, spec);
}
else if (action instanceof BasicAction) {
return false;
}
else if (action instanceof MuAction) {
CircusAction cact = ((MuAction)action).getCircusAction();
ZName name = ((MuAction)action).getZName();
Vector <String> vec = new Vector <String> (calls);//.add(name.toString());
vec.add(name.toString());
return variableIsInsideAction (var, cact, process, vec, inputFieldVariables, spec);
}
else if (action instanceof CallAction) {
if (calls.contains(((CallAction)action).getZName().toString())) {
return false;
}
if (action.getAnn(MuCallAnn.class) != null) {
return false;
}
Vector <String> newcalls = new Vector <String> ();
newcalls.addAll(calls);
newcalls.addElement(((CallAction)action).getZName().toString());
return variableIsInsideAction (var, CallUtil.getContentOfCallAction((CallAction)action, process, spec), process, newcalls, inputFieldVariables, spec);
//return false;
}
else {
if (action == null) {
ExceptionUtil.throwException ("ScopeUtil.variableIsInsideAction: null action!");
}
ExceptionUtil.throwException ("ScopeUtil.variableIsInsideAction: Still cannot return for " + action.getClass());
return false;
}
}
private static boolean variableIsInsideCommand (String var, CircusCommand command, BasicProcess process, Vector <String> calls, Vector <String> inputFieldVariables, Spec spec) {
if (command instanceof AssignmentCommand) {
AssignmentPairs asspairs = ((AssignmentCommand)command).getAssignmentPairs();
return
variableIsInsideZNameList (var, asspairs.getZLHS())
|| variableIsInsideZExprList (var, asspairs.getZRHS(), inputFieldVariables);
}
else if (command instanceof VarDeclCommand) {
ZDeclList decllist = ((VarDeclCommand)command).getZDeclList();
boolean aux = variableIsInsideZDeclList (var, decllist, inputFieldVariables);
boolean action = variableIsInsideAction (var, ((VarDeclCommand)command).getCircusAction(), process, calls, inputFieldVariables, spec);
return aux || action;
}
else if (command instanceof IfGuardedCommand) {
CircusActionList actionlist = ((IfGuardedCommand)command).getGuardedActionList();
int size = actionlist.size();
boolean aux = false;
for (int i = 0; i < size; i++) {
GuardedAction ga = (GuardedAction) actionlist.get(i);
aux = aux || variableIsInsideAction (var, ga, process, calls, inputFieldVariables, spec);
}
return aux;
}
else {
ExceptionUtil.throwException ("ScopeUtil.variableIsInsideCommand was not implemented yet for this command...");
return false;
}
}
private static boolean variableIsInsideZSchText (String var, ZSchText schtext, Vector <String> inputFieldVariables) {
ZDeclList decllist = schtext.getZDeclList();
Pred pred = schtext.getPred();
boolean isondecllist = variableIsInsideZDeclList (var, decllist, inputFieldVariables);
boolean isonpred = variableIsInsidePred (var, pred, inputFieldVariables);
return isondecllist || isonpred;
}
private static boolean variableIsInsideAxPara (String var, AxPara para, Vector <String> inputFieldVariables) {
ZSchText schtext = ((AxPara)para).getZSchText();
ZNameList namelist = ((AxPara)para).getZNameList();
boolean isonschtext = variableIsInsideZSchText (var, schtext, inputFieldVariables);
boolean isonnamelist = variableIsInsideZNameList (var, namelist);
return isonschtext || isonnamelist;
}
public static boolean variableIsInsideProcess (String var, CircusProcess process, Spec spec) {
if (process instanceof ParamProcess) {
boolean isondecllist = variableIsInsideZDeclList (var, ((ParamProcess)process).getZDeclList(), new Vector <String> ());
boolean isonotherprocess = variableIsInsideProcess (var, ((ParamProcess)process).getCircusProcess(), spec);
return isondecllist || isonotherprocess;
}
else if (process instanceof BasicProcess) {
ZParaList paralist = ((BasicProcess)process).getZParaList();
int size = paralist.size();
boolean aux = false;
for (int i = 0; i < size; i++) {
Para para = paralist.get(i);
if (para instanceof AxPara) {
aux = aux || variableIsInsideAxPara (var, (AxPara)para, new Vector <String> ());
}
else if (para instanceof ActionPara) {
aux = aux || variableIsInsideAction (var, ((ActionPara)para).getCircusAction(), (BasicProcess)process, spec);
}
}
return aux;
}
else if (process instanceof Process2) {
return
variableIsInsideProcess (var, ((Process2)process).getLeftProcess (), spec)
|| variableIsInsideProcess (var, ((Process2)process).getRightProcess (), spec);

}
else if (process instanceof Process1) {
return variableIsInsideProcess (var, ((Process1)process).getCircusProcess (), spec);
}
else {
ExceptionUtil.throwException ("ScopeUtil.variableIsInsideProcess: this process was not implemented yet on this method");
return false;
}
}
}