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

package org.eclipse.etrice.ui.behavior.commands

import com.google.common.collect.Sets
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.etrice.core.RoomStandaloneSetup
import org.eclipse.etrice.core.TestBase
import org.eclipse.etrice.core.fsm.fSM.StateGraphItem
import org.eclipse.etrice.core.genmodel.fsm.ExtendedFsmGenBuilder
import org.eclipse.etrice.core.genmodel.fsm.FsmGenChecker
import org.eclipse.etrice.core.genmodel.fsm.IDiagnostician
import org.eclipse.etrice.core.genmodel.fsm.NullDiagnostician
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.GraphContainer
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.Link
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.Node
import org.eclipse.etrice.core.room.ActorClass
import org.eclipse.etrice.core.room.RoomModel
import org.eclipse.etrice.ui.behavior.BehaviorTestActivator
import org.junit.Test

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertTrue
import org.eclipse.etrice.generator.base.logging.NullLogger
import org.eclipse.etrice.core.genmodel.fsm.ExtendedFsmGenBuilderFactory

class TestFSMGenModel extends TestBase {

	val injector = new RoomStandaloneSetup().createInjectorAndDoEMFRegistration()
	protected IDiagnostician diagnostician = new NullDiagnostician
	protected extension ExtendedFsmGenBuilder builder = injector.getInstance(ExtendedFsmGenBuilderFactory).create(diagnostician)

	protected def createResourceSetWithGc(String roomFile, String actorName) {
		prepare(BehaviorTestActivator.^default.getBundle())
		val res = getResource(roomFile)
		val actor = (res.contents.get(0) as RoomModel).actorClasses.findFirst[name.equals(actorName)]	
		
		val gc = builder.createTransformedModel(actor) => [gc |
			assertNotNull("graph context was created", gc)
			
			val logger = new NullLogger
			FsmGenChecker.check(gc, logger)
			assertFalse("logger has no errors", logger.hasErrors)
			save(gc, actor.name)
		]
		
		res.resourceSet.createResource(URI.createURI('dummy://' + actorName + '.fsmgen')) => [
			contents += gc
		]
		
		return res.resourceSet
	}
	
	protected def save(GraphContainer gc, String fileName) {
		val rs = new ResourceSetImpl
		val uri = URI.createFileURI("./output/" + fileName + ".fsmgen")
		val res = rs.createResource(uri)
		res.contents.add(gc)
		res.save(newHashMap)
	}
	
	@Test
	def void testBaseClass() {
		val rs = createResourceSetWithGc("InheritedStateMachine.room", "Base")
		val actorClasses = EcoreUtil.getAllContents(rs, true).filter(ActorClass).filter[name == 'Base'].toList
		val gc = rs.resources.map[contents].flatten.filter(GraphContainer).head
		
		checkAgainstRoom(gc, actorClasses)
	}
	
	@Test
	def void testDerivedClass() {
		val rs = createResourceSetWithGc("InheritedStateMachine.room", "Derived")
		val actorClasses = EcoreUtil.getAllContents(rs, true).filter(ActorClass).filter[name == 'Base' || name == 'Derived'].toList
		val gc = rs.resources.map[contents].flatten.filter(GraphContainer).head
		
		checkAgainstRoom(gc, actorClasses)
	}
	
	@Test
	def void testDoubleDerivedClass() {
		val rs = createResourceSetWithGc("InheritedStateMachine.room", "DoubleDerived")
		val actorClasses = EcoreUtil.getAllContents(rs, true).filter(ActorClass).filter[name == 'Base' || name == 'Derived' || name == 'DoubleDerived'].toList
		val gc = rs.resources.map[contents].flatten.filter(GraphContainer).head
		
		checkAgainstRoom(gc, actorClasses)
	}
	
	def void checkAgainstRoom(GraphContainer gc, List<ActorClass> actorClasses) {
		// genmodel should refer to latest fsm	(check in-memory ref)
		val genModelRefs = (EcoreUtil.getAllContents(gc, true).filter(Node).map[stateGraphNode] + EcoreUtil.getAllContents(gc, true).filter(Link).map[transition]).toSet
		EcoreUtil.getAllContents(actorClasses.last, false).filter(StateGraphItem).forEach[
			assertTrue('missing ' + EcoreUtil.getURI(it), genModelRefs.contains(it))
		]
		
		// all fsm items should be in genmodel	(check name) 
		// node, there are duplicate names in subgraphs => not complete
		val stateGraphItemsNames = EcoreUtil.getAllContents(actorClasses, true).filter(StateGraphItem).map[name].toIterable.toSortedSet
		val namesFromGenModel = genModelRefs.map[name].toSortedSet	
		assertEquals(stateGraphItemsNames, namesFromGenModel)
	}
	
	private def <T extends Comparable<?>> toSortedSet(Iterable<T> iterable) {
		 Sets.<T>newTreeSet(iterable)
	}
	
}

Back to the top