blob: 5055a1424c08b6699679cfc50e480be6d166ae02 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011, 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.build.utilities;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.lib.WorkflowComponentWithModelSlot;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
import org.eclipse.uml2.uml.Constraint;
/**
* Removes all constraints from the model in the designated <tt>modelSlot</tt>.
*/
public class ConstraintRemover extends WorkflowComponentWithModelSlot
{
private Logger log = Logger.getLogger(getClass());
@Override
public void invokeInternal(WorkflowContext ctx, ProgressMonitor arg1, Issues arg2) {
Resource resource = (Resource) ctx.get(getModelSlot());
log.info("Removing constraints from '" + resource.getURI() + "'");
List<EObject> allConstraints = new ArrayList<EObject>();
for (TreeIterator<EObject> tit = resource.getAllContents(); tit.hasNext(); ) {
EObject eObject = tit.next();
if (eObject instanceof Constraint) {
allConstraints.add(eObject);
tit.prune();
}
}
for (EObject constraint : allConstraints ) {
EReference eContainmentFeature = constraint.eContainmentFeature();
Object containingProperty = constraint.eContainer().eGet(eContainmentFeature);
if (eContainmentFeature.isMany()) {
((List<?>)containingProperty).remove(constraint);
}
else {
((EObject)containingProperty).eSet(constraint.eContainingFeature(), null);
}
}
}
}