Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: faa3c08a36405d181c696eb35ebbd82ef9391ea5 (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
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Tristan Faure (Atos Origin) tristan.faure@atosorigin.com - Initial API and implementation
 *
  *****************************************************************************/
package org.eclipse.papyrus.diagramprofile.utils;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EParameter;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.ecore.CallOperationAction;
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
import org.eclipse.ocl.ecore.OCL;
import org.eclipse.ocl.ecore.OCLExpression;
import org.eclipse.ocl.ecore.SendSignalAction;
import org.eclipse.ocl.helper.OCLHelper;

public class OCLUtils {

	/**
	 * variables for ocl
	 */
	private static org.eclipse.ocl.OCL<EPackage, EClassifier, EOperation, EStructuralFeature, EEnumLiteral, EParameter, EObject, CallOperationAction, SendSignalAction, org.eclipse.ocl.ecore.Constraint, EClass, EObject> ocl = OCL
			.newInstance(new EcoreEnvironmentFactory());

	private static OCLHelper<EClassifier, ?, ?, ?> oclHelper = ocl.createOCLHelper();

	/**
	 * The Ocl rule is evaluated to the eobject argument this methods returns an object and the type
	 * depends of the input rule
	 * 
	 * @param oclRule
	 *            the ocl rule
	 * @param object
	 *            the object
	 * 
	 * @return the object, result of the query
	 */
	public static Object runOclRule(String oclRule, EObject object) {
		if (object != null) {
			oclHelper.setContext(object.eClass());
		}
		OCLExpression exp;
		try {
			exp = (OCLExpression) oclHelper.createQuery(oclRule);
			Object result = ocl.evaluate(object, exp);
			return result;
		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}
}

Back to the top