| 1 | /* |
| 2 | * Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.seff.expressions; |
| 5 | |
| 6 | import java.lang.ref.WeakReference; |
| 7 | import java.util.Collections; |
| 8 | import java.util.Iterator; |
| 9 | import java.util.Map; |
| 10 | |
| 11 | import org.eclipse.core.runtime.IStatus; |
| 12 | import org.eclipse.emf.ecore.EClassifier; |
| 13 | import org.eclipse.ocl.Environment; |
| 14 | import org.eclipse.ocl.EvaluationEnvironment; |
| 15 | import org.eclipse.ocl.ParserException; |
| 16 | import org.eclipse.ocl.Query; |
| 17 | import org.eclipse.ocl.ecore.EcoreFactory; |
| 18 | import org.eclipse.ocl.expressions.OCLExpression; |
| 19 | import org.eclipse.ocl.expressions.OperationCallExp; |
| 20 | import org.eclipse.ocl.expressions.Variable; |
| 21 | import org.eclipse.ocl.helper.OCLHelper; |
| 22 | import org.eclipse.ocl.utilities.AbstractVisitor; |
| 23 | import org.eclipse.ocl.utilities.PredefinedType; |
| 24 | |
| 25 | /** |
| 26 | * @generated |
| 27 | */ |
| 28 | public class PalladioComponentModelOCLFactory { |
| 29 | |
| 30 | /** |
| 31 | * @generated |
| 32 | */ |
| 33 | private PalladioComponentModelOCLFactory() { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @generated |
| 38 | */ |
| 39 | public static PalladioComponentModelAbstractExpression getExpression( |
| 40 | String body, EClassifier context, Map environment) { |
| 41 | return new Expression(body, context, environment); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @generated |
| 46 | */ |
| 47 | public static PalladioComponentModelAbstractExpression getExpression( |
| 48 | String body, EClassifier context) { |
| 49 | return getExpression(body, context, Collections.EMPTY_MAP); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @generated |
| 54 | */ |
| 55 | private static class Expression extends |
| 56 | PalladioComponentModelAbstractExpression { |
| 57 | /** |
| 58 | * @generated |
| 59 | */ |
| 60 | private WeakReference queryRef; |
| 61 | |
| 62 | /** |
| 63 | * @generated |
| 64 | */ |
| 65 | private final org.eclipse.ocl.ecore.OCL oclInstance; |
| 66 | |
| 67 | /** |
| 68 | * @generated |
| 69 | */ |
| 70 | public Expression(String body, EClassifier context, Map environment) { |
| 71 | super(body, context); |
| 72 | oclInstance = org.eclipse.ocl.ecore.OCL.newInstance(); |
| 73 | initCustomEnv(oclInstance.getEnvironment(), environment); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @generated |
| 78 | */ |
| 79 | protected Query getQuery() { |
| 80 | Query oclQuery = null; |
| 81 | if (this.queryRef != null) { |
| 82 | oclQuery = (Query) this.queryRef.get(); |
| 83 | } |
| 84 | if (oclQuery == null) { |
| 85 | OCLHelper oclHelper = oclInstance.createOCLHelper(); |
| 86 | oclHelper.setContext(context()); |
| 87 | try { |
| 88 | OCLExpression oclExpression = oclHelper.createQuery(body()); |
| 89 | oclQuery = oclInstance.createQuery(oclExpression); |
| 90 | this.queryRef = new WeakReference(oclQuery); |
| 91 | setStatus(IStatus.OK, null, null); |
| 92 | } catch (ParserException e) { |
| 93 | setStatus(IStatus.ERROR, e.getMessage(), e); |
| 94 | } |
| 95 | } |
| 96 | return oclQuery; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @generated |
| 101 | */ |
| 102 | protected Object doEvaluate(Object context, Map env) { |
| 103 | Query oclQuery = getQuery(); |
| 104 | if (oclQuery == null) { |
| 105 | return null; |
| 106 | } |
| 107 | EvaluationEnvironment evalEnv = oclQuery.getEvaluationEnvironment(); |
| 108 | // init environment |
| 109 | for (Iterator it = env.entrySet().iterator(); it.hasNext();) { |
| 110 | Map.Entry nextEntry = (Map.Entry) it.next(); |
| 111 | evalEnv.replace((String) nextEntry.getKey(), nextEntry |
| 112 | .getValue()); |
| 113 | } |
| 114 | try { |
| 115 | initExtentMap(context); |
| 116 | Object result = oclQuery.evaluate(context); |
| 117 | return (result != oclInstance.getEnvironment() |
| 118 | .getOCLStandardLibrary().getOclInvalid()) ? result |
| 119 | : null; |
| 120 | } finally { |
| 121 | evalEnv.clear(); |
| 122 | oclQuery.getExtentMap().clear(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @generated |
| 128 | */ |
| 129 | private void initExtentMap(Object context) { |
| 130 | if (!getStatus().isOK() || context == null) { |
| 131 | return; |
| 132 | } |
| 133 | final Query queryToInit = getQuery(); |
| 134 | final Object extentContext = context; |
| 135 | queryToInit.getExtentMap().clear(); |
| 136 | if (queryToInit.queryText() != null |
| 137 | && queryToInit.queryText().indexOf( |
| 138 | PredefinedType.ALL_INSTANCES_NAME) >= 0) { |
| 139 | AbstractVisitor visitior = new AbstractVisitor() { |
| 140 | |
| 141 | private boolean usesAllInstances = false; |
| 142 | |
| 143 | public Object visitOperationCallExp(OperationCallExp oc) { |
| 144 | if (!usesAllInstances) { |
| 145 | usesAllInstances = PredefinedType.ALL_INSTANCES == oc |
| 146 | .getOperationCode(); |
| 147 | if (usesAllInstances) { |
| 148 | queryToInit |
| 149 | .getExtentMap() |
| 150 | .putAll( |
| 151 | oclInstance |
| 152 | .getEvaluationEnvironment() |
| 153 | .createExtentMap( |
| 154 | extentContext)); |
| 155 | } |
| 156 | } |
| 157 | return super.visitOperationCallExp(oc); |
| 158 | } |
| 159 | }; |
| 160 | queryToInit.getExpression().accept(visitior); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @generated |
| 166 | */ |
| 167 | private static void initCustomEnv(Environment ecoreEnv, Map environment) { |
| 168 | for (Iterator it = environment.keySet().iterator(); it.hasNext();) { |
| 169 | String varName = (String) it.next(); |
| 170 | EClassifier varType = (EClassifier) environment.get(varName); |
| 171 | ecoreEnv.addElement(varName, createVar(ecoreEnv, varName, |
| 172 | varType), false); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @generated |
| 178 | */ |
| 179 | private static Variable createVar(Environment ecoreEnv, String name, |
| 180 | EClassifier type) { |
| 181 | Variable var = EcoreFactory.eINSTANCE.createVariable(); |
| 182 | var.setName(name); |
| 183 | var.setType(ecoreEnv.getUMLReflection().getOCLType(type)); |
| 184 | return var; |
| 185 | } |
| 186 | } |
| 187 | } |