diff options
author | Ansgar Radermacher | 2014-09-11 15:17:22 +0000 |
---|---|---|
committer | Ansgar Radermacher | 2014-09-15 08:17:22 +0000 |
commit | 252f1b316da367e70ed6312385b6006330012ea5 (patch) | |
tree | 582f8ac250a547f6e2faef5aae225f1cdaf1dc3e /plugins | |
parent | 6ace73dd3a914645d70f9746856018ae5bd028ed (diff) | |
download | org.eclipse.papyrus-252f1b316da367e70ed6312385b6006330012ea5.tar.gz org.eclipse.papyrus-252f1b316da367e70ed6312385b6006330012ea5.tar.xz org.eclipse.papyrus-252f1b316da367e70ed6312385b6006330012ea5.zip |
436296 - [Validation] DSML plugin generation is broken
Diffstat (limited to 'plugins')
5 files changed, 327 insertions, 3 deletions
diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/META-INF/MANIFEST.MF b/plugins/uml/org.eclipse.papyrus.uml.service.validation/META-INF/MANIFEST.MF index 1e5bbeaeda4..50586e7b1bb 100644 --- a/plugins/uml/org.eclipse.papyrus.uml.service.validation/META-INF/MANIFEST.MF +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/META-INF/MANIFEST.MF @@ -1,6 +1,7 @@ Manifest-Version: 1.0
Export-Package: org.eclipse.papyrus.uml.service.validation,
- org.eclipse.papyrus.uml.service.validation.handler
+ org.eclipse.papyrus.uml.service.validation.handler,
+ org.eclipse.papyrus.uml.service.validation.oclpivot
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.papyrus.infra.services.validation;bundle-version="1.0.1",
@@ -8,7 +9,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.uml2.uml;bundle-version="4.1.0",
org.eclipse.papyrus.infra.gmfdiag.commands;bundle-version="1.0.1",
org.eclipse.gmf.runtime.emf.commands.core;bundle-version="1.4.0",
- org.eclipse.ocl.examples.pivot;bundle-version="3.4.0"
+ org.eclipse.ocl.examples.pivot;bundle-version="3.4.0",
+ org.eclipse.ocl;bundle-version="3.4.0"
Bundle-Vendor: %pluginProvider
Bundle-ActivationPolicy: lazy
Bundle-Version: 1.0.1.qualifier
diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/plugin.xml b/plugins/uml/org.eclipse.papyrus.uml.service.validation/plugin.xml index 2e18251a7a3..dcd811f74ff 100644 --- a/plugins/uml/org.eclipse.papyrus.uml.service.validation/plugin.xml +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/plugin.xml @@ -1,7 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
-
<extension
point="org.eclipse.ui.handlers">
<handler
@@ -41,4 +40,11 @@ </activeWhen>
</handler>
</extension>
+ <extension
+ point="org.eclipse.emf.validation.constraintParsers">
+ <constraintParser
+ class="org.eclipse.papyrus.uml.service.validation.oclpivot.OCLpivotConstraintParser"
+ lang="OCLpivot">
+ </constraintParser>
+ </extension>
</plugin>
diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/StereotypeUtil.java b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/StereotypeUtil.java new file mode 100644 index 00000000000..ce1cfe6a7ed --- /dev/null +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/StereotypeUtil.java @@ -0,0 +1,63 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Ansgar Radermacher (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.uml.service.validation; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.uml2.uml.Class; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Stereotype; +import org.eclipse.uml2.uml.util.UMLUtil; + +/** + * Provide the possibility to check whether a stereotype application corresponds + * to a stereotype provided as string. It takes sub-stereotypes into account. + * The objective is to filter the execution of validation rules within the plugin.xml, i.e. + * only schedule their execution, if a suitable context object is selected. + * + * Limitation: constraint context must be a stereotype (passed objects are stereotype applications) + */ +public class StereotypeUtil { + + public static boolean checkStereoApplication(Object stereotypeApplicationObj, String stereoName) { + if(!(stereotypeApplicationObj instanceof EObject)) { + return false; + } + + if(stereotypeApplicationObj instanceof Element) { + return false; + } + + EObject stereotypeApplication = ((EObject)stereotypeApplicationObj); + Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication); + if(stereotype == null) { + return false; + } + return checkStereotype(stereotype, stereoName); + } + + public static boolean checkStereotype(Stereotype stereotype, String stereoName) { + if(stereoName.equals(stereotype.getName())) { + return true; + } + for (Class superStereo : stereotype.getSuperClasses()) { + if (superStereo instanceof Stereotype) { + if (checkStereotype((Stereotype) superStereo, stereoName)) { + return true; + } + } + } + return false; + } +} diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/oclpivot/AbstractOCLpivotModelConstraint.java b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/oclpivot/AbstractOCLpivotModelConstraint.java new file mode 100644 index 00000000000..34ec0e76510 --- /dev/null +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/oclpivot/AbstractOCLpivotModelConstraint.java @@ -0,0 +1,193 @@ +/** + * <copyright> + * + * Copyright (c) 2003, 2007 IBM Corporation and others. + * 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: + * IBM - Initial API and implementation + * Radek Dvorak (Borland) - Bugzilla 165458 + * Ansgar Radermacher (CEA) - created variant for evaluation with OCL pivot element + * + * </copyright> + */ + +package org.eclipse.papyrus.uml.service.validation.oclpivot; + +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.emf.common.util.WrappedException; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.validation.IValidationContext; +import org.eclipse.emf.validation.model.ConstraintStatus; +import org.eclipse.emf.validation.model.IModelConstraint; +import org.eclipse.emf.validation.service.IConstraintDescriptor; +import org.eclipse.ocl.examples.pivot.ExpressionInOCL; +import org.eclipse.ocl.examples.pivot.NamedElement; +import org.eclipse.ocl.examples.pivot.OCL; +import org.eclipse.ocl.examples.pivot.ParserException; +import org.eclipse.ocl.examples.pivot.Type; +import org.eclipse.ocl.examples.pivot.helper.OCLHelper; +import org.eclipse.ocl.examples.pivot.manager.MetaModelManager; +import org.eclipse.ocl.examples.pivot.utilities.PivotEnvironmentFactory; +import org.eclipse.uml2.uml.Stereotype; +import org.eclipse.uml2.uml.util.UMLUtil; + +/** + * This class is based on the AbstractOCLModelConstraint in org.eclipse.emf.validation.ocl. The main difference is that it enforces + * the validation with the pivot OCL variant, see bug 436296 - [Validation] DSML plugin generation is broken + * + * @link org.eclipse.emf.validation.ocl.AbstractOCLModelConstraint + * + * @author Ansgar Radermacher + */ +public abstract class AbstractOCLpivotModelConstraint implements IModelConstraint { + + private final IConstraintDescriptor descriptor; + + /** + * A separate query is maintained for each EClass of model object that this + * constraint handles. Maintain the values in weak references also, because + * the queries reference the EClasses that are the keys! + */ + private final java.util.Map<Stereotype, Reference<?>> queries = new java.util.WeakHashMap<Stereotype, Reference<?>>(); + + private QueryManager queryManager; + + protected static OCL oclInstance = null; + + /** + * Initializes me with the <code>descriptor</code> which contains my OCL + * body. + * + * @param descriptor + * the descriptor, which must contain an OCL expression in its + * body + */ + public AbstractOCLpivotModelConstraint(IConstraintDescriptor descriptor) { + this.descriptor = descriptor; + } + + /** + * Obtains the cached OCL query/constraint that implements me for the + * specified element's metaclass. + * + * @param target + * a model element + * @return the corresponding OCL query + */ + public ExpressionInOCL getConstraintCondition(EObject target) { + ExpressionInOCL result = null; + + Stereotype umlStereotype = UMLUtil.getStereotype(target); + + if (umlStereotype == null) { + return null; + } + + @SuppressWarnings("unchecked") + Reference<ExpressionInOCL> reference = (Reference<ExpressionInOCL>) queries.get(umlStereotype); + if (reference != null) { + result = reference.get(); + } + + if (result == null) { + // lazily initialize the condition. + if (oclInstance == null) { + OCL.initialize(null); + PivotEnvironmentFactory pef = new PivotEnvironmentFactory(null, + new MetaModelManager()); + oclInstance = OCL.newInstance(pef); + } + + OCLHelper oclHelper = oclInstance.createOCLHelper(); + + try { + NamedElement context = + oclInstance.getMetaModelManager().getPivotOf(NamedElement.class, umlStereotype); + + oclHelper.setContext((Type) context); + + String expression = getDescriptor().getBody(); + result = oclHelper.createQuery(expression); + } catch (ParserException parserException) { + throw new WrappedException(parserException); + } + + queries.put(umlStereotype, new WeakReference<ExpressionInOCL>( + result)); + } + + return result; + } + + // implements the inherited method + public IStatus validate(IValidationContext ctx) { + EObject target = ctx.getTarget(); + + try { + if (getQueryManager().check(target)) { + return ctx.createSuccessStatus(); + } else { + // OCL constraints only support the target object as an extraction + // variable and result locus, as OCL has no way to provide + // additional extractions. Also, there is no way for the OCL + // to access the context object + return ctx.createFailureStatus(target); + } + + } catch (Exception e) { + // do not raise an exception, but create a failure status. This is consistent with + // the behavior of the "in-profile" OCL pivot validation. + String message = String.format("The '%s' constraint is invalid - %s", getDescriptor().getName(), e.getMessage()); + return new ConstraintStatus(this, target, IStatus.ERROR, -1, + message, null); + } + } + + private QueryManager getQueryManager() { + if (queryManager == null) { + queryManager = new QueryManager(); + } + + return queryManager; + } + + /* + * (non-Javadoc) Implements the interface method. + */ + public IConstraintDescriptor getDescriptor() { + return descriptor; + } + + /** + * An object that knows how to obtain and evaluate the query implementation + * appropriate to the constraint's environment factory, accounting for + * whether it is using the OCL 1.0 or later API. + * + * @author Christian W. Damus (cdamus) + */ + private final class QueryManager { + + QueryManager() { + } + + /** + * Obtains and checks the appropriate parsed constraint for the + * specified target element. + * + * @param target + * an element to be validated + * @return whether it passed the constraint + */ + boolean check(EObject target) { + ExpressionInOCL query = getConstraintCondition(target); + return (Boolean) oclInstance.evaluate(target, query); + } + } +} diff --git a/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/oclpivot/OCLpivotConstraintParser.java b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/oclpivot/OCLpivotConstraintParser.java new file mode 100644 index 00000000000..d56d55c9ffd --- /dev/null +++ b/plugins/uml/org.eclipse.papyrus.uml.service.validation/src/org/eclipse/papyrus/uml/service/validation/oclpivot/OCLpivotConstraintParser.java @@ -0,0 +1,60 @@ +/** + * <copyright> + * + * Copyright (c) 2003, 2007 IBM Corporation and others. + * 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: + * IBM - Initial API and implementation + * Radek Dvorak (Borland) - Bugzilla 165458 + * + * </copyright> + * + * $Id: OCLConstraintParser.java,v 1.5 2007/06/06 22:28:11 cdamus Exp $ + */ + +package org.eclipse.papyrus.uml.service.validation.oclpivot; + +import org.eclipse.emf.validation.model.IModelConstraint; +import org.eclipse.emf.validation.service.IConstraintDescriptor; +import org.eclipse.emf.validation.service.IParameterizedConstraintDescriptor; +import org.eclipse.emf.validation.service.IParameterizedConstraintParser; + +/** + * Variant of the OCLConstraintParser, it enforces the use of pivot variant of OCL + * + * + * @link org.eclipse.emf.validation.internal.ocl.OCLConstraintParser + * + * @author Ansgar Radermacher + */ +public class OCLpivotConstraintParser implements IParameterizedConstraintParser { + + /** + * Initializes me. + */ + public OCLpivotConstraintParser() { + super(); + } + + // implements the inherited method + public IModelConstraint parseConstraint( + IParameterizedConstraintDescriptor desc) { + return new EcoreOCLConstraint(desc); + } + + /** + * A concrete implementation of OCL constraints for the Ecore metamodel. + * + * @author Christian W. Damus (cdamus) + */ + private static class EcoreOCLConstraint extends AbstractOCLpivotModelConstraint { + + EcoreOCLConstraint(IConstraintDescriptor descriptor) { + super(descriptor); + } + } +} |