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
|
/******************************************************************************
* Copyright (c) 2005, 2020 Sven Efftinge, CEA LIST, Artal and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sven Efftinge - Initial API and implementation
* Artem Tikhomirov (Borland) - Migration to OCL expressions
* Aurelien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
*****************************************************************************/
package org.eclipse.papyrus.gmf.internal.xpand.ast;
import java.util.Set;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.ocl.cst.OCLExpressionCS;
import org.eclipse.papyrus.gmf.internal.xpand.model.AnalysationIssue;
import org.eclipse.papyrus.gmf.internal.xpand.model.EvaluationException;
import org.eclipse.papyrus.gmf.internal.xpand.model.ExecutionContext;
import org.eclipse.papyrus.gmf.internal.xpand.ocl.ExpressionHelper;
/**
* @author Sven Efftinge
*/
public class IfStatement extends Statement {
private final ExpressionHelper condition;
private final Statement[] thenPart;
private IfStatement elseIf;
public IfStatement(final int start, final int end, final int line, final OCLExpressionCS condition,
final Statement[] thenPart, final IfStatement elseIf) {
super(start, end, line);
this.condition = condition == null ? null : new ExpressionHelper(condition, this);
this.thenPart = thenPart;
this.elseIf = elseIf;
}
/**
* FIXME used in tests only, should I keep it?
*/
public ExpressionHelper getCondition() {
return condition;
}
public IfStatement getElseIf() {
return elseIf;
}
// XXX modifiable AST
public void setElseIf(final IfStatement elseIf) {
this.elseIf = elseIf;
}
public void analyze(final ExecutionContext ctx, final Set<AnalysationIssue> issues) {
if (condition != null) {
final EClassifier conType = condition.analyze(ctx, issues);
if (conType != ctx.getOCLEnvironment().getOCLStandardLibrary().getBoolean()) {
issues.add(new AnalysationIssue(AnalysationIssue.Type.INCOMPATIBLE_TYPES, "Boolean expected!", condition));
}
}
for (int i = 0; i < thenPart.length; i++) {
thenPart[i].analyze(ctx, issues);
}
if (elseIf != null) {
elseIf.analyze(ctx, issues);
}
}
@Override
public void evaluateInternal(final ExecutionContext ctx) {
if (condition != null) {
final Object result = condition.evaluate(ctx);
if (result == null) {
throw new EvaluationException("Nullevaluation!", condition);
}
if (!(result instanceof Boolean)) {
throw new EvaluationException("Boolean expected (was: " + result.getClass().getName() + ")!", condition);
}
if (((Boolean) result).booleanValue()) {
for (int i = 0; i < thenPart.length; i++) {
thenPart[i].evaluate(ctx);
}
} else if (elseIf != null) {
elseIf.evaluate(ctx);
}
} else {
for (int i = 0; i < thenPart.length; i++) {
thenPart[i].evaluate(ctx);
}
}
}
Statement[] getThenPart() {
return thenPart;
}
}
|