aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/xtend-gen/hu/bme/mit/inf/dslreasoner/domains/cps/dse/CpsStateCoder.java
blob: 3ae8e828e7b381cfb48b7d79772ae4eb7bd957b4 (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
package hu.bme.mit.inf.dslreasoner.domains.cps.dse;

import com.google.common.base.Objects;
import hu.bme.mit.inf.dslreasoner.domains.cps.ApplicationInstance;
import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem;
import hu.bme.mit.inf.dslreasoner.domains.cps.HostInstance;
import hu.bme.mit.inf.dslreasoner.domains.cps.HostType;
import hu.bme.mit.inf.dslreasoner.domains.cps.Request;
import hu.bme.mit.inf.dslreasoner.domains.cps.Requirement;
import java.util.Arrays;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.viatra.dse.statecode.IStateCoder;
import org.eclipse.viatra.dse.statecode.IStateCoderFactory;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtend2.lib.StringConcatenationClient;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("all")
public class CpsStateCoder implements IStateCoder {
  public static class Factory implements IStateCoderFactory {
    @Override
    public IStateCoder createStateCoder() {
      return new CpsStateCoder();
    }
  }
  
  private CyberPhysicalSystem cps;
  
  protected CpsStateCoder() {
  }
  
  @Override
  public void init(final Notifier notifier) {
    CyberPhysicalSystem _switchResult = null;
    boolean _matched = false;
    if (notifier instanceof ResourceSet) {
      _matched=true;
      _switchResult = this.getCpsFromResourceSet(((ResourceSet)notifier));
    }
    if (!_matched) {
      if (notifier instanceof Resource) {
        _matched=true;
        _switchResult = this.getCpsFromResource(((Resource)notifier));
      }
    }
    if (!_matched) {
      if (notifier instanceof CyberPhysicalSystem) {
        _matched=true;
        _switchResult = ((CyberPhysicalSystem)notifier);
      }
    }
    if (!_matched) {
      throw new IllegalArgumentException("notifier is not a CyberPhysicalSystem");
    }
    this.cps = _switchResult;
  }
  
  private CyberPhysicalSystem getCpsFromResourceSet(final ResourceSet resourceSet) {
    CyberPhysicalSystem _xblockexpression = null;
    {
      boolean _isEmpty = resourceSet.getResources().isEmpty();
      if (_isEmpty) {
        throw new IllegalArgumentException("No Resource in ResourceSet");
      }
      final Resource resource = IterableExtensions.<Resource>head(resourceSet.getResources());
      _xblockexpression = this.getCpsFromResource(resource);
    }
    return _xblockexpression;
  }
  
  private CyberPhysicalSystem getCpsFromResource(final Resource resource) {
    CyberPhysicalSystem _xblockexpression = null;
    {
      boolean _isEmpty = resource.getContents().isEmpty();
      if (_isEmpty) {
        throw new IllegalArgumentException("No EObject in Resource");
      }
      final EObject cps = IterableExtensions.<EObject>head(resource.getContents());
      CyberPhysicalSystem _xifexpression = null;
      if ((cps instanceof CyberPhysicalSystem)) {
        _xifexpression = ((CyberPhysicalSystem)cps);
      } else {
        throw new IllegalArgumentException("EObject in Resource is not a CyberPhysicalSystem");
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  @Override
  public String createStateCode() {
    StringConcatenation _builder = new StringConcatenation();
    StringConcatenationClient _createRequestsCode = this.createRequestsCode();
    _builder.append(_createRequestsCode);
    CharSequence _createHostTypesCode = this.createHostTypesCode();
    _builder.append(_createHostTypesCode);
    return _builder.toString();
  }
  
  private StringConcatenationClient createRequestsCode() {
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        {
          EList<Request> _requests = CpsStateCoder.this.cps.getRequests();
          for(final Request request : _requests) {
            StringConcatenationClient _createRequestCode = CpsStateCoder.this.createRequestCode(request);
            _builder.append(_createRequestCode);
          }
        }
      }
    };
    return _client;
  }
  
  private StringConcatenationClient createRequestCode(final Request request) {
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("[");
        {
          EList<Requirement> _requirements = request.getRequirements();
          for(final Requirement requirement : _requirements) {
            StringConcatenationClient _createRequirementCode = CpsStateCoder.this.createRequirementCode(requirement);
            _builder.append(_createRequirementCode);
          }
        }
        _builder.append("]");
      }
    };
    return _client;
  }
  
  private StringConcatenationClient createRequirementCode(final Requirement requirement) {
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("[");
        {
          EList<ApplicationInstance> _instances = requirement.getInstances();
          boolean _hasElements = false;
          for(final ApplicationInstance app : _instances) {
            if (!_hasElements) {
              _hasElements = true;
            } else {
              _builder.appendImmediate(",", "");
            }
            String _createAppCode = CpsStateCoder.this.createAppCode(app);
            _builder.append(_createAppCode);
          }
        }
        _builder.append("]");
      }
    };
    return _client;
  }
  
  private String createAppCode(final ApplicationInstance app) {
    String _xifexpression = null;
    HostInstance _allocatedTo = app.getAllocatedTo();
    boolean _tripleEquals = (_allocatedTo == null);
    if (_tripleEquals) {
      _xifexpression = "-";
    } else {
      _xifexpression = this.createMatchArgumentCode(app.getAllocatedTo());
    }
    return _xifexpression;
  }
  
  private CharSequence createHostTypesCode() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("(");
    {
      EList<HostType> _hostTypes = this.cps.getHostTypes();
      boolean _hasElements = false;
      for(final HostType hostType : _hostTypes) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",", "");
        }
        int _size = hostType.getInstances().size();
        _builder.append(_size);
      }
    }
    _builder.append(")");
    return _builder;
  }
  
  @Override
  public String createActivationCode(final IPatternMatch match) {
    StringConcatenation _builder = new StringConcatenation();
    String _simpleName = match.specification().getSimpleName();
    _builder.append(_simpleName);
    _builder.append("(");
    {
      Object[] _array = match.toArray();
      boolean _hasElements = false;
      for(final Object arg : _array) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",", "");
        }
        String _createMatchArgumentCode = this.createMatchArgumentCode(arg);
        _builder.append(_createMatchArgumentCode);
      }
    }
    _builder.append(")");
    return _builder.toString();
  }
  
  protected String _createMatchArgumentCode(final Requirement requirement) {
    String _xblockexpression = null;
    {
      final EObject request = requirement.eContainer();
      String _xifexpression = null;
      if ((request instanceof Request)) {
        String _xblockexpression_1 = null;
        {
          EObject _eContainer = ((Request)request).eContainer();
          boolean _notEquals = (!Objects.equal(_eContainer, this.cps));
          if (_notEquals) {
            throw new IllegalArgumentException("Request is not contained in the CPS");
          }
          final int requestIndex = this.cps.getRequests().indexOf(request);
          final int requirementIndex = ((Request)request).getRequirements().indexOf(requirement);
          String _plus = (Integer.valueOf(requestIndex) + ".");
          _xblockexpression_1 = (_plus + Integer.valueOf(requirementIndex));
        }
        _xifexpression = _xblockexpression_1;
      } else {
        throw new IllegalArgumentException("Requirement is not contained in a request");
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  protected String _createMatchArgumentCode(final ApplicationInstance app) {
    String _xblockexpression = null;
    {
      final Requirement requirement = app.getRequirement();
      if ((requirement == null)) {
        throw new IllegalArgumentException("Application instance is not associated with a requirement");
      }
      final int instanceIndex = requirement.getInstances().indexOf(app);
      String _createMatchArgumentCode = this.createMatchArgumentCode(requirement);
      String _plus = (_createMatchArgumentCode + ".");
      _xblockexpression = (_plus + Integer.valueOf(instanceIndex));
    }
    return _xblockexpression;
  }
  
  protected String _createMatchArgumentCode(final HostInstance host) {
    String _xblockexpression = null;
    {
      final EObject hostType = host.eContainer();
      String _xifexpression = null;
      if ((hostType instanceof HostType)) {
        String _xblockexpression_1 = null;
        {
          final int hostIndex = ((HostType)hostType).getInstances().indexOf(host);
          String _createMatchArgumentCode = this.createMatchArgumentCode(hostType);
          String _plus = (_createMatchArgumentCode + ".");
          _xblockexpression_1 = (_plus + Integer.valueOf(hostIndex));
        }
        _xifexpression = _xblockexpression_1;
      } else {
        throw new IllegalArgumentException("Host is not contained in a host type");
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  protected String _createMatchArgumentCode(final HostType hostType) {
    String _xblockexpression = null;
    {
      EObject _eContainer = hostType.eContainer();
      boolean _notEquals = (!Objects.equal(_eContainer, this.cps));
      if (_notEquals) {
        throw new IllegalArgumentException("Host type is not contained in the CPS");
      }
      final int hostTypeIndex = this.cps.getHostTypes().indexOf(hostType);
      _xblockexpression = Integer.valueOf(hostTypeIndex).toString();
    }
    return _xblockexpression;
  }
  
  protected String _createMatchArgumentCode(final Object object) {
    throw new IllegalArgumentException("Unknown match argument: ");
  }
  
  protected String createMatchArgumentCode(final Object app) {
    if (app instanceof ApplicationInstance) {
      return _createMatchArgumentCode((ApplicationInstance)app);
    } else if (app instanceof HostInstance) {
      return _createMatchArgumentCode((HostInstance)app);
    } else if (app instanceof HostType) {
      return _createMatchArgumentCode((HostType)app);
    } else if (app instanceof Requirement) {
      return _createMatchArgumentCode((Requirement)app);
    } else if (app != null) {
      return _createMatchArgumentCode(app);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(app).toString());
    }
  }
}