Blame view
circus/src/jcircus/newutil/ExprUtil.java
21 KB
8d0dc533f
![]() |
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
package jcircus.newutil; import java.util.ArrayList; import java.util.List; 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.AssignmentCommand; import net.sourceforge.czt.circus.ast.AssignmentPairs; import net.sourceforge.czt.circus.ast.BasicAction; 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.Communication; 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.ParamAction; import net.sourceforge.czt.circus.ast.PrefixingAction; import net.sourceforge.czt.circus.ast.VarDeclCommand; import net.sourceforge.czt.circus.util.Factory; import net.sourceforge.czt.z.ast.AndPred; import net.sourceforge.czt.z.ast.ApplExpr; 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.MemPred; import net.sourceforge.czt.z.ast.NegPred; 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.ProdExpr; import net.sourceforge.czt.z.ast.RefExpr; import net.sourceforge.czt.z.ast.SetExpr; import net.sourceforge.czt.z.ast.Spec; import net.sourceforge.czt.z.ast.TruePred; 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; public class ExprUtil { public static int sizeOfExpression (Expr expr) { if (expr instanceof RefExpr) { String name = ((RefExpr)expr).getZName().toString(); if (name.equals ("$$SYNCH")) { return 0; } return 1; } else if (expr instanceof ProdExpr) { ZExprList exprlist = ((ProdExpr)expr).getZExprList(); return exprlist.size(); } else { ExceptionUtil.throwImplementationException ("ExprUtil.sizeOfExpression", expr.getClass()); return 0; } } public static ZExprList fromVectorToRefExpr (Vector <String> names) { Factory f = new Factory (); ZExprList returnexprs = f.createZExprList(); int size = names.size(); for (int i = 0; i < size; i++) { returnexprs.add(f.createRefExpr(f.createZName(names.elementAt(i)))); } return returnexprs; } public static boolean exprContainsName (Expr expr, String name) { if (expr instanceof RefExpr) { String exprname = ((RefExpr)expr).getZName().toString(); if (exprname.equals(name)) { return true; } else { return false; } } else if (expr instanceof NumExpr) { return false; //Isto ocorre porque o nome do parâmetro não pode ser igual a uma expressão numérica } else if (expr instanceof TupleExpr) { boolean aux = false; ZExprList tupleexprs = ((TupleExpr)expr).getZExprList(); int size = tupleexprs.size(); for (int i = 0; i < size; i++) { aux = aux || exprContainsName (tupleexprs.get(i), name); } return aux; } else if (expr instanceof SetExpr) { boolean aux = false; ZExprList exprlist = ((SetExpr)expr).getZExprList(); int size = exprlist.size(); for (int i = 0; i < size; i++) { Expr expri = exprlist.get(i); aux = aux || exprContainsName (expri, name); } return aux; } else if (expr instanceof ApplExpr) { return exprContainsName (((ApplExpr)expr).getRightExpr(), name); } else /*if (expr instanceof ApplExpr)*/ { ExceptionUtil.throwException("ExprUtil" + ".exprContainsName :: " + "this method does not have a implementation for " + expr.getClass() + ". Why not implementing it now? ;)"); return false; } } public static Expr updateExpr (String namevar, Expr newExpr, Expr targetExpr) { /** * Atualiza o valor de uma variável em uma expressão. * Verifica se uma expressão-alvo (targetExpr) possui uma dada variável (namevar), e se a expressão-alvo tiver a variável, * Substitui a chamada da variável na expressão-alvo pela nova expressão (newExpr). * Por exemplo, supomos que temos a expressão x + y e queremos substituir x por 5. * Neste caso, x + y é a expressão-alvo (targetExpr), * x é a variável que queremos que seja substituída (namevar), * e 5 é a nova expressão (newExpr). Neste caso, updateExpr retornará x + 5 */ Factory f = new Factory (); FactoryUtil fu = new FactoryUtil (); if (targetExpr instanceof RefExpr) { String nametargetexpr = ((RefExpr)targetExpr).getName().toString(); Expr returnnewexpr = fu.createExpr (newExpr); Expr returntargetexpr = fu.createExpr (targetExpr); if (namevar.equals(nametargetexpr)) { return returnnewexpr; } else { return returntargetexpr; } } else if (targetExpr instanceof ApplExpr) { ApplExpr appl = (ApplExpr) fu.createExpr ((ApplExpr)targetExpr); appl.setRightExpr(updateExpr (namevar, newExpr, ((ApplExpr) targetExpr).getRightExpr())); return appl; } else if (targetExpr instanceof TupleExpr) { ZExprList exprList = fu.createZExprList (((TupleExpr)targetExpr).getZExprList()); for (int i = 0; i < exprList.size(); i++) { exprList.set(i, updateExpr (namevar, newExpr, exprList.get(i))); } TupleExpr tupleexpr = f.createTupleExpr(); tupleexpr.setExprList(exprList); return tupleexpr; } else if (targetExpr instanceof NumExpr) { /** * Uma variável nunca pode ser encontrada numa NumExpr... * */ return fu.createExpr (targetExpr); } //else if () {} //OUTROS TIPOS DE EXPRESSÃO? else { ExceptionUtil.throwImplementationException ("ExprUtil.updateExpr", targetExpr.getClass()); return fu.createExpr (targetExpr); } } public static Expr updateExpr (List <VarDecl> decls, ZExprList newExprs, Expr targetExpr) { Factory f = new Factory (); int size1 = decls.size(); Expr oldexprAux = f.createRefExpr(); for (int i = 0; i < size1; i++) { oldexprAux = ExprUtil.updateExpr (decls.get(i).getName().toString(), newExprs.get(i), targetExpr); } return oldexprAux; } protected static boolean areTheSameExpr (Expr expr1, Expr expr2) { if (expr1 instanceof RefExpr && expr2 instanceof RefExpr) { String name1 = ((RefExpr)expr1).getZName().toString(); String name2 = ((RefExpr)expr2).getZName().toString(); if (name1.equals(name2)) { return true; } else { return false; } } else { return false; } } public static CircusAction updateExpression (List <VarDecl> decls, ZExprList paramExprs, CircusAction action, CircusProcess process, Spec spec) { CircusAction action2; action2 = ExprUtil.actionWithUpdatedExpression (decls, paramExprs, action, process, spec); return action2; } /*public static CircusAction actionWithUpdatedExpression (ZDeclList decls, CircusAction action) { Factory f = new Factory (); ZExprList refexprlist = f.createZExprList(); int size = decls.size(); int counter = 0; for (int i = 0; i < size; i++) { Decl decl = decls.get(i); if (decl instanceof VarDecl) { VarDecl vardecl = (VarDecl)decl; ZNameList names = vardecl.getZNameList(); int namessize = names.size(); for (int j = 0; j < namessize; j++) { refexprlist.add(f.createRefExpr(f.createZName(names.get(j).toString()))); } } } return actionWithUpdatedExpression (decls, refexprlist, action); }*/ public static Vector <String> pActname2Vector (int n, String actionname) { Vector <String> names = new Vector <String> (); for (int i = 0; i < n; i++) { names.addElement ("p" + actionname + i); } return names; } public static ZExprList pActname2ExprList (int n, String actionname) { Vector <String> names = pActname2Vector (n, actionname); return fromVectorToRefExpr (names); } //actionWithUpdatedExpression (ZDeclList decls, ZExprList paramExprs, CircusAction action) //actionWithUpdatedExpression (decls2beupdated, pActname2ExprList (decls2beupdated.size(), ((CallAction)action).getName().toString()), updatedContent); public static CircusAction actionWithUpdatedExpression (List <VarDecl> decls, ZExprList paramExprs, CircusAction action, CircusProcess process, Spec spec) { return actionWithUpdatedExpression (decls, paramExprs, action, process, spec, new Vector <String> (), new Vector <String> (), new Consumer (new Vector <String> ())); } public static CircusAction actionWithUpdatedExpression (ZDeclList decls, ZExprList paramExprs, CircusAction action, CircusProcess process, Spec spec, Vector <String> mus, Vector <String> actionNames, Consumer c) { List <VarDecl> listdecls = new ArrayList <VarDecl> (); int size = decls.size(); for (int i = 0; i < size; i++) { Decl decl = decls.get(i); if (decl instanceof VarDecl) listdecls.add((VarDecl)decl); } return actionWithUpdatedExpression (listdecls, paramExprs, action, process, spec, mus, actionNames, c); } public static Pred predWithUpdatedExpressions (List <VarDecl> decls, ZExprList paramExprs, Pred pred, CircusProcess process, Spec spec, Vector <String> mus, Vector <String> actionNames, Consumer c) { Factory f = new Factory (); FactoryUtil fu = new FactoryUtil (); if (pred instanceof NegPred) { NegPred negpred = f.createNegPred (); negpred.setPred(predWithUpdatedExpressions (decls, paramExprs, ((NegPred) pred).getPred(), process, spec, mus, actionNames, c)); negpred.getAnns().addAll(pred.getAnns()); return negpred; } else if (pred instanceof TruePred) { return f.createTruePred(); } else if (pred instanceof FalsePred) { return f.createFalsePred (); } else if (pred instanceof AndPred) { AndPred andpred = f.createAndPred(); andpred.setAnd(((AndPred) pred).getAnd()); andpred.setLeftPred(fu.createPred(((AndPred) pred).getLeftPred())); andpred.setRightPred(fu.createPred(((AndPred) pred).getRightPred())); return andpred; } else if (pred instanceof OrPred) { OrPred orpred = f.createOrPred(); orpred.setLeftPred(fu.createPred(((OrPred) pred).getLeftPred())); orpred.setRightPred(fu.createPred(((OrPred) pred).getRightPred())); return orpred; } else if (pred instanceof MemPred) { MemPred mempred = f.createMemPred(); mempred.setMixfix(((MemPred) pred).getMixfix()); Expr leftexpr = ((MemPred) pred).getLeftExpr(); Expr rightexpr = ((MemPred) pred).getRightExpr(); Expr newleftexpr = fu.createExpr(leftexpr); Expr newrightexpr = fu.createExpr (rightexpr); Vector <String> vectordecls = DeclUtil.declListAsVector(DeclUtil.listVarDeclAsDeclList (decls)); int size = vectordecls.size(); for (int i = 0; i < size; i++) { Expr currentexpr = paramExprs.get(i); if (exprContainsName (leftexpr, vectordecls.elementAt(i))) { newleftexpr = fu.createExpr(updateExpr (vectordecls.elementAt(i), paramExprs.get(i), leftexpr)); } if (exprContainsName (rightexpr, vectordecls.elementAt(i))) { newrightexpr = fu.createExpr(updateExpr (vectordecls.elementAt(i), paramExprs.get(i), rightexpr)); } } System.out.println ("Old left: " + PrinterUtil.printExpression(leftexpr)); System.out.println ("Old right: " + PrinterUtil.printExpression(rightexpr)); System.out.println ("New left: " + PrinterUtil.printExpression(newleftexpr)); System.out.println ("New right: " + PrinterUtil.printExpression(newrightexpr)); mempred.setLeftExpr (newleftexpr); mempred.setRightExpr (newrightexpr); return mempred; } else { ExceptionUtil.throwImplementationException ("ExprUtil.predWithUpdatedExpression", pred.getClass()); return f.createTruePred(); } } public static CircusAction actionWithUpdatedExpression (List <VarDecl> decls, ZExprList paramExprs, CircusAction action, CircusProcess process, Spec spec, Vector <String> mus, Vector <String> actionNames, Consumer c) { Factory f = new Factory (); FactoryUtil fu = new FactoryUtil (); CircusAction auxaction = ParamUtil.pf.createCircusAction (action); if (auxaction instanceof ParamAction) { CircusAction action2 = ((ParamAction)auxaction).getCircusAction(); ParamAction returnaction = f.createParamAction(); returnaction.setDeclList(((ParamAction) auxaction).getZDeclList()); returnaction.setCircusAction(actionWithUpdatedExpression (decls, paramExprs, action2, process, spec, mus, actionNames, c)); return returnaction; } else if (auxaction instanceof CircusCommand) { if (auxaction instanceof AssignmentCommand) { AssignmentPairs assPairs = (new Factory ()).createAssignmentPairs(((AssignmentCommand)auxaction).getAssignmentPairs().getLHS(), ((AssignmentCommand)auxaction).getAssignmentPairs().getRHS()); ZExprList exprList = assPairs.getZRHS(); ZExprList finalExprList = (new Factory ()).createZExprList(); int counter = 0; for (int i = 0; i < exprList.size(); i++) { Decl decl = decls.get(i); if (decl instanceof VarDecl) { ZNameList namelist = ((VarDecl)decl).getZNameList(); for (int j = 0; j < namelist.size(); j++) { if (exprContainsName (exprList.get(i), namelist.get(j).toString())) { System.out.println (PrinterUtil.printExpression (exprList.get(i))); finalExprList.add(updateExpr (namelist.get(j).toString(), paramExprs.get(counter), exprList.get(i))); System.out.println (PrinterUtil.printExpression (exprList.get (i))); System.out.println (PrinterUtil.printExpression (finalExprList.get(counter))); System.out.print (""); } else { finalExprList.add(exprList.get(j)); } counter++; } } /*if (exprContainsName (exprList.get(i), decls.get(i).getName().toString())) { finalExprList.add(updateExpr (decls.get(i).getName().toString(), paramExprs.get(i), exprList.get(i))); } else { finalExprList.add(exprList.get(i)); }*/ } assPairs.setRHS(finalExprList/*exprList*/); ((AssignmentCommand)auxaction).setAssignmentPairs(assPairs); return auxaction; } else if (auxaction instanceof IfGuardedCommand) { int size = ((IfGuardedCommand)auxaction).getNumberOfGuards(); CircusActionList al = ((IfGuardedCommand)auxaction).getGuardedActionList(); for (int i = 0; i < size; i++) { CircusAction act = al.get(i); act = actionWithUpdatedExpression (decls, paramExprs, al.get(i), process, spec, mus, actionNames, c); al.set(i, act); } ((IfGuardedCommand)auxaction).setActionList(al); System.out.print (""); return auxaction; } else if (auxaction instanceof VarDeclCommand) { VarDeclCommand vdc = f.createVarDeclCommand(); vdc.setDeclList(((VarDeclCommand)auxaction).getZDeclList()); vdc.setCircusAction(actionWithUpdatedExpression (decls, paramExprs, ((VarDeclCommand)auxaction).getCircusAction(), process, spec, mus, actionNames, c)); return vdc; } else { ExceptionUtil.throwImplementationException("actionWithUpdatedExpression", auxaction.getClass()); return auxaction; } //return auxaction; } else if (auxaction instanceof Action2) { Action2 action2 = (Action2)auxaction; action2.setLeftAction(actionWithUpdatedExpression (decls, paramExprs, action2.getLeftAction(), process, spec, mus, actionNames, c)); action2.setRightAction (actionWithUpdatedExpression (decls, paramExprs, action2.getRightAction(), process, spec, mus, actionNames, c)); return action2; } else if (auxaction instanceof PrefixingAction) { Communication comm = ((PrefixingAction)auxaction).getCommunication(); CircusFieldList cfl = comm.getCircusFieldList(); int size = cfl.size(); int counter = 0; for (int i = 0; i < size; i++) { Field field = cfl.get(i); if (field instanceof DotField) { DotField dotfield = (DotField)field; Expr target = dotfield.getExpr(); int declssize = decls.size(); for (int j = 0; j < declssize; j++) { int namelistsize = 0; if (decls.get(j) instanceof VarDecl) { namelistsize = ((VarDecl)decls.get(j)).getZNameList().size(); ZNameList namelist = ((VarDecl)decls.get(j)).getZNameList(); for (int k = 0; k < namelistsize; k++) { boolean exprContainsName = exprContainsName (target, ((VarDecl)decls.get(j)).getZNameList().get(k).toString()); if (exprContainsName (target, ((VarDecl)decls.get(j)).getZNameList().get(k).toString())) { dotfield.setExpr(updateExpr (namelist.get(k).toString(), paramExprs.get(counter), target)); counter++; } } } else if (decls.get(j) instanceof ConstDecl) { namelistsize = 1; if (exprContainsName (target, ((ConstDecl)decls.get(j)).getZName().toString())) { dotfield.setExpr(updateExpr (((ConstDecl)decls.get(j)).getZName().toString(), paramExprs.get(counter), target)); counter++; } } else { ExceptionUtil.throwImplementationException("actionWithUpdatedExpression", decls.get(j).getClass()); } /* for (int k = 0; k < namelistsize; k++) { if (exprContainsName (target, decls.get(j).getName().toString())) { dotfield.setExpr(updateExpr (decls.get(i).getName().toString(), paramExprs.get(i), target)); } } */ } field = dotfield; } cfl.set(i, field); } comm.setFieldList(cfl); ((PrefixingAction)auxaction).setCommunication(comm); ((PrefixingAction)auxaction).setCircusAction(actionWithUpdatedExpression (decls, paramExprs, ((PrefixingAction) auxaction).getCircusAction(), process, spec, mus, actionNames, c)); return auxaction; } if (auxaction instanceof GuardedAction) { Pred pred = ((GuardedAction)auxaction).getPred(); Pred updatedpred = predWithUpdatedExpressions (decls, paramExprs, pred, process, spec, mus, actionNames, c); Pred newpred = fu.createPred (updatedpred); GuardedAction ga = f.createGuardedAction(); ga.setPred(newpred); ga.setCircusAction(actionWithUpdatedExpression (decls, paramExprs, ((GuardedAction)auxaction).getCircusAction(), process, spec, mus, actionNames, c)); return ga; } else if (auxaction instanceof Action1) { Action1 action1 = (Action1)auxaction; action1.setCircusAction(actionWithUpdatedExpression (decls, paramExprs, action1.getCircusAction(), process, spec, mus, actionNames, c)); return action1; } else if (auxaction instanceof CallAction) { if (auxaction.getAnn(MuCallAnn.class) == null) { ZExprList callexprs = ((CallAction)auxaction).getZExprList(); CircusAction auxcontentaction = fu.createCircusAction (CallUtil.getContentOfCallAction((CallAction)auxaction, process, spec)); if (callexprs.size() > 0 && auxcontentaction != null) { ZDeclList auxcontentdecls = ((ParamAction)auxcontentaction).getZDeclList(); ZExprList newlist = f.createZExprList (); Vector <String> commondecls = DeclUtil.commonDecls (DeclUtil.listVarDeclAsDeclList (decls), auxcontentdecls); Vector <String> auxcontentdeclsvector = DeclUtil.declListAsVector(auxcontentdecls); int size = auxcontentdeclsvector.size(); int counter = 0; for (int i = 0; i < size; i++) { if (commondecls.contains(auxcontentdeclsvector.get(i))) { Expr expr = updateExpr (auxcontentdeclsvector.get(i), paramExprs.get(i), callexprs.get(i)); newlist.add(fu.createExpr(expr)); counter++; } else { newlist.add(fu.createExpr(callexprs.get(i))); } } CallAction ca = f.createCallAction (); ca.setName(((CallAction)auxaction).getZName()); ca.setExprList(newlist); return ca; } } return auxaction; } else if (auxaction instanceof BasicAction) { return auxaction; } else { ExceptionUtil.throwImplementationException("ExprUtil.actionWithUpdatedExpression", auxaction.getClass()); return auxaction; } } /***USADO EM CCUPDATER***/ public static void replaceVarName (String targetVar, String newName, Expr expr) { replaceVarName (targetVar, newName, expr, ""); } public static void replaceVarName (String targetVar, String newName, Expr expr, String processname) { Factory f = new Factory (); if (expr instanceof RefExpr) { String exprName = ((RefExpr)expr).getName().toString(); if (exprName.equals(targetVar)) { ((RefExpr)expr).setName(f.createZName(newName)); } } else if (expr instanceof ApplExpr) { Expr left = ((ApplExpr)expr).getLeftExpr(); Expr right = ((ApplExpr)expr).getRightExpr(); replaceVarName (targetVar, newName, left, processname); replaceVarName (targetVar, newName, right, processname); } else if (expr instanceof TupleExpr) { ZExprList exprlist = ((TupleExpr)expr).getZExprList(); int size = exprlist.size(); for (int i = 0; i < size; i++) { replaceVarName (targetVar, newName, exprlist.get(i), processname); } } else if (expr instanceof NumExpr) { //Nothing to do } else { ExceptionUtil.throwImplementationException("ExprUtil.replaceVarName", expr.getClass()); } } } |