Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83d3da384bc704c149b12de49afe21ffb6141fb7 (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
/*******************************************************************************
 * 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 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:
 * 		Juergen Haug
 * 
 *******************************************************************************/

package org.eclipse.etrice.abstractexec.behavior.tests;

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

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.abstractexec.behavior.ReachabilityValidator;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.fsm.fSM.InitialTransition;
import org.eclipse.etrice.core.fsm.fSM.RefinedTransition;
import org.eclipse.etrice.core.room.RoomModel;
import org.eclipse.etrice.core.fsm.fSM.State;
import org.eclipse.etrice.core.fsm.fSM.StateGraph;
import org.eclipse.etrice.core.fsm.fSM.StateGraphItem;
import org.eclipse.xtext.validation.AbstractValidationDiagnostic;
import org.eclipse.xtext.validation.FeatureBasedDiagnostic;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestReachability extends TestBase {

	private RoomModel model = null;

	@Before
	public void setUp() {
		prepare("reachability.room");
		model = getRoomModel();
	}

	@Test
	public void test() {
		if (model == null)
			Assert.fail("could not find model");

		Set<StateGraphItem> items = new HashSet<StateGraphItem>();
		for (ActorClass ac : model.getActorClasses()) {
			if (ac.getStateMachine() == null)
				continue;
			TreeIterator<EObject> iter = ac.getStateMachine().eAllContents();
			while (iter.hasNext()) {
				EObject obj = iter.next();
				if (obj instanceof StateGraphItem)
					items.add((StateGraphItem) obj);
			}
		}

		List<AbstractValidationDiagnostic> diagnostics = getIssueCode2diagnostic().get(ReachabilityValidator.DIAG_CODE_UNREACHABLE);
		for (AbstractValidationDiagnostic d : diagnostics) {
			if (d instanceof FeatureBasedDiagnostic) {
				FeatureBasedDiagnostic dx = (FeatureBasedDiagnostic) d;
				StateGraph graph = (StateGraph) dx.getSourceEObject();
				Object feature = graph.eGet(dx.getFeature());
				if (feature instanceof EList) {
					EList<?> list = (EList<?>) feature;
					Object source = list.get(dx.getIndex());
					if (source instanceof StateGraphItem) {
						checkUnreachable((StateGraphItem) source);
						items.remove(source);
					} else
						Assert.fail("unexpected test item:" + source);
				}
			}
		}

		for (StateGraphItem item : items) {
			if (item instanceof InitialTransition) {
				if (item.eContainer().eContainer() instanceof ActorClass)
					continue;
				State container = (State) item.eContainer().eContainer();
				Assert.assertTrue(
						item + " is NOT marked Unreachable (or name doesn't end with '_1' ?)",
						container.getName().endsWith("_1"));
			} else
				Assert.assertTrue(
						item + " is NOT marked Unreachable (or name doesn't end with '_1' ?)",
						item.getName().endsWith("_1"));
		}
	}

	private void checkUnreachable(StateGraphItem item) {
		String name = item.getName();
		if (item instanceof RefinedTransition) {
			name = ((RefinedTransition) item).getTarget().getName();
		}
		Assert.assertTrue(
				name + " is incorrectly marked UNreachable (or name doesn't end with '_0' ?)",
				name.endsWith("_0"));
	}
}

Back to the top