blob: 0eeb2b6762852feb3578df3ce4f3da71a02ac110 [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 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:
* Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.interpreter.impl.rules;
import org.eclipse.viatra2.codegen.CodeOutputPlugin;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.gtasm.interpreter.exception.GTASMException;
import org.eclipse.viatra2.gtasm.interpreter.exception.ViatraTransformationException;
import org.eclipse.viatra2.gtasm.interpreter.executionEnvironment.IExecutionEnvironment;
import org.eclipse.viatra2.gtasm.interpreter.term.rules.JavaNativeValue;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.compoundRules.BlockRule;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.compoundRules.NestedRule;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.definitions.Rule;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.enums.ValueKind;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.simpleRules.ASMRuleInvocation;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.simpleRules.ConditionalRuleIf;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.simpleRules.ConditionalRuleTry;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.simpleRules.ModelManipulationRule;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.simpleRules.RuleUpdate;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.modelmanagement.queryFunctions.Multiplicity;
import org.eclipse.viatra2.interpreters.IProgressReport;
import org.eclipse.viatra2.logger.Logger;
/**
* The RuleInterpreter is the super class for all interpreters.
* @author Peter Pasztor, Akos Horvath
*
*/
public class RuleInterpreter {
private static RuleInterpreter INSTANCE = new RuleInterpreter();
//protected ExecutionEnvironment org.eclipse.viatra2.gtasm.interpreter.executionEnvironment;
protected Logger log;
protected CodeOutputPlugin codeout;
public CodeOutputPlugin getCodeout() {
return codeout;
}
public void setCodeout(CodeOutputPlugin codeout) {
this.codeout = codeout;
}
public Logger getLog() {
return log;
}
public void setLog(Logger log) {
this.log = log;
}
RuleInterpreter() {
}
public static RuleInterpreter getInstance() {
return INSTANCE;
}
/**
* @param executionEnvironment The actual execution environment to work with
* @param ruleToBeInterpreted the Rule to be interpreted
* @return True if the rule is interpreted without failure else False
* @throws GTASMException
*/
public Boolean interpretRule(IExecutionEnvironment executionEnvironment, Rule ruleToBeInterpreted, IProgressReport pr) throws ViatraTransformationException
{
try {
return interpretRule(executionEnvironment, ruleToBeInterpreted.getBody(), pr);
} catch (ViatraTransformationException e) {
throw e.addNewStackElement(ruleToBeInterpreted);
}
}
/**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";
}
/**
* @param executionEnvironment The actual execution environment to work with
* @param ruleToBeInterpreted the ASMRuleInvocation to be interpreted
* @return True if the rule is interpreted without failure else False
* @throws GTASMException
*/
public Boolean interpretRule(IExecutionEnvironment executionEnvironment, ASMRuleInvocation ruleToBeInterpreted, IProgressReport pr)
throws ViatraTransformationException
{
if (pr!=null) pr.progress(1);
if(ruleToBeInterpreted instanceof RuleUpdate)
{
return RuleUpdateInterpreter.getInstance().interpretRule(executionEnvironment, ruleToBeInterpreted,pr);
}
else if(ruleToBeInterpreted instanceof NestedRule)
{
return NestedRuleInterpreter.getInstance().interpretRule(executionEnvironment, ruleToBeInterpreted,pr);
}
else if(ruleToBeInterpreted instanceof ConditionalRuleIf || ruleToBeInterpreted instanceof ConditionalRuleTry)
{
return ConditionalRuleInterpreter.getInstance().interpretRule(executionEnvironment, ruleToBeInterpreted,pr);
}
else if(ruleToBeInterpreted instanceof BlockRule)
{
return BlockRuleInterpreter.getInstance().interpretRule(executionEnvironment, ruleToBeInterpreted,pr);
}
else if(ruleToBeInterpreted instanceof ModelManipulationRule)
{
return ModelManipulationRuleInterpreter.getInstance().interpretRule(executionEnvironment, ruleToBeInterpreted,pr);
}
else
{
return BasicRuleInterpreter.getInstance().interpretRule(executionEnvironment, ruleToBeInterpreted,pr);
}
}
public static void initInterpreters(Logger log, CodeOutputPlugin codeout) {
BasicRuleInterpreter.getInstance().setCodeout(codeout);
BasicRuleInterpreter.getInstance().setLog(log);
BlockRuleInterpreter.getInstance().setLog(log);
ConditionalRuleInterpreter.getInstance().setLog(log);
NestedRuleInterpreter.getInstance().setLog(log);
RuleUpdateInterpreter.getInstance().setLog(log);
}
}