Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4e668c2bc9b4e32f164d16457998292cd325f526 (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
/*****************************************************************************
 * Copyright (c) 2014, 2015 Christian W. Damus and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Christian W. Damus - Initial API and implementation
 * 
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.assistants.generator

import javax.inject.Singleton
import org.eclipse.papyrus.infra.filters.CompoundFilter
import org.eclipse.papyrus.infra.filters.Filter
import org.eclipse.papyrus.infra.filters.FiltersFactory
import org.eclipse.papyrus.infra.filters.OperatorKind
import org.eclipse.gmf.runtime.emf.type.core.IElementType
import org.eclipse.papyrus.infra.gmfdiag.assistant.AssistantFactory
import org.eclipse.uml2.uml.Profile
import org.eclipse.papyrus.uml.filters.UMLFiltersFactory
import org.eclipse.emf.ecore.util.EcoreUtil
import javax.inject.Inject
import org.eclipse.papyrus.infra.types.ElementTypeConfiguration
import org.eclipse.papyrus.uml.profile.types.generator.UML

/**
 * Utility extensions for working with {@link Filter}s.
 */
@Singleton
class FiltersUtil {
    static extension FiltersFactory filtersFactory = FiltersFactory.eINSTANCE
    static extension UMLFiltersFactory umlFiltersFactory = UMLFiltersFactory.eINSTANCE
    static extension AssistantFactory assistantFactory = AssistantFactory.eINSTANCE

    @Inject extension UML
    @Inject extension ModelingAssistantProviderRule

    def create createElementTypeFilter toElementTypeFilter(String typeID, Profile umlProfile) {
        elementTypeID = typeID
        name = typeID.substring(typeID.lastIndexOf('.') + 1)
        
        umlProfile.rootProfile.toModelingAssistantProvider.ownedFilters.add(it)
    }
    
    def toFilter(IElementType elementType, Profile umlProfile) {
        elementType.internalToFilter(umlProfile.rootProfile)
    }

    private def create createElementTypeFilter internalToFilter(IElementType elementType, Profile rootProfile) {
        elementTypeID = elementType.id
        name = elementType.displayName
        
        // Shared with all of the assistants
        rootProfile.toModelingAssistantProvider.ownedFilters.add(it)
    }

    def toFilter(ElementTypeConfiguration elementType, Profile umlProfile) {
        elementType.internalToFilter(umlProfile.rootProfile)
    }
    
    private def create createElementTypeFilter internalToFilter(ElementTypeConfiguration elementType, Profile rootProfile) {
        elementTypeID = elementType.identifier
        name = elementType.name
        
        // Shared with all of the assistants
        rootProfile.toModelingAssistantProvider.ownedFilters.add(it)
    }
    
    def create createCompoundFilter toFilter(Profile umlProfile) {
        operator = OperatorKind.OR
        name = "pertains to Profile " + umlProfile.qualifiedName
        ownedFilters.addAll(umlProfile.toAppliedFilter, umlProfile.toAssistedElementTypeFilter)
        
        // Shared with all of the assistants
        umlProfile.rootProfile.toModelingAssistantProvider.ownedFilters.add(it)
    }
    
    private def create createProfileApplied toAppliedFilter(Profile umlProfile) {
        profileURI = EcoreUtil.getURI(umlProfile).toString
        profileQualifiedName = umlProfile.qualifiedName // back-up in case of lost reference
        name = umlProfile.qualifiedName + " is applied in context"
    }
    
    def dispatch andProfileApplied(Void filter, Profile umlProfile) {
        null
    }

    def dispatch andProfileApplied(Filter filter, Profile umlProfile) {
        // Match an object that has the profile applied or is of an element type that is assisted by this model
        // (the latter is useful especially for creating connections to new elements, to determine eligible
        //  elements types to create)
        umlProfile.toFilter && filter
    }

    private def create createAssistedElementTypeFilter toAssistedElementTypeFilter(Profile umlProfile) {
        // No details
    }

    def dispatch isCompound(Void filter) {
        false
    }

    def dispatch isCompound(Filter filter) {
        false
    }

    def dispatch isCompound(CompoundFilter filter) {
        true
    }

    def dispatch reduce(Void filter) {
        null
    }

    def dispatch reduce(Filter filter) {
        filter
    }

    def dispatch reduce(CompoundFilter filter) {
        switch filter.filters.size {
            case 0: null
            case 1: filter.filters.get(0)
            default: filter
        }
    }

    private def add(CompoundFilter compound, Filter other) {
        if (other.eContainer != null) {
            // Sharing the filter
            compound.filters.add(other)
        } else {
            // Owning the filter
            compound.ownedFilters.add(other)
        }
    }
    
    private def addAll(CompoundFilter compound, CompoundFilter other) {
        if (other.eContainer != null) {
            // Sharing the filter's components
            other.filters.forEach[compound.add(it)]
        } else {
            // No need to share with the other
            compound.ownedFilters.addAll(other.ownedFilters)
            
            // This now has removed any that were owned
            compound.filters.addAll(other.filters)
        }
    }

    def dispatch operator_or(Void left, Filter right) {
        right
    }

    def dispatch operator_or(Filter left, Filter right) {
        createOr(left, right)
    }

    private def createOr(Filter left, Filter right) {
        createCompoundFilter => [
            operator = OperatorKind.OR
            add(left)
            add(right)
        ]
    }

    def dispatch operator_or(CompoundFilter left, Filter right) {
        switch left.operator {
            case OR: {
                left.add(right)
                left
            }
            default:
                createOr(left, right)
        }
    }

    def dispatch operator_or(Filter left, CompoundFilter right) {
        switch right.operator {
            case OR: {
                right.add(left)
                right
            }
            default:
                createOr(left, right)
        }
    }

    def dispatch operator_or(CompoundFilter left, CompoundFilter right) {
        switch left.operator {
            case OR: {
                switch right.operator {
                    case OR: {
                        // If the right is owned somewhere, then we should share it as is
                        // because it may yet accumulate more components
                        if (right.eContainer == null) left.addAll(right) else left.add(right)
                    }
                    default: {
                        left.add(right)
                    }
                }
                left
            }
            default:
                createOr(left, right)
        }
    }

    def dispatch operator_and(Void left, Filter right) {
        right
    }

    def dispatch operator_and(Filter left, Filter right) {
        createAnd(left, right)
    }

    private def createAnd(Filter left, Filter right) {
        createCompoundFilter => [
            operator = OperatorKind.AND
            add(left)
            add(right)
        ]
    }

    def dispatch operator_and(CompoundFilter left, Filter right) {
        switch left.operator {
            case AND: {
                left.add(right)
                left
            }
            default:
                createAnd(left, right)
        }
    }

    def dispatch operator_and(Filter left, CompoundFilter right) {
        switch right.operator {
            case AND: {
                right.add(left)
                right
            }
            default:
                createAnd(left, right)
        }
    }

    def dispatch operator_and(CompoundFilter left, CompoundFilter right) {
        switch left.operator {
            case AND: {
                switch right.operator {
                    case AND: {
                        // If the right is owned somewhere, then we should share it as is
                        // because it may yet accumulate more components
                        if (right.eContainer == null) left.addAll(right) else left.add(right)
                    }
                    default: {
                        left.add(right)
                    }
                }
                left
            }
            default:
                createAnd(left, right)
        }
    }
}

Back to the top