ComplexCommUpdater.java 19.4 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
 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
package jcircus.complexcomms;

import java.util.Vector;

import jcircus.complementaryenvs.ActStackVarEnv;
import jcircus.complementaryenvs.StackVarEnv;
import jcircus.newutil.ProcessUtil;
import jcircus.newutil.PrinterUtil;
import jcircus.parallelism.RenamedVarAnn;
import net.sourceforge.czt.circus.ast.Action1;
import net.sourceforge.czt.circus.ast.Action2;
import net.sourceforge.czt.circus.ast.ActionIte;
import net.sourceforge.czt.circus.ast.ActionPara;
import net.sourceforge.czt.circus.ast.AssignmentCommand;
import net.sourceforge.czt.circus.ast.BasicProcess;
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.ExtChoiceAction;
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.IntChoiceAction;
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.ProcessPara;
import net.sourceforge.czt.circus.ast.SpecStmtCommand;
import net.sourceforge.czt.circus.ast.VarDeclCommand;
import net.sourceforge.czt.circus.util.Factory;
import net.sourceforge.czt.z.ast.ApplExpr;
import net.sourceforge.czt.z.ast.Expr;
import net.sourceforge.czt.z.ast.MemPred;
import net.sourceforge.czt.z.ast.NarrSect;
import net.sourceforge.czt.z.ast.Para;
import net.sourceforge.czt.z.ast.Pred;
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.TupleExpr;
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.ZSect;
import java.util.Set;

import javax.swing.JOptionPane;

/**@author: Samuel Barrocas
* Performs the renaming of the repeated variables of communication input fields
* **/
public class ComplexCommUpdater {

public void updateSpec (Spec spec) {
ZSect zSect;
ActStackVarEnv asve = new ActStackVarEnv ();
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);
System.out.print("");
if (para instanceof ProcessPara) {
ProcessPara processPara = (ProcessPara) para;
//ParallelismPrinter.printProcessPara(processPara);
updateProcessPara (processPara, asve);
System.out.println ("Depois da atualização");
PrinterUtil.printProcessPara (processPara, spec);
}
}
}
public void updateProcessPara (ProcessPara processPara, ActStackVarEnv asve) {
CircusProcess process = processPara.getCircusProcess();
String procname = processPara.getZName().toString();
updateCircusProcess (procname, process, asve);
}
public void updateCircusProcess (String procname, CircusProcess process, ActStackVarEnv asve) {
if (process instanceof BasicProcess) {
BasicProcess basic = (BasicProcess)process;
ZParaList paraList = basic.getZParaList();
int size = paraList.size();
for (int i = 0; i < size; i++) {
if (paraList.get(i) instanceof ActionPara) {
ActionPara actionPara = (ActionPara)paraList.get(i);
if (!actionPara.getName().toString().contains("Previous"))
updateCircusAction (procname, actionPara.getCircusAction(), actionPara.getName().toString(), asve, actionPara.getCircusAction());
}
}
}
else if (process instanceof ParamProcess) {
updateCircusProcess (procname, ((ParamProcess) process).getCircusBasicProcess(), asve);
}
else if (process instanceof Process1) {
updateCircusProcess (procname, ((Process1)process).getCircusProcess (), asve);
}
else if (process instanceof Process2) {
updateCircusProcess (procname, ((Process2)process).getLeftProcess (), asve);
updateCircusProcess (procname, ((Process2)process).getRightProcess (), asve);
}
else {
/*anything else?*/
}
}
private Vector <String> getCommunicationVariables (Communication comm) {
CircusFieldList cfl = comm.getCircusFieldList();
int sizecfl = cfl.size();
Vector <String> vec = new Vector <String> ();
for (int i = 0; i < sizecfl; i++) {
Field field = cfl.get(i);
if (field instanceof InputField) {
InputField inpfield = (InputField)field;
String varname = inpfield.getVariableName().toString();
vec.addElement(varname);
}
}
return vec;
}
private void renameDotFieldVariables (Communication comm, StackVarEnv sve, String procName) {
/**
* Tenho dúvidas se esse troço vai funcionar...
* só esperando completar a implementação para checar
* */
Factory f = new Factory ();
CircusFieldList cfl = comm.getCircusFieldList();
int sizecfl = cfl.size();
for (int i = 0; i < sizecfl; i++) {
Field field = cfl.get(i);
if (field instanceof DotField) {
Expr expr = ((DotField)field).getExpr();
f = new Factory ();
updateVariablesAtExpr (expr, sve, procName);
/*if (expr instanceof RefExpr) {
String varname = ((RefExpr)expr).getName().toString();
((RefExpr)expr).setName(f.createZName(varname + sve.get(varname)));
}*/
}
}
}
private void renameCommunicationVariables (Communication comm, StackVarEnv sve) {
/**
* Tenho dúvidas se esse troço vai funcionar...
* só esperando completar a implementação para checar
* */
Factory f = new Factory ();
CircusFieldList cfl = comm.getCircusFieldList();
int sizecfl = cfl.size();
for (int i = 0; i < sizecfl; i++) {
Field field = cfl.get(i);
if (field instanceof InputField) {
InputField inpfield = (InputField)field;
String varname = inpfield.getVariableName().toString();
if (inpfield.getAnn(RenamedVarAnn.class) != null
&& isNumeral (varname.substring(varname.length() - 1))
) {
varname = varname.substring(0, varname.length() - 1);
}
if (sve.containsKey(varname)) {
if (!(sve.get(varname).equals(""))) {
inpfield.setVariableName(f.createZName(varname + sve.get(varname)));
}
}
}
else if (field instanceof DotField) {
Expr expr = ((DotField)field).getExpr();
f = new Factory ();
if (expr instanceof RefExpr) {
String varname = ((RefExpr)expr).getName().toString();
((RefExpr)expr).setName(f.createZName(varname + sve.get(varname)));
}
}
}
}
private void renameAssignmentCommand (AssignmentCommand ac, StackVarEnv sve) { //TESTADO
ZNameList leftVars = (ZNameList) ac.getAssignmentPairs().getLHS();
ZExprList rightExprs = (ZExprList) ac.getAssignmentPairs().getRHS();
Set <String> keys = sve.keySet();
int nkeys = sve.keySet().size();
for (int i = 0; i < rightExprs.size(); i++) {
for (int j = 0; j < keys.size(); j++) {
String target = (String) keys.toArray() [j];
replaceVarName (target, target + sve.get(target), rightExprs.get(i));
}
}
}
private void replaceVarName (String targetVar, String newName, Expr expr) {
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);
replaceVarName (targetVar, newName, right);
}
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));
}
}
}
private boolean isNumeral (String str) {
return str.equals("0") || str.equals("1") || str.equals("2") || str.equals("3") || str.equals("4") ||
str.equals("5") || str.equals("6") || str.equals("7") || str.equals("8") || str.equals("9");
}
public void updateVariablesAtExpr (Expr expr, StackVarEnv sve, String procName) {
if (expr instanceof RefExpr) {
String var = ((RefExpr)expr).getZName().toString();
String subvar = new String (var);
if (expr.getAnn(RenamedVarAnn.class) != null && var.length() > 1 && isNumeral (var.substring(var.length() - 1))) {
subvar = subvar.substring(0, var.length() - 1);
}
replaceVarName (var, subvar + sve.get(subvar), expr);
if (expr.getAnn(RenamedVarAnn.class) == null) {
expr.getAnns().add(new RenamedVarAnn ());
}
}
else if (expr instanceof SetExpr) {
ZExprList list = ((SetExpr)expr).getZExprList();
for (int i = 0; i < list.size(); i++) {
updateExpr (list.get(i), sve, procName);
}
}
else if (expr instanceof TupleExpr) {
ZExprList list = ((TupleExpr)expr).getZExprList();
for (int i = 0; i < list.size(); i++) {
updateExpr (list.get(i), sve, procName);
}
}
else {
//TODO DO FOR OTHER TYPES OF EXPRESSIONS
}
}
public void updateExpr (Expr expr, StackVarEnv sve, String procName) {
if (expr instanceof RefExpr) {
String var = ((RefExpr)expr).getZName().toString();
String subvar = new String (var);
if (expr.getAnn(RenamedVarAnn.class) != null && var.length() > 1 && isNumeral (var.substring(var.length() - 1))) {
subvar = subvar.substring(0, var.length() - 1);
}
String finstr = subvar + sve.get(subvar);
replaceVarName (var, subvar + sve.get(subvar), expr);
if (expr.getAnn(RenamedVarAnn.class) == null) {
expr.getAnns().add(new RenamedVarAnn ());
}
}
else if (expr instanceof SetExpr) {
ZExprList list = ((SetExpr)expr).getZExprList();
for (int i = 0; i < list.size(); i++) {
updateExpr (list.get(i), sve, procName);
}
}
else if (expr instanceof TupleExpr) {
ZExprList list = ((TupleExpr)expr).getZExprList();
for (int i = 0; i < list.size(); i++) {
updateExpr (list.get(i), sve, procName);
}
}
else {
//TODO DO FOR OTHER TYPES OF EXPRESSIONS
}
}
private boolean containsString (String str, Expr expr) {
if (expr instanceof RefExpr) {
//return ((RefExpr)expr).toString().equals(str);
return ((RefExpr)expr).toString().contains(str);
}
else if (expr instanceof SetExpr) {
boolean aux = false;
ZExprList list = ((SetExpr)expr).getZExprList();
for (int i = 0; i < list.size(); i++) {
aux = aux || containsString (str, list.get(i));
}
return aux;
}
else if (expr instanceof TupleExpr) {
ZExprList list = ((TupleExpr)expr).getZExprList();
boolean aux = false;
for (int i = 0; i < list.size(); i++) {
aux = aux || containsString (str, list.get(i));
}
return aux;
}
else {
return false;
//TODO DO FOR OTHER TYPES OF EXPRESSIONS
}
}
public void updateVariablesAtPred (Pred pred, StackVarEnv sve, String procName) {
if (pred instanceof MemPred) {
Expr left = ((MemPred)pred).getLeftExpr();
Expr right = ((MemPred)pred).getRightExpr();
updateExpr (left, sve, procName);
updateExpr (right, sve, procName);
}
else /*if (pred instanceof OtherPreds)*/ {
//TODO IMPLEMENT FOR OTHER PRED TYPES
}
}
public void updatePred (Pred pred, StackVarEnv sve, String procName) {
if (pred instanceof MemPred) {
Expr left = ((MemPred)pred).getLeftExpr();
Expr right = ((MemPred)pred).getRightExpr();
updateExpr (left, sve, procName);
updateExpr (right, sve, procName);
}
/*else //if (pred instanceof OtherPreds) {
//TODO IMPLEMENT FOR OTHER PRED TYPES
}*/
}
public void updateCircusAction (String procname, CircusAction action, String actionName, ActStackVarEnv asve, CircusAction motherAction) {
updateCircusAction (procname, action, actionName, asve, false, motherAction);
}
public void updateVariableNames (String procname, CircusAction action, String actionName, ActStackVarEnv asve) {
/**
* Atualiza os nomes de variáveis...
* -> Este método só não atualiza quando os nomes estiverem em um InputField
* -> Este método não altera o conteúdo de ActStackVarEnv, apenas atualiza os valores das variáveis
* */
if (action instanceof CircusCommand) {
if (action instanceof AssignmentCommand) {
renameAssignmentCommand ((AssignmentCommand)action, asve.get(actionName));
}
else if (action instanceof IfGuardedCommand) {
CircusActionList cal = ((IfGuardedCommand) action).getGuardedActionList();
int size = cal.size();
for (int i = 0; i < size; i++) {
GuardedAction ga = (GuardedAction) cal.get(i);
Pred pred = ga.getPred();
updateVariablesAtPred (pred, asve.get(actionName), procname);
updateVariableNames (procname, ga, actionName, asve);
}
}
else if (action instanceof VarDeclCommand) { //TODO NAO TESTADO
VarDeclCommand vdc = ((VarDeclCommand)action);
updateVariableNames (procname, vdc.getCircusAction(), actionName, asve);
}
}
else if (action instanceof PrefixingAction) {
PrefixingAction prefaction = (PrefixingAction)action;
Communication comm = prefaction.getCommunication();
CircusFieldList cfl = comm.getCircusFieldList();

StackVarEnv sve = asve.get(actionName);
Vector <String> vars = getCommunicationVariables (comm);
int size = vars.size();
for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
/*if (field.getAnn(RenamedVarAnn.class) == null && !choice)
sve.push(vars.elementAt(i));*/
}
renameDotFieldVariables (comm, sve, procname);
for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
if (field.getAnn(RenamedVarAnn.class) == null)
field.getAnns().add(new RenamedVarAnn ());
}
//asve.update(actionName, sve);
updateVariableNames (procname, ((PrefixingAction)action).getCircusAction(), actionName, asve);

/*for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
if (field.getAnn(RenamedVarAnn.class) != null) {
field.getAnns().remove(RenamedVarAnn.class);
}
}*/
}
else if (action instanceof Action2) {
CircusAction leftaction = ((Action2)action).getLeftAction();
CircusAction rightaction = ((Action2)action).getRightAction();
updateVariableNames (procname, leftaction, actionName, asve);
updateVariableNames (procname, rightaction, actionName, asve);
}
else if (action instanceof Action1 && !(action instanceof PrefixingAction)) {
updateVariableNames (procname, ((Action1)action).getCircusAction(), actionName, asve);
}
/*else if (action instanceof CallAction) {
}*/
}
public void updateCircusAction (String procname, CircusAction action, String actionName, ActStackVarEnv asve, boolean choice, CircusAction motherAction) {
/**Aqui atualizaremos as variáveis dos campos de entrada das comunicações
* e dos comandos de atribuição de acordo com
* */
if (action instanceof CircusCommand) {
/**Todas as subinterfaces de CircusCommand são estas abaixo*/
if (action instanceof AssignmentCommand) {
renameAssignmentCommand ((AssignmentCommand)action, asve.get(actionName));
}
else if (action instanceof IfGuardedCommand) {
CircusActionList cal = ((IfGuardedCommand) action).getGuardedActionList();
int size = cal.size();
for (int i = 0; i < size; i++) {
GuardedAction ga = (GuardedAction) cal.get(i);
Pred pred = ga.getPred();
updatePred (pred, asve.get(actionName), procname);
updateCircusAction (procname, ga, actionName, asve, choice, motherAction);
}
}
else if (action instanceof VarDeclCommand) { //TODO NAO TESTADO
VarDeclCommand vdc = ((VarDeclCommand)action);
updateCircusAction (procname, vdc.getCircusAction(), actionName, asve, choice, motherAction);
}
else if (action instanceof SpecStmtCommand) {
/*something to do here?*/
//TODO
}
}
else if (action instanceof PrefixingAction) {
PrefixingAction prefaction = (PrefixingAction)action;
Communication comm = prefaction.getCommunication();
CircusFieldList cfl = comm.getCircusFieldList();

StackVarEnv sve = asve.get(actionName);
Vector <String> vars = getCommunicationVariables (comm);
int size = vars.size();
int counter = 0;
for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
if (//field.getAnn(RenamedVarAnn.class) != null &&
field instanceof InputField
&& !choice
) {
if (vars.elementAt(counter).contains("ident")) {
System.out.print ("");
}
sve.push(vars.elementAt(counter));
counter++;
}
}
renameCommunicationVariables (comm, sve);
for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
if (field.getAnn(RenamedVarAnn.class) == null
&& !choice
)
field.getAnns().add(new RenamedVarAnn ());
}
asve.update(actionName, sve);
if (procname.equals("Cassino")) {
System.out.print ("");
}
updateVariableNames (procname, motherAction, actionName, asve);
updateCircusAction (procname, ((PrefixingAction)action).getCircusAction(), actionName, asve, choice, motherAction);

counter = 0;
/*for (int i = 0; i < size; i++) {
Field field = cfl.get(i);
if (//field.getAnn(RenamedVarAnn.class) != null &&
field instanceof InputField
//&& !choice
) {
sve.pop(vars.elementAt(counter));
field.getAnns().remove(RenamedVarAnn.class);
counter++;
}
}*/
//asve.update(actionName, sve);
//updateVariableNames (procname, ((PrefixingAction)action).getCircusAction(), actionName, asve);
//updateCircusAction (procname, ((PrefixingAction)action).getCircusAction(), actionName, asve, choice);
System.out.print ("");
/**
* Temos que pensar melhor isso aqui...
* vamos ver como fica a AST quando a expressão dada por Marcel é compilada...
* **/
}
else if (action instanceof Action1) {
updateCircusAction (procname, ((Action1)action).getCircusAction(), actionName, asve, choice, motherAction);
}
else if (action instanceof Action2) {
if (action instanceof ExtChoiceAction) {
updateCircusAction (procname, ((ExtChoiceAction)action).getLeftAction(), actionName, asve, true, motherAction);
updateCircusAction (procname, ((ExtChoiceAction)action).getRightAction(), actionName, asve, true, motherAction);
}
else if (action instanceof IntChoiceAction) {
updateCircusAction (procname, ((IntChoiceAction)action).getLeftAction(), actionName, asve, true, motherAction);
updateCircusAction (procname, ((IntChoiceAction)action).getRightAction(), actionName, asve, true, motherAction);
}
else {
updateCircusAction (procname, ((Action2)action).getLeftAction(), actionName, asve, choice, motherAction);
updateCircusAction (procname, ((Action2)action).getRightAction(), actionName, asve, choice, motherAction);
}
System.out.print ("");
}
/*else if (action instanceof BasicAction) {
}*/
else {
/*anything to do?*/
}
}
}