summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor/src/rba/tool/editor/generator/z3/ConstraintCodeGenerationSupporter.java
blob: b587cc78d3ec3fe318f12badec1d69f9051137d3 (plain)
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
package rba.tool.editor.generator.z3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;

import rba.core.AbstractAllocatable;
import rba.core.AbstractConstraint;
import rba.core.AbstractContent;
import rba.core.ActiveContents;
import rba.core.Allocatable;
import rba.core.AllocatableSet;
import rba.core.AllocatedContent;
import rba.core.AndOperator;
import rba.core.ComplexExpression;
import rba.core.Constraint;
import rba.core.Content;
import rba.core.ContentSet;
import rba.core.ContentValue;
import rba.core.ExistsOperator;
import rba.core.Expression;
import rba.core.ExpressionType;
import rba.core.ForAllOperator;
import rba.core.GetAllocatables;
import rba.core.GetContentsList;
import rba.core.GetProperty;
import rba.core.HasBeenDisplayed;
import rba.core.HasComeEarlierThan;
import rba.core.HasComeLaterThan;
import rba.core.IfStatement;
import rba.core.ImpliesOperator;
import rba.core.IntegerProperty;
import rba.core.IntegerValue;
import rba.core.IsActive;
import rba.core.IsCanceled;
import rba.core.IsDisappeared;
import rba.core.IsEqualToOperator;
import rba.core.IsGreaterThanEqualOperator;
import rba.core.IsGreaterThanOperator;
import rba.core.IsLowerThanEqualOperator;
import rba.core.IsLowerThanOperator;
import rba.core.IsOn;
import rba.core.LambdaContext;
import rba.core.LambdaExpression;
import rba.core.NotOperator;
import rba.core.ObjectCompare;
import rba.core.ObjectReference;
import rba.core.OrOperator;
import rba.core.PreviousModifier;
import rba.core.RuleObject;
import rba.core.Scene;
import rba.core.SetOfOperator;
import rba.core.SugarExpression;
import rba.core.Variable;
import rba.sound.AllInstanceOfSoundContent;
import rba.sound.AllInstanceOfZone;
import rba.sound.IsAttenuated;
import rba.sound.IsMuted;
import rba.sound.IsOutputted;
import rba.sound.IsSounding;
import rba.sound.OutputtingSound;
import rba.sound.SoundContent;
import rba.sound.SoundContentSet;
import rba.sound.Zone;
import rba.sound.ZoneSet;
import rba.view.AllInstanceOfArea;
import rba.view.AllInstanceOfViewContent;
import rba.view.Area;
import rba.view.AreaSet;
import rba.view.DisplayingContent;
import rba.view.IsDisplayed;
import rba.view.IsHidden;
import rba.view.IsVisible;
import rba.view.ViewContent;
import rba.view.ViewContentSet;

public class ConstraintCodeGenerationSupporter implements GeneratorConstants {

    static ConstraintCodeTemplate template = new ConstraintCodeTemplate();

    private List<Allocatable> allocatableList = new ArrayList<Allocatable>();

    private int areaListEndIndex;

    private List<Content> contentList = new ArrayList<Content>();

    private int viewContentListEndIndex;

    private int soundContentListEndIndex;

    private List<ContentSet> contentSetList = new ArrayList<ContentSet>();

    private int viewContentSetListEndIndex;

    private List<AllocatableSet> allocatableSetList = new ArrayList<AllocatableSet>();

    private int areaSetListEndIndex;

    private List<Variable> variableList;

    private List<Constraint> constraintList;

    private List<Scene> sceneList;

    private List<SetOfOperator> setOfOpeList;

    private Map<Variable, String> variableNameMap = new HashMap<Variable, String>();

    private Map<AllocatableSet, String> allocatableSetNameMap = new HashMap<AllocatableSet, String>();

    private Map<ContentSet, String> contentSetNameMap = new HashMap<ContentSet, String>();

    private Map<String, String> SetOfNameMap = new HashMap<String, String>();

    private List<ImpliseConstraintInfo> impliseMethodList = new ArrayList<ImpliseConstraintInfo>();

    private List<ImpliseConstraintInfo> postImpliseMethodList = new ArrayList<ImpliseConstraintInfo>();

    public String generate(List<ViewContent> viewContentList, List<SoundContent> soundContentList, List<Area> areaList, List<Zone> zoneList,
            List<ViewContentSet> viewContentSetList, List<SoundContentSet> soundContentSetList, List<AreaSet> areaSetList, List<ZoneSet> zoneSetList, List<Variable> variables,
            List<Constraint> constraints, List<Scene> scenes, List<SetOfOperator> setOfOperators) {
        this.variableList = variables;
        this.constraintList = constraints;
        this.sceneList = scenes;
        this.setOfOpeList = setOfOperators;

        variableNameMap.clear();
        allocatableSetNameMap.clear();
        contentSetNameMap.clear();
        SetOfNameMap.clear();
        impliseMethodList.clear();
        postImpliseMethodList.clear();

        allocatableList.clear();
        contentList.clear();
        allocatableSetList.clear();
        contentSetList.clear();

        areaListEndIndex = areaList.size();
        allocatableList.addAll(areaList);
        allocatableList.addAll(zoneList);

        viewContentListEndIndex = viewContentList.size();
        soundContentListEndIndex = viewContentListEndIndex + soundContentList.size();
        contentList.addAll(viewContentList);
        contentList.addAll(soundContentList);

        areaSetListEndIndex = areaSetList.size();
        allocatableSetList.addAll(areaSetList);
        allocatableSetList.addAll(zoneSetList);

        viewContentSetListEndIndex = viewContentSetList.size();
        contentSetList.addAll(viewContentSetList);
        contentSetList.addAll(soundContentSetList);

        StringBuilder setupList_Adding = new StringBuilder();
        StringBuilder init_Adding = new StringBuilder();
        StringBuilder fieldVariableAdding = new StringBuilder();
        StringBuilder allocatable_content_sizeAdding = new StringBuilder();

        StringBuilder constraintList_Adding = new StringBuilder();
        StringBuilder impliesConstraintMethodList_Adding = new StringBuilder();
        StringBuilder impliesConstraintMethodCallList_Adding = new StringBuilder();

        setAllocatableContext(allocatableList.subList(0, areaListEndIndex), init_Adding, "Area");
        setAllocatableContext(allocatableList.subList(areaListEndIndex, allocatableList.size()), init_Adding, "Zone");
        setContetContext(contentList.subList(0, viewContentListEndIndex), init_Adding, "ViewContent");
        setContetContext(contentList.subList(viewContentListEndIndex, soundContentListEndIndex), init_Adding, "SoundContent");

        setAllocatableSetContext(AREASET_VAR_NAME, allocatableSetList.subList(0, areaSetListEndIndex), init_Adding, fieldVariableAdding, setupList_Adding);
        setAllocatableSetContext(ZONESET_VAR_NAME, allocatableSetList.subList(areaSetListEndIndex, allocatableSetList.size()), init_Adding, fieldVariableAdding, setupList_Adding);
        setContentSetContext(VIEW_CONTENTSET_VAR_NAME, contentSetList.subList(0, viewContentSetListEndIndex), init_Adding, fieldVariableAdding, setupList_Adding);
        setContentSetContext(SOUND_CONTENTSET_VAR_NAME, contentSetList.subList(viewContentSetListEndIndex, contentSetList.size()), init_Adding, fieldVariableAdding,
                setupList_Adding);

        allocatable_content_sizeAdding.append(_8SPACES + "allocatableSize = ctx.mkInt(" + allocatableList.size() + ");" + NL);
        allocatable_content_sizeAdding.append(_8SPACES + "contentSize = ctx.mkInt(" + contentList.size() + ");");

        for (int i = 0; i < variableList.size(); i++) {
            Variable variable = variableList.get(i);
            String varName = VARIABLE_VAR_NAME + (i + 1);
            String define = String.format(VARIABLE_DEFINE, varName);

            constraintList_Adding.append(_8SPACES + VARIABLE_CMT + variable.getName() + NL);
            constraintList_Adding.append(_8SPACES + define + NL);
            constraintList_Adding.append(_8SPACES + varName + ARRAY_TOP + String.format(SET_VARIABLE_VALUE, variable.getName()) + NL);

            variableNameMap.put(variable, varName);
        }

        for (int i = 0; i < setOfOpeList.size(); i++) {
            SetOfOperator setOfOp = setOfOpeList.get(i);

            String varName = SETOF_OP_VAR_NAME + (i + 1);
            String expressionText = setOfOp.getExpressionText();
            String string = SetOfNameMap.get(expressionText);
            if (string != null) {
                continue;
            }
            SetOfNameMap.put(expressionText, varName);

            String label = STR_QUOTE + CONSTRAINT_PREFIX + varName + STR_QUOTE;
            String storeStr = getStoreExpr(setOfOp);

            fieldVariableAdding.append(_4SPACES + ARRAY_EXPR_SORT + varName + END + NL);

            setupList_Adding
                    .append(_8SPACES + varName + EQUAL + EX_CTX_MK_ARRAY_CONST + OPEN + STR_QUOTE + varName + STR_QUOTE + COMMA + INT_TYPE + COMMA + BOOL_TYPE + CLOSE + END + NL);

            init_Adding.append(_8SPACES + SL_COMMENT + varName + EQUAL + expressionText + NL);
            init_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + EX_CTX_MK_EQ + OPEN + varName + COMMA + storeStr + CLOSE + CLOSE + END + NL);
            init_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + label + CLOSE + CLOSE + END + NL);
        }

        constraintList_Adding.append(NL);

        for (int i = 0; i < constraintList.size(); i++) {
            Constraint constraint = constraintList.get(i);
            if (constraint.getExpression() == null) {
                continue;
            }

            setConstraintList(constraintList_Adding, impliesConstraintMethodList_Adding, constraint, constraint.isRuntime(), i);
        }

        for (ImpliseConstraintInfo info : impliseMethodList) {
            setImpliesConstraintMethod(impliesConstraintMethodCallList_Adding, info);
        }

        String invarianceConstraintStr = getInvarianceConstraintStr(false);
        String invariancePreConstraintStr = getInvarianceConstraintStr(true);

        template.allocatable_content_set_code = fieldVariableAdding.toString();
        template.allocatable_content_size_code = allocatable_content_sizeAdding.toString();
        template.setup_code = setupList_Adding.toString();
        template.init_code = init_Adding.toString();
        template.constraints_code = constraintList_Adding.toString();
        template.implies_constraints_call_code = impliesConstraintMethodCallList_Adding.toString();
        template.implies_constraints_method_code = impliesConstraintMethodList_Adding.toString();
        template.invariance_constr_code = invarianceConstraintStr;
        template.invariance_pre_constr_code = invariancePreConstraintStr;

        return template.getCompleteCode();
    }

    private void setAllocatableContext(List<Allocatable> list, StringBuilder init_Adding, String elementId) {
        if (list.isEmpty()) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            Allocatable area = list.get(i);

            String contents = getContents(area);
            String storeStr = getStoreExpr(area);

            int index = allocatableList.indexOf(area);

            init_Adding.append(_8SPACES + SL_COMMENT + area.getName() + " = { " + contents + " }" + NL);

            String selectLeft = EX_CTX_MK_SELECT + OPEN + OP_CONTENTS_LIST + COMMA + EX_CTX_MK_INT + OPEN + (index + 1) + CLOSE + CLOSE;
            init_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + EX_CTX_MK_EQ + OPEN + selectLeft + COMMA + storeStr + CLOSE + CLOSE + END + NL);
            String labelContentList = STR_QUOTE + CONSTRAINT_PREFIX + area.getName() + DOT + OP_CONTENTS_LIST + STR_QUOTE;
            init_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + labelContentList + CLOSE + CLOSE + END + NL);
        }
        init_Adding.append(NL);
    }

    private void setContetContext(List<Content> list, StringBuilder init_Adding, String elementId) {
        if (list.isEmpty()) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            Content content = list.get(i);

            String allocatable = getAllocatables(content);
            String storeStr = getStoreExpr(content);

            int index = contentList.indexOf(content);

            init_Adding.append(_8SPACES + SL_COMMENT + content.getName() + " = { " + allocatable + " }" + NL);

            String selectLeft = EX_CTX_MK_SELECT + OPEN + OP_ALLOCATABLE + COMMA + EX_CTX_MK_INT + OPEN + (index + 1) + CLOSE + CLOSE;
            init_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + EX_CTX_MK_EQ + OPEN + selectLeft + COMMA + storeStr + CLOSE + CLOSE + END + NL);
            String labelContentList = STR_QUOTE + CONSTRAINT_PREFIX + content.getName() + DOT + OP_ALLOCATABLE + STR_QUOTE;
            init_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + labelContentList + CLOSE + CLOSE + END + NL);
        }
        init_Adding.append(NL);
    }

    private void setAllocatableSetContext(String name, List<AllocatableSet> list, StringBuilder init_Adding, StringBuilder fieldVariableAdding, StringBuilder setupList_Adding) {
        if (list.isEmpty()) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            AllocatableSet set = list.get(i);
            String varName = name + (i + 1);
            String label = STR_QUOTE + CONSTRAINT_PREFIX + set.getName() + STR_QUOTE;
            String areas = getAreas(set);
            String storeStr = getStoreExpr(set);

            fieldVariableAdding.append(_4SPACES + ARRAY_EXPR_SORT + varName + END + NL);

            setupList_Adding
                    .append(_8SPACES + varName + EQUAL + EX_CTX_MK_ARRAY_CONST + OPEN + STR_QUOTE + varName + STR_QUOTE + COMMA + INT_TYPE + COMMA + BOOL_TYPE + CLOSE + END + NL);

            init_Adding.append(_8SPACES + SL_COMMENT + set.getName() + " = { " + areas + " }" + NL);
            init_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + EX_CTX_MK_EQ + OPEN + varName + COMMA + storeStr + CLOSE + CLOSE + END + NL);
            init_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + label + CLOSE + CLOSE + END + NL);

            allocatableSetNameMap.put(set, varName);
        }
        setupList_Adding.append(NL);
        init_Adding.append(NL);
    }

    private void setContentSetContext(String name, List<ContentSet> list, StringBuilder init_Adding, StringBuilder fieldVariableAdding, StringBuilder setupList_Adding) {
        if (list.isEmpty()) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            ContentSet contentSet = list.get(i);
            String varName = name + (i + 1);
            String label = STR_QUOTE + CONSTRAINT_PREFIX + contentSet.getName() + STR_QUOTE;
            String contents = getContents(contentSet);
            String storeStr = getStoreExpr(contentSet);

            fieldVariableAdding.append(_4SPACES + ARRAY_EXPR_SORT + varName + END + NL);

            setupList_Adding
                    .append(_8SPACES + varName + EQUAL + EX_CTX_MK_ARRAY_CONST + OPEN + STR_QUOTE + varName + STR_QUOTE + COMMA + INT_TYPE + COMMA + BOOL_TYPE + CLOSE + END + NL);

            init_Adding.append(_8SPACES + SL_COMMENT + contentSet.getName() + " = { " + contents + " }" + NL);
            init_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + EX_CTX_MK_EQ + OPEN + varName + COMMA + storeStr + CLOSE + CLOSE + END + NL);
            init_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + label + CLOSE + CLOSE + END + NL);

            contentSetNameMap.put(contentSet, varName);
        }
        init_Adding.append(NL);
    }

    private void setConstraintList(StringBuilder constraintList_Adding, StringBuilder impliesConstraintMethodList_Adding, AbstractConstraint constraint, boolean runtime,
            int index) {

        String label = STR_QUOTE + NUMBER + String.valueOf(index + 1) + ":" + constraint.getName() + STR_QUOTE;
        String expressionText = constraint.getExpression().getExpressionText();
        constraintList_Adding.append(_8SPACES + SL_COMMENT + expressionText + NL);
        String constraintContextString = "";

        try {
            constraintContextString = getContextString(constraint.getExpression());
        } catch (Exception e) {
            constraintList_Adding.append(NL);
            e.printStackTrace();
            return;
        }
        
        Expression targetExpression = constraint.getExpression() instanceof ComplexExpression ? ((ComplexExpression)constraint.getExpression()).getOtherExpression() : constraint.getExpression();

        if (isBoolOperator(targetExpression)) {
            constraintContextString = EX_BOOL_EXPR + constraintContextString;
        }

        String indent = "";
        if (!runtime) {
            indent = _4SPACES;
            constraintList_Adding.append(_8SPACES + IF_OFFLINE + NL);
        }
        constraintList_Adding.append(indent + _8SPACES + CONSTR_LIST_ADD + OPEN + constraintContextString + CLOSE + END + NL);
        constraintList_Adding.append(indent + _8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + label + CLOSE + CLOSE + END + NL);
        if (!runtime) {
            constraintList_Adding.append(_8SPACES + BLOCK_CLOSE + NL);
        }
        constraintList_Adding.append(NL);

        if (isImplieseOperator(targetExpression)) {
            impliesConstraintMethodList_Adding.append(getimpliesConstraintMethodsStr(constraint, index));
        }
    }

    private void setImpliesConstraintMethod(StringBuilder impliesConstraintMethodCallList_Adding, ImpliseConstraintInfo info) {
        int index = info.getIndex() + 1;
        String indent = "";
        if (!info.isRuntime()) {
            indent = _4SPACES;
            impliesConstraintMethodCallList_Adding.append(_8SPACES + IF_OFFLINE + NL);
        }
        String methodName = info.getMethodName();

        impliesConstraintMethodCallList_Adding.append(indent + _8SPACES + String.format(IMPLIES_CONSTRAINT_METHOD_DETAIL1, index) + methodName + END + NL);
        impliesConstraintMethodCallList_Adding.append(indent + _8SPACES + String.format(IMPLIES_CONSTRAINT_METHOD_DETAIL2, index, index) + NL);
        impliesConstraintMethodCallList_Adding
                .append(indent + _4SPACES + _8SPACES + String.format(IMPLIES_CONSTRAINT_METHOD_DETAIL3, index, info.getConstraint().getName(), index) + END + NL);
        impliesConstraintMethodCallList_Adding.append(indent + _4SPACES + _8SPACES + String.format(IMPLIES_CONSTRAINT_METHOD_DETAIL4, index) + NL);
        impliesConstraintMethodCallList_Adding.append(indent + _8SPACES + _8SPACES + IMPLIES_CONSTRAINT_METHOD_DETAIL5 + END + NL);
        impliesConstraintMethodCallList_Adding.append(indent + _4SPACES + _8SPACES + BLOCK_CLOSE + NL);
        impliesConstraintMethodCallList_Adding.append(indent + _8SPACES + BLOCK_CLOSE + NL);
        if (!info.isRuntime()) {
            impliesConstraintMethodCallList_Adding.append(_8SPACES + BLOCK_CLOSE + NL);
        }
        impliesConstraintMethodCallList_Adding.append(NL);
    }

    private String getimpliesConstraintMethodsStr(AbstractConstraint constraint, int index) {
        StringBuilder methodsCommentList_Adding = new StringBuilder();
        StringBuilder constraintList_Adding = new StringBuilder();

        Expression targetExpression = constraint.getExpression() instanceof ComplexExpression ? ((ComplexExpression)constraint.getExpression()).getOtherExpression() : constraint.getExpression();
        Expression leftExpression = ((ImpliesOperator) targetExpression).getOperand().get(0);

        ImpliseConstraintInfo info = new ImpliseConstraintInfo();
        info.setIndex(index);
        info.setConstraint(constraint);
        String methodName = "";
        String allConstMethodName = "";

        if (constraint instanceof Constraint) {
            // method name
            methodName = String.format(IMPLIES_CONSTRAINT_METHOD_NAME, (index + 1));
            allConstMethodName = "setAllConstraintContext";
            info.setMethodName(methodName);
            impliseMethodList.add(info);
        }

        // comment
        methodsCommentList_Adding.append(_4SPACES + CMT_CONTENT_IMPLIES_METHOD1 + NL);
        String leftText = leftExpression.getExpressionText();
        methodsCommentList_Adding.append(_4SPACES + String.format(CMT_CONTENT_IMPLIES_METHOD2, leftText) + NL);
        methodsCommentList_Adding.append(_4SPACES + CMT_CONTENT_IMPLIES_METHOD3 + constraint.getName() + NL);
        methodsCommentList_Adding.append(_4SPACES + CMT_CONTENT_IMPLIES_METHOD4 + constraint.getExpression().getExpressionText());

        if (isIncludeLambdaExpression(leftExpression)) {
            List<Variable> list = getVariables(leftExpression);

            for (int i = 0; i < list.size(); i++) {
                Variable variable = list.get(i);
                String varName = variableNameMap.get(variable);
                String define = String.format(VARIABLE_DEFINE, varName);

                constraintList_Adding.append(_8SPACES + VARIABLE_CMT + variable.getName() + NL);
                constraintList_Adding.append(_8SPACES + define + NL);
                constraintList_Adding.append(_8SPACES + varName + ARRAY_TOP + String.format(SET_VARIABLE_VALUE, variable.getName()) + NL);
            }
            constraintList_Adding.append(NL);
        }

        // constraint
        String expression = getContextString(leftExpression);
        String label = STR_QUOTE + NUMBER_ZERO + constraint.getName() + STR_QUOTE;
        constraintList_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + EX_CTX_MK_EQ + OPEN + expression + COMMA + EX_CTX_MK_TRUE + CLOSE + CLOSE + END + NL);
        constraintList_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + label + CLOSE + CLOSE + END + NL);

        template.implies_constrName = label;
        template.implies_constraints_comment_code = methodsCommentList_Adding.toString();
        template.implies_constraints_methodName_code = _4SPACES + String.format(IMPLIES_CONSTRAINT_METHOD, methodName);
        template.implies_constraints_true_code = constraintList_Adding.toString();

        return template.getImpliesConstraintMethodCode(allConstMethodName);
    }

    private String getInvarianceConstraintStr(boolean isPre) {
        StringBuilder invariance_constr_Adding = new StringBuilder();

        invariance_constr_Adding.append(getInvarianceConstraintStr_IN01(isPre));
        invariance_constr_Adding.append(NL);
        invariance_constr_Adding.append(getInvarianceConstraintStr_IN02(isPre));
        invariance_constr_Adding.append(NL);
        invariance_constr_Adding.append(getInvarianceConstraintStr_IN03(isPre));

        return invariance_constr_Adding.toString();
    }

    private String getInvarianceConstraintStr_IN01(boolean isPre) {
        String preLabel = "";
        String opDisplayingContent = OP_DISPLAYING_CONTENT;
        if (isPre) {
            opDisplayingContent = OP_PRE_DISPLAYING_CONTENT;
            preLabel = "(pre)";
        }
        return getInvarianceConstraintStr_IN01_IN03_Impl(preLabel, opDisplayingContent, "IN01");
    }

    private String getInvarianceConstraintStr_IN01_IN03_Impl(String preLabel, String expr, String inKind) {
        StringBuilder invariance_constr_Adding = new StringBuilder();

        invariance_constr_Adding.append(_8SPACES + "// Area." + expr + " == contentsList.get(n) OR Area." + expr + " == contentsList.get(n+1) ...)");
        invariance_constr_Adding.append(NL);

        for (int i = 0; i < allocatableList.size(); i++) {
            Allocatable mainArea = allocatableList.get(i);
            String leftLabel = mainArea.getName() + "." + expr + " == ";
            String areaMkInt = EX_CTX_MK_INT + OPEN + String.valueOf(i + 1) + CLOSE;
            String leftconstr = EX_CTX_MK_EQ + OPEN + EX_CTX_MK_SELECT + OPEN + expr + COMMA + areaMkInt + CLOSE + COMMA;

            StringBuffer constrLabel = new StringBuffer();
            StringBuffer constr = new StringBuffer();

            constrLabel.append(STR_QUOTE + inKind +":Constraint " + preLabel + "_" + String.valueOf(i + 1) + " - (" + leftLabel);

            List<Content> mainContents = mainArea.getContentsList();

            int childSize = mainContents.size();
            if (childSize == 0) {
                constrLabel.append("NULL");
                constr.append(leftconstr + "defNull" + CLOSE);
            } else {
                for (int k = 0; k < mainContents.size(); k++) {
                    Content content = mainContents.get(k);
                    String contentName = content.getName();

                    int index = contentList.indexOf(content);
                    String contentMkInt = EX_CTX_MK_INT + OPEN + String.valueOf(index + 1) + CLOSE;
                    String subConstr = "";
                    if (k == 0 && k != mainContents.size() - 1) {
                        subConstr = EX_CTX_MK_OR + OPEN;
                    }

                    constrLabel.append(contentName);
                    constr.append(subConstr + leftconstr + contentMkInt + CLOSE);

                    if (k != mainContents.size() - 1) {
                        constrLabel.append(" OR ");
                        constr.append(COMMA);
                    } else {
                        if (mainContents.size() != 1) {
                            constr.append(CLOSE);
                        }
                    }
                }
            }
            constrLabel.append(")" + STR_QUOTE);

            invariance_constr_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + constr.toString() + CLOSE + END + NL);
            invariance_constr_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + constrLabel.toString() + CLOSE + CLOSE + END + NL);
        }

        return invariance_constr_Adding.toString();
    }

    private String getInvarianceConstraintStr_IN02(boolean isPre) {

        int constNum = 1;
        StringBuilder invariance_constr_Adding = new StringBuilder();
        String opIsDisplayed = OP_IS_DISPLAYED;
        String opIsVisible = OP_IS_VISIBLE;
        String preLabel = "";
        if (isPre) {
            opIsDisplayed = OP_PRE_IS_DISPLAYED;
            opIsVisible = OP_PRE_IS_VISIBLE;
            preLabel = "(pre)";
        }

        invariance_constr_Adding.append(_8SPACES + "// a." + opIsDisplayed + "->c." + opIsVisible + " AND c." + opIsVisible + "->a." + opIsDisplayed);
        invariance_constr_Adding.append(NL);

        for (int i = 0; i < allocatableList.size(); i++) {
            Allocatable mainArea = allocatableList.get(i);
            String areaIsDisplayed = mainArea.getName() + "." + opIsDisplayed;
            String areaMkInt = EX_CTX_MK_INT + OPEN + String.valueOf(i + 1) + CLOSE;

            List<Content> mainContents = mainArea.getContentsList();

            if (mainContents.size() != 1) {
                continue;
            }

            for (int j = 0; j < mainContents.size(); j++) {
                StringBuffer constrLabel = new StringBuffer();
                StringBuffer constr = new StringBuffer();
                Content mainContent = mainContents.get(j);

                if (mainContent.getAllocatableList().size() != 1) {
                    continue;
                }

                int index = contentList.indexOf(mainContent);
                String contentIsVisible = mainContent.getName() + "." + opIsVisible;
                String mainContentMkInt = EX_CTX_MK_INT + OPEN + String.valueOf(index + 1) + CLOSE;

                constrLabel.append(STR_QUOTE + "IN02:Constraint" + preLabel + "_" + String.valueOf(constNum) + " - " + areaIsDisplayed + " -> " + contentIsVisible + " AND "
                        + contentIsVisible + " -> " + areaIsDisplayed + STR_QUOTE);
                constNum++;

                constr.append(EX_CTX_MK_AND + OPEN);
                constr.append(EX_CTX_MK_IMPLIES + OPEN + EX_BOOL_EXPR + EX_CTX_MK_SELECT + OPEN + opIsDisplayed + COMMA + areaMkInt + CLOSE + COMMA + EX_BOOL_EXPR
                        + EX_CTX_MK_SELECT + OPEN + opIsVisible + COMMA + mainContentMkInt + CLOSE + CLOSE);
                constr.append(COMMA);
                constr.append(EX_CTX_MK_IMPLIES + OPEN + EX_BOOL_EXPR + EX_CTX_MK_SELECT + OPEN + opIsVisible + COMMA + mainContentMkInt + CLOSE + COMMA + EX_BOOL_EXPR
                        + EX_CTX_MK_SELECT + OPEN + opIsDisplayed + COMMA + areaMkInt + CLOSE + CLOSE);
                constr.append(CLOSE);

                invariance_constr_Adding.append(_8SPACES + CONSTR_LIST_ADD + OPEN + constr.toString() + CLOSE + END + NL);
                invariance_constr_Adding.append(_8SPACES + CONSTR_LABEL_LIST_ADD + OPEN + EX_CTX_MK_BOOL_CONST + OPEN + constrLabel.toString() + CLOSE + CLOSE + END + NL);
            }
        }

        return invariance_constr_Adding.toString();
    }

    private String getInvarianceConstraintStr_IN03(boolean isPre) {
        String preLabel = "";
        String opAllocatedContent = OP_ALLOCATED_CONTENT;
        if (isPre) {
            opAllocatedContent = OP_PRE_ALLOCATED_CONTENT;
            preLabel = "(pre)";
        }
        return getInvarianceConstraintStr_IN01_IN03_Impl(preLabel, opAllocatedContent, "IN03");
    }

    private boolean isIncludeLambdaExpression(Expression expression) {

        for (Iterator<EObject> iterator = expression.eAllContents(); iterator.hasNext();) {
            EObject object = iterator.next();
            if (object instanceof LambdaContext | object instanceof LambdaExpression) {
                return true;
            }
        }
        return false;
    }

    private List<Variable> getVariables(Expression expression) {
        List<Variable> list = new ArrayList<Variable>();

        for (Iterator<EObject> iterator = expression.eAllContents(); iterator.hasNext();) {
            EObject object = iterator.next();
            if (object instanceof Variable) {
                list.add((Variable) object);
            }
        }
        return list;
    }

    private boolean isImplieseOperator(Expression expression) {
        if (expression instanceof ImpliesOperator) {
            return true;
        }
        return false;
    }

    private boolean isBoolOperator(Expression expression) {
        if (expression instanceof IsVisible) {
            return true;
        } else if (expression instanceof IsDisplayed) {
            return true;
        } else if (expression instanceof IsActive) {
            return true;
        } else if (expression instanceof IsHidden) {
            return true;
        } else if (expression instanceof IfStatement) {
            return true;
        } else if (expression instanceof IsDisappeared) {
            return true;
        } else if (expression instanceof IsCanceled) {
            return true;
        } else if (expression instanceof IsMuted) {
            return true;
        } else if (expression instanceof IsOutputted) {
            return true;
        } else if (expression instanceof IsAttenuated) {
            return true;
        } else if (expression instanceof IsSounding) {
            return true;
        } else if (expression instanceof IsOn) {
            return true;
        } else if (expression instanceof HasBeenDisplayed) {
            return true;
        } else if (expression instanceof HasComeEarlierThan) {
            return true;
        } else if (expression instanceof HasComeLaterThan) {
            return true;
        }
        return false;
    }

    private String getStoreExpr(Allocatable allocatable) {
        String context = EMP_ARRAY_CONST;

        List<Content> contentsList = allocatable.getContentsList();

        for (int i = 0; i < contentList.size(); i++) {
            Content content = contentList.get(i);
            if (contentsList.contains(content)) {
                context = String.format(STORE_TEMPLATE, context, (i + 1));
            }
        }

        return context;
    }

    private String getStoreExpr(Content content) {
        String context = EMP_ARRAY_CONST;

        for (int i = 0; i < allocatableList.size(); i++) {
            Allocatable area = allocatableList.get(i);
            if (content.getAllocatableList().contains(area)) {
                context = String.format(STORE_TEMPLATE, context, (i + 1));
            }
        }

        return context;
    }

    private String getStoreExpr(AllocatableSet allocatableSet) {
        String context = EMP_ARRAY_CONST;
        List<RuleObject> set = getRuleObjectsBySet(allocatableSet, new ArrayList<RuleObject>());

        for (int i = 0; i < allocatableList.size(); i++) {
            Allocatable area = allocatableList.get(i);
            if (set.contains(area)) {
                context = String.format(STORE_TEMPLATE, context, (i + 1));
            }
        }

        return context;
    }

    private String getStoreExpr(ContentSet contentSet) {
        String context = EMP_ARRAY_CONST;
        List<RuleObject> set = getRuleObjectsBySet(contentSet, new ArrayList<RuleObject>());

        for (int i = 0; i < contentList.size(); i++) {
            Content content = contentList.get(i);
            if (set.contains(content)) {
                context = String.format(STORE_TEMPLATE, context, (i + 1));
            }
        }

        return context;
    }

    private String getStoreExpr(SetOfOperator op) {
        String context = EMP_ARRAY_CONST;

        List<RuleObject> objects = new ArrayList<RuleObject>();
        EList<Expression> operand = op.getOperand();
        if (operand.isEmpty()) {
            return context;
        }

        for (Expression expression : operand) {
            if (!(expression instanceof ObjectReference)) {
                continue;
            }

            RuleObject refObject = ((ObjectReference) expression).getRefObject();
            if (refObject instanceof Content || refObject instanceof Allocatable) {
                objects.add(refObject);
            } else if (refObject instanceof ContentSet | refObject instanceof AllocatableSet) {
                objects.addAll(getRuleObjectsBySet(refObject, new ArrayList<RuleObject>()));
            }
        }

        if (objects.isEmpty()) {
            return context;
        }

        if (objects.get(0) instanceof Area) {
            for (int i = 0; i < allocatableList.size(); i++) {
                Allocatable area = allocatableList.get(i);
                if (objects.contains(area)) {
                    context = String.format(STORE_TEMPLATE, context, (i + 1));
                }
            }
        } else if (objects.get(0) instanceof Zone) {
            for (int i = 0; i < allocatableList.size(); i++) {
                Allocatable zone = allocatableList.get(i);
                if (objects.contains(zone)) {
                    context = String.format(STORE_TEMPLATE, context, (i + 1));
                }
            }
        } else if (objects.get(0) instanceof ViewContent) {
            for (int i = 0; i < contentList.size(); i++) {
                Content content = contentList.get(i);
                if (objects.contains(content)) {
                    context = String.format(STORE_TEMPLATE, context, (i + 1));
                }
            }
        } else if (objects.get(0) instanceof SoundContent) {
            for (int i = 0; i < contentList.size(); i++) {
                Content content = contentList.get(i);
                if (objects.contains(content)) {
                    context = String.format(STORE_TEMPLATE, context, (i + 1));
                }
            }
        }

        return context;
    }

    private List<RuleObject> getRuleObjectsBySet(RuleObject refObject, List<RuleObject> elementSetList) {
        Set<RuleObject> sets = new HashSet<RuleObject>();

        if (refObject instanceof AllocatableSet) {
            AllocatableSet set = (AllocatableSet) refObject;
            for (AbstractAllocatable abst : set.getTarget()) {
                if (abst instanceof Allocatable) {
                    sets.add(abst);
                } else if (abst instanceof AllocatableSet) {
                    if (!elementSetList.contains(abst)) {
                        elementSetList.add(abst);
                        sets.addAll(getRuleObjectsBySet(abst, elementSetList));
                    }
                }
            }
        }
        if (refObject instanceof ContentSet) {
            ContentSet set = (ContentSet) refObject;
            for (AbstractContent abst : set.getTarget()) {
                if (abst instanceof Content) {
                    sets.add(abst);
                } else if (abst instanceof ContentSet) {
                    if (!elementSetList.contains(abst)) {
                        elementSetList.add(abst);
                        sets.addAll(getRuleObjectsBySet(abst, elementSetList));
                    }
                }
            }
        }

        return new ArrayList<RuleObject>(sets);
    }

    private String getContents(Allocatable allocatable) {
        String contentStr = "";
        List<Content> contents = allocatable.getContentsList();

        for (Content content : contents) {
            contentStr += content.getName();
            if (contents.indexOf(content) != contents.size() - 1) {
                contentStr += COMMA + " ";
            }
        }
        return contentStr;
    }

    private String getAllocatables(Content content) {
        String areas = "";
        EList<Allocatable> allocatables = content.getAllocatableList();
        for (AbstractAllocatable allocatable : allocatables) {
            areas += allocatable.getName();
            if (allocatables.indexOf(allocatable) != allocatables.size() - 1) {
                areas += COMMA + " ";
            }
        }
        return areas;
    }

    private String getAreas(AllocatableSet set) {
        String areas = "";

        for (AbstractAllocatable area : set.getTarget()) {
            areas += area.getName();
            if (set.getTarget().indexOf(area) != set.getTarget().size() - 1) {
                areas += COMMA + " ";
            }
        }
        return areas;
    }

    private String getContents(ContentSet contentSet) {
        String contents = "";

        for (AbstractContent content : contentSet.getTarget()) {
            contents += content.getName();
            if (contentSet.getTarget().indexOf(content) != contentSet.getTarget().size() - 1) {
                contents += COMMA + " ";
            }
        }
        return contents;
    }

    private String getContextString(Expression expression) {
        String contextString = null;

        if (expression instanceof ComplexExpression) {
            contextString = getContextString(((ComplexExpression) expression).getOtherExpression());
        } else if (expression instanceof AndOperator) {
            contextString = resolveExpression((AndOperator) expression);
        } else if (expression instanceof OrOperator) {
            contextString = resolveExpression((OrOperator) expression);
        } else if (expression instanceof NotOperator) {
            contextString = resolveExpression((NotOperator) expression);
        } else if (expression instanceof ImpliesOperator) {
            contextString = resolveExpression((ImpliesOperator) expression);
        } else if (expression instanceof ObjectCompare) {
            contextString = resolveExpression((ObjectCompare) expression);
        } else if (expression instanceof IsEqualToOperator) {
            contextString = resolveExpression((IsEqualToOperator) expression);
        } else if (expression instanceof IsGreaterThanOperator) {
            contextString = resolveExpression((IsGreaterThanOperator) expression);
        } else if (expression instanceof IsGreaterThanEqualOperator) {
            contextString = resolveExpression((IsGreaterThanEqualOperator) expression);
        } else if (expression instanceof IsLowerThanOperator) {
            contextString = resolveExpression((IsLowerThanOperator) expression);
        } else if (expression instanceof IsLowerThanEqualOperator) {
            contextString = resolveExpression((IsLowerThanEqualOperator) expression);
        } else if (expression instanceof IsDisplayed) {
            contextString = resolveExpression((IsDisplayed) expression);
        } else if (expression instanceof DisplayingContent) {
            contextString = resolveExpression((DisplayingContent) expression);
        } else if (expression instanceof AllocatedContent) {
            contextString = resolveExpression((AllocatedContent) expression);
        } else if (expression instanceof GetContentsList) {
            System.out.println("GetContentsList is not supported yet!");
        } else if (expression instanceof IsHidden) {
            contextString = resolveExpression((IsHidden) expression);
        } else if (expression instanceof IsActive) {
            contextString = resolveExpression((IsActive) expression);
        } else if (expression instanceof IsVisible) {
            contextString = resolveExpression((IsVisible) expression);
        } else if (expression instanceof GetAllocatables) {
            System.out.println("GetAllocatables is not supported yet!");
        } else if (expression instanceof ContentValue) {
            contextString = resolveExpression((ContentValue) expression);
        } else if (expression instanceof ExistsOperator) {
            contextString = resolveExpression((ExistsOperator) expression);
        } else if (expression instanceof ForAllOperator) {
            contextString = resolveExpression((ForAllOperator) expression);
        } else if (expression instanceof IfStatement) {
            contextString = resolveExpression((IfStatement) expression);
        } else if (expression instanceof IsOn) {
            contextString = resolveExpression((IsOn) expression);
        } else if (expression instanceof ObjectReference) {
            contextString = resolveExpression((ObjectReference) expression);
        } else if (expression instanceof PreviousModifier) {
            contextString = resolveExpression((PreviousModifier) expression);
        } else if (expression instanceof IsDisappeared) {
            contextString = resolveExpression((IsDisappeared) expression);
        } else if (expression instanceof IsCanceled) {
            contextString = resolveExpression((IsCanceled) expression);
        } else if (expression instanceof IsMuted) {
            contextString = resolveExpression((IsMuted) expression);
        } else if (expression instanceof IsSounding) {
            contextString = resolveExpression((IsSounding) expression);
        } else if (expression instanceof OutputtingSound) {
            contextString = resolveExpression((OutputtingSound) expression);
        } else if (expression instanceof IsAttenuated) {
            contextString = resolveExpression((IsAttenuated) expression);
        } else if (expression instanceof IsOutputted) {
            contextString = resolveExpression((IsOutputted) expression);
        } else if (expression instanceof GetProperty) {
            contextString = resolveExpression((GetProperty) expression);
        } else if (expression instanceof IntegerValue) {
            contextString = resolveExpression((IntegerValue) expression);
        } else if (expression instanceof HasComeEarlierThan) {
            contextString = resolveExpression((HasComeEarlierThan) expression);
        } else if (expression instanceof HasComeLaterThan) {
            contextString = resolveExpression((HasComeLaterThan) expression);
        } else if (expression instanceof HasBeenDisplayed) {
            contextString = resolveExpression((HasBeenDisplayed) expression);
        } else if (expression instanceof SugarExpression) {
            contextString = getContextString(((SugarExpression) expression).getExpanded());
        } else {
            // TODO Specification not found
            throw new RuntimeException("Expected Expression type is not supported yet! : " + expression.getExpressionText());
        }

        return contextString;
    }

    private String resolveExpression(AndOperator expression) {
        List<Expression> operand = expression.getOperand();
        String context = EX_CTX_MK_AND + OPEN;
        for (int i = 0; i < operand.size(); i++) {
            Expression op = operand.get(i);
            String opContext = getContextString(op);
            context += EX_BOOL_EXPR + opContext;

            if (i != operand.size() - 1) {
                context += COMMA;
            }
        }

        return context + CLOSE;
    }

    private String resolveExpression(OrOperator expression) {
        List<Expression> operand = expression.getOperand();

        String context = EX_CTX_MK_OR + OPEN;
        for (int i = 0; i < operand.size(); i++) {
            Expression op = operand.get(i);
            String opContext = getContextString(op);
            context += EX_BOOL_EXPR + opContext;

            if (i != operand.size() - 1) {
                context += COMMA;
            }
        }

        return context + CLOSE;
    }

    private String resolveExpression(NotOperator expression) {
        List<Expression> operand = ((NotOperator) expression).getOperand();
        String context = getContextString(operand.get(0));

        return EX_CTX_MK_NOT + OPEN + EX_BOOL_EXPR + context + CLOSE;
    }

    private String resolveExpression(ImpliesOperator expression) {
        List<Expression> operand = ((ImpliesOperator) expression).getOperand();
        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_IMPLIES + OPEN + EX_BOOL_EXPR + leftContext + COMMA + EX_BOOL_EXPR + rightContext + CLOSE;
    }

    private String resolveExpression(ObjectCompare expression) {
        List<Expression> operand = expression.getOperand();
        String leftCast = EX_BOOL_EXPR;
        String rightCast = EX_BOOL_EXPR;
        if ((operand.get(0) instanceof ContentValue) || (operand.get(0) instanceof DisplayingContent) || (operand.get(0) instanceof AllocatedContent)
                || (operand.get(0) instanceof OutputtingSound) || (operand.get(0) instanceof GetProperty) || (operand.get(0) instanceof ObjectReference)) {
            leftCast = EX_INT_EXPR;
        }
        if ((operand.get(1) instanceof ContentValue) || (operand.get(1) instanceof DisplayingContent) || (operand.get(1) instanceof AllocatedContent)
                || (operand.get(1) instanceof OutputtingSound) || (operand.get(1) instanceof GetProperty) || (operand.get(1) instanceof ObjectReference)) {
            rightCast = EX_INT_EXPR;
        }

        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_EQ + OPEN + leftCast + leftContext + COMMA + rightCast + rightContext + CLOSE;
    }

    private String resolveExpression(IsLowerThanEqualOperator expression) {
        List<Expression> operand = expression.getOperand();
        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_LE + OPEN + EX_INT_EXPR + leftContext + COMMA + EX_INT_EXPR + rightContext + CLOSE;
    }

    private String resolveExpression(IsLowerThanOperator expression) {
        List<Expression> operand = expression.getOperand();
        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_LT + OPEN + EX_INT_EXPR + leftContext + COMMA + EX_INT_EXPR + rightContext + CLOSE;
    }

    private String resolveExpression(IsGreaterThanEqualOperator expression) {
        List<Expression> operand = expression.getOperand();
        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_GE + OPEN + EX_INT_EXPR + leftContext + COMMA + EX_INT_EXPR + rightContext + CLOSE;
    }

    private String resolveExpression(IsGreaterThanOperator expression) {
        List<Expression> operand = expression.getOperand();
        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_GT + OPEN + EX_INT_EXPR + leftContext + COMMA + EX_INT_EXPR + rightContext + CLOSE;
    }

    private String resolveExpression(IsEqualToOperator expression) {
        List<Expression> operand = expression.getOperand();
        String cast = EX_INT_EXPR;

        String leftContext = getContextString(operand.get(0));
        String rightContext = getContextString(operand.get(1));

        return EX_CTX_MK_EQ + OPEN + cast + leftContext + COMMA + cast + rightContext + CLOSE;
    }

    private String resolveExpression(ExistsOperator expression) {
        return resolveExpression(expression, EX_CTX_MK_EXISTS, "");
    }

    private String resolveExpression(ForAllOperator expression) {
        return resolveExpression(expression, EX_CTX_MK_FORALL, EX_CTX_MK_IMPLIES);
    }

    private String resolveExpression(LambdaContext expression, String mkMethod, String implies) {
        LambdaExpression lambda = expression.getLambda();
        Variable x = lambda.getX();

        EList<Expression> operand = expression.getOperand();

        String size = "";
        String setName = "";
        String variableName = variableNameMap.get(x);
        if (operand.get(0) instanceof ObjectReference) {
            ObjectReference ref = (ObjectReference) operand.get(0);
            if (ref.getUnderlyingType() == ExpressionType.SET_OF_AREA || ref.getUnderlyingType() == ExpressionType.SET_OF_ZONE) {
                size = ALLOCATABLE_SIZE;
            } else if (ref.getUnderlyingType() == ExpressionType.SET_OF_CONTENT || ref.getUnderlyingType() == ExpressionType.SET_OF_SOUND) {
                size = CONTENT_SIZE;
            }

            setName = resolveExpression((ObjectReference) operand.get(0));

        } else if (operand.get(0) instanceof GetAllocatables) {
            size = CONTENT_SIZE;

            GetAllocatables getAllocatable = (GetAllocatables) operand.get(0);
            if (getAllocatable.getOperand().get(0) instanceof PreviousModifier) {
                PreviousModifier pre = (PreviousModifier) getAllocatable.getOperand().get(0);
                String select = resolveExpression(pre.getObjReference());
                setName = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_PRE_ALLOCATABLE + COMMA + select + CLOSE;
            } else {
                String select = resolveExpression((ObjectReference) getAllocatable.getOperand().get(0));
                setName = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_ALLOCATABLE + COMMA + select + CLOSE;
            }

        } else if (operand.get(0) instanceof GetContentsList) {
            size = ALLOCATABLE_SIZE;

            GetContentsList getContentsList = (GetContentsList) operand.get(0);
            if (getContentsList.getOperand().get(0) instanceof PreviousModifier) {
                PreviousModifier pre = (PreviousModifier) getContentsList.getOperand().get(0);
                String select = resolveExpression(pre.getObjReference());
                setName = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_PRE_CONTENTS_LIST + COMMA + select + CLOSE;
            } else {
                String select = resolveExpression((ObjectReference) getContentsList.getOperand().get(0));
                setName = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_CONTENTS_LIST + COMMA + select + CLOSE;
            }

        } else if (operand.get(0) instanceof AllInstanceOfArea) {
            size = ALLOCATABLE_SIZE;
            setName = ALL_AREA_CONST;
        } else if (operand.get(0) instanceof AllInstanceOfZone) {
            size = ALLOCATABLE_SIZE;
            setName = ALL_ZONE_CONST;
        } else if (operand.get(0) instanceof AllInstanceOfViewContent) {
            size = CONTENT_SIZE;
            setName = ALL_VIEWCONTENT_CONST;
        } else if (operand.get(0) instanceof AllInstanceOfSoundContent) {
            size = CONTENT_SIZE;
            setName = ALL_SOUNDCONTENT_CONST;
        } else if (operand.get(0) instanceof SetOfOperator) {
            SetOfOperator setOfOp = (SetOfOperator) operand.get(0);
            if (setOfOp.getUnderlyingType() == ExpressionType.SET_OF_AREA || setOfOp.getUnderlyingType() == ExpressionType.SET_OF_ZONE) {
                size = ALLOCATABLE_SIZE;
            } else if (setOfOp.getUnderlyingType() == ExpressionType.SET_OF_CONTENT || setOfOp.getUnderlyingType() == ExpressionType.SET_OF_SOUND) {
                size = CONTENT_SIZE;
            }

            String expressionText = setOfOp.getExpressionText();
            setName = SetOfNameMap.get(expressionText);
        } else if (operand.get(0) instanceof ActiveContents) {
            size = CONTENT_SIZE;
            ActiveContents activeContents = (ActiveContents) operand.get(0);
            if (activeContents.getOperand().get(0) instanceof PreviousModifier) {
                PreviousModifier pre = (PreviousModifier) activeContents.getOperand().get(0);
                String select = resolveExpression(pre.getObjReference());
                setName = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_PRE_ACTIVE_CONTENTS + COMMA + select + CLOSE;
            } else {
                String select = resolveExpression((ObjectReference) activeContents.getOperand().get(0));
                setName = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_ACTIVE_CONTENTS + COMMA + select + CLOSE;
            }
        }

        String setContext = EX_CTX_MK_SELECT + OPEN + setName + COMMA + variableName + ARRAY_TOP + CLOSE;
        String expContext = getContextString(lambda.getBodyText());
        String rangeContext = EX_CTX_MK_AND + OPEN + EX_CTX_MK_LE + OPEN + EX_CTX_MK_INT + OPEN + 1 + CLOSE + COMMA + EX_ARITH_EXPR + variableName + ARRAY_TOP + CLOSE + COMMA
                + EX_CTX_MK_LE + OPEN + EX_ARITH_EXPR + variableName + ARRAY_TOP + COMMA + size + CLOSE + CLOSE;
        String bodyContext = EX_CTX_MK_AND + OPEN + rangeContext + COMMA + EX_BOOL_EXPR + setContext;

        if (!implies.isEmpty()) {
            bodyContext = implies + OPEN + bodyContext + CLOSE + COMMA + EX_BOOL_EXPR + expContext + CLOSE;
        } else {
            bodyContext += COMMA + EX_BOOL_EXPR + expContext + CLOSE;
        }

        String context = mkMethod + OPEN + variableName + COMMA + bodyContext + COMMA + 1 + COMMA + NULL_STRING + COMMA + NULL_STRING + COMMA + NULL_STRING + COMMA + NULL_STRING
                + CLOSE;
        return context;
    }

    private String resolveExpression(IsDisplayed expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_DISPLAYED + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_DISPLAYED + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsVisible expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_VISIBLE + COMMA + context + CLOSE;
        }

        String context = getContextString(operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_VISIBLE + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsHidden expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_HIDDEN + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_HIDDEN + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsActive expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_ACTIVE + COMMA + context + CLOSE;
        }

        String context = getContextString(operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_ACTIVE + COMMA + context + CLOSE;
    }

    private String resolveExpression(DisplayingContent expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_DISPLAYING_CONTENT + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_DISPLAYING_CONTENT + COMMA + context + CLOSE;
    }

    private String resolveExpression(AllocatedContent expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_ALLOCATED_CONTENT + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_ALLOCATED_CONTENT + COMMA + context + CLOSE;
    }

    private String resolveExpression(ContentValue expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_CONTENT_VALUE + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_CONTENT_VALUE + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsMuted expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_MUTED + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_MUTED + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsOutputted expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_OUTPUTTED + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_OUTPUTTED + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsAttenuated expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_ATTENUATED + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_ATTENUATED + COMMA + context + CLOSE;
    }

    private String resolveExpression(OutputtingSound expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_OUTPUTTING_SOUND + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_OUTPUTTING_SOUND + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsSounding expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_SOUNDING + COMMA + context + CLOSE;
        }

        String context = getContextString(operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_SOUNDING + COMMA + context + CLOSE;
    }

    private String resolveExpression(GetProperty expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return String.format(context, OP_PRE_GET_PROPERTY);
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return String.format(context, OP_GET_PROPERTY);
    }

    private String resolveExpression(IntegerValue expression) {
        int value = expression.getValue();
        return EX_CTX_MK_INT + OPEN + String.valueOf(value) + CLOSE;
    }

    private String resolveExpression(IfStatement expression) {

        Expression condition = expression.getCondition();
        Expression thenExpression = expression.getThenExpression();
        Expression elseExpression = expression.getElseExpression();

        String conditionContext = getContextString(condition);
        String thenContext = getContextString(thenExpression);
        String elseContext = getContextString(elseExpression);

        String context = EX_CTX_MK_ITE + OPEN + EX_BOOL_EXPR + conditionContext + COMMA + thenContext + COMMA + elseContext + CLOSE;
        return context;
    }

    private String resolveExpression(IsOn expression) {
        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            String context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_ON + COMMA + context + CLOSE;
        } else if (!(operand.get(0) instanceof ObjectReference)) {
            String context = getContextString(operand.get(0));
            return EX_CTX_MK_SELECT + OPEN + OP_IS_ON + COMMA + context + CLOSE;
        }

        String context = resolveExpression((ObjectReference) operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_ON + COMMA + context + CLOSE;
    }

    private String resolveExpression(ObjectReference expression) {
        if (expression.getRefObject() instanceof Area) {
            Area target = (Area) expression.getRefObject();
            for (int i = 0; i < allocatableList.size(); i++) {
                Allocatable area = allocatableList.get(i);
                if (target == area) {
                    return EX_CTX_MK_INT + OPEN + (i + 1) + CLOSE;
                }
            }
        } else if (expression.getRefObject() instanceof Zone) {
            Zone target = (Zone) expression.getRefObject();
            for (int i = 0; i < allocatableList.size(); i++) {
                Allocatable allocatable = allocatableList.get(i);
                if (target == allocatable) {
                    return EX_CTX_MK_INT + OPEN + (i + 1) + CLOSE;
                }
            }
        } else if (expression.getRefObject() instanceof ViewContent) {
            Content target = (Content) expression.getRefObject();
            for (int i = 0; i < contentList.size(); i++) {
                Content content = contentList.get(i);
                if (target == content) {
                    return EX_CTX_MK_INT + OPEN + (i + 1) + CLOSE;
                }
            }
        } else if (expression.getRefObject() instanceof SoundContent) {
            Content target = (Content) expression.getRefObject();
            for (int i = 0; i < contentList.size(); i++) {
                Content content = contentList.get(i);
                if (target == content) {
                    return EX_CTX_MK_INT + OPEN + (i + 1) + CLOSE;
                }
            }
        } else if (expression.getRefObject() instanceof Variable) {
            Variable target = (Variable) expression.getRefObject();

            if ((expression.eContainer() instanceof GetAllocatables) || (expression.eContainer() instanceof GetContentsList)) {
                return EX_CTX_MK_INT_CONST + OPEN + EX_CTX_MK_SYMBOL + OPEN + STR_QUOTE + target.getName() + STR_QUOTE + CLOSE + CLOSE;
            }

            return variableNameMap.get(target) + ARRAY_TOP;
        } else if (expression.getRefObject() instanceof AllocatableSet) {
            AllocatableSet target = (AllocatableSet) expression.getRefObject();
            return allocatableSetNameMap.get(target);
        } else if (expression.getRefObject() instanceof ContentSet) {
            ContentSet target = (ContentSet) expression.getRefObject();
            return contentSetNameMap.get(target);
        } else if (expression.getRefObject() instanceof Scene) {
            Scene target = (Scene) expression.getRefObject();
            for (int i = 0; i < sceneList.size(); i++) {
                Scene scene = sceneList.get(i);
                if (target == scene) {
                    return EX_CTX_MK_INT + OPEN + (i + 1) + CLOSE;
                }
            }
        } else if (expression.getRefObject() instanceof IntegerProperty) {
            IntegerProperty target = (IntegerProperty) expression.getRefObject();
            Scene parent = (Scene) target.eContainer();

            for (int i = 0; i < sceneList.size(); i++) {
                Scene scene = sceneList.get(i);
                if (parent == scene) {
                    int propIndex = scene.getProperties().indexOf(target) + 1;

                    String propContext = EX_CTX_MK_SELECT + OPEN + "%s" + COMMA + EX_CTX_MK_INT + OPEN + propIndex + CLOSE + CLOSE;
                    return EX_CTX_MK_SELECT + OPEN + EX_ARRAY_EXPR_CATS + propContext + COMMA + EX_CTX_MK_INT + OPEN + (i + 1) + CLOSE + CLOSE;
                }
            }
        }
        return null;
    }

    private String resolveExpression(PreviousModifier expression) {
        String context = resolveExpression(expression.getObjReference());
        return context;
    }

    private String resolveExpression(IsDisappeared expression) {
        EList<Expression> operand = expression.getOperand();

        String context = "";
        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_DISAPPEARED + COMMA + context + CLOSE;
        }

        context = getContextString(operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_DISAPPEARED + COMMA + context + CLOSE;
    }

    private String resolveExpression(IsCanceled expression) {
        EList<Expression> operand = expression.getOperand();

        String context = "";
        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_IS_CANCELED + COMMA + context + CLOSE;
        }

        context = getContextString(operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_IS_CANCELED + COMMA + context + CLOSE;
    }

    private String resolveExpression(HasBeenDisplayed expression) {
        EList<Expression> operand = expression.getOperand();

        String context = "";
        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            context = resolveExpression(pre.getObjReference());
            return EX_CTX_MK_SELECT + OPEN + OP_PRE_HAS_BEEN_DISPLAYED + COMMA + context + CLOSE;
        }

        context = getContextString(operand.get(0));
        return EX_CTX_MK_SELECT + OPEN + OP_HAS_BEEN_DISPLAYED + COMMA + context + CLOSE;
    }    
    private String resolveExpression(HasComeEarlierThan expression) {
        String context = "";
        String leftContext = "";
        String rightContext = "";

        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            leftContext = resolveExpression(pre.getObjReference());
        } else {
            leftContext = getContextString(operand.get(0));
        }

        if (operand.get(1) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(1);
            rightContext = resolveExpression(pre.getObjReference());
        } else {
            rightContext = getContextString(operand.get(1));
        }

        String context1 = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_HAS_COME_EARLIER_THAN + COMMA + leftContext + CLOSE;
        context = EX_CTX_MK_SELECT + OPEN + context1 + COMMA + rightContext + CLOSE;

        return context;
    }

    private String resolveExpression(HasComeLaterThan expression) {
        String context = "";
        String leftContext = "";
        String rightContext = "";

        EList<Expression> operand = expression.getOperand();

        if (operand.get(0) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(0);
            leftContext = resolveExpression(pre.getObjReference());
        } else {
            leftContext = getContextString(operand.get(0));
        }

        if (operand.get(1) instanceof PreviousModifier) {
            PreviousModifier pre = (PreviousModifier) operand.get(1);
            rightContext = resolveExpression(pre.getObjReference());
        } else {
            rightContext = getContextString(operand.get(1));
        }

        String context1 = EX_ARRAY_EXPR_CATS + EX_CTX_MK_SELECT + OPEN + OP_HAS_COME_LATER_THAN + COMMA + leftContext + CLOSE;
        context = EX_CTX_MK_SELECT + OPEN + context1 + COMMA + rightContext + CLOSE;

        return context;
    }

    class ImpliseConstraintInfo {
        int index;

        AbstractConstraint constraint;

        String methodName;

        public int getIndex() {
            return index;
        }

        public String getMethodName() {
            return methodName;
        }

        public boolean isRuntime() {
            if (constraint instanceof Constraint) {
                return ((Constraint) constraint).isRuntime();
            }
            return true;
        }

        public void setMethodName(String methodName) {
            this.methodName = methodName;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public AbstractConstraint getConstraint() {
            return constraint;
        }

        public void setConstraint(AbstractConstraint constraint) {
            this.constraint = constraint;
        }
    }
}