Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 165a942714b7a9a778837428ee69ea0002578044 (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
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
/*******************************************************************************
 * 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.PortClassConfig;
import org.eclipse.etrice.core.config.PortInstanceConfig;
import org.eclipse.etrice.core.config.ProtocolClassConfig;
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.InterfaceItem;
import org.eclipse.etrice.core.room.LiteralType;
import org.eclipse.etrice.core.room.PrimitiveType;
import org.eclipse.etrice.core.room.ProtocolClass;
import org.eclipse.xtext.validation.Check;

public class ConfigJavaValidator extends AbstractConfigJavaValidator {

	@Check
	public void checkConfigModel(ConfigModel model) {
		// duplicate actor 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 actor instance config check
		Set<String> actorRefs = new HashSet<String>();
		for (ActorInstanceConfig instanceConfig : model
				.getActorInstanceConfigs()) {
			String ref = instanceConfig.getRoot().getName()
					+ refPathToString(instanceConfig.getPath());
			if (actorRefs.contains(ref))
				error("duplicate actor instance config", model,
						ConfigPackage.Literals.CONFIG_MODEL__CONFIG_ELEMENTS,
						model.getConfigElements().indexOf(instanceConfig));
			else
				actorRefs.add(ref);
		}
		// duplicate protocol class config check
		Set<ProtocolClass> protocolClasses = new HashSet<ProtocolClass>();
		for (ProtocolClassConfig protocolConfig : model
				.getProtocolClassConfigs()) {
			if (protocolClasses.contains(protocolConfig.getProtocol()))
				error("duplicate protocol class config", model,
						ConfigPackage.Literals.CONFIG_MODEL__CONFIG_ELEMENTS,
						model.getConfigElements().indexOf(protocolConfig));
			else
				protocolClasses.add(protocolConfig.getProtocol());
		}
	}

	@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());
			}
		}
		// duplicate port instance config check
		Set<InterfaceItem> items = new HashSet<InterfaceItem>();
		for (PortInstanceConfig portConfig : config.getPorts()) {
			InterfaceItem item = portConfig.getItem();
			if (items.contains(item))
				error("duplicate port instance config",
						ConfigPackage.Literals.ACTOR_INSTANCE_CONFIG__PORTS,
						config.getPorts().indexOf(portConfig));
			else
				items.add(item);
		}

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

	@Check
	public void checkPortClassConfig(PortClassConfig config) {
		checkDuplicateAttributes(config.getAttributes(),
				ConfigPackage.Literals.PORT_CLASS_CONFIG__ATTRIBUTES);
	}

	@Check
	public void checkPortInstanceConfig(PortInstanceConfig config) {
		checkDuplicateAttributes(config.getAttributes(),
				ConfigPackage.Literals.PORT_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) {
		if (config.getValue() == null)
			return;

		List<Literal> values = config.getValue().getLiterals();
		EReference valueRef = ConfigPackage.eINSTANCE.getAttrConfig_Value();
		EReference arrayRef = ConfigPackage.eINSTANCE
				.getLiteralArray_Literals();
		LiteralType type = primitive.getType();
		Attribute attribute = config.getAttribute();
		int attrMult = (attribute.getSize() > 0) ? attribute.getSize() : 1;
		if (values.size() > attrMult)
			error("too many values, multiplicity is " + attrMult, valueRef);
		if (values.size() > 1 && values.size() < attrMult)
			error("not enough values, multiplicity is " + attrMult, valueRef);
		// type check
		for (Literal value : values) {
			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 {
					if (values.size() > 1)
						error("multiplicity must be one", valueRef);
					StringLiteral strValue = (StringLiteral) value;
					if (attrMult < strValue.getValue().length())
						error("too many characters - maximal length is "
								+ attrMult, valueRef);
				}
				break;
			}
		}

		// numeric check
		if ((type == LiteralType.INT || type == LiteralType.REAL)) {
			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();
				for (Literal value : values) {
					if (!(value instanceof NumberLiteral))
						continue;

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

	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