Skip to main content
summaryrefslogtreecommitdiffstats
blob: e949d938e0ed132ab7efdd960d78808b95f7b98a (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
/**
 * 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug (initial contribution)
 */
package org.eclipse.etrice.ui.behavior.commands;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
import com.google.inject.Injector;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
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.fsm.fSM.StateGraphNode;
import org.eclipse.etrice.core.fsm.fSM.TransitionBase;
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.NullLogger;
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.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.RoomModel;
import org.eclipse.etrice.ui.behavior.BehaviorTestActivator;
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.IteratorExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.Bundle;

@SuppressWarnings("all")
public class TestFSMGenModel extends TestBase {
  protected IDiagnostician diagnostician = new NullDiagnostician();
  
  @Extension
  protected ExtendedFsmGenBuilder builder;
  
  protected ResourceSet createResourceSetWithGc(final String roomFile, final String actorName) {
    BehaviorTestActivator _default = BehaviorTestActivator.getDefault();
    Bundle _bundle = _default.getBundle();
    this.prepare(_bundle);
    final Resource res = this.getResource(roomFile);
    EList<EObject> _contents = res.getContents();
    EObject _get = _contents.get(0);
    EList<ActorClass> _actorClasses = ((RoomModel) _get).getActorClasses();
    final Function1<ActorClass, Boolean> _function = new Function1<ActorClass, Boolean>() {
      @Override
      public Boolean apply(final ActorClass it) {
        String _name = it.getName();
        return Boolean.valueOf(_name.equals(actorName));
      }
    };
    final ActorClass actor = IterableExtensions.<ActorClass>findFirst(_actorClasses, _function);
    RoomStandaloneSetup _roomStandaloneSetup = new RoomStandaloneSetup();
    final Injector injector = _roomStandaloneSetup.createInjectorAndDoEMFRegistration();
    ExtendedFsmGenBuilder _extendedFsmGenBuilder = new ExtendedFsmGenBuilder(injector, this.diagnostician);
    this.builder = _extendedFsmGenBuilder;
    GraphContainer _createTransformedModel = this.builder.createTransformedModel(actor);
    final Procedure1<GraphContainer> _function_1 = new Procedure1<GraphContainer>() {
      @Override
      public void apply(final GraphContainer gc) {
        Assert.assertNotNull("graph context was created", gc);
        final NullLogger logger = new NullLogger();
        FsmGenChecker.check(gc, logger);
        boolean _hasErrors = logger.hasErrors();
        Assert.assertFalse("logger has no errors", _hasErrors);
      }
    };
    final GraphContainer gc = ObjectExtensions.<GraphContainer>operator_doubleArrow(_createTransformedModel, _function_1);
    ResourceSet _resourceSet = res.getResourceSet();
    URI _createURI = URI.createURI((("dummy://" + actorName) + ".fsmgen"));
    Resource _createResource = _resourceSet.createResource(_createURI);
    final Procedure1<Resource> _function_2 = new Procedure1<Resource>() {
      @Override
      public void apply(final Resource it) {
        EList<EObject> _contents = it.getContents();
        _contents.add(gc);
      }
    };
    ObjectExtensions.<Resource>operator_doubleArrow(_createResource, _function_2);
    return res.getResourceSet();
  }
  
  @Test
  public void testBaseClass() {
    final ResourceSet rs = this.createResourceSetWithGc("InheritedStateMachine.room", "Base");
    TreeIterator<Object> _allContents = EcoreUtil.<Object>getAllContents(rs, true);
    Iterator<ActorClass> _filter = Iterators.<ActorClass>filter(_allContents, ActorClass.class);
    final Function1<ActorClass, Boolean> _function = new Function1<ActorClass, Boolean>() {
      @Override
      public Boolean apply(final ActorClass it) {
        String _name = it.getName();
        return Boolean.valueOf(Objects.equal(_name, "Base"));
      }
    };
    Iterator<ActorClass> _filter_1 = IteratorExtensions.<ActorClass>filter(_filter, _function);
    final List<ActorClass> actorClasses = IteratorExtensions.<ActorClass>toList(_filter_1);
    EList<Resource> _resources = rs.getResources();
    final Function1<Resource, EList<EObject>> _function_1 = new Function1<Resource, EList<EObject>>() {
      @Override
      public EList<EObject> apply(final Resource it) {
        return it.getContents();
      }
    };
    List<EList<EObject>> _map = ListExtensions.<Resource, EList<EObject>>map(_resources, _function_1);
    Iterable<EObject> _flatten = Iterables.<EObject>concat(_map);
    Iterable<GraphContainer> _filter_2 = Iterables.<GraphContainer>filter(_flatten, GraphContainer.class);
    final GraphContainer gc = IterableExtensions.<GraphContainer>head(_filter_2);
    this.checkAgainstRoom(gc, actorClasses);
  }
  
  @Test
  public void testDerivedClass() {
    final ResourceSet rs = this.createResourceSetWithGc("InheritedStateMachine.room", "Derived");
    TreeIterator<Object> _allContents = EcoreUtil.<Object>getAllContents(rs, true);
    Iterator<ActorClass> _filter = Iterators.<ActorClass>filter(_allContents, ActorClass.class);
    final Function1<ActorClass, Boolean> _function = new Function1<ActorClass, Boolean>() {
      @Override
      public Boolean apply(final ActorClass it) {
        return Boolean.valueOf((Objects.equal(it.getName(), "Base") || Objects.equal(it.getName(), "Derived")));
      }
    };
    Iterator<ActorClass> _filter_1 = IteratorExtensions.<ActorClass>filter(_filter, _function);
    final List<ActorClass> actorClasses = IteratorExtensions.<ActorClass>toList(_filter_1);
    EList<Resource> _resources = rs.getResources();
    final Function1<Resource, EList<EObject>> _function_1 = new Function1<Resource, EList<EObject>>() {
      @Override
      public EList<EObject> apply(final Resource it) {
        return it.getContents();
      }
    };
    List<EList<EObject>> _map = ListExtensions.<Resource, EList<EObject>>map(_resources, _function_1);
    Iterable<EObject> _flatten = Iterables.<EObject>concat(_map);
    Iterable<GraphContainer> _filter_2 = Iterables.<GraphContainer>filter(_flatten, GraphContainer.class);
    final GraphContainer gc = IterableExtensions.<GraphContainer>head(_filter_2);
    this.checkAgainstRoom(gc, actorClasses);
  }
  
  @Test
  public void testDoubleDerivedClass() {
    final ResourceSet rs = this.createResourceSetWithGc("InheritedStateMachine.room", "DoubleDerived");
    TreeIterator<Object> _allContents = EcoreUtil.<Object>getAllContents(rs, true);
    Iterator<ActorClass> _filter = Iterators.<ActorClass>filter(_allContents, ActorClass.class);
    final Function1<ActorClass, Boolean> _function = new Function1<ActorClass, Boolean>() {
      @Override
      public Boolean apply(final ActorClass it) {
        return Boolean.valueOf(((Objects.equal(it.getName(), "Base") || Objects.equal(it.getName(), "Derived")) || Objects.equal(it.getName(), "DoubleDerived")));
      }
    };
    Iterator<ActorClass> _filter_1 = IteratorExtensions.<ActorClass>filter(_filter, _function);
    final List<ActorClass> actorClasses = IteratorExtensions.<ActorClass>toList(_filter_1);
    EList<Resource> _resources = rs.getResources();
    final Function1<Resource, EList<EObject>> _function_1 = new Function1<Resource, EList<EObject>>() {
      @Override
      public EList<EObject> apply(final Resource it) {
        return it.getContents();
      }
    };
    List<EList<EObject>> _map = ListExtensions.<Resource, EList<EObject>>map(_resources, _function_1);
    Iterable<EObject> _flatten = Iterables.<EObject>concat(_map);
    Iterable<GraphContainer> _filter_2 = Iterables.<GraphContainer>filter(_flatten, GraphContainer.class);
    final GraphContainer gc = IterableExtensions.<GraphContainer>head(_filter_2);
    this.checkAgainstRoom(gc, actorClasses);
  }
  
  public void checkAgainstRoom(final GraphContainer gc, final List<ActorClass> actorClasses) {
    Graph _graph = gc.getGraph();
    EList<Node> _nodes = _graph.getNodes();
    final Function1<Node, StateGraphNode> _function = new Function1<Node, StateGraphNode>() {
      @Override
      public StateGraphNode apply(final Node it) {
        return it.getStateGraphNode();
      }
    };
    List<StateGraphNode> _map = ListExtensions.<Node, StateGraphNode>map(_nodes, _function);
    Graph _graph_1 = gc.getGraph();
    EList<Link> _links = _graph_1.getLinks();
    final Function1<Link, TransitionBase> _function_1 = new Function1<Link, TransitionBase>() {
      @Override
      public TransitionBase apply(final Link it) {
        return it.getTransition();
      }
    };
    List<TransitionBase> _map_1 = ListExtensions.<Link, TransitionBase>map(_links, _function_1);
    Iterable<StateGraphItem> _plus = Iterables.<StateGraphItem>concat(_map, _map_1);
    final Set<StateGraphItem> genModelRefs = IterableExtensions.<StateGraphItem>toSet(_plus);
    ActorClass _last = IterableExtensions.<ActorClass>last(actorClasses);
    TreeIterator<Object> _allContents = EcoreUtil.<Object>getAllContents(_last, false);
    Iterator<StateGraphItem> _filter = Iterators.<StateGraphItem>filter(_allContents, StateGraphItem.class);
    final Procedure1<StateGraphItem> _function_2 = new Procedure1<StateGraphItem>() {
      @Override
      public void apply(final StateGraphItem it) {
        String _string = it.toString();
        String _plus = ("missing " + _string);
        boolean _contains = genModelRefs.contains(it);
        Assert.assertTrue(_plus, _contains);
      }
    };
    IteratorExtensions.<StateGraphItem>forEach(_filter, _function_2);
    TreeIterator<Object> _allContents_1 = EcoreUtil.<Object>getAllContents(actorClasses, true);
    Iterator<StateGraphItem> _filter_1 = Iterators.<StateGraphItem>filter(_allContents_1, StateGraphItem.class);
    final Function1<StateGraphItem, String> _function_3 = new Function1<StateGraphItem, String>() {
      @Override
      public String apply(final StateGraphItem it) {
        return it.getName();
      }
    };
    Iterator<String> _map_2 = IteratorExtensions.<StateGraphItem, String>map(_filter_1, _function_3);
    Iterable<String> _iterable = IteratorExtensions.<String>toIterable(_map_2);
    final TreeSet<String> stateGraphItemsNames = this.<String>toSortedSet(_iterable);
    Graph _graph_2 = gc.getGraph();
    EList<Node> _nodes_1 = _graph_2.getNodes();
    final Function1<Node, String> _function_4 = new Function1<Node, String>() {
      @Override
      public String apply(final Node it) {
        StateGraphNode _stateGraphNode = it.getStateGraphNode();
        return _stateGraphNode.getName();
      }
    };
    List<String> _map_3 = ListExtensions.<Node, String>map(_nodes_1, _function_4);
    Graph _graph_3 = gc.getGraph();
    EList<Link> _links_1 = _graph_3.getLinks();
    final Function1<Link, String> _function_5 = new Function1<Link, String>() {
      @Override
      public String apply(final Link it) {
        TransitionBase _transition = it.getTransition();
        return _transition.getName();
      }
    };
    List<String> _map_4 = ListExtensions.<Link, String>map(_links_1, _function_5);
    Iterable<String> _plus_1 = Iterables.<String>concat(_map_3, _map_4);
    final TreeSet<String> namesFromGenModel = this.<String>toSortedSet(_plus_1);
    Assert.assertEquals(stateGraphItemsNames, namesFromGenModel);
  }
  
  private <T extends Comparable> TreeSet<T> toSortedSet(final Iterable<T> iterable) {
    return Sets.<T>newTreeSet(iterable);
  }
}

Back to the top