diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/emf/helper/ViatraQueryRuntimeHelper.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/emf/helper/ViatraQueryRuntimeHelper.java | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/emf/helper/ViatraQueryRuntimeHelper.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/emf/helper/ViatraQueryRuntimeHelper.java new file mode 100644 index 00000000..93ac97f2 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/emf/helper/ViatraQueryRuntimeHelper.java | |||
@@ -0,0 +1,161 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Abel Hegedus, Istvan Rath 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.emf.helper; | ||
10 | |||
11 | import java.util.function.Function; | ||
12 | |||
13 | import org.eclipse.emf.ecore.EClassifier; | ||
14 | import org.eclipse.emf.ecore.EObject; | ||
15 | import org.eclipse.emf.ecore.EPackage; | ||
16 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
17 | import tools.refinery.viatra.runtime.api.IPatternMatch; | ||
18 | |||
19 | /** | ||
20 | * Helper functions for dealing with the EMF objects with VIATRA Queries. | ||
21 | * | ||
22 | * @author Abel Hegedus | ||
23 | * @since 0.9 | ||
24 | * | ||
25 | */ | ||
26 | public class ViatraQueryRuntimeHelper { | ||
27 | |||
28 | private ViatraQueryRuntimeHelper() {/*Utility class constructor*/} | ||
29 | |||
30 | private static final Function<Object, String> STRING_VALUE_TRANSFORMER = input -> (input == null) ? "(null)" : input.toString(); | ||
31 | |||
32 | /** | ||
33 | * Gives a human-readable name of an EMF type. | ||
34 | */ | ||
35 | public static String prettyPrintEMFType(Object typeObject) { | ||
36 | if (typeObject == null) { | ||
37 | return "(null)"; | ||
38 | } else if (typeObject instanceof EClassifier) { | ||
39 | final EClassifier eClassifier = (EClassifier) typeObject; | ||
40 | final EPackage ePackage = eClassifier.getEPackage(); | ||
41 | final String nsURI = ePackage == null ? null : ePackage.getNsURI(); | ||
42 | final String typeName = eClassifier.getName(); | ||
43 | return "" + nsURI + "/" + typeName; | ||
44 | } else if (typeObject instanceof EStructuralFeature) { | ||
45 | final EStructuralFeature feature = (EStructuralFeature) typeObject; | ||
46 | return prettyPrintEMFType(feature.getEContainingClass()) + "." + feature.getName(); | ||
47 | } else | ||
48 | return typeObject.toString(); | ||
49 | } | ||
50 | |||
51 | |||
52 | /** | ||
53 | * Get the structural feature with the given name of the given object. | ||
54 | * | ||
55 | * @param o | ||
56 | * the object (must be an EObject) | ||
57 | * @param featureName | ||
58 | * the name of the feature | ||
59 | * @return the EStructuralFeature of the object or null if it can not be found | ||
60 | */ | ||
61 | public static EStructuralFeature getFeature(Object o, String featureName) { | ||
62 | if (o instanceof EObject) { | ||
63 | EStructuralFeature feature = ((EObject) o).eClass().getEStructuralFeature(featureName); | ||
64 | return feature; | ||
65 | } | ||
66 | return null; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Returns the message for the given match using the given format. The format string can refer to the value of | ||
71 | * parameter A of the match with $A$ and even access features of A (if it's an EObject), e.g. $A.id$. | ||
72 | * | ||
73 | * <p/> | ||
74 | * If the selected parameter does not exist, the string "[no such parameter]" is added | ||
75 | * | ||
76 | * <p/> | ||
77 | * If no feature is defined, but A has a feature called "name", then its value is used. | ||
78 | * | ||
79 | * <p/> | ||
80 | * If the selected feature does not exist, A.toString() is added. | ||
81 | * | ||
82 | * <p/> | ||
83 | * If the selected feature is null, the string "null" is added. | ||
84 | * | ||
85 | * @param match | ||
86 | * cannot be null! | ||
87 | * @param messageFormat | ||
88 | * cannot be null! | ||
89 | */ | ||
90 | public static String getMessage(IPatternMatch match, String messageFormat) { | ||
91 | return getMessage(match, messageFormat, STRING_VALUE_TRANSFORMER); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Returns the message for the given match using the given format while transforming values with the given function. | ||
96 | * The format string can refer to the value of parameter A of the match with $A$ and even access features of A (if | ||
97 | * it's an EObject), e.g. $A.id$. The function will be called to compute the final string representation of the | ||
98 | * values selected by the message format. | ||
99 | * | ||
100 | * <p/> | ||
101 | * If the selected parameter does not exist, the string "[no such parameter]" is added | ||
102 | * | ||
103 | * <p/> | ||
104 | * If no feature is defined, but A has a feature called "name", then its value is passed to the function. | ||
105 | * | ||
106 | * <p/> | ||
107 | * If the selected feature does not exist, A is passed to the function. | ||
108 | * | ||
109 | * <p/> | ||
110 | * If the selected feature is null, the string "null" is added. | ||
111 | * | ||
112 | * @param match | ||
113 | * cannot be null! | ||
114 | * @param messageFormat | ||
115 | * cannot be null! | ||
116 | * @param parameterValueTransformer | ||
117 | * cannot be null! | ||
118 | * @since 2.0 | ||
119 | */ | ||
120 | public static String getMessage(IPatternMatch match, String messageFormat, Function<Object,String> parameterValueTransformer) { | ||
121 | String[] tokens = messageFormat.split("\\$"); | ||
122 | StringBuilder newText = new StringBuilder(); | ||
123 | |||
124 | for (int i = 0; i < tokens.length; i++) { | ||
125 | if (i % 2 == 0) { | ||
126 | newText.append(tokens[i]); | ||
127 | } else { | ||
128 | String[] objectTokens = tokens[i].split("\\."); | ||
129 | if (objectTokens.length > 0) { | ||
130 | Object o = null; | ||
131 | EStructuralFeature feature = null; | ||
132 | |||
133 | if (objectTokens.length == 1) { | ||
134 | o = match.get(objectTokens[0]); | ||
135 | feature = getFeature(o, "name"); | ||
136 | } | ||
137 | if (objectTokens.length == 2) { | ||
138 | o = match.get(objectTokens[0]); | ||
139 | feature = getFeature(o, objectTokens[1]); | ||
140 | } | ||
141 | |||
142 | if (o != null && feature != null) { | ||
143 | Object value = ((EObject) o).eGet(feature); | ||
144 | if (value != null) { | ||
145 | newText.append(parameterValueTransformer.apply(value)); | ||
146 | } else { | ||
147 | newText.append("null"); | ||
148 | } | ||
149 | } else if (o != null) { | ||
150 | newText.append(parameterValueTransformer.apply(o)); | ||
151 | } | ||
152 | } else { | ||
153 | newText.append("[no such parameter]"); | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | return newText.toString(); | ||
159 | } | ||
160 | |||
161 | } | ||