-
Notifications
You must be signed in to change notification settings - Fork 11
/
TextualScenarioHelper.java
241 lines (226 loc) · 9.17 KB
/
TextualScenarioHelper.java
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
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
******************************************************************************/
/**
* Copyright (c) 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*/
package org.polarsys.capella.scenario.editor.dsl.helpers;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.polarsys.capella.core.data.fa.ComponentExchange;
import org.polarsys.capella.core.data.fa.FunctionalExchange;
import org.polarsys.capella.core.data.information.AbstractEventOperation;
import org.polarsys.capella.core.model.helpers.CapellaElementExt;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Block;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Element;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.LostMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Message;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Model;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Operand;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Participant;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage;
import org.polarsys.capella.scenario.editor.helper.EmbeddedEditorInstanceHelper;
/**
* See https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#content-assist
* on how to customize the content assistant.
*/
@SuppressWarnings("all")
public class TextualScenarioHelper {
private static final String TYPE_FE = "FE";
private static final String TYPE_CE = "CE";
/**
* calculate the type of exchanges allowed to be declared in the text
*/
public static Object getScenarioAllowedExchangesType(final EList<Element> elements) {
boolean _isESScenario = EmbeddedEditorInstanceHelper.isESScenario();
if (_isESScenario) {
boolean _isCEScenario = EmbeddedEditorInstanceHelper.isCEScenario();
if (_isCEScenario) {
return TextualScenarioHelper.TYPE_CE;
}
boolean _isFEScenario = EmbeddedEditorInstanceHelper.isFEScenario();
if (_isFEScenario) {
return TextualScenarioHelper.TYPE_FE;
}
for (final Element element : elements) {
{
if ((element instanceof Message)) {
Message message = ((Message) element);
return TextualScenarioHelper.getMessageExchangeType(message);
}
if ((element instanceof CombinedFragment)) {
CombinedFragment combinedFragment = ((CombinedFragment) element);
return TextualScenarioHelper.getScenarioAllowedExchangesType(combinedFragment.getBlock().getBlockElements());
}
}
}
}
return null;
}
/**
* we return CE or FE or null in case we allow both or other type
*/
public static Object getMessageExchangeType(final Message message) {
List<AbstractEventOperation> exchangesAvailable = EmbeddedEditorInstanceHelper.getExchangeMessages(TextualScenarioHelper.getSourceOfMessage(message),
TextualScenarioHelper.getTargetOfMessage(message));
HashSet<Object> _newHashSet = CollectionLiterals.<Object>newHashSet();
Set<Object> allowedTypes = ((Set<Object>) _newHashSet);
for (final AbstractEventOperation exchange : exchangesAvailable) {
if (((message.getName() != null) && message.getName().equals(CapellaElementExt.getName(exchange)))) {
String type = TextualScenarioHelper.getExchangeType(exchange);
if ((type != null)) {
allowedTypes.add(type);
}
}
}
int _size = allowedTypes.size();
boolean _equals = (_size == 1);
if (_equals) {
final Set<Object> _converted_allowedTypes = (Set<Object>)allowedTypes;
return ((Object[])Conversions.unwrapArray(_converted_allowedTypes, Object.class))[0];
}
return null;
}
/**
* we return a list of available exchanges CE or FE
*/
public static Set getAllMessageExchangeType(final Message message) {
HashSet<Object> _newHashSet = CollectionLiterals.<Object>newHashSet();
Set<Object> allowedTypes = ((Set<Object>) _newHashSet);
List<AbstractEventOperation> exchangesAvailable = EmbeddedEditorInstanceHelper.getExchangeMessages(TextualScenarioHelper.getSourceOfMessage(message),
TextualScenarioHelper.getTargetOfMessage(message));
for (final AbstractEventOperation exchange : exchangesAvailable) {
if (((message.getName() != null) && message.getName().equals(CapellaElementExt.getName(exchange)))) {
String type = TextualScenarioHelper.getExchangeType(exchange);
if ((type != null)) {
allowedTypes.add(type);
}
}
}
return allowedTypes;
}
public static String getExchangeType(final EObject exchangeElement) {
if ((exchangeElement instanceof ComponentExchange)) {
return TextualScenarioHelper.TYPE_CE;
} else {
if ((exchangeElement instanceof FunctionalExchange)) {
return TextualScenarioHelper.TYPE_FE;
}
}
return null;
}
private static String getSourceOfMessage(final Message message) {
String source = ((String) null);
if ((message instanceof SequenceMessage)) {
source = ((SequenceMessage) message).getSource();
} else {
if ((message instanceof LostMessage)) {
source = ((LostMessage) message).getSource();
}
}
return source;
}
private static String getTargetOfMessage(final Message message) {
String target = ((String) null);
if ((message instanceof SequenceMessage)) {
target = ((SequenceMessage) message).getTarget();
} else {
if ((message instanceof FoundMessage)) {
target = ((FoundMessage) message).getTarget();
}
}
return target;
}
public static EList<Participant> participantsDefinedBefore(final Model rootModel) {
return rootModel.getParticipants();
}
public static ArrayList<String> participantsDefinedBeforeNames(final EObject element) {
ArrayList<String> participantsNames = CollectionLiterals.<String>newArrayList();
EObject container = TextualScenarioHelper.getModelContainer(element);
if ((container instanceof Model)) {
Model model = ((Model) container);
EList<Participant> participants = TextualScenarioHelper.participantsDefinedBefore(model);
for (final Participant participant : participants) {
participantsNames.add(participant.getName());
}
}
return participantsNames;
}
public static boolean isParticipantKeyword(final String keywordValue) {
List<String> participantsKeywords = EmbeddedEditorInstanceHelper.PARTICIPANT_KEYWORDS;
return participantsKeywords.contains(keywordValue);
}
/**
* get all elements on the same level as modelContainer
*/
public static List<Element> getElements(final EObject modelContainer) {
if ((modelContainer instanceof Model)) {
return ((Model) modelContainer).getElements();
}
if ((modelContainer instanceof CombinedFragment)) {
ArrayList<Element> elements = CollectionLiterals.<Element>newArrayList();
elements.addAll(((CombinedFragment) modelContainer).getBlock().getBlockElements());
EList<Operand> operands = ((CombinedFragment) modelContainer).getOperands();
for (final Operand operand : operands) {
elements.addAll(operand.getBlock().getBlockElements());
}
return elements;
}
if ((modelContainer instanceof Block)) {
return ((Block) modelContainer).getBlockElements();
}
return null;
}
/**
* get the root model that contains the object given as para
*/
public static EObject getModelContainer(final EObject object) {
if ((object instanceof Model)) {
return ((Model) object);
}
if ((object != null)) {
return TextualScenarioHelper.getModelContainer(object.eContainer());
}
return null;
}
/**
* get the logic container of an element
* (the container can be the model or a combined fragment)
*/
public static EObject getDirectContainer(final EObject element) {
if ((element != null)) {
EObject container = element.eContainer();
if (((container instanceof Model) || (container instanceof CombinedFragment))) {
return container;
}
return TextualScenarioHelper.getDirectContainer(container);
}
return null;
}
}