blob: 7efc660521f218490b7235c9364575a48dd4a1b9 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2014, 2018 Willink Transformations and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* E.D.Willink - Initial API and implementation
*******************************************************************************/
package org.eclipse.ocl.examples.debug.ui.delegate;
import java.util.Map;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.pivot.ExpressionInOCL;
import org.eclipse.ocl.pivot.evaluation.AbstractConstraintEvaluator;
import org.eclipse.ocl.pivot.evaluation.EvaluationException;
import org.eclipse.ocl.pivot.evaluation.EvaluationVisitor;
import org.eclipse.ocl.pivot.internal.delegate.OCLDelegateDomain;
import org.eclipse.ocl.pivot.internal.delegate.OCLDelegateException;
import org.eclipse.ocl.pivot.internal.delegate.OCLValidationDelegate;
import org.eclipse.ocl.pivot.utilities.NameUtil;
import org.eclipse.ocl.pivot.utilities.OCL;
/**
* An implementation of the dynamic validation delegate API, maintaining a cache
* of compiled constraints and invariants.
*/
public class OCLDebugValidationDelegate extends OCLValidationDelegate
{
public OCLDebugValidationDelegate(@NonNull OCLDelegateDomain delegateDomain, @NonNull EClassifier classifier) {
super(delegateDomain, classifier);
}
protected boolean validateExpressionInOCL(final @NonNull EClassifier eClassifier, final @NonNull Object value, final @Nullable DiagnosticChain diagnostics,
final Map<Object, Object> context, String constraintName, final String source, final int code, @NonNull ExpressionInOCL query) {
AbstractConstraintEvaluator<Boolean> constraintEvaluator = new CheckingConstraintEvaluator(eClassifier, query)
{
@Override
protected String getObjectLabel() {
return NameUtil.qualifiedNameFor(value);
// return ClassUtil.getLabel(eClassifier, value, context);
}
@Override
protected Boolean handleFailureResult(@Nullable Object result) {
if (result == null) {
String message = getConstraintResultMessage(result);
throw new OCLDelegateException(new EvaluationException(message));
}
if (diagnostics != null) {
String message = getConstraintResultMessage(result);
int severity = getConstraintResultSeverity(result);
diagnostics.add(new BasicDiagnostic(severity, source, code, message, new Object [] { value }));
}
return Boolean.FALSE;
}
};
OCL ocl = delegateDomain.getOCL();
EvaluationVisitor evaluationVisitor = ocl.createEvaluationVisitor(value, query);
return constraintEvaluator.evaluate(evaluationVisitor);
}
}