blob: 73df5d972dfc19cb14d084c0b08b7c92d260f3ad [file] [log] [blame]
/**
*
* Copyright (c) 2009-2010 Thales Corporate Services S.A.S.
* 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:
* Thales Corporate Services S.A.S - initial API and implementation
*
*/
package org.eclipse.egf.model.fprod.util;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.egf.model.EGFFprodPlugin;
import org.eclipse.egf.model.fcore.util.FcoreValidator;
import org.eclipse.egf.model.fprod.FprodPackage;
import org.eclipse.egf.model.fprod.ProductionPlan;
import org.eclipse.egf.model.fprod.ProductionPlanInvocation;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.util.EObjectValidator;
import org.eclipse.emf.validation.model.EvaluationMode;
import org.eclipse.emf.validation.model.IConstraintStatus;
import org.eclipse.emf.validation.service.IBatchValidator;
import org.eclipse.emf.validation.service.ModelValidationService;
import org.eclipse.emf.validation.service.ITraversalStrategy.Recursive;
/**
* <!-- begin-user-doc -->
* The <b>Validator</b> for the model.
* <!-- end-user-doc -->
*
* @see org.eclipse.egf.model.fprod.FprodPackage
* @generated
*/
public class FprodValidator extends EObjectValidator {
/**
* The cached model package
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@SuppressWarnings("hiding")
public static final FprodValidator INSTANCE = new FprodValidator();
/**
* A constant for the {@link org.eclipse.emf.common.util.Diagnostic#getSource() source} of
* diagnostic {@link org.eclipse.emf.common.util.Diagnostic#getCode() codes} from this package.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @see org.eclipse.emf.common.util.Diagnostic#getSource()
* @see org.eclipse.emf.common.util.Diagnostic#getCode()
* @generated
*/
@SuppressWarnings("hiding")
public static final String DIAGNOSTIC_SOURCE = "org.eclipse.egf.model.fprod"; //$NON-NLS-1$
/**
* A constant with a fixed name that can be used as the base value for additional hand written
* constants.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
private static final int GENERATED_DIAGNOSTIC_CODE_COUNT = 0;
/**
* A constant with a fixed name that can be used as the base value for additional hand written
* constants in a derived class.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
protected static final int DIAGNOSTIC_CODE_COUNT = GENERATED_DIAGNOSTIC_CODE_COUNT;
/**
* Model Validation Service interface for batch validation of EMF elements.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
private final IBatchValidator batchValidator;
/**
* The cached base package validator.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
protected FcoreValidator fcoreValidator;
/**
* Creates an instance of the switch.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
public FprodValidator() {
super();
fcoreValidator = FcoreValidator.INSTANCE;
batchValidator = (IBatchValidator) ModelValidationService.getInstance().newValidator(EvaluationMode.BATCH);
batchValidator.setTraversalStrategy(new Recursive());
batchValidator.setIncludeLiveConstraints(true);
batchValidator.setReportSuccesses(false);
}
/**
* Returns the package of this validator switch.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@Override
protected EPackage getEPackage() {
return FprodPackage.eINSTANCE;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@Override
public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context) {
IStatus status = Status.OK_STATUS;
// do whatever the basic EcoreValidator does
super.validate(eClass, eObject, diagnostics, context);
// no point in validating if we can't report results
if (diagnostics != null) {
// if EMF Mode Validation Service already covered the sub-tree,
// which it does for efficient computation and error reporting,
// then don't repeat (the Diagnostician does the recursion
// externally). If there is no context map, then we can't
// help it
if (hasProcessed(eObject, context) == false) {
status = batchValidator.validate(eObject, new NullProgressMonitor());
processed(eObject, context, status);
appendDiagnostics(status, diagnostics);
}
}
return status.isOK();
}
/**
* Calls <code>validateXXX</code> for the corresponding classifier of the model.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@Override
protected boolean validate(int classifierID, Object value, DiagnosticChain diagnostics, Map<Object, Object> context) {
switch (classifierID) {
case FprodPackage.PRODUCTION_PLAN:
return validateProductionPlan((ProductionPlan) value, diagnostics, context);
case FprodPackage.PRODUCTION_PLAN_INVOCATION:
return validateProductionPlanInvocation((ProductionPlanInvocation) value, diagnostics, context);
default:
return true;
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
public boolean validateProductionPlan(ProductionPlan productionPlan, DiagnosticChain diagnostics, Map<Object, Object> context) {
return validate_EveryDefaultConstraint(productionPlan, diagnostics, context);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
public boolean validateProductionPlanInvocation(ProductionPlanInvocation productionPlanInvocation, DiagnosticChain diagnostics, Map<Object, Object> context) {
return validate_EveryDefaultConstraint(productionPlanInvocation, diagnostics, context);
}
/**
* Returns the resource locator that will be used to fetch messages for this validator's
* diagnostics.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@Override
public ResourceLocator getResourceLocator() {
return EGFFprodPlugin.INSTANCE;
}
/**
* If we have a context map, record this object's <code>status</code> in it
* so that we will know later that we have processed it and its sub-tree.
*
* @param eObject
* an element that we have validated
* @param context
* the context (may be <code>null</code>)
* @param status
* the element's validation status
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
private void processed(EObject eObject, Map<Object, Object> context, IStatus status) {
if (context != null) {
context.put(eObject, status);
}
}
/**
* Determines whether we have processed this <code>eObject</code> before,
* by automatic recursion of the EMF Model Validation Service. This is
* only possible if we do, indeed, have a context.
*
* @param eObject
* an element to be validated (we hope not)
* @param context
* the context (may be <code>null</code>)
* @return <code>true</code> if the context is not <code>null</code> and
* the <code>eObject</code> or one of its containers has already been
* validated; <code>false</code>, otherwise
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
private boolean hasProcessed(EObject eObject, Map<Object, Object> context) {
boolean result = false;
if (context != null) {
// this is O(NlogN) but there's no helping it
while (eObject != null) {
if (context.containsKey(eObject)) {
result = true;
eObject = null;
} else {
eObject = eObject.eContainer();
}
}
}
return result;
}
/**
* Converts a status result from the EMF validation service to diagnostics.
*
* @param status
* the EMF validation service's status result
* @param diagnostics
* a diagnostic chain to accumulate results on
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
private void appendDiagnostics(IStatus status, DiagnosticChain diagnostics) {
if (status.isMultiStatus()) {
IStatus[] children = status.getChildren();
for (int i = 0; i < children.length; i++) {
appendDiagnostics(children[i], diagnostics);
}
} else if (status instanceof IConstraintStatus) {
diagnostics.add(new BasicDiagnostic(status.getSeverity(), status.getPlugin(), status.getCode(), status.getMessage(), ((IConstraintStatus) status).getResultLocus().toArray()));
}
}
} // FprodValidator