-
Notifications
You must be signed in to change notification settings - Fork 11
/
TextualScenarioHelper.xtend
205 lines (186 loc) · 6.48 KB
/
TextualScenarioHelper.xtend
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
/*******************************************************************************
* 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
*******************************************************************************/
/*
* generated by Xtext 2.18.0.M3
*/
package org.polarsys.capella.scenario.editor.dsl.helpers
import org.eclipse.emf.ecore.EObject
import org.polarsys.capella.scenario.editor.helper.EmbeddedEditorInstanceHelper
import org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage
import org.polarsys.capella.core.model.helpers.CapellaElementExt
import org.eclipse.emf.common.util.EList
import org.polarsys.capella.core.data.fa.ComponentExchange
import org.polarsys.capella.core.data.fa.FunctionalExchange
import org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Model
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Block
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Element
import org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessageType
import java.util.Set
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Message
import org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage
import org.polarsys.capella.scenario.editor.dsl.textualScenario.LostMessage
/**
* See https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#content-assist
* on how to customize the content assistant.
*/
class TextualScenarioHelper {
static val TYPE_FE = "FE";
static val TYPE_CE = "CE";
/*
* calculate the type of exchanges allowed to be declared in the text
*/
def static getScenarioAllowedExchangesType(EList<Element> elements) {
if (EmbeddedEditorInstanceHelper.isESScenario()) {
if(EmbeddedEditorInstanceHelper.isCEScenario())
return TYPE_CE
if(EmbeddedEditorInstanceHelper.isFEScenario())
return TYPE_FE
for (element : elements) {
if (element instanceof Message) {
var message = element as Message
return getMessageExchangeType(message)
}
if (element instanceof CombinedFragment) {
var combinedFragment = element as CombinedFragment
return getScenarioAllowedExchangesType(combinedFragment.block.blockElements)
}
}
}
return null
}
/*
* we return CE or FE or null in case we allow both or other type
*/
def static getMessageExchangeType(Message message) {
var exchangesAvailable = EmbeddedEditorInstanceHelper.getExchangeMessages(getSourceOfMessage(message),
getTargetOfMessage(message)
)
var allowedTypes = newHashSet as Set<Object>
for(exchange : exchangesAvailable) {
if(message.name !== null && message.name.equals(CapellaElementExt.getName(exchange))) {
var type = getExchangeType(exchange)
if(type !== null)
allowedTypes.add(type)
}
}
if(allowedTypes.size() == 1) {
return allowedTypes.get(0)
}
return null
}
/*
* we return a list of available exchanges CE or FE
*/
def static Set getAllMessageExchangeType(Message message) {
var allowedTypes = newHashSet as Set<Object>
var exchangesAvailable = EmbeddedEditorInstanceHelper.getExchangeMessages(getSourceOfMessage(message),
getTargetOfMessage(message))
for(exchange : exchangesAvailable) {
if(message.name !== null && message.name.equals(CapellaElementExt.getName(exchange))) {
var type = getExchangeType(exchange)
if(type !== null)
allowedTypes.add(type)
}
}
return allowedTypes
}
def static getExchangeType(EObject exchangeElement) {
if (exchangeElement instanceof ComponentExchange)
return TYPE_CE
else if (exchangeElement instanceof FunctionalExchange)
return TYPE_FE
return null
}
def private static String getSourceOfMessage(Message message) {
var source = null as String
if(message instanceof SequenceMessage) {
source = (message as SequenceMessage).source
} else if(message instanceof LostMessage) {
source = (message as LostMessage).source
}
return source
}
def private static String getTargetOfMessage(Message message) {
var target = null as String
if(message instanceof SequenceMessage) {
target = (message as SequenceMessage).target
} else if(message instanceof FoundMessage) {
target = (message as FoundMessage).target
}
return target
}
def static participantsDefinedBefore(Model rootModel) {
return rootModel.participants
}
def static participantsDefinedBeforeNames(EObject element) {
var participantsNames = newArrayList
var container = getModelContainer(element)
if (container instanceof Model) {
var model = container as Model
var participants = participantsDefinedBefore(model)
for (participant : participants) {
participantsNames.add(participant.name)
}
}
return participantsNames
}
def static isParticipantKeyword(String keywordValue) {
var participantsKeywords = EmbeddedEditorInstanceHelper.PARTICIPANT_KEYWORDS;
return participantsKeywords.contains(keywordValue)
}
/*
* get all elements on the same level as modelContainer
*/
def static getElements(EObject modelContainer) {
if (modelContainer instanceof Model) {
return (modelContainer as Model).elements
}
if (modelContainer instanceof CombinedFragment) {
var elements = newArrayList
elements.addAll((modelContainer as CombinedFragment).block.blockElements)
var operands = (modelContainer as CombinedFragment).operands
for(operand : operands) {
elements.addAll(operand.block.blockElements)
}
return elements
}
if (modelContainer instanceof Block) {
return (modelContainer as Block).blockElements
}
}
/*
* get the root model that contains the object given as para
*/
def static EObject getModelContainer(EObject object) {
if(object instanceof Model)
return object as Model
if(object !== null)
return getModelContainer(object.eContainer)
return null
}
/*
* get the logic container of an element
* (the container can be the model or a combined fragment)
*/
def static EObject getDirectContainer(EObject element) {
if (element !== null) {
var container = element.eContainer
if (container instanceof Model || container instanceof CombinedFragment) {
return container
}
return getDirectContainer(container)
}
return null
}
}