blob: cc57d599c2cd68a91b7dc0679916a8cfdd5f33ba [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under theterms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.interpreter.term.rules;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.gtasm.interpreter.exception.ViatraTransformationException;
import org.eclipse.viatra2.gtasm.interpreter.executionEnvironment.IExecutionEnvironment;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.enums.ValueKind;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.ModelElementQuery;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.Term;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.builtInFunctions.ArithmeticOperation;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.builtInFunctions.ConversionOperation;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.builtInFunctions.RelationalOperation;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.modelmanagement.queryFunctions.Multiplicity;
/**
* This class is used to evaluate terms in the GTASM machine.
*
* @author Peter Pasztor, Akos Horvath
*
*/
public class TermEvaluator {
private static TermEvaluator _instance = new TermEvaluator();
TermEvaluator() {}
public static TermEvaluator getInstance() {
return _instance;
}
/**
* Evaluates the given term
*
* @param org.eclipse.viatra2.gtasm.interpreter.executionEnvironment
* The execution environment on which the term has to be
* evaluated
* @param termToBeEvaluated
* The term to be evaluated
* @return The result of the term evaluation
* @throws ViatraTransformationException
*/
public Object evaluate(IExecutionEnvironment executionEnvironment,
Term termToBeEvaluated) throws ViatraTransformationException {
if (termToBeEvaluated instanceof ArithmeticOperation) {
// call the ArithmeticOperationEvaluator...
return (ArithmeticOperationEvaluator.getInstance().evaluate(
executionEnvironment,
termToBeEvaluated));
} else if (termToBeEvaluated instanceof ConversionOperation) {
return ConversionOperationEvaluator.getInstance().evaluate(
executionEnvironment, termToBeEvaluated);
} else if (termToBeEvaluated instanceof RelationalOperation) {
return RelationalOperationEvaluator.getInstance().evaluate(
executionEnvironment, termToBeEvaluated);
} else if (termToBeEvaluated instanceof ModelElementQuery) {
return ModelElementQueryEvaluator.getInstance().evaluate(
executionEnvironment, termToBeEvaluated);
} else if (null == termToBeEvaluated) {
// TODO: this is a hack. The parser returns null instead
// ValueKind.UNDEF_LITERAL when finding an undef constant.
return ValueKind.UNDEF_LITERAL;
} else {
return BasicTermEvaluator.getInstance().evaluate(
executionEnvironment, termToBeEvaluated);
}
}
// /**
// * This method tries to convert a String to an arithmetic value.
// *
// * At first it tries to convert is to Integer, then it tries to convert it
// * to Double. If it fails, throws a TermEvaluatorException.
// *
// * @return Integer or Double according to the String itself.
// * @throws GTASMException
// */
// public Object convertStringToArithmeticValue(String str)
// throws ViatraTransformationException {
// try {
// return new Integer(str);
// } catch (NumberFormatException e) {
// try {
// return new Double(str);
// } catch (NumberFormatException e1) {
// throw new TermInterpreterException("String \"" + str
// + "\" is not a valid arithmetic value!", null);
// }
// }
// }
/** Returns true if the input Object o is a Native Java type, else false
* @param o The input object to be checked.
* @return
*/
public Boolean isASMNativeType(Object o) {
if (o instanceof String || o instanceof Integer || o instanceof Double
|| o instanceof Boolean || o instanceof Multiplicity
|| ValueKind.UNDEF_LITERAL.equals(o)
|| o instanceof IModelElement)
return Boolean.TRUE;
return Boolean.FALSE;
}
/**Returns the type of the input parameter in a serialized form
* @param obj The object whose type is returned
* @return The java type in serialized form.
*/
protected String convertToJavaType(Object obj){
if(obj == null)
return "null";
if(obj instanceof String)
return "String";
if(obj instanceof Integer)
return "Integer";
if(obj instanceof Double)
return "Double";
if(obj instanceof Boolean)
return "Boolean";
if(obj instanceof Multiplicity)
return "Multiplicity";
if(ValueKind.UNDEF_LITERAL.equals(obj))
return "UNDEF";
if(obj instanceof IRelation)
return "Relation (ModelElement)";
if(obj instanceof IEntity)
return "Entity (ModelElement)";
if(obj instanceof JavaNativeValue)
return "JavaNativeValue";
return "unknown type";
}
/** Converts the input type to its corresponding Java type String
* @param type The type to be converted
* @return The java equivalent type
* @throws ViatraCompiledCompileTimeException
*/
public String convertValueKindtoJavaType(ValueKind type){
if(type == null)
return "null";
if(type.equals(ValueKind.BOOLEAN_LITERAL))
return "Boolean";
if(type.equals(ValueKind.DOUBLE_LITERAL))
return "Double";
if(type.equals(ValueKind.INTEGER_LITERAL))
return "Integer";
if(type.equals(ValueKind.MODELELEMENT_LITERAL))
return "EObject";
if(type.equals(ValueKind.MULTIPLICITY_LITERAL))
return "[Multiplicity]";
if(type.equals(ValueKind.STRING_LITERAL))
return "String";
// it is a ModelElement!!
if(type.equals(ValueKind.UNDEF_LITERAL))
return "Object";
return "error: unknown type";
}
}