Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0ce37699866bfd20edd578fa756e820d26e2e38f (plain) (blame)
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
/******************************************************************************
 * 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.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.ocl.cst.OCLExpressionCS;
import org.eclipse.ocl.ecore.CollectionType;
import org.eclipse.papyrus.gmf.internal.xpand.BuiltinMetaModel;
import org.eclipse.papyrus.gmf.internal.xpand.expression.ast.Identifier;
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.model.Variable;
import org.eclipse.papyrus.gmf.internal.xpand.model.XpandIterator;
import org.eclipse.papyrus.gmf.internal.xpand.ocl.ExpressionHelper;

/**
 * @author Sven Efftinge
 */
public class ForEachStatement extends Statement {

	public static final String ITERATOR_VAR_NAME = "iterator";

	private final Statement[] body;

	private final ExpressionHelper target;

	private final ExpressionHelper separator;

	private final Identifier variable;

	private final Identifier iteratorName;

	public ForEachStatement(final int start, final int end, final int line, final Identifier variable, final OCLExpressionCS target, final Statement[] body, final OCLExpressionCS separator, final Identifier iterator) {
		super(start, end, line);
		this.variable = variable;
		this.target = new ExpressionHelper(target, this);
		this.body = body;
		this.separator = separator == null ? null : new ExpressionHelper(separator, this);
		iteratorName = iterator;
	}

	public void analyze(ExecutionContext ctx, final Set<AnalysationIssue> issues) {
		EClassifier t = target.analyze(ctx, issues);
		if (separator != null) {
			final EClassifier sepT = separator.analyze(ctx, issues);
			if (ctx.getOCLEnvironment().getOCLStandardLibrary().getString() != sepT) {
				issues.add(new AnalysationIssue(AnalysationIssue.Type.INCOMPATIBLE_TYPES, "String expected!", target));
			}
		}
		if (t != null) {
			if (t instanceof CollectionType) {
				t = ((CollectionType) t).getElementType();
			} else {
				issues.add(new AnalysationIssue(AnalysationIssue.Type.INCOMPATIBLE_TYPES, "Collection type expected!", target));
				return;
			}
		}
		// XXX odd - is t == null ok here?
		ctx = ctx.cloneWithVariable(new Variable(variable.getValue(), t, null));
		if (iteratorName != null) {
			ctx = ctx.cloneWithVariable(new Variable(iteratorName.getValue(), BuiltinMetaModel.ITERATOR_TYPE, null));
		}
		for (Statement statement : body) {
			statement.analyze(ctx, issues);
		}
	}

	@Override
	public void evaluateInternal(ExecutionContext ctx) {
		Set<AnalysationIssue> issues = new HashSet<AnalysationIssue>();
		EClassifier targetType = target.analyze(ctx, issues);
		if (issues.size() > 0 || false == targetType instanceof CollectionType) {
			throw new EvaluationException("Can't evaluate FOREACH expression: target collection type cannot be defined", target);
		}
		EClassifier targetElementType = ((CollectionType) targetType).getElementType();
		final Object o = target.evaluate(ctx);

		if (!(o instanceof Collection<?>)) {
			throw new EvaluationException("Collection expected (was: " + o.getClass().getName() + ")!", target);
		}
		final Collection<?> col = (Collection<?>) o;
		final String sep = (String) (separator != null ? separator.evaluate(ctx) : null);
		final XpandIterator iterator = new XpandIterator(col.size());

		if (iteratorName != null) {
			ctx = ctx.cloneWithVariable(new Variable(iteratorName.getValue(), BuiltinMetaModel.ITERATOR_TYPE, iterator));
		}
		for (final Iterator<?> iter = col.iterator(); iter.hasNext();) {
			final Object element = iter.next();
			if (!BuiltinMetaModel.isAssignableFrom(ctx, targetElementType, BuiltinMetaModel.getType(ctx, element))) {
				throw new EvaluationException("Can't evaluate FOREACH expression: actual collection element type is not assignable to declared collection element type", this);
			}
			ctx = ctx.cloneWithVariable(new Variable(variable.getValue(), targetElementType, element));
			for (int i = 0; i < body.length; i++) {
				body[i].evaluate(ctx);
			}
			if ((sep != null) && iter.hasNext()) {
				ctx.getScope().getOutput().write(sep);
			}
			iterator.increment();
		}
	}
	
	ExpressionHelper getSeparator() {
		return separator;
	}
	
	ExpressionHelper getTarget() {
		return target;
	}
	
	Statement[] getBody() {
		return body;
	}
	
}

Back to the top