CCCode.java 31.8 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
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
package jcircus.complexcomms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.jcsp.lang.Guard;

import net.sourceforge.czt.util.Visitor;
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.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.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.circus.ast.Action1;
import net.sourceforge.czt.circus.ast.Action2;
import net.sourceforge.czt.circus.ast.CircusAction;
import net.sourceforge.czt.circus.ast.Communication;
import net.sourceforge.czt.circus.ast.CircusFieldList;
import net.sourceforge.czt.circus.ast.DotField;
import net.sourceforge.czt.circus.ast.GuardedAction;
import net.sourceforge.czt.circus.ast.HideAction;
import net.sourceforge.czt.circus.ast.InputField;
import net.sourceforge.czt.circus.ast.ExtChoiceAction;
import net.sourceforge.czt.circus.ast.IntChoiceAction;
import net.sourceforge.czt.circus.ast.MuAction;
import net.sourceforge.czt.circus.ast.ParAction;
import net.sourceforge.czt.circus.ast.PrefixingAction;
import net.sourceforge.czt.circus.util.Factory;

import java.math.BigInteger;

import jcircus.newfrontendmethod.FrontEndAnn;
import jcircus.newutil.DeclUtil;
import jcircus.newutil.ExceptionUtil;
import jcircus.newutil.TypeUtil;
import jcircus.util.Constants;
import jcircus.util.Util;
import jcircus.visitor.TranslatorVisitor;

//public static String guardsCode (String chanName, Communication communication, int min, int max, Visitor c)
public class CCCode {
public static String inputDecoration (Communication communication) {
String str = "";
CircusFieldList cfl = communication.getCircusFieldList();
for (int i = 0; i < cfl.size(); i++) {
if (cfl.get(i) instanceof InputField) {
str = str + "true";
}
else {
str = str + "false";
}
if (i != cfl.size() - 1) {
str = str + " , ";
}
}
return str;
}
public static String commValues (Communication c, Visitor v) {
String str = "";
CircusFieldList cfl = c.getCircusFieldList();
int size = cfl.size();
for (int i = 0; i < size; i++) {
if (cfl.get(i) instanceof InputField) {
str = str + "-1";
}
else {
str = str + "(" + ((DotField)cfl.get(i)).getExpr().accept(v) + ").getValue()";
}
if (i != size - 1) {
str = str + " , ";
}
}
return str;
}
public static String varNames (Communication c, Visitor v) {
String str = "";
CircusFieldList cfl = c.getCircusFieldList();
int size = cfl.size();
for (int i = 0; i < size; i++) {
if (cfl.get(i) instanceof InputField) {
str = str + "\"" + ((InputField)cfl.get(i)).getVariableZName().toString() + "\"";
}
else {
str = str + "\"" + "none" + "\"";
}
if (i != size - 1) {
str = str + " , ";
}
}
return str;
}
public static String [] varNamesAsArray (Communication c, Visitor v) {
CircusFieldList cfl = c.getCircusFieldList();
int size = cfl.size();
String [] str = new String [size];
for (int i = 0; i < size; i++) {
str [i] = "";
if (cfl.get(i) instanceof InputField) {
str [i] = ((InputField)cfl.get(i)).getVariableZName().toString();
}
else {
str [i] = "none";
}
}
return str;
}
/*private static String declarationsCode (int indent, String [] varNames, int label, String nameComplement, Vector <String> putNames, String typename) {
String str = "";
for (int i = 0; i < varNames.length; i++) {
if (!varNames[i].equals("none") && !putNames.contains(varNames [i])) {
str = str + ident (indent) + "CircusInteger " + varNames [i] + " = new CircusInteger (0);" + "\n";
putNames.addElement(varNames [i]);
System.out.print ("");
}
}
return str;
}*/
private static String [] getTypesOfFields (Expr expr) {
Factory f = new Factory ();
if (expr instanceof RefExpr || expr instanceof SetExpr) {
String typename = ((RefExpr)expr).getZName().toString();
String [] types = new String [1] ;
types [0] = typename;
return types;
}
else if (expr instanceof ProdExpr) {
ZExprList exprs = ((ProdExpr)expr).getZExprList();
int size = exprs.size();
String [] types = new String [size];
for (int i = 0; i < size; i++) {
types [i] = ((RefExpr)exprs.get(i)).getZName().toString();
}
return types;
}
else {
ExceptionUtil.throwImplementationException("getTypesOfFields", expr.getClass());
return new String [0];
}
}
public static String assignmentsAndDeclarationCode (int indent, String [] varNames, int label, String nameComplement, Expr expr) {
//Ex:
//int x = vfe.get("x");
//int y = vfe.get("y");
//
String [] typenames = getTypesOfFields (expr);
String str = "";
for (int i = 0; i < varNames.length; i++) {
if (!varNames[i].equals("none") /*&& !putNames.contains(varNames [i])*/) {
//str = str + ident (indent) + "final CircusInteger " + varNames [i] + " = new CircusInteger ((Integer)((HashMap) ccMaps_" + nameComplement + label + ".mapFromSelectToVarEnv.get (select_" + nameComplement + label + ")).get (\"" + varNames [i] + "\"));\n";
String auxtypename = typenames [i].contains("â„•")? "CircusInteger" : typenames [i];
str = str + ident (indent) + "final " + auxtypename + " " + varNames [i] + " = new " + auxtypename + " ((Integer)((HashMap) ccMaps_" + nameComplement + label + ".mapFromSelectToVarEnv.get (select_" + nameComplement + label + ")).get (\"" + varNames [i] + "\"));\n";
str = str + ident (indent) + "System.out.println (\"" + varNames[i] + " = \" + " + varNames [i] + ");\n";
}
}
return str;
}
public static String assignmentsCode (int indent, String [] varNames, int label, String nameComplement) {
//Ex:
//int x = vfe.get("x");
//int y = vfe.get("y");
//
String str = "";
for (int i = 0; i < varNames.length; i++) {
if (!varNames[i].equals("none")) {
str = str + ident (indent) + "/*CircusInteger*/ " + varNames [i] + " = new CircusInteger ((Integer)((HashMap) ccMaps_" + nameComplement + label + ".mapFromSelectToVarEnv.get (select_" + nameComplement + label + ")).get (\"" + varNames [i] + "\"));\n";
str = str + ident (indent) + "System.out.println (\"" + varNames[i] + " = \" + " + varNames [i] + ");\n";
}
}
return str;
}
public static String ident (int n) {
String str = "";
for (int i = 0; i < n; i++) {
str = str + " ";
}
return str;
}
public static boolean isComplexCommCase (ExtChoiceAction eca) {
PrefixingAction leftPrefixingAction, rightPrefixingAction;
boolean b = false;
if (eca.getLeftAction() instanceof PrefixingAction && eca.getRightAction() instanceof PrefixingAction) {
leftPrefixingAction = (PrefixingAction)eca.getLeftAction();
rightPrefixingAction = (PrefixingAction)eca.getRightAction();
Communication fstComm = leftPrefixingAction.getCommunication();
Communication sndComm = rightPrefixingAction.getCommunication();
if (fstComm.getCircusFieldList().size() > 1 && fstComm.getCircusFieldList().size() > 1) {
b = true;
}
}
return b;
}
public static boolean isComplexCommCase (IntChoiceAction eca) {
PrefixingAction leftPrefixingAction, rightPrefixingAction;
boolean b = false;
if (eca.getLeftAction() instanceof PrefixingAction && eca.getRightAction() instanceof PrefixingAction) {
leftPrefixingAction = (PrefixingAction)eca.getLeftAction();
rightPrefixingAction = (PrefixingAction)eca.getRightAction();
Communication fstComm = leftPrefixingAction.getCommunication();
Communication sndComm = rightPrefixingAction.getCommunication();
if (fstComm.getCircusFieldList().size() > 1 && fstComm.getCircusFieldList().size() > 1) {
b = true;
}
}
return b;
}
public static boolean isComplexCommCase (ParAction eca) {
PrefixingAction leftPrefixingAction, rightPrefixingAction;
boolean b = false;
if (eca.getLeftAction() instanceof PrefixingAction && eca.getRightAction() instanceof PrefixingAction) {
leftPrefixingAction = (PrefixingAction)eca.getLeftAction();
rightPrefixingAction = (PrefixingAction)eca.getRightAction();
Communication fstComm = leftPrefixingAction.getCommunication();
Communication sndComm = rightPrefixingAction.getCommunication();
if (fstComm.getCircusFieldList().size() > 1 && fstComm.getCircusFieldList().size() > 1) {
b = true;
}
}
return b;
}
public static String complexCommsSingleCommunicationCode (
Communication comm,
CircusAction prefixedAction,
Visitor c,
int min, int max,
Channel2TypeEnvironment env,
String paper, String src, String projectDir, String specName,
String procName,
int counter,
Vector <CCMapsGenerator> ccMapsGens,
HashMap <String, String> vars
) {
HashMap <String, String> singlecommvars = new HashMap <String, String> (vars);
String channelName = comm.getChannelExpr().getName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(channelName));
String code = "";
CCMapsGenerator ccmg = new CCMapsGenerator (paper, src, projectDir, specName, procName, counter, "comm_");
ccmg.concatCode (
" public HashMap mapFromSelectToVarEnv = new HashMap ();\n" +
" public HashMap mapLeftOrRight = new HashMap ();\n" +
" public CCMaps_comm_" + procName + "_" + counter + " () {\n" +
" makeEnvChannels ();\n" +
" makeOtherEnvs ();\n" +
" }\n" +
CCUtil.makeEnvChannelsCode (channelName, env.get(channelName)) +
" public void makeOtherEnvs () {\n" +
" " + CCUtil.envsCode (channelName, comm, mam.min, mam.max, c, 0, 0, true, env.get(channelName)).str +
" }\n" +
CCUtil.indexesbutfirstcode() +
CCUtil.makeSyncOnOtherEndsButFirstCode(channelName, comm, mam.min, mam.max, c, env.get(channelName), counter, env)
);
ccMapsGens.add(ccmg);
code = code +
"class SingleCommBranch" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (singlecommvars) +
" public SingleCommBranch" + counter + "(" + paramChoiceVariables (singlecommvars) + ") {\n" +
assignChoiceVariables (singlecommvars) +
" }\n" +
" public void run () {\n" +
" int select_comm_" + counter + ";\n" +
" abs_" + comm.getChannelExpr().getName().toString() + " = new " + "Abs_" + comm.getChannelExpr().getName().toString() + "();\n" +
" CCMaps_comm_" + procName + "_" + counter + " ccMaps_comm_" + counter + " = new CCMaps_comm_" + procName + "_" + counter + " ();\n" +
" select_comm_" + counter + " = (new Alternative (";
code = code + "\n" +
" new Guard []\n " +
" {\n" +
" \n" +
//" " + "\n" +
CCUtil.guardsCode(comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, env.get(comm.getChannelExpr().getName().toString()), counter) +
" }" +
" )).select();\n" +
" GPSE.resetEnds (" + channelName + ", " + "new int [] {" + TypeUtil.getTypeSizes(channelName, env) + "});\n" +
assignmentsAndDeclarationCode (3, varNamesAsArray (comm, c), counter, "comm_", env.getExpr(channelName));
code = code + prefixedAction.accept(c);
code = code +
" }\n" +
"}\n";
code = code +
"SingleCommBranch" + counter +
" singlecommbranch" + counter + " = new SingleCommBranch" + counter +
" (" + paramCallExtChoiceVariables (singlecommvars) + ");\n";

code = code +
"class FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (singlecommvars) +
" public FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " (" + paramChoiceVariables (singlecommvars) + ") {\n" +
assignChoiceVariables (singlecommvars) +
" }\n" +
" public void run () {\n";
code = code +
" CCMaps_comm_" + procName + "_" + counter +
".syncOnOtherEndsButFirst_" +
comm.getChannelExpr().getName().toString() +
"(" +
comm.getChannelExpr().getName().toString() + ", " +
"abs_" + comm.getChannelExpr().getName().toString() +
CCUtil.complementarCommParams (false, comm, env) + ", " +
FrontEndAnn.getFrontEndAnn(comm).toString() +
");\n" +
" }\n" +
"};\n";
code = code +
"FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " " +
"frontendsbutfirstcommbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " = " +
" new FrontEndsButFirstCommBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter +
" (" + paramCallExtChoiceVariables (singlecommvars) + ");\n";
code = code + "(new Parallel (new CSProcess [] {singlecommbranch" + counter + ", " + "frontendsbutfirstcommbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + "})).run();\n";
code = code + updateVariableValuesCode (singlecommvars, "singlecommbranch" + counter);
System.out.println ("*****************************\nCurrent time milis 2: " + System.currentTimeMillis());
return code;
}
public static String complexCommsPrefixingActionCode (
PrefixingAction pa,
Visitor c,
int min, int max,
Channel2TypeEnvironment env,
String paper,
String src,
String projectDir,
String specName,
String procName,
int counter,
Vector <CCMapsGenerator> ccMapsGens,
HashMap <String, String> vars
)
{
Communication comm = pa.getCommunication();
String channelName = comm.getChannelExpr().getZName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(comm.getChannelExpr().getName().toString()));
String code = "";
HashMap <String, String> prefactvars = new HashMap <String, String> (vars);
CCMapsGenerator ccmg = new CCMapsGenerator (paper, src, projectDir, specName, procName, counter, "");
ccmg.concatCode (
" public HashMap mapFromSelectToVarEnv = new HashMap ();\n" +
" public HashMap mapLeftOrRight = new HashMap ();\n" +
" public CCMaps_" + procName + "_" + counter + " () {\n" +
" makeEnvChannels ();\n" +
" makeOtherEnvs ();\n" +
" }\n" +
CCUtil.makeEnvChannelsCode (comm.getChannelExpr().getName().toString(), env.get(comm.getChannelExpr().getName().toString())) +
" public void makeOtherEnvs () {\n" +
" " + CCUtil.envsCode (comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, 0, 0, true, env.get(comm.getChannelExpr().getName().toString())).str +
" }\n" +
CCUtil.indexesbutfirstcode() +
CCUtil.makeSyncOnOtherEndsButFirstCode(comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, env.get(comm.getChannelExpr().getName().toString()), counter, env)
);
ccMapsGens.add(ccmg);
code = code +
"class PrefActBranch" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (vars) +
" public PrefActBranch" + counter + "(" + paramChoiceVariables (vars) + ") {\n" +
assignChoiceVariables (vars) +
" }\n" +
" public void run () {\n" +
" int select_" + counter + ";\n" +
" abs_" + comm.getChannelExpr().getName().toString() + " = new " + "Abs_" + comm.getChannelExpr().getName().toString() + "();\n" +
" CCMaps_" + procName + "_" + counter + " ccMaps_" + counter + " = new CCMaps_" + procName + "_" + counter + " ();\n" +
" select_" + counter + " = (new Alternative (";
code = code + "\n" +
" new Guard []\n " +
" {\n" +
" \n" +
CCUtil.guardsCode(comm.getChannelExpr().getName().toString(), comm, mam.min, mam.max, c, env.get(comm.getChannelExpr().getName().toString()), counter) +
" }" +
" )).select();\n" +
" GPSE.resetEnds (" + channelName + ", " + "new int [] {" + TypeUtil.getTypeSizes(channelName, env) + "});\n" +
assignmentsAndDeclarationCode (3, varNamesAsArray (comm, c), counter, "", env.getExpr(channelName));
code = code + pa.getCircusAction().accept(c);
code = code +
" }\n" +
"}\n";
code = code +
"PrefActBranch" + counter +
" prefactbranch" + counter + " = new PrefActBranch" + counter +
" (" + paramCallExtChoiceVariables (prefactvars) + ");\n";

code = code +
"class FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (prefactvars) +
" public FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " (" + paramChoiceVariables (prefactvars) + ") {\n" +
assignChoiceVariables (prefactvars) +
" }\n" +
" public void run () {\n";
code = code +
" CCMaps_" + procName + "_" + counter +
".syncOnOtherEndsButFirst_" +
comm.getChannelExpr().getName().toString() +
"(" +
comm.getChannelExpr().getName().toString() + ", " +
"abs_" + comm.getChannelExpr().getName().toString() +
CCUtil.complementarCommParams (false, comm, env) + ", " +
FrontEndAnn.getFrontEndAnn(comm).toString() +
");\n" +
" }\n" +
"};\n";
code = code +
"FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " " +
"frontendsbutfirstbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + " = " +
" new FrontEndsButFirstBranch_" + comm.getChannelExpr().getName().toString() + "_" + counter +
" (" + paramCallExtChoiceVariables (prefactvars) + ");\n";
code = code + "(new Parallel (new CSProcess [] {prefactbranch" + counter + ", " + "frontendsbutfirstbranch_" + comm.getChannelExpr().getName().toString() + "_" + counter + "})).run()";

System.out.println ("*****************************\nCurrent time milis 2: " + System.currentTimeMillis());
return code;
}

public static String [] getChannelNames (Communication [] c) { //TODO NAO TESTADO, MAS PROVAVELMENTE FUNCIONA, POIS EH TRIVIAL
String [] chanNames = new String [c.length];
for (int i = 0; i < chanNames.length; i++) {
chanNames [i] = c [i].getChannelExpr().getName().toString();
}
return chanNames;
}
public static List <Communication> getCommunications (CircusAction action) { //TODO NAO TESTADO
List <Communication> comms = new ArrayList <Communication> ();
if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
comms.addAll(getCommunications (left));
comms.addAll(getCommunications (right));
}
else if (action instanceof Action1) {
if (action instanceof GuardedAction || action instanceof HideAction || action instanceof MuAction /*|| TODO outras acoes unarias*/) {
comms.addAll(getCommunications (((Action1)action).getCircusAction()));
}
else if (action instanceof PrefixingAction) {
comms.add(((PrefixingAction)action).getCommunication());
}
}
return comms;
}
public static List <CircusAction> getPrefixedActions (CircusAction action) { //TODO NAO TESTADO
List <CircusAction> comms = new ArrayList <CircusAction> ();
if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
comms.addAll(getPrefixedActions (left));
comms.addAll(getPrefixedActions (right));
}
else if (action instanceof Action1) {
if (action instanceof GuardedAction || action instanceof HideAction || action instanceof MuAction /*|| TODO outras a��es un�rias*/) {
comms.addAll(getPrefixedActions (((Action1)action).getCircusAction()));
}
else if (action instanceof PrefixingAction) {
comms.add(((PrefixingAction)action).getCircusAction());
}
}
return comms;
}
public static List <CircusAction> getBranchedActions (CircusAction action) { //TODO NAO TESTADO
List <CircusAction> comms = new ArrayList <CircusAction> ();
if (action instanceof Action2) {
CircusAction left = ((Action2)action).getLeftAction();
CircusAction right = ((Action2)action).getRightAction();
comms.addAll(getBranchedActions (left));
comms.addAll(getBranchedActions (right));
}
else if (action instanceof Action1) {
if (/*action instanceof GuardedAction ||*/ action instanceof HideAction || action instanceof MuAction /*|| TODO outras a��es un�rias*/) {
comms.addAll(getBranchedActions (((Action1)action).getCircusAction()));
}
else if (action instanceof PrefixingAction) {
comms.add(((PrefixingAction)action));
}
else if (action instanceof GuardedAction) {
comms.add(((GuardedAction)action));
}
}
return comms;
}
public static List <Pred> getPredicates (List <CircusAction> actions) {
List <Pred> predicates = new ArrayList <Pred> ();
Factory f = new Factory ();
for (int i = 0; i < actions.size(); i++) {
if (!(actions.get(i) instanceof GuardedAction)) {
predicates.add(f.createTruePred());
}
else {
GuardedAction ga = (GuardedAction)actions.get(i);
predicates.add(ga.getPred());
}
}
return predicates;
}
public static boolean isTranslatable (ExtChoiceAction eca) {
List <CircusAction> actions = getBranchedActions(eca);
List <Pred> predicates = getPredicates (actions);
CircusAction [] actionsArray = actionsToArray (actions);
boolean aux = true;
for (int i = 0; i < actionsArray.length; i++) {
if (!(actionsArray [i] instanceof PrefixingAction || actionsArray [i] instanceof GuardedAction))
aux = false;
}
return aux;
}
public static CircusAction [] actionsToArray (List <CircusAction> actions) {
CircusAction [] actionsArray = new CircusAction [actions.size()];
for (int i = 0; i < actions.size(); i++) {
actionsArray [i] = actions.get(i);
}
return actionsArray;
}
public static Communication [] commsToArray (List <Communication> comms) {
Communication [] commsArray = new Communication [comms.size()];
for (int i = 0; i < comms.size(); i++) {
commsArray [i] = comms.get(i);
}
return commsArray;
}
//countGuards (String chanName, Communication communication, int [] min, int [] max, Visitor c, Vector <Vector <BigInteger>> valuesSet, int label)
private static String booleansArrayCode (List <Pred> predicates, List <Communication> comms, Visitor v, Channel2TypeEnvironment env/*Vector <Vector <BigInteger>> valuesSet*/, int label) {
String strpredicates = "";
for (int i = 0; i < predicates.size(); i++) {
String chanName = comms.get(i).getChannelExpr().getZName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(chanName));
int numberOfGuards = CCUtil.countGuards
(chanName, comms.get(i), mam.min, mam.max, v, env.get(chanName), label);
for (int j = 0; j < numberOfGuards; j++) {
strpredicates += predicates.get(i).accept (v);
if (j < numberOfGuards - 1)
strpredicates += ", ";
}
if (i < predicates.size() - 1)
strpredicates += ", ";
}
String strfinal = "final boolean [] b_" + label + " = new boolean [] {" + strpredicates + "};";
return strfinal;
}
private Vector <String> getChannelNames (List <Communication> comms) {
Vector <String> names = new Vector <String> ();
int size = comms.size();
for (int i = 0; i < size; i++) {
names.addElement(comms.get(i).getChannelExpr().getZName().toString());
}
return names;
}
private static String declareExtChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + " " + var2type.get((String)keys [i]) + " " + ((String)keys [i]) + ";\n";
}
return str;
}
private static String assignChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + " this." + ((String)keys [i]) + " = " + ((String)keys [i]) + ";\n";
}
return str;
}
private static String paramChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + var2type.get((String)keys [i]) + " " + ((String)keys [i]);
if (i < keys.length - 1)
str = str + ", ";
}
return str;
}
private static String paramCallExtChoiceVariables (HashMap <String, String> var2type) {
Object [] keys = var2type.keySet().toArray();
String str = "";
for (int i = 0; i < keys.length; i++) {
str = str + ((String)keys [i]);
if (i < keys.length - 1)
str = str + ", ";
}
return str;
}
private static String updateVariableValuesCode (HashMap <String, String> vars, String classname) {
int size = vars.keySet().toArray().length;
String strreturn = "";
for (int i = 0; i < size; i++) {
String varname = (String) vars.keySet().toArray() [i];
strreturn = varname + " = " + classname + "." + varname + ";\n";
}
return strreturn;
}
public static String complexCommsExtChoiceCode (
ExtChoiceAction eca,
TranslatorVisitor c,
int min,
int max,
Channel2TypeEnvironment env,
String paper,
String src,
String projectDir,
String specName,
String procName,
int counter,
Vector <CCMapsGenerator> ccMapsGens,
HashMap <String, String> vars
) { //OK
HashMap <String, String> extchoicevars = new HashMap <String, String> (vars);
PrefixingAction leftPrefixingAction, rightPrefixingAction;
String code = "";
List <CircusAction> actionsWithoutPrefixing = getPrefixedActions(eca);
List <CircusAction> branchedActions = getBranchedActions (eca);
List <Pred> predicates = getPredicates (branchedActions);
CircusAction [] actionsArray = actionsToArray (actionsWithoutPrefixing);
List <Communication> comms = getCommunications (eca);
Communication [] commsArray = commsToArray (comms);
String [] channelNames = getChannelNames (commsArray);
CCMapsGenerator ccmg = new CCMapsGenerator (paper, src, projectDir, specName, procName, counter, "");
String concatCode = "";
concatCode =
" public HashMap mapFromSelectToVarEnv = new HashMap ();\n" +
" public HashMap mapLeftOrRight = new HashMap ();\n" +
" public CCMaps_" + procName + "_" + counter + " () {\n";
for (int i = 0; i < commsArray.length; i++) {
concatCode += " makeEnvChannels" + i + " ();\n";
concatCode += " makeOtherEnvs" + i + " ();\n";
}
concatCode += " }\n" +
CCUtil.makeEnvChannelsCode (channelNames, env);
int initCounterSelect = 0;
Vector <String> seenchans = new Vector <String> ();
for (int i = 0; i < commsArray.length; i++) {
String channame = commsArray [i].getChannelExpr().getZName().toString();
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(channelNames [i]));
StringAndCounter sac = CCUtil.envsCode (channelNames [i],
commsArray [i], mam.min, mam.max, c, i, initCounterSelect, true, env.get(channelNames [i]));
concatCode +=
" public void makeOtherEnvs" + i + " () {\n" +
" " + sac.str +
" }\n";
if (!seenchans.contains(channame)) {
concatCode += CCUtil.makeSyncOnOtherEndsButFirstCode(comms.get(i).getChannelExpr().getName().toString(), comms.get (i), mam.min, mam.max, c, env.get(comms.get(i).getChannelExpr().getName().toString()), counter, env);
initCounterSelect += sac.counter;
seenchans.addElement(channame);
System.out.print ("");
}
}
concatCode += CCUtil.indexesbutfirstcode();

ccmg.concatCode(concatCode);
ccMapsGens.add(ccmg);

code =
//"int select_" + counter + ";\n" +
"final CCMaps_" + procName + "_" + counter + " ccMaps_" + counter
+ " = new CCMaps_" + procName + "_" + counter + " ();\n"
;

for (int i = 0; i < comms.size(); i++) {
code = code +
" abs_" + comms.get(i).getChannelExpr().getName().toString()
+ " = new " + "Abs_" + comms.get(i).getChannelExpr().getName().toString() + "();\n"
;
}
code += "\n" + booleansArrayCode (predicates, comms, c, env, counter);
code +=
"class ExtBranch" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (extchoicevars) +
" public ExtBranch" + counter + " (" + paramChoiceVariables (extchoicevars) + ") {\n" +
assignChoiceVariables (extchoicevars) +
" }\n" +
" public void run () {\n" +
" int select_" + counter + " = (new Alternative (" + "\n" +
" new Guard [] {\n";
for (int i = 0; i < channelNames.length; i++) {
MinsAndMaxs mam = MinsAndMaxs.getMinsAndMaxs(env.get(channelNames [i]));
code = code +
"\n"
+ CCUtil.guardsCode(channelNames [i], commsArray [i], mam.min, mam.max, c, env.get(channelNames [i]), counter) + "\n";
}
code = code +
"\n })).select(" + "b_" + counter + ");\n";
for (int i = 0; i < channelNames.length; i++) {
code = code +
" GPSE.resetEnds (" + channelNames [i] + ", " + "new int [] {" + TypeUtil.getTypeSizes(channelNames [i], env) + "});\n";
}
Vector <String> putNames = new Vector <String> ();
//c.extChoiceCaseSwitch = true;
code = code +
" switch (((Integer)ccMaps_" + counter + ".mapLeftOrRight.get(select_" + counter + ")).intValue()) {\n";
for (int i = 0; i < commsArray.length; i++) {
code = code +
" case " + i + ": {\n" +
assignmentsAndDeclarationCode (3, varNamesAsArray (commsArray [i], c), counter, "", env.getExpr(commsArray [i].getChannelExpr().getZName().toString())) +
" " + actionsArray [i].accept(c) + "\n" +
" break;\n" +
" }\n";
}
//c.extChoiceCaseSwitch = false;
code = code +
" }\n" +
" }\n" +
"};\n";
if (counter == 60) {
System.out.print ("");
}
code = code +
"ExtBranch" + counter +
" extbranch" + counter + " = new ExtBranch" + counter +
" (" + paramCallExtChoiceVariables (extchoicevars) + ");\n";
seenchans = new Vector <String> ();
for (int i = 0; i < commsArray.length; i++) {
String channame = commsArray [i].getChannelExpr().getZName().toString();
if (!seenchans.contains(channame)) {
code = code +
"class FrontEndsButFirstBranch_" + channame + "_" + counter + " implements CSProcess {\n" +
declareExtChoiceVariables (extchoicevars) +
" public FrontEndsButFirstBranch_" + channame + "_" + counter + " (" + paramChoiceVariables (extchoicevars) + ") {\n" +
assignChoiceVariables (extchoicevars) +
" }\n" +
" public void run () {\n";
code = code +
" CCMaps_" + procName + "_" + counter +
".syncOnOtherEndsButFirst_" +
commsArray [i].getChannelExpr().getName().toString() +
"(" +
commsArray [i].getChannelExpr().getName().toString() + ", " +
"abs_" + commsArray [i].getChannelExpr().getName().toString() +
CCUtil.complementarCommParams (false, comms.get(i), env) + ", " +
FrontEndAnn.getFrontEndAnn(comms.get(i)).toString() +
");\n" +
" }\n" +
"};\n";
code = code +
"FrontEndsButFirstBranch_" + channame + "_" + counter + " " +
"frontendsbutfirstbranch_" + channame + "_" + counter + " = " +
" new FrontEndsButFirstBranch_" + channame + "_" + counter +
" (" + paramCallExtChoiceVariables (extchoicevars) + ");\n";
seenchans.addElement(channame);
}
}
code = code +
"(new Parallel (new CSProcess [] {extbranch" + counter + ", ";
seenchans = new Vector <String> ();
for (int i = 0; i < commsArray.length; i++) {
String channame = commsArray [i].getChannelExpr().getZName().toString();
if (!seenchans.contains(channame)) {
code = code + "frontendsbutfirstbranch_" + channame + "_" + counter;
seenchans.addElement(channame);
if (i < commsArray.length - 1)
code = code + ", ";
}
}
code = code + "})).run();\n";
code = code + updateVariableValuesCode (extchoicevars, "extbranch" + counter);
return code;
}
}