Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa41c4f22fd5f5748ff37c4a0ad4cdd3acb2bba1 (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
/*******************************************************************************
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.etmap.validation;

import java.util.HashSet;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.etrice.core.common.base.BasePackage;
import org.eclipse.etrice.core.common.base.Import;
import org.eclipse.etrice.core.etmap.eTMap.ETMapPackage;
import org.eclipse.etrice.core.etmap.eTMap.Mapping;
import org.eclipse.etrice.core.etmap.eTMap.SubSystemMapping;
import org.eclipse.etrice.core.etmap.eTMap.ThreadMapping;
import org.eclipse.etrice.core.etphys.eTPhys.PhysicalModel;
import org.eclipse.etrice.core.room.LogicalThread;
import org.eclipse.etrice.core.room.RoomModel;
import org.eclipse.etrice.core.room.SubSystemRef;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
import org.eclipse.xtext.validation.Check;

import com.google.inject.Inject;


public class ETMapJavaValidator extends AbstractETMapJavaValidator {

	public static final String DUPLICATE_SUBSYS_MAPPING = "ETMapJavaValidator.DulicateSubSysMapping";
	public static final String UNMAPPED_SUBSYS_REFS = "ETMapJavaValidator.UnmappedSubSysRefs";
	public static final String DUPLICATE_THREAD_MAPPING = "ETMapJavaValidator.DulicateThreadMapping";
	public static final String UNMAPPED_THREAD_REFS = "ETMapJavaValidator.UnmappedThreadRefs";
	public static final String WRONG_NAMESPACE = "ETMapJavaValidator.WrongNamespace";
	public static final String NOT_EMPTY = "empty";
	public static final String EMPTY = "not-empty";
	
	@Inject ImportUriResolver importUriResolver;
	
	@Check
	public void checkMapping(Mapping mp) {
		HashSet<SubSystemRef> mapped = new HashSet<SubSystemRef>();
		for (SubSystemMapping ssm : mp.getSubsysMappings()) {
			if (!mapped.add(ssm.getLogicalSubSys())) {
				int idx = mp.getSubsysMappings().indexOf(ssm);
				error("duplicate mapping", ETMapPackage.Literals.MAPPING__SUBSYS_MAPPINGS, idx, DUPLICATE_SUBSYS_MAPPING);
			}
		}

		StringBuilder msg = new StringBuilder();
		StringBuilder fix = new StringBuilder();
		for (SubSystemRef ssr : mp.getLogicalSys().getSubSystems()) {
			if (!mapped.contains(ssr)) {
				msg.append("unmapped sub system reference '"+ssr.getName()+"'\n");
				String nodeRef = mp.getPhysicalSys().getNodeRefs().isEmpty()?
						"no_node_ref_defined" : mp.getPhysicalSys().getNodeRefs().get(0).getName();
				fix.append("\t\tSubSystemMapping "+ssr.getName()+" -> "+nodeRef+" {}\n");
			}
		}
		if (msg.length()>0)
			error(
					msg.substring(0, msg.length()-2),
					ETMapPackage.Literals.MAPPING__SUBSYS_MAPPINGS,
					UNMAPPED_SUBSYS_REFS,
					fix.toString(),
					mapped.isEmpty()? EMPTY:NOT_EMPTY);
	}
	
	@Check
	public void checkSubSystemMapping(SubSystemMapping ssm) {
		HashSet<LogicalThread> mapped = new HashSet<LogicalThread>();
		for (ThreadMapping tm : ssm.getThreadMappings()) {
			if (!mapped.add(tm.getLogicalThread())) {
				int idx = ssm.getThreadMappings().indexOf(tm);
				error("duplicate mapping", ETMapPackage.Literals.SUB_SYSTEM_MAPPING__THREAD_MAPPINGS, idx, DUPLICATE_THREAD_MAPPING);
			}
		}
		
		StringBuilder sb = new StringBuilder();
		StringBuilder fix = new StringBuilder();
		for (LogicalThread lt : ssm.getLogicalSubSys().getType().getThreads()) {
			if (!mapped.contains(lt)) {
				sb.append("unmapped logical thread '"+lt.getName()+"'\n");
				String pthread = ssm.getNode().getType().getThreads().isEmpty()?
						"no_physical_thread_defined" : ssm.getNode().getType().getThreads().get(0).getName();
				fix.append("\t\t\tThreadMapping "+lt.getName()+" -> "+pthread+"\n");
			}
		}
		if (sb.length()>0)
			error(
					sb.substring(0, sb.length()-2),
					ETMapPackage.Literals.SUB_SYSTEM_MAPPING__THREAD_MAPPINGS,
					UNMAPPED_THREAD_REFS,
					fix.toString(),
					mapped.isEmpty()? EMPTY:NOT_EMPTY);
	}
	
	@Check
	public void checkImportedNamespace(Import imp) {
		if (imp.getImportedNamespace()==null)
			return;
		
		if (imp.getImportURI()==null)
			return;
		
		String uriString = importUriResolver.resolve(imp);
		
		URI uri = URI.createURI(uriString);
		ResourceSet rs = new ResourceSetImpl();

		try {
			Resource res = rs.getResource(uri, true);
			if (res==null)
				return;
			
			if (res.getContents().isEmpty()) {
				error("referenced model is empty", BasePackage.Literals.IMPORT__IMPORT_URI);
				return;
			}
			
			if (uri.lastSegment().endsWith(".room")) {
				if (!(res.getContents().get(0) instanceof RoomModel)) {
					error("referenced model is no ROOM model (but has .room extension)", BasePackage.Literals.IMPORT__IMPORT_URI);
					return;
				}
				
				RoomModel model = (RoomModel) res.getContents().get(0);
				if (!imp.getImportedNamespace().equals(model.getName()+".*")) {
					error("the imported namespace should be '"+model.getName()+".*'", BasePackage.Literals.IMPORT__IMPORTED_NAMESPACE, WRONG_NAMESPACE, model.getName()+".*");
				}
			}
			else if (uri.lastSegment().endsWith(".etphys")) {
				if (!(res.getContents().get(0) instanceof PhysicalModel)) {
					error("referenced model is no eTrice physical model (but has .etphys extension)", BasePackage.Literals.IMPORT__IMPORT_URI);
					return;
				}
				
				PhysicalModel model = (PhysicalModel) res.getContents().get(0);
				if (!imp.getImportedNamespace().equals(model.getName()+".*")) {
					error("the imported namespace should be '"+model.getName()+".*'", BasePackage.Literals.IMPORT__IMPORTED_NAMESPACE, WRONG_NAMESPACE, model.getName()+".*");
				}
			}
			else {
				error("referenced model has unexpected file extension", BasePackage.Literals.IMPORT__IMPORT_URI);
			}
		}
		catch (RuntimeException re) {
			warning("could not load referenced model", BasePackage.Literals.IMPORT__IMPORT_URI);
			return;
		}
	}
}

Back to the top