Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a46cb02c7da7e0793233ab4f28990f99cb1c272 (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
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
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.validation;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.etrice.core.config.ActorClassConfig;
import org.eclipse.etrice.core.config.ActorInstanceConfig;
import org.eclipse.etrice.core.config.AttrClassConfig;
import org.eclipse.etrice.core.config.AttrConfig;
import org.eclipse.etrice.core.config.AttrInstanceConfig;
import org.eclipse.etrice.core.config.BooleanLiteral;
import org.eclipse.etrice.core.config.ConfigModel;
import org.eclipse.etrice.core.config.ConfigPackage;
import org.eclipse.etrice.core.config.IntLiteral;
import org.eclipse.etrice.core.config.Literal;
import org.eclipse.etrice.core.config.NumberLiteral;
import org.eclipse.etrice.core.config.RealLiteral;
import org.eclipse.etrice.core.config.RefPath;
import org.eclipse.etrice.core.config.StringLiteral;
import org.eclipse.etrice.core.config.util.ConfigUtil;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
import org.eclipse.etrice.core.room.Attribute;
import org.eclipse.etrice.core.room.DataType;
import org.eclipse.etrice.core.room.LiteralType;
import org.eclipse.etrice.core.room.PrimitiveType;
import org.eclipse.xtext.validation.Check;

public class ConfigJavaValidator extends AbstractConfigJavaValidator {

	@Check
	public void checkConfigModel(ConfigModel model) {
		// duplicate class config check
		Set<ActorClass> actorClasses = new HashSet<ActorClass>();
		for (ActorClassConfig classConfig : model.getActorClassConfigs()) {
			if (actorClasses.contains(classConfig.getActor()))
				error("duplicate class config", model,
						ConfigPackage.Literals.CONFIG_MODEL__CONFIG_ELEMENTS,
						model.getConfigElements().indexOf(classConfig));
			else
				actorClasses.add(classConfig.getActor());
		}
		// duplicate instance config check
		Set<String> instanceRefs = new HashSet<String>();
		for (ActorInstanceConfig instanceConfig : model
				.getActorInstanceConfigs()) {
			String ref = instanceConfig.getRoot().getName()
					+ refPathToString(instanceConfig.getPath());
			if (instanceRefs.contains(ref))
				error("duplicate instance config", model,
						ConfigPackage.Literals.CONFIG_MODEL__CONFIG_ELEMENTS,
						model.getConfigElements().indexOf(instanceConfig));
			else
				instanceRefs.add(ref);
		}
	}

	@Check
	public void checkActorClassConfig(ActorClassConfig config) {
		checkDuplicateAttributes(config.getAttributes(),
				ConfigPackage.Literals.ACTOR_CLASS_CONFIG__ATTRIBUTES);

	}

	@Check
	public void checkActorInstanceConfig(ActorInstanceConfig config) {
		ActorContainerClass root = config.getRoot();
		if (root != null && !root.eIsProxy()) {
			RefPath path = config.getPath();
			if (path != null) {
				String invalidSegment = ConfigUtil.checkPath(root, path);
				if (invalidSegment != null)
					error("no match for segment '" + invalidSegment + "'",
							ConfigPackage.eINSTANCE
									.getActorInstanceConfig_Path());
			}
		}

		checkDuplicateAttributes(config.getAttributes(),
				ConfigPackage.Literals.ACTOR_INSTANCE_CONFIG__ATTRIBUTES);
	}

	private void checkDuplicateAttributes(
			List<? extends AttrConfig> attrConfigs, EReference ref) {
		Set<Attribute> attributes = new HashSet<Attribute>();
		for (AttrConfig config : attrConfigs) {
			if (attributes.contains(config.getAttribute()))
				error("duplicate attribute entry", ref,
						attrConfigs.indexOf(config));
			else
				attributes.add(config.getAttribute());
		}
	}

	@Check
	public void checkAttrConfig(AttrConfig config) {
		Attribute attr = config.getAttribute();
		if (attr == null)
			return;

		DataType type = attr.getRefType().getType();
		if (type instanceof PrimitiveType) {
			PrimitiveType primitive = (PrimitiveType) type;

			checkAttrConfigValue(primitive, config);
		}
	}

	@Check
	public void checkAttrClassConfig(AttrClassConfig config) {
		Attribute attr = config.getAttribute();
		if (attr == null)
			return;

		DataType type = attr.getRefType().getType();
		if (type instanceof PrimitiveType) {
			PrimitiveType primitive = (PrimitiveType) type;

			checkAttrConfigMin(primitive, config);
			checkAttrConfigMax(primitive, config);
		}
	}

	@Check
	public void checkAttrInstanceConfig(AttrInstanceConfig config) {

	}

	private void checkAttrConfigValue(PrimitiveType primitive, AttrConfig config) {
		Literal value = config.getValue();
		if (value == null)
			return;

		EReference valueRef = ConfigPackage.eINSTANCE.getAttrConfig_Value();
		LiteralType type = primitive.getType();
		Attribute attribute = config.getAttribute();
		// type check
		switch (type) {
		case BOOL:
			if (!(value instanceof BooleanLiteral))
				error("must be boolean value", valueRef);
			break;
		case REAL:
			if (!(value instanceof NumberLiteral))
				error("must be an integer or real value", valueRef);
			break;
		case INT:
			if (!(value instanceof IntLiteral))
				error("must be an integer", valueRef);
			break;
		case CHAR:
			if (!(value instanceof StringLiteral))
				error("must be a string", valueRef);
			else {
				StringLiteral strValue = (StringLiteral) value;
				int maxLength = attribute.getSize() + 1;
				if (maxLength < strValue.getValue().length())
					error("too many characters - maximal length is "
							+ maxLength, valueRef);
			}
			break;
		}

		// numeric check
		if ((type == LiteralType.INT || type == LiteralType.REAL)
				&& value instanceof NumberLiteral) {
			ActorClassConfig classConfig = null;
			if (config.eContainer() instanceof ActorInstanceConfig) {
				ActorInstanceConfig actorConfig = (ActorInstanceConfig) config
						.eContainer();
				ActorContainerClass actorClass = ConfigUtil.resolve(
						actorConfig.getRoot(), actorConfig.getPath());
				// find ActorClassConfig
				ConfigModel model = getConfigModel(actorConfig);
				for (ActorClassConfig cf : model.getActorClassConfigs()) {
					if (cf.getActor().equals(actorClass)) {
						classConfig = cf;
						break;
					}
				}
			} else if (config.eContainer() instanceof ActorClassConfig)
				classConfig = (ActorClassConfig) config.eContainer();

			AttrClassConfig attrClassConfig = null;
			if (classConfig != null) {
				for (AttrClassConfig acf : classConfig.getAttributes())
					if (config.getAttribute().equals(acf.getAttribute())) {
						attrClassConfig = acf;
						break;
					}
			}

			if (attrClassConfig != null) {
				NumberLiteral min = attrClassConfig.getMin();
				NumberLiteral max = attrClassConfig.getMax();
				double dValue = ConfigUtil
						.literalToDouble((NumberLiteral) value);

				if (min != null) {
					double dMin = ConfigUtil.literalToDouble(min);
					if (dMin > dValue)
						error("value is less than minimum", valueRef);
				}
				if (max != null) {
					double dMax = ConfigUtil.literalToDouble(max);
					if (dMax < dValue)
						error("value exceeds maximum", valueRef);
				}
			}
		}
	}

	private void checkAttrConfigMin(PrimitiveType primitive,
			AttrClassConfig config) {
		NumberLiteral min = config.getMin();
		if (min == null)
			return;

		EReference minRef = ConfigPackage.eINSTANCE.getAttrClassConfig_Min();
		LiteralType type = primitive.getType();
		switch (type) {
		case INT:
			if (config instanceof RealLiteral)
				error("must be an integer", minRef);
			break;
		default:
			if (config != null)
				error("no minimum allowed", minRef);
		}

		if (type == LiteralType.INT || type == LiteralType.REAL) {
			// check room default if config default is not set
			if (config.getValue() == null) {
				double dMin = ConfigUtil.literalToDouble(min);
				String defaultValue = config.getAttribute()
						.getDefaultValueLiteral();
				try {
					double dDefaulValue = Double.parseDouble(defaultValue);
					if (dMin > dDefaulValue)
						error("default value in ROOM model is less than this minimun",
								minRef);
				} catch (NumberFormatException e) {
				}
			}
		}

	}

	private void checkAttrConfigMax(PrimitiveType primitive,
			AttrClassConfig config) {
		NumberLiteral max = config.getMin();
		if (max == null)
			return;

		EReference maxRef = ConfigPackage.eINSTANCE.getAttrClassConfig_Max();
		LiteralType type = primitive.getType();
		switch (type) {
		case INT:
			if (max instanceof RealLiteral)
				error("must be an integer", maxRef);
			break;
		default:
			if (max != null)
				error("no maximum allowed", maxRef);
		}

		if (type == LiteralType.INT || type == LiteralType.REAL) {
			// check room default if config default is not set
			if (config.getValue() == null) {
				double dMax = ConfigUtil.literalToDouble(max);
				String defaultValue = config.getAttribute()
						.getDefaultValueLiteral();
				try {
					double dDefaulValue = Double.parseDouble(defaultValue);
					if (dMax < dDefaulValue)
						error("default value in ROOM model exceeds this maximum",
								maxRef);
				} catch (NumberFormatException e) {
				}
			}
		}
	}

	private ConfigModel getConfigModel(EObject object) {
		EObject root = object;
		while (root.eContainer() != null)
			root = root.eContainer();

		return (ConfigModel) root;
	}

	private String refPathToString(RefPath path) {
		String str = "";
		for (String s : path.getRefs())
			str += "/" + s;

		return str;
	}
}

Back to the top