diff options
author | Ed Willink | 2017-02-20 17:40:49 +0000 |
---|---|---|
committer | Ed Willink | 2017-02-21 13:06:56 +0000 |
commit | 474ae0f564aa6b4e0ab5b14ba71cc90801ba49da (patch) | |
tree | 03c4fb5661fc09251fe9305813b3d1ebabe3da09 | |
parent | af077c5bbe0fd7440050ca2ca4d83caf0f462f06 (diff) | |
download | org.eclipse.ocl-474ae0f564aa6b4e0ab5b14ba71cc90801ba49da.tar.gz org.eclipse.ocl-474ae0f564aa6b4e0ab5b14ba71cc90801ba49da.tar.xz org.eclipse.ocl-474ae0f564aa6b4e0ab5b14ba71cc90801ba49da.zip |
[512454] Validate OCL in UML expression bodies
3 files changed, 38 insertions, 18 deletions
diff --git a/plugins/org.eclipse.ocl.pivot.uml/src/org/eclipse/ocl/pivot/uml/internal/validation/UMLOCLEValidator.java b/plugins/org.eclipse.ocl.pivot.uml/src/org/eclipse/ocl/pivot/uml/internal/validation/UMLOCLEValidator.java index d8e8e0bb07..fda1ad1d90 100644 --- a/plugins/org.eclipse.ocl.pivot.uml/src/org/eclipse/ocl/pivot/uml/internal/validation/UMLOCLEValidator.java +++ b/plugins/org.eclipse.ocl.pivot.uml/src/org/eclipse/ocl/pivot/uml/internal/validation/UMLOCLEValidator.java @@ -10,6 +10,7 @@ */ package org.eclipse.ocl.pivot.uml.internal.validation; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -24,6 +25,7 @@ import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EValidator; import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.Diagnostician; import org.eclipse.emf.ecore.util.EObjectValidator; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -145,7 +147,7 @@ public class UMLOCLEValidator implements EValidator message = message.replace("\n", ""); } diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, UMLValidator.DIAGNOSTIC_SOURCE, - 0, message, new Object[] { diagnosticEObject })); + 0, message, new Object[] { diagnosticEObject })); return Boolean.FALSE; } @@ -218,7 +220,7 @@ public class UMLOCLEValidator implements EValidator } } } - + protected final boolean mayUseNewLines; public UMLOCLEValidator(boolean mayUseNewLines) { @@ -227,8 +229,8 @@ public class UMLOCLEValidator implements EValidator @Override public boolean validate(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) { - return validate(eObject.eClass(), eObject, diagnostics, context); -// return true; + return validate(eObject.eClass(), eObject, diagnostics, context); + // return true; } @Override @@ -289,8 +291,8 @@ public class UMLOCLEValidator implements EValidator } } catch (ParserException e) { // TODO Auto-generated catch block -// e.printStackTrace(); - } + // e.printStackTrace(); + } } } } @@ -412,7 +414,7 @@ public class UMLOCLEValidator implements EValidator */ public boolean validateOpaqueAction(@NonNull OpaqueAction opaqueAction, DiagnosticChain diagnostics, Map<Object, Object> context) { return UMLOCLEValidator.INSTANCE.validateOpaqueElement(opaqueAction.getLanguages(), - opaqueAction.getBodies(), opaqueAction, diagnostics, context); + opaqueAction.getBodies(), opaqueAction, diagnostics, context); } /** @@ -422,9 +424,9 @@ public class UMLOCLEValidator implements EValidator */ public boolean validateOpaqueBehavior(@NonNull OpaqueBehavior opaqueBehavior, DiagnosticChain diagnostics, Map<Object, Object> context) { return UMLOCLEValidator.INSTANCE.validateOpaqueElement(opaqueBehavior.getLanguages(), - opaqueBehavior.getBodies(), opaqueBehavior, diagnostics, context); + opaqueBehavior.getBodies(), opaqueBehavior, diagnostics, context); } - + /** * Validate the syntax and semantics of any OCL bofy. * <p> @@ -432,7 +434,7 @@ public class UMLOCLEValidator implements EValidator */ public boolean validateOpaqueExpression(@NonNull OpaqueExpression opaqueExpression, DiagnosticChain diagnostics, Map<Object, Object> context) { return UMLOCLEValidator.INSTANCE.validateOpaqueElement(opaqueExpression.getLanguages(), - opaqueExpression.getBodies(), opaqueExpression, diagnostics, context); + opaqueExpression.getBodies(), opaqueExpression, diagnostics, context); } /** @@ -497,6 +499,9 @@ public class UMLOCLEValidator implements EValidator } @SuppressWarnings("unused") ExpressionInOCL asQuery = metamodelManager.parseSpecification(asSpecification); + Map<Object,Object> newContext = new HashMap<>(context); + newContext.remove(EObjectValidator.ROOT_OBJECT); + return Diagnostician.INSTANCE.validate(asQuery, diagnostics, newContext); } catch (ParserException e) { if (diagnostics != null) { String objectLabel = LabelUtil.getLabel(opaqueElement); @@ -509,7 +514,6 @@ public class UMLOCLEValidator implements EValidator } return false; } - return true; } protected boolean validateSyntax2(@NonNull EObject instance, @NonNull String body, org.eclipse.uml2.uml.@NonNull Element opaqueElement, final @Nullable DiagnosticChain diagnostics, @NonNull Map<Object, Object> context) { OCL ocl = PivotDiagnostician.getOCL(context); @@ -530,6 +534,11 @@ public class UMLOCLEValidator implements EValidator return false; } asQuery = metamodelManager.parseSpecification(asSpecification); + Map<Object,Object> newContext = new HashMap<>(context); + newContext.remove(EObjectValidator.ROOT_OBJECT); + if (!Diagnostician.INSTANCE.validate(asQuery, diagnostics, newContext)) { + return false; + } } catch (ParserException e) { if (diagnostics != null) { String objectLabel = LabelUtil.getLabel(opaqueElement); diff --git a/plugins/org.eclipse.ocl.pivot/src/org/eclipse/ocl/pivot/utilities/LabelUtil.java b/plugins/org.eclipse.ocl.pivot/src/org/eclipse/ocl/pivot/utilities/LabelUtil.java index 68d4e3d6fd..d39f3afbb6 100644 --- a/plugins/org.eclipse.ocl.pivot/src/org/eclipse/ocl/pivot/utilities/LabelUtil.java +++ b/plugins/org.eclipse.ocl.pivot/src/org/eclipse/ocl/pivot/utilities/LabelUtil.java @@ -108,7 +108,7 @@ public class LabelUtil } /** - * Return a context map for use by EValidator.validate in which the EVlaidator.class key + * Return a context map for use by EValidator.validate in which the EValidator.class key * is mapped to the eValidator, and the EValidator.SubstitutionLabelProvider.class key * is mapped to a SubstitutionLabelProvider that uses getLabel(). */ diff --git a/tests/org.eclipse.ocl.examples.xtext.tests/src/org/eclipse/ocl/examples/pivot/tests/UMLValidateTest.java b/tests/org.eclipse.ocl.examples.xtext.tests/src/org/eclipse/ocl/examples/pivot/tests/UMLValidateTest.java index 9e02b52f2a..6737ec78ab 100644 --- a/tests/org.eclipse.ocl.examples.xtext.tests/src/org/eclipse/ocl/examples/pivot/tests/UMLValidateTest.java +++ b/tests/org.eclipse.ocl.examples.xtext.tests/src/org/eclipse/ocl/examples/pivot/tests/UMLValidateTest.java @@ -667,19 +667,30 @@ public class UMLValidateTest extends AbstractValidateTests assertNoResourceErrors("Loading", umlResource); Map<Object, Object> validationContext = LabelUtil.createDefaultContext(Diagnostician.INSTANCE); OCLDelegateDomain.initializePivotOnlyDiagnosticianContext(validationContext); - Model model = (Model) umlResource.getContents().get(0); - org.eclipse.uml2.uml.NamedElement xx = model.getOwnedMember("InstanceSpecification1"); +// Model model = (Model) umlResource.getContents().get(0); +// org.eclipse.uml2.uml.NamedElement xx = model.getOwnedMember("InstanceSpecification1"); assertValidationDiagnostics("Loading", umlResource, validationContext);//, assertUMLOCLValidationDiagnostics(ocl, "UML Load", umlResource, StringUtil.bind(PivotMessagesInternal.ParsingError, "CustomPrimitiveTypes::Class1::SimpleDataTypeArithmetic::" + - "self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute ", + "self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute ", "The 'Class1::SimpleDataTypeArithmetic' constraint is invalid: 'self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute'\n" + "1: Unresolved Operation 'CustomPrimitiveTypes::SimpleDataType::+(CustomPrimitiveTypes::SimpleDataType)'"), - StringUtil.bind(PivotMessagesInternal.ValidationResultIsInvalid_ERROR_, "Class1", "SimpleDataTypeArithmetic", LabelUtil.getLabel(xx), - StringUtil.bind(PivotMessagesInternal.FailedToEvaluate_ERROR_, "OclInvalid::oclBadOperation() : OclInvalid[1]", "6.0", "self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)"))); //, + "The 'CallExp::TypeIsNotInvalid' constraint is violated for 'self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)'", + "The 'OperationCallExp::ArgumentCount' constraint is violated for 'self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)'", + "The 'OperationCallExp::ArgumentTypeIsConformant' constraint is violated for 'self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)'" + ); +// assertUMLOCLValidationDiagnostics(ocl, "UML Load", umlResource, +// StringUtil.bind(PivotMessagesInternal.ParsingError, "CustomPrimitiveTypes::Class1::SimpleDataTypeArithmetic::" + +// "self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute ", +// "The 'Class1::SimpleDataTypeArithmetic' constraint is invalid: 'self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute'\n" + +// "1: Unresolved Operation 'CustomPrimitiveTypes::SimpleDataType::+(CustomPrimitiveTypes::SimpleDataType)'"), +// StringUtil.bind(PivotMessagesInternal.ValidationResultIsInvalid_ERROR_, "Class1", "SimpleDataTypeArithmetic", LabelUtil.getLabel(xx), +// StringUtil.bind(PivotMessagesInternal.FailedToEvaluate_ERROR_, "OclInvalid::oclBadOperation() : OclInvalid[1]", "6.0", "self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)"))); //, ocl.dispose(); } - +// Diagnostic ERROR source=org.eclipse.emf.ecore code=0 Diagnosis of org.eclipse.uml2.uml.internal.impl.ModelImpl@6325f352{file:/E:/GIT/org.eclipse.ocl/tests/org.eclipse.ocl.examples.xtext.tests/bin/org/eclipse/ocl/examples/pivot/tests/models/Bug467192.uml#_7AJBoNeUEeSuVqZ4Q6mWmQ} data=[org.eclipse.uml2.uml.internal.impl.ModelImpl@6325f352 (name: CustomPrimitiveTypes, visibility: <unset>) (URI: null) (viewpoint: <unset>)] [Diagnostic ERROR source=org.eclipse.uml2.uml code=0 "Parsing error for CustomPrimitiveTypes::Class1::SimpleDataTypeArithmetic::self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute : +// The 'Class1::SimpleDataTypeArithmetic' constraint is invalid: 'self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute' +// 1: Unresolved Operation 'CustomPrimitiveTypes::SimpleDataType::+(CustomPrimitiveTypes::SimpleDataType)'" data=[org.eclipse.uml2.uml.internal.impl.OpaqueExpressionImpl@15405bd6 (name: <unset>, visibility: <unset>) (body: [self.simpleDataTypeAttribute + self.simpleDataTypeAttribute <> self.simpleDataTypeAttribute ], language: [OCL])], Diagnostic WARNING source=org.eclipse.ocl.pivot code=0 The 'CallExp::TypeIsNotInvalid' constraint is violated for 'self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)' data=[self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)], Diagnostic WARNING source=org.eclipse.ocl.pivot code=0 The 'OperationCallExp::ArgumentCount' constraint is violated for 'self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)' data=[self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)], Diagnostic WARNING source=org.eclipse.ocl.pivot code=0 The 'OperationCallExp::ArgumentTypeIsConformant' constraint is violated for 'self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)' data=[self.simpleDataTypeAttribute.oclBadOperation(self.simpleDataTypeAttribute)]] public void test_umlValidation_Bug472461() throws IOException { resetRegistries(); CommonOptions.DEFAULT_DELEGATION_MODE.setDefaultValue(PivotConstants.OCL_DELEGATE_URI_PIVOT); |