blob: 25db53c0836f59ec5564c6d9989d45e374544416 [file] [log] [blame]
/*******************************************************************************
* 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;
/**
* @author Istvan Rath
*
*/
@VIATRANativeFunction(name = "add", returns = { ParameterType.INTEGER,
ParameterType.DOUBLE, ParameterType.STRING }, params = {
@NativeFunctionParameter(name = "operand1", description = "first operand", type = {
ParameterType.INTEGER, ParameterType.DOUBLE,
ParameterType.STRING }),
@NativeFunctionParameter(name = "operand2", description = "second operand", type = {
ParameterType.INTEGER, ParameterType.DOUBLE,
ParameterType.STRING }), }, remark = "Operand types need to be of the same type at run-time (integer+double combinations are allowed).")
public class AdderFunction 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 {
if (params.length == 2) {
if ((params[0] instanceof Integer)
&& (params[1] instanceof Integer))
return new Integer(((Integer) params[0]).intValue()
+ ((Integer) params[1]).intValue());
if ((params[0] instanceof Double) && (params[1] instanceof Double))
return new Double(((Double) params[0]).doubleValue()
+ ((Double) params[1]).doubleValue());
if ((params[0] instanceof String) && (params[1] instanceof String))
return params[0].toString() + params[1].toString();
if ((params[0] instanceof Integer) && (params[1] instanceof Double))
return new Double(((Integer) params[0]).intValue()
+ ((Double) params[1]).doubleValue());
if ((params[0] instanceof Double) && (params[1] instanceof Integer))
return new Double(((Double) params[0]).doubleValue()
+ ((Integer) params[1]).intValue());
throw new VPMRuntimeException("Parameter type mismatch!");
}
throw new VPMRuntimeException("Parameter count mismatch!");
}
public String getName() {
return "add"; // VTCL
}
public String getDescription() {
return "adds the two parameters. Numbers are added, strings are concatenated.";
}
public String getID() {
return this.getClass().getCanonicalName();
}
}