Skip to main content
summaryrefslogtreecommitdiffstats
blob: 990c475ae964826df74f171854966a9bd0960e3c (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
 * 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 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.genmodel.fsm;

import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.Injector;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.fsm.fSM.ChoicePoint;
import org.eclipse.etrice.core.fsm.fSM.ChoicepointTerminal;
import org.eclipse.etrice.core.fsm.fSM.ModelComponent;
import org.eclipse.etrice.core.fsm.fSM.NonInitialTransition;
import org.eclipse.etrice.core.fsm.fSM.RefinedState;
import org.eclipse.etrice.core.fsm.fSM.RefinedTransition;
import org.eclipse.etrice.core.fsm.fSM.SimpleState;
import org.eclipse.etrice.core.fsm.fSM.State;
import org.eclipse.etrice.core.fsm.fSM.StateGraph;
import org.eclipse.etrice.core.fsm.fSM.StateGraphNode;
import org.eclipse.etrice.core.fsm.fSM.StateTerminal;
import org.eclipse.etrice.core.fsm.fSM.SubStateTrPointTerminal;
import org.eclipse.etrice.core.fsm.fSM.TrPoint;
import org.eclipse.etrice.core.fsm.fSM.TrPointTerminal;
import org.eclipse.etrice.core.fsm.fSM.Transition;
import org.eclipse.etrice.core.fsm.fSM.TransitionTerminal;
import org.eclipse.etrice.core.fsm.util.FSMHelpers;
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.FsmGenFactory;
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.Graph;
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.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;

@SuppressWarnings("all")
public class BasicFsmGenBuilder {
  @Inject
  @Extension
  protected FSMHelpers fsmHelpers;
  
  protected final FsmGenFactory factory = FsmGenFactory.eINSTANCE;
  
  private int inheritanceLevel = 0;
  
  public BasicFsmGenBuilder(final Injector injector) {
    injector.injectMembers(this);
  }
  
  protected BasicFsmGenBuilder() {
  }
  
  public GraphContainer createTransformedModel(final ModelComponent mc) {
    return this.createContainer(mc);
  }
  
  private GraphContainer createContainer(final ModelComponent mc) {
    final ArrayList<?> _cacheKey = CollectionLiterals.newArrayList(mc);
    final GraphContainer _result;
    synchronized (_createCache_createContainer) {
      if (_createCache_createContainer.containsKey(_cacheKey)) {
        return _createCache_createContainer.get(_cacheKey);
      }
      GraphContainer _createGraphContainer = this.factory.createGraphContainer();
      _result = _createGraphContainer;
      _createCache_createContainer.put(_cacheKey, _result);
    }
    _init_createContainer(_result, mc);
    return _result;
  }
  
  private final HashMap<ArrayList<?>, GraphContainer> _createCache_createContainer = CollectionLiterals.newHashMap();
  
  private void _init_createContainer(final GraphContainer it, final ModelComponent mc) {
    it.setComponent(mc);
    Graph _createStateMachine = this.createStateMachine(mc);
    it.setGraph(_createStateMachine);
  }
  
  private Graph createStateMachine(final ModelComponent mc) {
    Graph _xblockexpression = null;
    {
      final StateGraph superFSM = this.fsmHelpers.getSuperStateMachine(mc);
      Graph _xifexpression = null;
      if ((superFSM != null)) {
        Graph _xblockexpression_1 = null;
        {
          ModelComponent _modelComponent = this.fsmHelpers.getModelComponent(superFSM);
          final Graph baseGraph = this.createStateMachine(_modelComponent);
          this.inheritanceLevel++;
          StateGraph _actualStateMachine = this.fsmHelpers.getActualStateMachine(mc);
          _xblockexpression_1 = this.mergeGraph(baseGraph, _actualStateMachine);
        }
        _xifexpression = _xblockexpression_1;
      } else {
        Graph _xifexpression_1 = null;
        StateGraph _actualStateMachine = this.fsmHelpers.getActualStateMachine(mc);
        boolean _tripleNotEquals = (_actualStateMachine != null);
        if (_tripleNotEquals) {
          StateGraph _actualStateMachine_1 = this.fsmHelpers.getActualStateMachine(mc);
          Graph _createGraph = null;
          if (_actualStateMachine_1!=null) {
            _createGraph=this.createGraph(_actualStateMachine_1);
          }
          _xifexpression_1 = _createGraph;
        }
        _xifexpression = _xifexpression_1;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  private Graph mergeGraph(final Graph graph, final StateGraph sg) {
    EList<Node> _nodes = graph.getNodes();
    final Consumer<Node> _function = new Consumer<Node>() {
      @Override
      public void accept(final Node it) {
        it.setInherited(true);
      }
    };
    _nodes.forEach(_function);
    EList<Link> _links = graph.getLinks();
    final Consumer<Link> _function_1 = new Consumer<Link>() {
      @Override
      public void accept(final Link it) {
        it.setInherited(true);
      }
    };
    _links.forEach(_function_1);
    this.createContents(graph, sg);
    graph.setStateGraph(sg);
    return graph;
  }
  
  private Graph createGraph(final StateGraph sg) {
    final ArrayList<?> _cacheKey = CollectionLiterals.newArrayList(sg);
    final Graph _result;
    synchronized (_createCache_createGraph) {
      if (_createCache_createGraph.containsKey(_cacheKey)) {
        return _createCache_createGraph.get(_cacheKey);
      }
      Graph _createGraph = this.factory.createGraph();
      _result = _createGraph;
      _createCache_createGraph.put(_cacheKey, _result);
    }
    _init_createGraph(_result, sg);
    return _result;
  }
  
  private final HashMap<ArrayList<?>, Graph> _createCache_createGraph = CollectionLiterals.newHashMap();
  
  private void _init_createGraph(final Graph it, final StateGraph sg) {
    it.setStateGraph(sg);
    this.createContents(it, sg);
  }
  
  private void createContents(final Graph graph, final StateGraph sg) {
    EList<Node> _nodes = graph.getNodes();
    EList<ChoicePoint> _chPoints = sg.getChPoints();
    final Function1<ChoicePoint, Node> _function = new Function1<ChoicePoint, Node>() {
      @Override
      public Node apply(final ChoicePoint it) {
        return BasicFsmGenBuilder.this.createNode(it);
      }
    };
    List<Node> _map = ListExtensions.<ChoicePoint, Node>map(_chPoints, _function);
    _nodes.addAll(_map);
    EList<Node> _nodes_1 = graph.getNodes();
    EList<TrPoint> _trPoints = sg.getTrPoints();
    final Function1<TrPoint, Node> _function_1 = new Function1<TrPoint, Node>() {
      @Override
      public Node apply(final TrPoint it) {
        return BasicFsmGenBuilder.this.createNode(it);
      }
    };
    List<Node> _map_1 = ListExtensions.<TrPoint, Node>map(_trPoints, _function_1);
    _nodes_1.addAll(_map_1);
    EList<Node> _nodes_2 = graph.getNodes();
    EList<State> _states = sg.getStates();
    Iterable<SimpleState> _filter = Iterables.<SimpleState>filter(_states, SimpleState.class);
    final Function1<SimpleState, Node> _function_2 = new Function1<SimpleState, Node>() {
      @Override
      public Node apply(final SimpleState it) {
        return BasicFsmGenBuilder.this.createNode(it);
      }
    };
    Iterable<Node> _map_2 = IterableExtensions.<SimpleState, Node>map(_filter, _function_2);
    Iterables.<Node>addAll(_nodes_2, _map_2);
    EList<Link> _links = graph.getLinks();
    EList<Transition> _transitions = sg.getTransitions();
    final Function1<Transition, Link> _function_3 = new Function1<Transition, Link>() {
      @Override
      public Link apply(final Transition it) {
        return BasicFsmGenBuilder.this.createLink(it);
      }
    };
    List<Link> _map_3 = ListExtensions.<Transition, Link>map(_transitions, _function_3);
    _links.addAll(_map_3);
    EList<State> _states_1 = sg.getStates();
    Iterable<RefinedState> _filter_1 = Iterables.<RefinedState>filter(_states_1, RefinedState.class);
    final Consumer<RefinedState> _function_4 = new Consumer<RefinedState>() {
      @Override
      public void accept(final RefinedState it) {
        BasicFsmGenBuilder.this.handleRefinedState(it);
      }
    };
    _filter_1.forEach(_function_4);
    EList<RefinedTransition> _refinedTransitions = sg.getRefinedTransitions();
    final Consumer<RefinedTransition> _function_5 = new Consumer<RefinedTransition>() {
      @Override
      public void accept(final RefinedTransition it) {
        BasicFsmGenBuilder.this.handleRefinedTransition(it);
      }
    };
    _refinedTransitions.forEach(_function_5);
  }
  
  private void handleRefinedState(final RefinedState s) {
    SimpleState _finalTarget = this.fsmHelpers.getFinalTarget(s);
    final Node refinedNode = this.createNode(_finalTarget);
    StateGraph _subgraph = s.getSubgraph();
    boolean _tripleNotEquals = (_subgraph != null);
    if (_tripleNotEquals) {
      Graph _subgraph_1 = refinedNode.getSubgraph();
      boolean _tripleNotEquals_1 = (_subgraph_1 != null);
      if (_tripleNotEquals_1) {
        Graph _subgraph_2 = refinedNode.getSubgraph();
        StateGraph _subgraph_3 = s.getSubgraph();
        this.mergeGraph(_subgraph_2, _subgraph_3);
      } else {
        StateGraph _subgraph_4 = s.getSubgraph();
        Graph _createGraph = this.createGraph(_subgraph_4);
        refinedNode.setSubgraph(_createGraph);
      }
    }
    final Node target = refinedNode;
    target.setStateGraphNode(s);
  }
  
  private void handleRefinedTransition(final RefinedTransition t) {
    Transition _target = t.getTarget();
    Link _createLink = this.createLink(_target);
    _createLink.setTransition(t);
  }
  
  private Link createLink(final Transition tr) {
    final ArrayList<?> _cacheKey = CollectionLiterals.newArrayList(tr);
    final Link _result;
    synchronized (_createCache_createLink) {
      if (_createCache_createLink.containsKey(_cacheKey)) {
        return _createCache_createLink.get(_cacheKey);
      }
      Link _createLink = this.factory.createLink();
      _result = _createLink;
      _createCache_createLink.put(_cacheKey, _result);
    }
    _init_createLink(_result, tr);
    return _result;
  }
  
  private final HashMap<ArrayList<?>, Link> _createCache_createLink = CollectionLiterals.newHashMap();
  
  private void _init_createLink(final Link it, final Transition tr) {
    it.setTransition(tr);
    Node _xifexpression = null;
    if ((tr instanceof NonInitialTransition)) {
      TransitionTerminal _from = ((NonInitialTransition)tr).getFrom();
      _xifexpression = this.createNode(_from);
    }
    it.setSource(_xifexpression);
    TransitionTerminal _to = tr.getTo();
    Node _createNode = this.createNode(_to);
    it.setTarget(_createNode);
  }
  
  private Node _createNode(final StateGraphNode s) {
    final ArrayList<?> _cacheKey = CollectionLiterals.newArrayList(s);
    final Node _result;
    synchronized (_createCache_createNode) {
      if (_createCache_createNode.containsKey(_cacheKey)) {
        return _createCache_createNode.get(_cacheKey);
      }
      Node _createNode = this.factory.createNode();
      _result = _createNode;
      _createCache_createNode.put(_cacheKey, _result);
    }
    _init_createNode(_result, s);
    return _result;
  }
  
  private final HashMap<ArrayList<?>, Node> _createCache_createNode = CollectionLiterals.newHashMap();
  
  private void _init_createNode(final Node it, final StateGraphNode s) {
    it.setStateGraphNode(s);
    it.setInheritanceLevel(this.inheritanceLevel);
    Graph _xifexpression = null;
    if ((s instanceof State)) {
      StateGraph _subgraph = ((State)s).getSubgraph();
      Graph _createGraph = null;
      if (_subgraph!=null) {
        _createGraph=this.createGraph(_subgraph);
      }
      _xifexpression = _createGraph;
    }
    it.setSubgraph(_xifexpression);
  }
  
  private Node _createNode(final ChoicepointTerminal tt) {
    ChoicePoint _cp = tt.getCp();
    return this.createNode(_cp);
  }
  
  private Node _createNode(final TrPointTerminal tt) {
    TrPoint _trPoint = tt.getTrPoint();
    return this.createNode(_trPoint);
  }
  
  private Node _createNode(final StateTerminal tt) {
    State _state = tt.getState();
    return this.createNode(_state);
  }
  
  private Node _createNode(final SubStateTrPointTerminal tt) {
    TrPoint _trPoint = tt.getTrPoint();
    return this.createNode(_trPoint);
  }
  
  private Node createNode(final EObject tt) {
    if (tt instanceof ChoicepointTerminal) {
      return _createNode((ChoicepointTerminal)tt);
    } else if (tt instanceof StateGraphNode) {
      return _createNode((StateGraphNode)tt);
    } else if (tt instanceof StateTerminal) {
      return _createNode((StateTerminal)tt);
    } else if (tt instanceof SubStateTrPointTerminal) {
      return _createNode((SubStateTrPointTerminal)tt);
    } else if (tt instanceof TrPointTerminal) {
      return _createNode((TrPointTerminal)tt);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(tt).toString());
    }
  }
}

Back to the top