blob: a387bb990c852defa50e4dc7ee3a3ce8f56a3359 [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.gtasm.interpreter.exception.ViatraTransformationException;
import org.eclipse.viatra2.gtasm.interpreter.executionEnvironment.IExecutionEnvironment;
import org.eclipse.viatra2.gtasm.interpreter.term.rules.TermEvaluator;
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.interpreters.IProgressReport;
public class ConditionalRuleInterpreter extends RuleInterpreter {
private static ConditionalRuleInterpreter _instance = new ConditionalRuleInterpreter();
private ConditionalRuleInterpreter() {
;
}
public static ConditionalRuleInterpreter getInstance() {
return _instance;
}
public Boolean interpretRule(IExecutionEnvironment executionEnvironment,
ASMRuleInvocation ruleToBeInterpreted, IProgressReport pr)
throws ViatraTransformationException {
Boolean condition = null;
/*
* This long line is a concise form of determining whether the rule is
* an if or a try type rule. In case it is an if type, the condition
* term is evaluated, in the other case, the rule is interpreted.
* Thereafter the appropriate rule is interpreted.
*/
condition = (ruleToBeInterpreted instanceof ConditionalRuleIf) ? (TermEvaluator.getInstance().evaluate(
executionEnvironment,
((ConditionalRuleIf) ruleToBeInterpreted)
.getExpressionToTest()).equals(Boolean.TRUE))
: (RuleInterpreter.getInstance().interpretRule(
executionEnvironment,
((ConditionalRuleTry) ruleToBeInterpreted)
.getRuleToTry(),pr));
if (condition.equals(Boolean.TRUE)) {
return (ruleToBeInterpreted instanceof ConditionalRuleIf) ?
(RuleInterpreter.getInstance().interpretRule(executionEnvironment,
((ConditionalRuleIf) ruleToBeInterpreted).getRuleTrue(),pr))
: Boolean.TRUE;
} else {
ASMRuleInvocation elseRule = (ruleToBeInterpreted instanceof ConditionalRuleIf) ? (((ConditionalRuleIf) ruleToBeInterpreted)
.getRuleFalse())
: (((ConditionalRuleTry) ruleToBeInterpreted).getRuleElse());
if (elseRule == null)
return Boolean.TRUE;
return RuleInterpreter.getInstance().interpretRule(
executionEnvironment, elseRule,pr);
}
}
}