Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73350287dd6a6c7e9f854f5d9f6798c6adfd734a (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
/*******************************************************************************
 * Copyright (c) 2011 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 (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.genmodel;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.common.validation.ICustomValidator;
import org.eclipse.etrice.core.genmodel.builder.GeneratorModelBuilder;
import org.eclipse.etrice.core.genmodel.fsm.IDiagnostician;
import org.eclipse.etrice.core.room.RoomModel;
import org.eclipse.etrice.core.room.RoomPackage;
import org.eclipse.etrice.generator.base.logging.NullLogger;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.ValidationMessageAcceptor;


public class RoomGenmodelValidator implements ICustomValidator {

	private static final Set<EClass> classesToCheck = new HashSet<EClass>();
	{
		classesToCheck.add(RoomPackage.Literals.ROOM_MODEL);
	}
	
	private class Diag implements IDiagnostician {
		private ValidationMessageAcceptor acceptor;
		
		private Diag(ValidationMessageAcceptor acceptor){
			this.acceptor = acceptor;
		}
		
		@Override
		public void warning(String msg, EObject source, EStructuralFeature feature) {
			acceptor.acceptWarning(msg, source, feature, INSIGNIFICANT_INDEX, null);
		}

		@Override
		public void warning(String msg, EObject source, EStructuralFeature feature, int index) {
			acceptor.acceptWarning(msg, source, feature, index, null);
		}

		@Override
		public void error(String msg, EObject source, EStructuralFeature feature) {
			acceptor.acceptError(msg, source, feature, INSIGNIFICANT_INDEX, null);
		}

		@Override
		public void error(String msg, EObject source, EStructuralFeature feature, int index) {
			 acceptor.acceptError(msg, source, feature, index, null);
		}

		@Override
		public boolean isFailed() {
			return false;
		}
	}
	
	
	@Override
	public String getName() {
		return "Genmodel Validator";
	}

	
	@Override
	public String getDescription() {
		return "This validator checks ROOM models by creating actual system instances, which covers aspects like wiring.";
	}

	
	@Override
	public Set<EClass> getClassesToCheck() {
		return classesToCheck;
	}


	@Override
	public void validate(EObject object, ValidationMessageAcceptor messageAcceptor, ValidationContext context) {
		if(!(object instanceof RoomModel))
			return;
		
		if(context.isGeneration())
			return;
		
		RoomModel model = (RoomModel) object;
		if (context.getCheckMode() == CheckMode.ALL) {
//			System.out.println("checking model " + model.getName());

			ArrayList<RoomModel> models = new ArrayList<RoomModel>();
			ArrayList<RoomModel> importedModels = new ArrayList<RoomModel>();

			models.add(model);
			
			Resource resource = model.eResource();
			if (resource != null) {
				ResourceSet rs = resource.getResourceSet();
				if (rs != null) {
					EcoreUtil.resolveAll(rs);
					for (Resource res : rs.getResources()) {
						for (EObject obj : res.getContents()) {
							if (obj instanceof RoomModel && obj!=model)
								importedModels.add((RoomModel) obj);
						}
					}
				}
			}

			Diag diagnostician = new Diag(messageAcceptor);
			GeneratorModelBuilder builder = new GeneratorModelBuilder(new NullLogger(), diagnostician);
			builder.createGeneratorModel(models, importedModels, true);

//			System.out.println("done checking model " + model.getName() + " with result: "
//					+ (diagnostician.isFailed() ? "failed" : "ok"));
		}
		
	}

}

Back to the top