Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c3aa2b4b1050f210d7867ca6aa40a94ee0697f9 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
 * 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.generator.generic;

import org.eclipse.emf.common.util.EList;
import org.eclipse.etrice.core.fsm.fSM.DetailCode;
import org.eclipse.etrice.core.fsm.fSM.Guard;
import org.eclipse.etrice.core.fsm.fSM.GuardedTransition;
import org.eclipse.etrice.core.fsm.fSM.ModelComponent;
import org.eclipse.etrice.core.fsm.fSM.NonInitialTransition;
import org.eclipse.etrice.core.fsm.fSM.State;
import org.eclipse.etrice.core.fsm.fSM.Transition;
import org.eclipse.etrice.core.fsm.fSM.Trigger;
import org.eclipse.etrice.core.fsm.fSM.TriggeredTransition;
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.ExpandedModelComponent;
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.ExpandedRefinedState;
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.TransitionChain;
import org.eclipse.etrice.generator.fsm.generic.AbstractStateMachineGenerator;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

/**
 * A target language independent generator of the state machine implementation
 */
@SuppressWarnings("all")
public class GenericStateMachineGenerator extends AbstractStateMachineGenerator {
  /**
   * generates the code of the whole state machine, consisting of constants + methods
   * 
   * @param xpmc the {@link ExpandedModelComponent}
   * @return the generated code
   */
  public CharSequence genStateMachine(final ExpandedModelComponent xpmc) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _genStateMachineConstants = this.genStateMachineConstants(xpmc);
    _builder.append(_genStateMachineConstants, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _genStateMachineMethods = this.genStateMachineMethods(xpmc, true);
    _builder.append(_genStateMachineMethods, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  /**
   * generates the constants for the state machine
   */
  public CharSequence genStateMachineConstants(final ExpandedModelComponent xpmc) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* state IDs */");
    _builder.newLine();
    String _genStateIdConstants = this.genStateIdConstants(xpmc);
    _builder.append(_genStateIdConstants, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/* transition chains */");
    _builder.newLine();
    String _genTransitionChainConstants = this.genTransitionChainConstants(xpmc);
    _builder.append(_genTransitionChainConstants, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/* triggers */");
    _builder.newLine();
    String _genTriggerConstants = this.genTriggerConstants(xpmc);
    _builder.append(_genTriggerConstants, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  /**
   * generates the methods for the state machine codes and state switching
   * 
   * @param xpmc the {@link ExpandedModelComponent}
   * @param generateImplemenation or declaration only
   * @return the generated code
   */
  public CharSequence genStateMachineMethods(final ExpandedModelComponent xpmc, final boolean generateImplementation) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _genExtra = this.genExtra(xpmc, generateImplementation);
    _builder.append(_genExtra, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/* Entry and Exit Codes */");
    _builder.newLine();
    String _genEntryAndExitCodes = this.genEntryAndExitCodes(xpmc, generateImplementation);
    _builder.append(_genEntryAndExitCodes, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/* Action Codes */");
    _builder.newLine();
    String _genActionCodes = this.genActionCodes(xpmc, generateImplementation);
    _builder.append(_genActionCodes, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("/* State Switch Methods */");
    _builder.newLine();
    String _genStateSwitchMethods = this.genStateSwitchMethods(xpmc, generateImplementation);
    _builder.append(_genStateSwitchMethods, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  /**
   * generate a transition guard if applicable
   * 
   * @param tt a {@link TriggeredTransition}
   * @param trigger a trigger string
   * @param xpmc an expanded actor class
   * @return the generated code
   */
  public String guard(final TriggeredTransition tt, final String trigger, final ExpandedModelComponent mc) {
    String _xblockexpression = null;
    {
      EList<Trigger> _triggers = tt.getTriggers();
      final Function1<Trigger, Boolean> _function = new Function1<Trigger, Boolean>() {
        public Boolean apply(final Trigger e) {
          return Boolean.valueOf(mc.isMatching(e, trigger));
        }
      };
      final Trigger tr = IterableExtensions.<Trigger>findFirst(_triggers, _function);
      StringConcatenation _builder = new StringConcatenation();
      {
        boolean _hasGuard = this._fSMHelpers.hasGuard(tr);
        if (_hasGuard) {
          _builder.append("if (");
          Guard _guard = tr.getGuard();
          DetailCode _guard_1 = _guard.getGuard();
          String _translatedCode = this.translator.getTranslatedCode(_guard_1);
          _builder.append(_translatedCode, "");
          _builder.append(")");
          _builder.newLineIfNotEmpty();
        }
      }
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  public String guard(final GuardedTransition tt, final String trigger, final ExpandedModelComponent mc) {
    StringConcatenation _builder = new StringConcatenation();
    DetailCode _guard = tt.getGuard();
    String _translatedCode = this.translator.getTranslatedCode(_guard);
    _builder.append(_translatedCode, "");
    _builder.newLineIfNotEmpty();
    return _builder.toString();
  }
  
  public String genActionCodeMethod(final ExpandedModelComponent xpmc, final Transition tr, final boolean generateImplementation) {
    String _xblockexpression = null;
    {
      EList<TransitionChain> chain = xpmc.getChains(tr);
      boolean _and = false;
      boolean _isEmpty = chain.isEmpty();
      boolean _not = (!_isEmpty);
      if (!_not) {
        _and = false;
      } else {
        final Function1<TransitionChain, Boolean> _function = new Function1<TransitionChain, Boolean>() {
          public Boolean apply(final TransitionChain it) {
            boolean _and = false;
            Transition _transition = it.getTransition();
            if (!(_transition instanceof NonInitialTransition)) {
              _and = false;
            } else {
              _and = (!(it.getTransition() instanceof GuardedTransition));
            }
            return Boolean.valueOf(_and);
          }
        };
        boolean _forall = IterableExtensions.<TransitionChain>forall(chain, _function);
        _and = _forall;
      }
      boolean hasArgs = _and;
      String _className = this.getClassName(xpmc);
      final String opScope = this.langExt.operationScope(_className, false);
      String _xifexpression = null;
      boolean _usesInheritance = this.langExt.usesInheritance();
      if (_usesInheritance) {
        _xifexpression = opScope;
      } else {
        _xifexpression = "";
      }
      final String opScopePriv = _xifexpression;
      String _pointerLiteral = this.langExt.pointerLiteral();
      final String ifItemPtr = ("InterfaceItemBase" + _pointerLiteral);
      String _xifexpression_1 = null;
      boolean _usesPointers = this.langExt.usesPointers();
      if (_usesPointers) {
        _xifexpression_1 = ("const " + ifItemPtr);
      } else {
        _xifexpression_1 = ifItemPtr;
      }
      final String constIfItemPtr = _xifexpression_1;
      String _xifexpression_2 = null;
      if (generateImplementation) {
        StringConcatenation _builder = new StringConcatenation();
        String _accessLevelProtected = this.langExt.accessLevelProtected();
        _builder.append(_accessLevelProtected, "");
        _builder.append("void ");
        _builder.append(opScopePriv, "");
        String _actionCodeOperationName = this._codegenHelpers.getActionCodeOperationName(tr);
        _builder.append(_actionCodeOperationName, "");
        _builder.append("(");
        String _className_1 = this.getClassName(xpmc);
        String _selfPointer = this.langExt.selfPointer(_className_1, hasArgs);
        _builder.append(_selfPointer, "");
        {
          if (hasArgs) {
            _builder.append(constIfItemPtr, "");
            _builder.append(" ifitem");
            String _generateArgumentList = this.transitionChainGenerator.generateArgumentList(xpmc, tr);
            _builder.append(_generateArgumentList, "");
          }
        }
        _builder.append(") {");
        _builder.newLineIfNotEmpty();
        _builder.append("    ");
        DetailCode _action = tr.getAction();
        String _translatedCode = this.translator.getTranslatedCode(_action);
        _builder.append(_translatedCode, "    ");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
        _xifexpression_2 = _builder.toString();
      } else {
        StringConcatenation _builder_1 = new StringConcatenation();
        String _accessLevelProtected_1 = this.langExt.accessLevelProtected();
        _builder_1.append(_accessLevelProtected_1, "");
        String _makeOverridable = this.langExt.makeOverridable();
        _builder_1.append(_makeOverridable, "");
        _builder_1.append("void ");
        String _actionCodeOperationName_1 = this._codegenHelpers.getActionCodeOperationName(tr);
        _builder_1.append(_actionCodeOperationName_1, "");
        _builder_1.append("(");
        String _className_2 = this.getClassName(xpmc);
        String _selfPointer_1 = this.langExt.selfPointer(_className_2, hasArgs);
        _builder_1.append(_selfPointer_1, "");
        {
          if (hasArgs) {
            _builder_1.append(constIfItemPtr, "");
            _builder_1.append(" ifitem");
            String _generateArgumentList_1 = this.transitionChainGenerator.generateArgumentList(xpmc, tr);
            _builder_1.append(_generateArgumentList_1, "");
          }
        }
        _builder_1.append(");");
        _builder_1.newLineIfNotEmpty();
        _xifexpression_2 = _builder_1.toString();
      }
      _xblockexpression = _xifexpression_2;
    }
    return _xblockexpression;
  }
  
  /**
   * generate action code method implementations or declarations
   * 
   * @param xpax the {@link ExpandedModelComponent}
   * @param state the {@link State}
   * @param generateImplementation if only declarations should be generated then <code>false</code> has to be passed
   * @return the generated code
   */
  public String genActionCodeMethods(final ExpandedModelComponent xpmc, final State state, final boolean generateImplementation) {
    String _xblockexpression = null;
    {
      final ModelComponent mc = xpmc.getModelComponent();
      String _className = this.getClassName(mc);
      final String selfPtr = this.langExt.selfPointer(_className, false);
      String _className_1 = this.getClassName(mc);
      final String opScope = this.langExt.operationScope(_className_1, false);
      String _xifexpression = null;
      boolean _usesInheritance = this.langExt.usesInheritance();
      if (_usesInheritance) {
        _xifexpression = opScope;
      } else {
        _xifexpression = "";
      }
      final String opScopePriv = _xifexpression;
      final String entryOp = this._codegenHelpers.getEntryCodeOperationName(state);
      final String exitOp = this._codegenHelpers.getExitCodeOperationName(state);
      final String doOp = this._codegenHelpers.getDoCodeOperationName(state);
      DetailCode _entryCode = state.getEntryCode();
      String entry = this.translator.getTranslatedCode(_entryCode);
      DetailCode _exitCode = state.getExitCode();
      String exit = this.translator.getTranslatedCode(_exitCode);
      DetailCode _doCode = state.getDoCode();
      String docode = this.translator.getTranslatedCode(_doCode);
      if ((state instanceof ExpandedRefinedState)) {
        final ExpandedRefinedState rs = ((ExpandedRefinedState) state);
        DetailCode _inheritedEntry = rs.getInheritedEntry();
        final String inhEntry = this.translator.getTranslatedCode(_inheritedEntry);
        DetailCode _inheritedExit = rs.getInheritedExit();
        final String inhExit = this.translator.getTranslatedCode(_inheritedExit);
        DetailCode _inheritedDo = rs.getInheritedDo();
        final String inhDo = this.translator.getTranslatedCode(_inheritedDo);
        boolean _usesInheritance_1 = this.langExt.usesInheritance();
        if (_usesInheritance_1) {
          ModelComponent _base = mc.getBase();
          final String baseName = this.getClassName(_base);
          DetailCode _inheritedEntry_1 = rs.getInheritedEntry();
          boolean _hasDetailCode = this._fSMHelpers.hasDetailCode(_inheritedEntry_1);
          if (_hasDetailCode) {
            String _superCall = this.langExt.superCall(baseName, entryOp, "");
            String _plus = (_superCall + entry);
            entry = _plus;
          }
          DetailCode _inheritedExit_1 = rs.getInheritedExit();
          boolean _hasDetailCode_1 = this._fSMHelpers.hasDetailCode(_inheritedExit_1);
          if (_hasDetailCode_1) {
            String _superCall_1 = this.langExt.superCall(baseName, exitOp, "");
            String _plus_1 = (exit + _superCall_1);
            exit = _plus_1;
          }
          DetailCode _inheritedDo_1 = rs.getInheritedDo();
          boolean _hasDetailCode_2 = this._fSMHelpers.hasDetailCode(_inheritedDo_1);
          if (_hasDetailCode_2) {
            String _superCall_2 = this.langExt.superCall(baseName, doOp, "");
            String _plus_2 = (_superCall_2 + docode);
            docode = _plus_2;
          }
        } else {
          entry = (inhEntry + entry);
          exit = (exit + inhExit);
          docode = (inhDo + docode);
        }
      }
      StringConcatenation _builder = new StringConcatenation();
      {
        boolean _isEmpty = entry.isEmpty();
        boolean _not = (!_isEmpty);
        if (_not) {
          {
            if (generateImplementation) {
              String _accessLevelProtected = this.langExt.accessLevelProtected();
              _builder.append(_accessLevelProtected, "");
              _builder.append("void ");
              _builder.append(opScopePriv, "");
              _builder.append(entryOp, "");
              _builder.append("(");
              _builder.append(selfPtr, "");
              _builder.append(") {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append(entry, "\t");
              _builder.newLineIfNotEmpty();
              _builder.append("}");
              _builder.newLine();
            } else {
              String _accessLevelProtected_1 = this.langExt.accessLevelProtected();
              _builder.append(_accessLevelProtected_1, "");
              String _makeOverridable = this.langExt.makeOverridable();
              _builder.append(_makeOverridable, "");
              _builder.append("void ");
              _builder.append(entryOp, "");
              _builder.append("(");
              _builder.append(selfPtr, "");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
        }
      }
      {
        boolean _isEmpty_1 = exit.isEmpty();
        boolean _not_1 = (!_isEmpty_1);
        if (_not_1) {
          {
            if (generateImplementation) {
              String _accessLevelProtected_2 = this.langExt.accessLevelProtected();
              _builder.append(_accessLevelProtected_2, "");
              _builder.append("void ");
              _builder.append(opScopePriv, "");
              _builder.append(exitOp, "");
              _builder.append("(");
              _builder.append(selfPtr, "");
              _builder.append(") {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append(exit, "\t");
              _builder.newLineIfNotEmpty();
              _builder.append("}");
              _builder.newLine();
            } else {
              String _accessLevelProtected_3 = this.langExt.accessLevelProtected();
              _builder.append(_accessLevelProtected_3, "");
              String _makeOverridable_1 = this.langExt.makeOverridable();
              _builder.append(_makeOverridable_1, "");
              _builder.append("void ");
              _builder.append(exitOp, "");
              _builder.append("(");
              _builder.append(selfPtr, "");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
        }
      }
      {
        boolean _isEmpty_2 = docode.isEmpty();
        boolean _not_2 = (!_isEmpty_2);
        if (_not_2) {
          {
            if (generateImplementation) {
              String _accessLevelProtected_4 = this.langExt.accessLevelProtected();
              _builder.append(_accessLevelProtected_4, "");
              _builder.append(" void ");
              _builder.append(opScopePriv, "");
              _builder.append(doOp, "");
              _builder.append("(");
              _builder.append(selfPtr, "");
              _builder.append(") {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append(docode, "\t");
              _builder.newLineIfNotEmpty();
              _builder.append("}");
              _builder.newLine();
            } else {
              String _accessLevelProtected_5 = this.langExt.accessLevelProtected();
              _builder.append(_accessLevelProtected_5, "");
              String _makeOverridable_2 = this.langExt.makeOverridable();
              _builder.append(_makeOverridable_2, "");
              _builder.append("void ");
              _builder.append(doOp, "");
              _builder.append("(");
              _builder.append(selfPtr, "");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
        }
      }
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  /**
   * let derived class add extra code after definition of constants
   * 
   * @param xpmc an expanded actor class
   * @param generateImplementation or declaration only
   * @return the generated code
   */
  public CharSequence genExtra(final ExpandedModelComponent xpmc, final boolean generateImplementation) {
    StringConcatenation _builder = new StringConcatenation();
    return _builder;
  }
}

Back to the top