1 package org.andromda.translation.ocl.validation;
2
3 import java.io.InputStream;
4 import java.net.URL;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Properties;
10 import java.util.Stack;
11 import org.andromda.core.engine.ModelProcessorException;
12 import org.andromda.core.translation.TranslationUtils;
13 import org.andromda.metafacades.uml.ModelElementFacade;
14 import org.andromda.translation.ocl.BaseTranslator;
15 import org.andromda.translation.ocl.node.*;
16 import org.andromda.translation.ocl.syntax.ConcreteSyntaxUtils;
17 import org.andromda.translation.ocl.syntax.OCLFeatures;
18 import org.andromda.translation.ocl.syntax.OCLPatterns;
19 import org.apache.commons.lang.StringUtils;
20
21
22
23
24
25
26
27
28 public class ValidationJavaTranslator
29 extends BaseTranslator
30 {
31 private static Properties features = null;
32
33 static
34 {
35 try
36 {
37 URL featuresUri = ValidationJavaTranslator.class.getResource("features.properties");
38 if (featuresUri == null)
39 {
40 throw new ModelProcessorException("Could not load file --> '" + featuresUri + '\'');
41 }
42 features = new Properties();
43 InputStream stream = featuresUri.openStream();
44 features.load(stream);
45 stream.close();
46 stream = null;
47 }
48 catch (final Throwable throwable)
49 {
50 throw new ValidationTranslatorException(throwable);
51 }
52 }
53
54
55
56
57 private static final String OCL_TRANSLATOR_PACKAGE = "org.andromda.translation.ocl.validation";
58
59
60
61
62
63 public void caseAContextDeclaration(AContextDeclaration node)
64 {
65 newTranslationLayer();
66 {
67 Object[] temp = node.getContextDeclaration().toArray();
68 for (int ctr = 0; ctr < temp.length; ctr++)
69 {
70 ((PContextDeclaration) temp[ctr]).apply(this);
71 }
72 }
73 mergeTranslationLayers();
74 this.getExpression().appendToTranslatedExpression(translationLayers.peek());
75 translationLayers.clear();
76 }
77
78
79
80
81 public void caseAClassifierContextDeclaration(AClassifierContextDeclaration node)
82 {
83
84
85 super.inAClassifierContextDeclaration(node);
86 Object[] temp = node.getClassifierExpressionBody().toArray();
87 for (int ctr = 0; ctr < temp.length; ctr++)
88 ((PClassifierExpressionBody) temp[ctr]).apply(this);
89 }
90
91
92
93
94 public void caseAOperationContextDeclaration(AOperationContextDeclaration node)
95 {
96
97
98 super.inAOperationContextDeclaration(node);
99 Object[] temp = node.getOperationExpressionBody().toArray();
100 for (int ctr = 0; ctr < temp.length; ctr++)
101 ((POperationExpressionBody) temp[ctr]).apply(this);
102 }
103
104
105
106
107 public void caseAAttributeOrAssociationContextDeclaration(AAttributeOrAssociationContextDeclaration node)
108 {
109 super.inAAttributeOrAssociationContextDeclaration(node);
110 Object[] temp = node.getAttributeOrAssociationExpressionBody().toArray();
111 for (int ctr = 0; ctr < temp.length; ctr++)
112 ((PAttributeOrAssociationExpressionBody) temp[ctr]).apply(this);
113 }
114
115
116
117
118 public void caseAInvClassifierExpressionBody(AInvClassifierExpressionBody node)
119 {
120
121
122 super.inAInvClassifierExpressionBody(node);
123 node.getExpression().apply(this);
124 }
125
126
127
128
129 public void caseADefClassifierExpressionBody(ADefClassifierExpressionBody node)
130 {
131
132
133 super.inADefClassifierExpressionBody(node);
134 node.getDefinitionExpression().apply(this);
135 }
136
137
138
139
140
141
142
143 public void inAArrowPropertyCallExpressionTail(AArrowPropertyCallExpressionTail node)
144 {
145 this.arrowPropertyCallStack.push(Boolean.TRUE);
146 }
147
148
149
150
151
152 public void outAArrowPropertyCallExpressionTail(AArrowPropertyCallExpressionTail node)
153 {
154 this.arrowPropertyCallStack.pop();
155 }
156
157
158
159
160
161
162 public void inADotPropertyCallExpressionTail(ADotPropertyCallExpressionTail node)
163 {
164 this.arrowPropertyCallStack.push(Boolean.FALSE);
165 }
166
167
168
169
170
171 public void outADotPropertyCallExpressionTail(ADotPropertyCallExpressionTail node)
172 {
173 this.arrowPropertyCallStack.pop();
174 }
175
176
177
178
179
180
181
182 public void caseALetVariableDeclaration(ALetVariableDeclaration node)
183 {
184 inALetVariableDeclaration(node);
185 if (node.getVariableDeclaration() != null)
186 {
187 node.getVariableDeclaration().apply(this);
188 }
189 if (node.getEqual() != null)
190 {
191 write("=");
192 }
193 if (node.getExpression() != null)
194 {
195 node.getExpression().apply(this);
196 }
197 outALetVariableDeclaration(node);
198 }
199
200
201
202
203
204 public void inALetVariableDeclaration(ALetVariableDeclaration node)
205 {
206 newTranslationLayer();
207
208
209 AVariableDeclaration variableDeclaration = (AVariableDeclaration) node.getVariableDeclaration();
210 String variableName = variableDeclaration.getName().getText();
211
212 newTranslationLayer();
213 node.getExpression().apply(this);
214
215 String variableValue = translationLayers.pop().toString();
216
217 addLetVariableToContext(variableName, variableValue);
218 }
219
220
221
222
223
224 public void outALetVariableDeclaration(ALetVariableDeclaration node)
225 {
226 write(";");
227 translationLayers.pop();
228 }
229
230
231
232
233
234 public void caseAVariableDeclaration(AVariableDeclaration node)
235 {
236 if (node.getTypeDeclaration() == null)
237 write("Object");
238 else
239 node.getTypeDeclaration().apply(this);
240
241 write(" ");
242
243 node.getName().apply(this);
244 }
245
246
247
248
249 public void caseATypeDeclaration(ATypeDeclaration node)
250 {
251 node.getType().apply(this);
252 }
253
254
255
256
257 public void caseAVariableDeclarationList(AVariableDeclarationList node)
258 {
259 node.getVariableDeclaration().apply(this);
260
261 if (node.getVariableDeclarationValue() != null)
262 node.getVariableDeclarationValue().apply(this);
263
264 Object[] temp = node.getVariableDeclarationListTail().toArray();
265 for (int ctr = 0; ctr < temp.length; ctr++)
266 ((PVariableDeclarationListTail) temp[ctr]).apply(this);
267 }
268
269
270
271
272 public void caseAVariableDeclarationListTail(AVariableDeclarationListTail node)
273 {
274 node.getComma().apply(this);
275 node.getVariableDeclaration().apply(this);
276
277 if (node.getVariableDeclarationValue() != null)
278 node.getVariableDeclarationValue().apply(this);
279 }
280
281
282
283
284 public void caseAEqualExpression(AEqualExpression node)
285 {
286 node.getEqual().apply(this);
287 node.getExpression().apply(this);
288 }
289
290
291
292
293 public void caseABodyOperationStereotype(ABodyOperationStereotype node)
294 {
295 }
296
297
298
299
300 public void caseAPreOperationStereotype(APreOperationStereotype node)
301 {
302 }
303
304
305
306
307 public void caseAPostOperationStereotype(APostOperationStereotype node)
308 {
309 }
310
311
312
313
314 public void caseAMessageExpression(AMessageExpression node)
315 {
316 }
317
318
319
320
321 public void caseAIfExpression(AIfExpression node)
322 {
323 node.getIf().apply(this);
324
325 write("(");
326 node.getIfBranch().apply(this);
327 write(")");
328
329 node.getThen().apply(this);
330
331 write("{");
332 node.getThenBranch().apply(this);
333 write(";");
334 write("}");
335
336 node.getElse().apply(this);
337
338 write("{");
339 node.getElseBranch().apply(this);
340 write(";");
341 write("}");
342 }
343
344
345
346
347 public void caseAPropertyCallExpression(APropertyCallExpression node)
348 {
349 newTranslationLayer();
350 node.getPrimaryExpression().apply(this);
351 Object[] temp = node.getPropertyCallExpressionTail().toArray();
352 for (int ctr = 0; ctr < temp.length; ctr++)
353 ((PPropertyCallExpressionTail) temp[ctr]).apply(this);
354 mergeTranslationLayerAfter();
355 }
356
357
358
359
360 public void caseADotPropertyCallExpressionTail(ADotPropertyCallExpressionTail node)
361 {
362 inADotPropertyCallExpressionTail(node);
363 String expression = TranslationUtils.trimToEmpty(node);
364
365
366 if (OCLPatterns.isOperation(expression))
367 {
368 AFeatureCall featureCall = (AFeatureCall) node.getFeatureCall();
369 String featureCallExpression = TranslationUtils.trimToEmpty(node.getFeatureCall());
370 if (OCLFeatures.isOclIsKindOf(featureCallExpression))
371 {
372 this.handleOclIsKindOf(featureCall);
373 } else if (OCLFeatures.isOclIsTypeOf(featureCallExpression))
374 {
375 this.handleOclIsTypeOf(featureCall);
376 } else if (OCLFeatures.isConcat(featureCallExpression))
377 {
378 this.handleConcat(featureCall);
379 } else
380 {
381 this.handleDotFeatureCall(featureCall);
382 }
383 }
384 outADotPropertyCallExpressionTail(node);
385 }
386
387
388
389
390 private void handleOclIsKindOf(Object node)
391 {
392 String type = this.getParametersAsType(node);
393 if (type != null)
394 {
395 write(" instanceof ");
396 write(type);
397 }
398 }
399
400
401
402
403 private void handleOclIsTypeOf(Object node)
404 {
405 String type = this.getParametersAsType(node);
406 if (type != null)
407 {
408 write(".getClass().getName().equals(");
409 write(type);
410 write(".class.getName())");
411 }
412 }
413
414
415
416
417
418
419
420
421 private String getParametersAsType(Object node)
422 {
423 String type = null;
424 if (node instanceof AFeatureCall)
425 {
426 type = ConcreteSyntaxUtils.getParametersAsString((AFeatureCall) node);
427 } else if (node instanceof AFeatureCallParameters)
428 {
429 type = ConcreteSyntaxUtils.getParametersAsString((AFeatureCallParameters) node);
430 }
431 if (type != null)
432 {
433 type = type.replaceAll("\\s*::\\s*", ".");
434
435
436
437 if (type.indexOf('.') == -1)
438 {
439 if (this.getModelElement() != null)
440 {
441 type = this.getModelElement().getPackageName() + '.' + type;
442 }
443 }
444 }
445 return type;
446 }
447
448
449
450
451 private void handleConcat(AFeatureCall featureCall)
452 {
453 write(" + \"\" + ");
454 write(OCL_INTROSPECTOR_INVOKE_PREFIX);
455 write(CONTEXT_ELEMENT_NAME);
456 write(",\"");
457 write(ConcreteSyntaxUtils.getParametersAsString(featureCall).replaceAll("\\s*", ""));
458 write("\")");
459 }
460
461
462
463
464
465
466
467
468 public void handleDotFeatureCall(AFeatureCall featureCall)
469 {
470 this.prependToTranslationLayer(OCL_INTROSPECTOR_INVOKE_PREFIX);
471 this.appendToTranslationLayer(",\"");
472 this.appendToTranslationLayer(TranslationUtils.deleteWhitespace(featureCall));
473 this.appendToTranslationLayer("\"");
474 if (featureCall.getFeatureCallParameters() != null)
475 {
476 List parameters = ConcreteSyntaxUtils.getParameters(featureCall);
477 if (parameters != null && !parameters.isEmpty())
478 {
479 write(",new Object[]{");
480 this.appendToTranslationLayer(OCL_INTROSPECTOR_INVOKE_PREFIX);
481 this.appendToTranslationLayer(CONTEXT_ELEMENT_NAME);
482 this.appendToTranslationLayer(",\"");
483 this.appendToTranslationLayer(ConcreteSyntaxUtils.getParameters(featureCall).get(0));
484 this.appendToTranslationLayer("\")}");
485 }
486 }
487 this.appendToTranslationLayer(")");
488 }
489
490
491
492
493 public void caseAArrowPropertyCallExpressionTail(AArrowPropertyCallExpressionTail node)
494 {
495 inAArrowPropertyCallExpressionTail(node);
496 node.getArrow().apply(this);
497 this.handleArrowFeatureCall((AFeatureCall) node.getFeatureCall());
498 outAArrowPropertyCallExpressionTail(node);
499 }
500
501
502
503
504 protected boolean isOperationArgument(String argument)
505 {
506 return super.isOperationArgument(this.getRootName(argument));
507 }
508
509
510
511
512
513
514
515
516 private String getRootName(String navigationalPath)
517 {
518 return StringUtils.trimToEmpty(navigationalPath).replaceAll("\\..*", "");
519 }
520
521
522
523
524
525
526
527 private String getPathTail(String navigationalPath)
528 {
529 final int dotIndex = navigationalPath.indexOf('.');
530 return dotIndex != -1 ? navigationalPath.substring(dotIndex + 1, navigationalPath.length()) : navigationalPath;
531 }
532
533
534
535
536
537 public void caseAFeaturePrimaryExpression(AFeaturePrimaryExpression node)
538 {
539 inAFeaturePrimaryExpression(node);
540 if (node.getPathName() != null)
541 {
542 final String variableName = ((APathName) node.getPathName()).getName().getText();
543 final String variableValue = getDeclaredLetVariableValue(variableName);
544 final boolean isDeclaredAsLetVariable = (variableValue != null);
545 String featureExpression = TranslationUtils.deleteWhitespace(node);
546 if (isDeclaredAsLetVariable)
547 {
548 write(variableValue);
549 } else if (node.getFeatureCallParameters() == null || OCLPatterns.isOperation(featureExpression))
550 {
551 APropertyCallExpression expression = (APropertyCallExpression) node.parent();
552 String expressionAsString = ConcreteSyntaxUtils.getPrimaryExpression(expression);
553
554 expressionAsString = expressionAsString.replaceAll("self\\.", "");
555 if (OCLFeatures.isSelf(expressionAsString))
556 {
557 write(CONTEXT_ELEMENT_NAME);
558 } else if (StringUtils.isNotBlank(expressionAsString))
559 {
560 boolean convertToBoolean = false;
561 if (node.parent().parent() instanceof AUnaryExpression)
562 {
563 AUnaryExpression unaryExpression = (AUnaryExpression) node.parent().parent();
564
565 convertToBoolean = unaryExpression.getUnaryOperator() instanceof ANotUnaryOperator;
566 if (convertToBoolean)
567 {
568 this.write(BOOLEAN_WRAP_PREFIX);
569 }
570 }
571 if (OCLFeatures.isOclIsKindOf(expressionAsString))
572 {
573 this.write("object");
574 this.handleOclIsKindOf(node.getFeatureCallParameters());
575 } else if (OCLFeatures.isOclIsTypeOf(expressionAsString))
576 {
577 this.write("object");
578 this.handleOclIsTypeOf(node.getFeatureCallParameters());
579 } else
580 {
581
582 boolean introspectorCall = true;
583 String invokedObject = CONTEXT_ELEMENT_NAME;
584
585
586 if (this.arrowPropertyCallStack.peek().equals(Boolean.TRUE))
587 {
588 invokedObject = "object";
589 }
590 if (this.isOperationArgument(expressionAsString))
591 {
592
593
594 invokedObject = this.getRootName(expressionAsString);
595 expressionAsString = this.getPathTail(expressionAsString);
596 introspectorCall = !invokedObject.equals(expressionAsString);
597 }
598 if (introspectorCall)
599 {
600 write(OCL_INTROSPECTOR_INVOKE_PREFIX);
601 }
602 write(invokedObject);
603 if (introspectorCall)
604 {
605 write(",\"");
606 write(expressionAsString);
607 }
608 if (introspectorCall)
609 {
610 write("\")");
611 }
612 if (convertToBoolean)
613 {
614 this.write(BOOLEAN_WRAP_SUFFIX);
615 }
616 }
617 if (this.requiresBooleanConversion)
618 {
619 this.write(BOOLEAN_WRAP_SUFFIX);
620 this.requiresBooleanConversion = false;
621 }
622 }
623 } else
624 {
625 node.getPathName().apply(this);
626 }
627 }
628 if (node.getIsMarkedPre() != null)
629 {
630 node.getIsMarkedPre().apply(this);
631 }
632 if (node.getQualifiers() != null)
633 {
634
635
636 if (this.arrowPropertyCallStack.peek().equals(Boolean.FALSE))
637 {
638 node.getQualifiers().apply(this);
639 }
640 }
641 outAFeaturePrimaryExpression(node);
642 }
643
644
645
646
647
648
649
650
651 public void handleArrowFeatureCall(AFeatureCall featureCall)
652 {
653 AFeatureCallParameters params = (AFeatureCallParameters) featureCall.getFeatureCallParameters();
654 AActualParameterList list = null;
655 if (params != null)
656 {
657 list = (AActualParameterList) params.getActualParameterList();
658 }
659 boolean arrow = this.arrowPropertyCallStack.peek().equals(Boolean.TRUE) &&
660 StringUtils.isNotBlank(String.valueOf(list));
661 {
662 newTranslationLayer();
663 final String navigationalPath = ConcreteSyntaxUtils.getArrowFeatureCallResultNavigationalPath(
664 (APropertyCallExpression) featureCall.parent().parent());
665
666
667
668
669 boolean resultNavigationalPath = StringUtils.isNotBlank(navigationalPath);
670 if (resultNavigationalPath)
671 {
672 write(OCL_INTROSPECTOR_INVOKE_PREFIX);
673 }
674 write(OCL_TRANSLATOR_PACKAGE);
675 write(".OCLCollections.");
676 inAFeatureCall(featureCall);
677 if (featureCall.getPathName() != null)
678 {
679 featureCall.getPathName().apply(this);
680 }
681 String featureCallName = TranslationUtils.trimToEmpty(featureCall.getPathName());
682 AFeatureCallParameters parameters = (AFeatureCallParameters) featureCall.getFeatureCallParameters();
683 if (parameters != null)
684 {
685 if (parameters.getLParen() != null)
686 {
687 parameters.getLParen().apply(this);
688 }
689 mergeTranslationLayerBefore();
690 AActualParameterList parameterList = (AActualParameterList) parameters.getActualParameterList();
691 if (parameterList != null)
692 {
693 List expressions = parameterList.getCommaExpression();
694
695 if (parameterList.getExpression() != null)
696 {
697 if (arrow)
698 {
699 write(",");
700 write(features.getProperty(featureCallName));
701 write(" ");
702 if (OCLPredicateFeatures.isPredicateFeature(featureCallName))
703 {
704 write(BOOLEAN_WRAP_PREFIX);
705 }
706 }
707 parameterList.getExpression().apply(this);
708 }
709 for (int ctr = 0; ctr < expressions.size(); ctr++)
710 {
711 Node expression = (Node) expressions.get(ctr);
712 if (expression != null)
713 {
714 write(",");
715 expression.apply(this);
716 }
717 }
718 if (parameterList.getExpression() != null)
719 {
720 if (OCLPredicateFeatures.isPredicateFeature(featureCallName))
721 {
722 write(BOOLEAN_WRAP_SUFFIX);
723 }
724 if (arrow)
725 write(";}}");
726 }
727 }
728 if (parameters.getRParen() != null)
729 {
730 parameters.getRParen().apply(this);
731 }
732 }
733
734
735
736 if (resultNavigationalPath)
737 {
738 write(",\"");
739 write(navigationalPath);
740 write("\"");
741 write(")");
742 }
743 this.outAFeatureCall(featureCall);
744 }
745 }
746
747
748
749
750 public void caseALetExp(ALetExp node)
751 {
752 inALetExp(node);
753 if (node.getLet() != null)
754 {
755 node.getLet().apply(this);
756 }
757 if (node.getLetVariableDeclaration() != null)
758 {
759 node.getLetVariableDeclaration().apply(this);
760 }
761 if (node.getLetExpSub() != null)
762 {
763 node.getLetExpSub().apply(this);
764 }
765 outALetExp(node);
766 }
767
768
769
770
771
772 public void inALetExp(ALetExp node)
773 {
774 newLetVariableContext();
775 }
776
777
778
779
780
781 public void outALetExp(ALetExp node)
782 {
783 dropLetVariableContext();
784 }
785
786
787
788
789 public void caseAVariableDeclarationLetExpSub(AVariableDeclarationLetExpSub node)
790 {
791 node.getComma().apply(this);
792 node.getLetVariableDeclaration().apply(this);
793 node.getLetExpSub().apply(this);
794 }
795
796
797
798
799 public void caseALogicalExp(ALogicalExp node)
800 {
801 newTranslationLayer();
802 if (node.getRelationalExpression() != null)
803 {
804 node.getRelationalExpression().apply(this);
805 }
806 Object[] tails = node.getLogicalExpressionTail().toArray();
807 for (int ctr = 0; ctr < tails.length; ctr++)
808 {
809 ((ALogicalExpressionTail) tails[ctr]).apply(this);
810 }
811 mergeTranslationLayerAfter();
812 }
813
814
815
816
817 public void caseALogicalExpressionTail(ALogicalExpressionTail node)
818 {
819 node.getLogicalOperator().apply(this);
820 if (node.getLogicalOperator() instanceof AImpliesLogicalOperator)
821 {
822 prependToTranslationLayer("(");
823 }
824 if (node.getRelationalExpression() != null)
825 {
826 node.getRelationalExpression().apply(this);
827 }
828 if (node.getLogicalOperator() instanceof AImpliesLogicalOperator)
829 {
830 write(":true)");
831 }
832 }
833
834
835
836
837 public void caseARelationalExpressionTail(ARelationalExpressionTail node)
838 {
839 inARelationalExpressionTail(node);
840
841 newTranslationLayer();
842 write(OCL_TRANSLATOR_PACKAGE);
843 write(".OCLExpressions.");
844 node.getRelationalOperator().apply(this);
845 write("(");
846 mergeTranslationLayerBefore();
847 if (node.getAdditiveExpression() != null)
848 {
849 write(",");
850 node.getAdditiveExpression().apply(this);
851 }
852 write(")");
853 outARelationalExpressionTail(node);
854 }
855
856
857
858
859 private boolean requiresBooleanConversion = false;
860
861
862
863
864 public void inARelationalExpression(ARelationalExpression node)
865 {
866
867
868
869 if (node.getRelationalExpressionTail() == null)
870 {
871 Object parent = node.parent();
872 Object expression = null;
873 if (parent instanceof ALogicalExp)
874 {
875 List tails = ((ALogicalExp) parent).getLogicalExpressionTail();
876 if (tails != null && !tails.isEmpty())
877 {
878 expression = tails.get(0);
879
880
881 if (OCLPatterns.isAndOrOrExpression(expression))
882 {
883 expression = node;
884 }
885 }
886 } else if (parent instanceof ALogicalExpressionTail)
887 {
888 expression = node;
889 }
890 requiresBooleanConversion = expression != null
891 && OCLPatterns.isNavigationalPath(expression)
892 && !OCLPatterns.isOperation(node);
893 if (this.requiresBooleanConversion)
894 {
895 this.write(BOOLEAN_WRAP_PREFIX);
896 }
897 }
898 newTranslationLayer();
899 }
900
901
902
903
904 public void outARelationalExpression(ARelationalExpression node)
905 {
906 mergeTranslationLayerAfter();
907 }
908
909
910
911
912 public void caseTName(TName node)
913 {
914 write(node.getText());
915 }
916
917
918
919
920 public void caseTAnd(TAnd tAnd)
921 {
922 write("&&");
923 }
924
925
926
927
928 public void caseTOr(TOr tOr)
929 {
930 write("||");
931 }
932
933
934
935
936 public void caseTXor(TXor tXor)
937 {
938 write("^");
939 }
940
941
942
943
944 public void caseTImplies(TImplies tImplies)
945 {
946
947 this.prependToTranslationLayer(BOOLEAN_WRAP_PREFIX);
948 this.appendToTranslationLayer(BOOLEAN_WRAP_SUFFIX);
949 write("?");
950 }
951
952
953
954
955 public void caseTNot(TNot tNot)
956 {
957 write("!");
958 }
959
960
961
962
963 public void caseTPlus(TPlus tPlus)
964 {
965 write("+");
966 }
967
968
969
970
971 public void caseTMinus(TMinus tMinus)
972 {
973 write("-");
974 }
975
976
977
978
979 public void caseTMult(TMult tMult)
980 {
981 write("*");
982 }
983
984
985
986
987 public void caseTDiv(TDiv tDiv)
988 {
989 write("/");
990 }
991
992
993
994
995 public void caseTEqual(TEqual tEqual)
996 {
997 write("equal");
998 }
999
1000
1001
1002
1003 public void caseTNotEqual(TNotEqual tNotEqual)
1004 {
1005 write("notEqual");
1006 }
1007
1008
1009
1010
1011 public void caseTLt(TLt tLt)
1012 {
1013 write("less");
1014 }
1015
1016
1017
1018
1019 public void caseTLteq(TLteq tLteq)
1020 {
1021 write("lessOrEqual");
1022 }
1023
1024
1025
1026
1027 public void caseTGt(TGt tGt)
1028 {
1029 write("greater");
1030 }
1031
1032
1033
1034
1035 public void caseTGteq(TGteq tGteq)
1036 {
1037 write("greaterOrEqual");
1038 }
1039
1040
1041
1042
1043 public void caseTInv(TInv tInv)
1044 {
1045 }
1046
1047
1048
1049
1050 public void caseTDef(TDef tDef)
1051 {
1052 }
1053
1054
1055
1056
1057 public void caseTLet(TLet tLet)
1058 {
1059 }
1060
1061
1062
1063
1064 public void caseTColon(TColon tColon)
1065 {
1066 }
1067
1068
1069
1070
1071 public void caseTLBrace(TLBrace tlBrace)
1072 {
1073 write("{");
1074 }
1075
1076
1077
1078
1079 public void caseTLBracket(TLBracket tlBracket)
1080 {
1081 write("[");
1082 }
1083
1084
1085
1086
1087 public void caseTLParen(TLParen tlParen)
1088 {
1089 write("(");
1090 }
1091
1092
1093
1094
1095 public void caseTRBrace(TRBrace trBrace)
1096 {
1097 write("}");
1098 }
1099
1100
1101
1102
1103 public void caseTRBracket(TRBracket trBracket)
1104 {
1105 write("]");
1106 }
1107
1108
1109
1110
1111 public void caseTRParen(TRParen trParen)
1112 {
1113 write(")");
1114 }
1115
1116
1117
1118
1119 public void caseTContext(TContext tContext)
1120 {
1121 }
1122
1123
1124
1125
1126 public void caseTBoolean(TBoolean tBoolean)
1127 {
1128 write(tBoolean.getText());
1129 }
1130
1131
1132
1133
1134 public void caseTApostrophe(TApostrophe tApostrophe)
1135 {
1136 write("\'");
1137 }
1138
1139
1140
1141
1142 public void caseTBlank(TBlank tBlank)
1143 {
1144 write(" ");
1145 }
1146
1147
1148
1149
1150 public void caseTCollection(TCollection tCollection)
1151 {
1152 write("java.util.Collection ");
1153 }
1154
1155
1156
1157
1158 public void caseTComment(TSingleLineComment tSingleLineComment)
1159 {
1160 write("// ");
1161 }
1162
1163
1164
1165
1166 public void caseTEndif(TEndif tEndif)
1167 {
1168 }
1169
1170
1171
1172
1173 public void caseTAttr(TAttr tAttr)
1174 {
1175 }
1176
1177
1178
1179
1180 public void caseTBag(TBag tBag)
1181 {
1182 }
1183
1184
1185
1186
1187 public void caseTBar(TBar tBar)
1188 {
1189 }
1190
1191
1192
1193
1194 public void caseTBody(TBody tBody)
1195 {
1196 }
1197
1198
1199
1200
1201 public void caseTCommercialAt(TCommercialAt tCommercialAt)
1202 {
1203 }
1204
1205
1206
1207
1208 public void caseTDerive(TDerive tDerive)
1209 {
1210 }
1211
1212
1213
1214
1215 public void caseTEndpackage(TEndpackage tEndpackage)
1216 {
1217 }
1218
1219
1220
1221
1222 public void caseTEnum(TEnum tEnum)
1223 {
1224 }
1225
1226
1227
1228
1229 public void caseTIn(TIn tIn)
1230 {
1231 }
1232
1233
1234
1235
1236 public void caseTInit(TInit tInit)
1237 {
1238 }
1239
1240
1241
1242
1243 public void caseTInt(TInt tInt)
1244 {
1245 write(tInt.getText());
1246 }
1247
1248
1249
1250
1251 public void caseTIsSentOperator(TIsSentOperator tIsSentOperator)
1252 {
1253 }
1254
1255
1256
1257
1258 public void caseTMessageOperator(TMessageOperator tMessageOperator)
1259 {
1260 }
1261
1262
1263
1264
1265 public void caseTNewLine(TNewLine tNewLine)
1266 {
1267 }
1268
1269
1270
1271
1272 public void caseTOper(TOper tOper)
1273 {
1274 }
1275
1276
1277
1278
1279 public void caseTOrderedset(TOrderedset tOrderedset)
1280 {
1281 }
1282
1283
1284
1285
1286 public void caseTPackage(TPackage tPackage)
1287 {
1288 }
1289
1290
1291
1292
1293 public void caseTPost(TPost tPost)
1294 {
1295 }
1296
1297
1298
1299
1300 public void caseTPre(TPre tPre)
1301 {
1302 }
1303
1304
1305
1306
1307 public void caseTArrow(TArrow tArrow)
1308 {
1309 }
1310
1311
1312
1313
1314 public void caseTIf(TIf tIf)
1315 {
1316 write("if");
1317 }
1318
1319
1320
1321
1322 public void caseTElse(TElse tElse)
1323 {
1324 write("else");
1325 }
1326
1327
1328
1329
1330 public void caseTThen(TThen tThen)
1331 {
1332 }
1333
1334
1335
1336
1337 public void caseTRange(TRange tRange)
1338 {
1339 }
1340
1341
1342
1343
1344 public void caseTReal(TReal tReal)
1345 {
1346 write(tReal.getText());
1347 }
1348
1349
1350
1351
1352 public void caseTComma(TComma tComma)
1353 {
1354 write(", ");
1355 }
1356
1357
1358
1359
1360 public void caseTDot(TDot tDot)
1361 {
1362 write(".");
1363 }
1364
1365
1366
1367
1368 public void caseTSemicolon(TSemicolon tSemicolon)
1369 {
1370 }
1371
1372
1373
1374
1375 public void caseTUnknown(TUnknown tUnknown)
1376 {
1377 }
1378
1379
1380
1381
1382 public void caseTScopeOperator(TScopeOperator tScopeOperator)
1383 {
1384 write(".");
1385 }
1386
1387
1388
1389
1390 public void caseTSequence(TSequence tSequence)
1391 {
1392 }
1393
1394
1395
1396
1397 public void caseTSet(TSet tSet)
1398 {
1399 }
1400
1401
1402
1403
1404
1405 public void caseTStringLit(TStringLit tStringLit)
1406 {
1407 final StringBuffer buffer = new StringBuffer(tStringLit.getText().replace('\'', '\"'));
1408 write(buffer);
1409 }
1410
1411
1412
1413
1414 public void caseTTab(TTab tTab)
1415 {
1416 }
1417
1418
1419
1420
1421 public void caseTTuple(TTuple tTuple)
1422 {
1423 }
1424
1425
1426
1427
1428 public void caseTTupletype(TTupletype tTupletype)
1429 {
1430 }
1431
1432 private final Stack translationLayers = new Stack();
1433
1434
1435
1436
1437
1438 private final Stack arrowPropertyCallStack = new Stack();
1439
1440
1441
1442
1443
1444
1445
1446 private final Stack letVariableStack = new Stack();
1447
1448 private void write(Object object)
1449 {
1450 appendToTranslationLayer(String.valueOf(object));
1451 }
1452
1453 private StringBuffer newTranslationLayer()
1454 {
1455 return (StringBuffer) translationLayers.push(new StringBuffer());
1456 }
1457
1458 private StringBuffer appendToTranslationLayer(Object appendix)
1459 {
1460 return ((StringBuffer) translationLayers.peek()).append(appendix);
1461 }
1462
1463 private StringBuffer prependToTranslationLayer(Object appendix)
1464 {
1465 return ((StringBuffer) translationLayers.peek()).insert(0, appendix);
1466 }
1467
1468 private StringBuffer mergeTranslationLayerAfter()
1469 {
1470 StringBuffer newTop = null;
1471
1472 if (translationLayers.size() > 1)
1473 {
1474 newTop = appendToTranslationLayer(translationLayers.pop());
1475 }
1476
1477 return newTop;
1478 }
1479
1480 private StringBuffer mergeTranslationLayerBefore()
1481 {
1482 StringBuffer newTop = null;
1483
1484 if (translationLayers.size() > 1)
1485 {
1486 newTop = prependToTranslationLayer(translationLayers.pop());
1487 }
1488
1489 return newTop;
1490 }
1491
1492 private StringBuffer mergeTranslationLayers()
1493 {
1494 while (mergeTranslationLayerAfter() != null) ;
1495 return (StringBuffer) translationLayers.peek();
1496 }
1497
1498 private String getDeclaredLetVariableValue(String variableName)
1499 {
1500 for (final Iterator iterator = letVariableStack.iterator(); iterator.hasNext();)
1501 {
1502 Map variableMap = (Map) iterator.next();
1503 if (variableMap.containsKey(variableName))
1504 {
1505 return (String) variableMap.get(variableName);
1506 }
1507 }
1508 return null;
1509 }
1510
1511 private void newLetVariableContext()
1512 {
1513 letVariableStack.push(new HashMap(4));
1514 }
1515
1516 private void dropLetVariableContext()
1517 {
1518 if (!letVariableStack.isEmpty())
1519 letVariableStack.pop();
1520 }
1521
1522 private void addLetVariableToContext(String variableName, String variableValue)
1523 {
1524 ((Map) letVariableStack.peek()).put(variableName, variableValue);
1525 }
1526
1527
1528
1529
1530
1531
1532 private ModelElementFacade getModelElement()
1533 {
1534 return (ModelElementFacade) this.getContextElement();
1535 }
1536
1537
1538
1539
1540 public ValidationJavaTranslator()
1541 {
1542 arrowPropertyCallStack.push(Boolean.FALSE);
1543 }
1544
1545
1546
1547
1548 private static final String CONTEXT_ELEMENT_NAME = "contextElement";
1549
1550
1551
1552
1553 private static final String OCL_INTROSPECTOR_INVOKE_PREFIX = OCL_TRANSLATOR_PACKAGE + ".OCLIntrospector.invoke(";
1554
1555
1556
1557
1558 private static final String BOOLEAN_WRAP_PREFIX = "Boolean.valueOf(String.valueOf(";
1559
1560
1561
1562
1563 private static final String BOOLEAN_WRAP_SUFFIX = ")).booleanValue()";
1564
1565
1566
1567
1568
1569
1570
1571 @Override
1572 public void postProcess()
1573 {
1574 this.getExpression().insertInTranslatedExpression(0, OCL_TRANSLATOR_PACKAGE + ".OCLResultEnsurer.ensure(");
1575 this.getExpression().insertInTranslatedExpression(0, "boolean constraintValid = ");
1576 this.getExpression().insertInTranslatedExpression(0,
1577 "final Object " + CONTEXT_ELEMENT_NAME + " = this; ");
1578 this.getExpression().appendToTranslatedExpression(");");
1579 }
1580 }