diff options
Diffstat (limited to 'subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/comprehension/WellbehavingDerivedFeatureRegistry.java')
-rw-r--r-- | subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/comprehension/WellbehavingDerivedFeatureRegistry.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/comprehension/WellbehavingDerivedFeatureRegistry.java b/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/comprehension/WellbehavingDerivedFeatureRegistry.java new file mode 100644 index 00000000..d696ddd6 --- /dev/null +++ b/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/comprehension/WellbehavingDerivedFeatureRegistry.java | |||
@@ -0,0 +1,154 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2011 Abel Hegedus and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.base.comprehension; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | import java.util.WeakHashMap; | ||
14 | |||
15 | import org.apache.log4j.Logger; | ||
16 | import org.eclipse.core.runtime.IConfigurationElement; | ||
17 | import org.eclipse.core.runtime.IExtension; | ||
18 | import org.eclipse.core.runtime.IExtensionPoint; | ||
19 | import org.eclipse.core.runtime.IExtensionRegistry; | ||
20 | import org.eclipse.core.runtime.Platform; | ||
21 | import org.eclipse.emf.ecore.EClass; | ||
22 | import org.eclipse.emf.ecore.EClassifier; | ||
23 | import org.eclipse.emf.ecore.EPackage; | ||
24 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
25 | import tools.refinery.viatra.runtime.base.ViatraBasePlugin; | ||
26 | |||
27 | /** | ||
28 | * @author Abel Hegedus | ||
29 | * | ||
30 | */ | ||
31 | public class WellbehavingDerivedFeatureRegistry { | ||
32 | |||
33 | |||
34 | private static Collection<EStructuralFeature> contributedWellbehavingDerivedFeatures = Collections.newSetFromMap(new WeakHashMap<EStructuralFeature, Boolean>()); | ||
35 | private static Collection<EClass> contributedWellbehavingDerivedClasses = Collections.newSetFromMap(new WeakHashMap<EClass, Boolean>()); | ||
36 | private static Collection<EPackage> contributedWellbehavingDerivedPackages = Collections.newSetFromMap(new WeakHashMap<EPackage, Boolean>()); | ||
37 | |||
38 | private WellbehavingDerivedFeatureRegistry() { | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Called by ViatraBasePlugin. | ||
43 | */ | ||
44 | public static void initRegistry() { | ||
45 | getContributedWellbehavingDerivedFeatures().clear(); | ||
46 | getContributedWellbehavingDerivedClasses().clear(); | ||
47 | getContributedWellbehavingDerivedPackages().clear(); | ||
48 | |||
49 | IExtensionRegistry reg = Platform.getExtensionRegistry(); | ||
50 | IExtensionPoint poi; | ||
51 | |||
52 | poi = reg.getExtensionPoint(ViatraBasePlugin.WELLBEHAVING_DERIVED_FEATURE_EXTENSION_POINT_ID); | ||
53 | if (poi != null) { | ||
54 | IExtension[] exts = poi.getExtensions(); | ||
55 | |||
56 | for (IExtension ext : exts) { | ||
57 | |||
58 | IConfigurationElement[] els = ext.getConfigurationElements(); | ||
59 | for (IConfigurationElement el : els) { | ||
60 | if (el.getName().equals("wellbehaving-derived-feature")) { | ||
61 | processWellbehavingExtension(el); | ||
62 | } else { | ||
63 | throw new UnsupportedOperationException("Unknown configuration element " + el.getName() | ||
64 | + " in plugin.xml of " + el.getDeclaringExtension().getUniqueIdentifier()); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | private static void processWellbehavingExtension(IConfigurationElement el) { | ||
72 | try { | ||
73 | String packageUri = el.getAttribute("package-nsUri"); | ||
74 | String featureName = el.getAttribute("feature-name"); | ||
75 | String classifierName = el.getAttribute("classifier-name"); | ||
76 | String contributorName = el.getContributor().getName(); | ||
77 | StringBuilder featureIdBuilder = new StringBuilder(); | ||
78 | if (packageUri != null) { | ||
79 | EPackage pckg = EPackage.Registry.INSTANCE.getEPackage(packageUri); | ||
80 | featureIdBuilder.append(packageUri); | ||
81 | if (pckg != null) { | ||
82 | if (classifierName != null) { | ||
83 | EClassifier clsr = pckg.getEClassifier(classifierName); | ||
84 | featureIdBuilder.append("##").append(classifierName); | ||
85 | if (clsr instanceof EClass) { | ||
86 | if (featureName != null) { | ||
87 | EClass cls = (EClass) clsr; | ||
88 | EStructuralFeature feature = cls.getEStructuralFeature(featureName); | ||
89 | featureIdBuilder.append("##").append(featureName); | ||
90 | if (feature != null) { | ||
91 | registerWellbehavingDerivedFeature(feature); | ||
92 | } else { | ||
93 | throw new IllegalStateException(String.format("Feature %s of EClass %s in package %s not found! (plug-in %s)", featureName, classifierName, packageUri, contributorName)); | ||
94 | } | ||
95 | } else { | ||
96 | registerWellbehavingDerivedClass((EClass) clsr); | ||
97 | } | ||
98 | } else { | ||
99 | throw new IllegalStateException(String.format("EClassifier %s does not exist in package %s! (plug-in %s)", classifierName, packageUri, contributorName)); | ||
100 | } | ||
101 | } else { | ||
102 | if(featureName != null){ | ||
103 | throw new IllegalStateException(String.format("Feature name must be empty if classifier name is not set! (package %s, plug-in %s)", packageUri, contributorName)); | ||
104 | } | ||
105 | registerWellbehavingDerivedPackage(pckg); | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | } catch (Exception e) { | ||
110 | final Logger logger = Logger.getLogger(WellbehavingDerivedFeatureRegistry.class); | ||
111 | logger.error("Well-behaving feature registration failed", e); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * | ||
117 | * @param feature | ||
118 | * @return true if the feature (or its defining EClass or ) is registered as well-behaving | ||
119 | */ | ||
120 | public static boolean isWellbehavingFeature(EStructuralFeature feature) { | ||
121 | if(feature == null){ | ||
122 | return false; | ||
123 | } else if (contributedWellbehavingDerivedFeatures.contains(feature)) { | ||
124 | return true; | ||
125 | } else if (contributedWellbehavingDerivedClasses.contains(feature.getEContainingClass())) { | ||
126 | return true; | ||
127 | } else return contributedWellbehavingDerivedPackages.contains(feature.getEContainingClass().getEPackage()); | ||
128 | } | ||
129 | |||
130 | public static void registerWellbehavingDerivedFeature(EStructuralFeature feature) { | ||
131 | contributedWellbehavingDerivedFeatures.add(feature); | ||
132 | } | ||
133 | |||
134 | public static void registerWellbehavingDerivedClass(EClass cls) { | ||
135 | contributedWellbehavingDerivedClasses.add(cls); | ||
136 | } | ||
137 | |||
138 | public static void registerWellbehavingDerivedPackage(EPackage pkg) { | ||
139 | contributedWellbehavingDerivedPackages.add(pkg); | ||
140 | } | ||
141 | |||
142 | public static Collection<EStructuralFeature> getContributedWellbehavingDerivedFeatures() { | ||
143 | return contributedWellbehavingDerivedFeatures; | ||
144 | } | ||
145 | |||
146 | public static Collection<EClass> getContributedWellbehavingDerivedClasses() { | ||
147 | return contributedWellbehavingDerivedClasses; | ||
148 | } | ||
149 | |||
150 | public static Collection<EPackage> getContributedWellbehavingDerivedPackages() { | ||
151 | return contributedWellbehavingDerivedPackages; | ||
152 | } | ||
153 | |||
154 | } | ||