| /******************************************************************************* |
| * 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; |
| |
| // fqn: org.eclipse.viatra2.natives.AdderFun |
| import org.eclipse.viatra2.core.IModelSpace; |
| import org.eclipse.viatra2.errors.VPMRuntimeException; |
| import org.eclipse.viatra2.natives.NativeFunctionParameter.ParameterType; |
| |
| /** |
| * Native function to provide a way for easily checking for the existence of |
| * certain model elements in VTCL programs (note: the 'ref' rule throws an |
| * exception if a non-existing model element is passed as the parameter, whereas |
| * this native function returns "false" in that case. It returns "true" for |
| * existing elements.) |
| * |
| * @author Istvan Rath |
| * |
| */ |
| @VIATRANativeFunction(name = "exists", returns = ParameterType.BOOLEAN, remark = "The 'ref' rule throws an exception " |
| + "if a non-existing model element is passed as the parameter, whereas this native function " |
| + "returns \"false\" in that case.", |
| params = @NativeFunctionParameter(name = "elements", |
| description = "FQNs of model elements to be checked for existence", isVarArg = true, |
| type = { NativeFunctionParameter.ParameterType.STRING })) |
| public class ExistsFunction implements ASMNativeFunction { |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.viatra2.natives.ASMNativeFunction#evaluate(org.eclipse.viatra2 |
| * .core.IModelSpace, java.lang.String[]) |
| */ |
| public Object evaluate(IModelSpace msp, Object[] params) |
| throws VPMRuntimeException { |
| boolean allOK = true; |
| for (Object ref : params) { |
| if (ref instanceof String) |
| allOK &= (msp.getModelManager().getElementByName((String) ref) != null); |
| else |
| throw new VPMRuntimeException("Parameter type mismatch!"); |
| } |
| return allOK; |
| } |
| |
| public String getName() { |
| return "exists"; // VTCL
|
| } |
| |
| public String getDescription() { |
| return "checks for the existence of model elements; returns \"true\" if all parameters exist, \"false\" otherwise"; |
| } |
| |
| public String getID() { |
| return this.getClass().getCanonicalName(); |
| } |
| |
| } |