Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 092a8d2fd5ffc1f6b22380a255f49ddbc7fe1913 (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
/** 
 * Copyright (c) 2010-2016, Abel Hegedus, IncQuery Labs Ltd.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.viatra.query.tooling.ui.queryregistry

import com.google.common.collect.Maps
import java.util.Map
import org.eclipse.jface.viewers.TreePath
import org.eclipse.viatra.query.runtime.registry.IQuerySpecificationRegistry
import org.eclipse.viatra.query.runtime.registry.IQuerySpecificationRegistryChangeListener
import org.eclipse.viatra.query.runtime.registry.IQuerySpecificationRegistryEntry
import org.eclipse.viatra.query.runtime.registry.IRegistryView
import org.eclipse.viatra.query.runtime.registry.view.AbstractRegistryView
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.viatra.query.patternlanguage.emf.ui.EMFPatternLanguageUIPlugin

/** 
 * @author Abel Hegedus
 */
class QueryRegistryTreeInput {
 
    @Accessors(PUBLIC_GETTER)   
    Map<String,QueryRegistryTreeSource> sources = Maps.newTreeMap
    
    @Accessors(PUBLIC_GETTER)
    IQuerySpecificationRegistry registry
    
    @Accessors(PUBLIC_GETTER)
    IRegistryView view
    
    @Accessors(PUBLIC_GETTER)
    IQuerySpecificationRegistryChangeListener listener
    
    new(IQuerySpecificationRegistry registry) {
        this.registry = registry
        view = registry.createView[
            return new AbstractRegistryView(registry, true) {
                override protected isEntryRelevant(IQuerySpecificationRegistryEntry entry) {
                    true
                }
            }
        ]
        view.entries.forEach[
            val source = sourceIdentifier.getOrCreateSource
            val treePackage = source.source.getOrCreatePackage(fullyQualifiedName.packageName)
            val entry = new QueryRegistryTreeEntry(treePackage.pckg, it)
            treePackage.pckg.entries.put(fullyQualifiedName, entry)
        ]
    }
    
    def void setListener(IQuerySpecificationRegistryChangeListener listener) {
        if (this.listener !== null) {
            view.removeViewListener(this.listener)
        }
        this.listener = listener
        if (listener !== null) {
            view.addViewListener(listener)
        }
    }
    
    def QueryRegistryTreeInputChange addEntryToInput(IQuerySpecificationRegistryEntry entry) {
        val source = entry.sourceIdentifier.getOrCreateSource
        val treePackage = source.source.getOrCreatePackage(entry.fullyQualifiedName.packageName)
        val treePckg = treePackage.pckg
        val treeEntry = new QueryRegistryTreeEntry(treePckg, entry)
        val emptyPckg = treePckg.entries.empty
        treePackage.pckg.entries.put(entry.fullyQualifiedName, treeEntry)
        return new QueryRegistryTreeInputChange(true, treeEntry, treePackage.pckgAffected || emptyPckg, treePckg, source.sourceAffected, source.source)
    }
    
    def QueryRegistryTreeInputChange removeEntry(IQuerySpecificationRegistryEntry entry) {
        val sourceDTO = entry.sourceIdentifier.getOrCreateSource
        val source = sourceDTO.source
        val treePackageDTO = sourceDTO.source.getOrCreatePackage(entry.fullyQualifiedName.packageName)
        val treePckg = treePackageDTO.pckg
        val treeEntry = treePckg.entries.remove(entry.fullyQualifiedName)
        val emptyPckg = treePckg.entries.empty
        if(emptyPckg) {
            source.packages.remove(treePckg.packageName)
        }
        val emptySource = source.packages.empty
        if(emptySource) {
            sources.remove(source.sourceIdentifier)
        }
        return new QueryRegistryTreeInputChange(true, treeEntry, treePackageDTO.pckgAffected || emptyPckg, treePckg, sourceDTO.sourceAffected || emptySource, source)
    }
    
    def QueryRegistryTreeInputChange getOrCreateSource(String sourceIdentifier) {
        val existingSource = sources.get(sourceIdentifier)
        if(existingSource === null) {
            val newSource = new QueryRegistryTreeSource(this, sourceIdentifier)
            sources.put(sourceIdentifier, newSource)
            return new QueryRegistryTreeInputChange(false, null, false, null, true, newSource)
        } else {
            return new QueryRegistryTreeInputChange(false, null, false, null, false, existingSource)
        }
    }
    
    def QueryRegistryTreeInputChange getOrCreatePackage(QueryRegistryTreeSource source, String packageName) {
        val existingPackage = source.packages.get(packageName)
        if(existingPackage === null) {
            val newPackage = new QueryRegistryTreePackage(source, packageName)
            source.packages.put(packageName, newPackage)
            return new QueryRegistryTreeInputChange(false, null, true, newPackage, false, null)
        } else {
            return new QueryRegistryTreeInputChange(false, null, false, existingPackage, false, null)
        }
    }
    
    def String getPackageName(String fullyQualifiedName) {
        return fullyQualifiedName.substring(0, Math.max(0, fullyQualifiedName.lastIndexOf('.')))
    }
    
    def dispatch TreePath getTreePath(QueryRegistryTreePackage pckg) {
        return new TreePath(#[pckg.parent, pckg])
    }
    
    def dispatch TreePath getTreePath(QueryRegistryTreeEntry entry) {
        return entry.parent.treePath.createChildPath(entry)
    }
}

@FinalFieldsConstructor
class QueryRegistryTreeSource {
    
    @Accessors(PUBLIC_GETTER)
    final QueryRegistryTreeInput parent
    @Accessors(PUBLIC_GETTER)
    final String sourceIdentifier
    @Accessors(PUBLIC_GETTER)
    final Map<String, QueryRegistryTreePackage> packages = Maps.newTreeMap 
    
}

@FinalFieldsConstructor
class QueryRegistryTreePackage {
    
    @Accessors(PUBLIC_GETTER)
    final QueryRegistryTreeSource parent
    @Accessors(PUBLIC_GETTER)
    final String packageName
    @Accessors(PUBLIC_GETTER)
    final Map<String, QueryRegistryTreeEntry> entries = Maps.newTreeMap 
    
}

@FinalFieldsConstructor
class QueryRegistryTreeEntry {
    
    @Accessors(PUBLIC_GETTER)
    final QueryRegistryTreePackage parent
    @Accessors(PUBLIC_GETTER)
    final IQuerySpecificationRegistryEntry entry
    
    @Accessors(PUBLIC_GETTER)
    boolean isLoaded = false
    
    def boolean load() {
        if(!loaded) {
            try{
                // load specification class
                val specification = entry.get
                if(specification !== null){
                    isLoaded = true
                }
                return true
            } catch (Exception ex) {
                val logMessage = String.format("Query Registry has encountered an error during loading of query %s: %s", entry.fullyQualifiedName, ex.message)
                EMFPatternLanguageUIPlugin.instance.logException(logMessage, ex)
            }
        }
        return false
    }
    
    def String getSimpleName() {
        val fqn = entry.fullyQualifiedName
        val lastDotIndex = fqn.lastIndexOf('.')
        if(lastDotIndex > 0 && lastDotIndex < fqn.length){
            fqn.substring(lastDotIndex + 1, fqn.length)
        } else {
            return fqn
        }
    }
    
    def String getPackageName() {
        val fqn = entry.fullyQualifiedName
        val lastDotIndex = fqn.lastIndexOf('.')
        if(lastDotIndex > 0 && lastDotIndex < fqn.length){
            fqn.substring(0, fqn.lastIndexOf('.'))
        } else {
            return fqn
        }
    }
}

@Data
class QueryRegistryTreeInputChange {
    boolean entryAffected
    QueryRegistryTreeEntry entry
    boolean pckgAffected
    QueryRegistryTreePackage pckg
    boolean sourceAffected
    QueryRegistryTreeSource source
}

Back to the top