| /******************************************************************************* |
| * Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro |
| * 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: |
| * Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.natives; |
| |
| import org.eclipse.viatra2.core.EDeleteSemantics; |
| import org.eclipse.viatra2.core.IEntity; |
| import org.eclipse.viatra2.core.IModelSpace; |
| import org.eclipse.viatra2.errors.VPMCoreException; |
| import org.eclipse.viatra2.errors.VPMRuntimeException; |
| import org.eclipse.viatra2.natives.NativeFunctionParameter.ParameterType; |
| |
| @VIATRANativeFunction(name = "clean", remark = "Deletes model elements faster than the \"delete()\" rule (recursive).", params = { @NativeFunctionParameter(name = "entities", type = { ParameterType.MODEL_ELEMENT }, isVarArg = true, description = "model element references to top-level entities to be deleted.") }, returns = { ParameterType.BOOLEAN }) |
| public class CleanFunction implements ASMNativeFunction { |
| |
| private void recursive_delete(IEntity target) throws VPMCoreException { |
| // delete all children recursively
|
| while (!target.getContents().isEmpty()) { |
| try { |
| recursive_delete((IEntity) target.getContents().iterator() |
| .next()); |
| } catch (ClassCastException e) { |
| System.out.println("Non-entity content detected at " |
| + target.getFullyQualifiedName()); |
| } |
| } |
| target.getModelSpace().getModelManager().deleteEntity(target, |
| EDeleteSemantics.DELETE_SEMANTICS_FORCE); |
| |
| } |
| |
| public Object evaluate(IModelSpace msp, Object[] params) |
| throws VPMRuntimeException { |
| |
| try { |
| for (Object param : params) { |
| if (param instanceof IEntity) { |
| recursive_delete((IEntity) param); |
| } |
| } |
| } catch (VPMCoreException cex) { |
| return Boolean.FALSE; |
| } |
| |
| return Boolean.TRUE; |
| } |
| |
| public String getDescription() { |
| return "Implements a fast, recursive model element delete algorithm."; |
| } |
| |
| public String getID() { |
| return this.getClass().getCanonicalName(); |
| } |
| |
| public String getName() { |
| return "clean"; |
| } |
| |
| } |