Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bd5500b70bfe41967768769298ea737dada7fa6 (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
/*******************************************************************************
 * Copyright (c) 2017 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.fsm.support.util

import java.util.HashMap
import org.eclipse.etrice.core.fsm.fSM.FSMFactory
import org.eclipse.etrice.core.fsm.fSM.ModelComponent
import org.eclipse.etrice.core.fsm.fSM.RefinedState
import org.eclipse.etrice.core.fsm.fSM.State
import org.eclipse.etrice.core.fsm.fSM.StateGraph
import org.eclipse.graphiti.features.IFeatureProvider
import org.eclipse.graphiti.mm.pictograms.ContainerShape

/**
 *  Shared model editing
 */
class ModelEditingUtil {
	
	def static public RefinedState getOrCreateRefinedStateFor(State s, ModelComponent mc) {
		val target2rs = new HashMap<State, RefinedState>();
		for (State st : mc.getStateMachine().getStates()) {
			if (st instanceof RefinedState)
				target2rs.put((st as RefinedState).getTarget(), st as RefinedState);
		}
		
		var RefinedState rs = null;
		
		// do we already have a RefinedState pointing to s?
		if (target2rs.containsKey(s)) {
			rs = target2rs.get(s);
		}
		else {
			// we have to create one and place it in the best fitting context
			var StateGraph sg = null;
			var State parent = s;
			var break = false;
			while (parent.eContainer().eContainer() instanceof State && !break) {
				parent =  s.eContainer().eContainer() as State;
				if (target2rs.containsKey(parent)) {
					val bestFitting = target2rs.get(parent);
					if (bestFitting.getSubgraph()===null)
						bestFitting.setSubgraph(FSMFactory.eINSTANCE.createStateGraph());
					sg = bestFitting.getSubgraph();
					break = true;
				}
			}
			
			if (sg===null)
				sg = mc.getStateMachine();
			
			rs = FSMFactory.eINSTANCE.createRefinedState();
			rs.setTarget(s);
			sg.getStates().add(rs);
		}
		return rs;
	}
	
	
	
	def static public StateGraph getOrCreateSubGraphOfRefinedStateFor(State s, ModelComponent mc) {
		val rs = getOrCreateRefinedStateFor(s, mc);
		
		if (rs.getSubgraph()===null)
			rs.setSubgraph(FSMFactory.eINSTANCE.createStateGraph());
	
		return rs.getSubgraph();
	}
	
	def static public StateGraph insertRefinedState(StateGraph sg, ModelComponent mc, ContainerShape targetContainer, IFeatureProvider fp) {
		val sg2 = getOrCreateSubGraphOfRefinedStateFor(sg.eContainer() as State, mc);
		fp.link(targetContainer, sg2);
		return sg2;
	}
}

Back to the top