blob: 4e3ada1b9997304e2110d60e70b67170f8baed92 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Akos Horvath, Gergely Varro 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:
* Akos Horvath, Gergely Varro - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.impl.gtmatcher.internal;
import org.eclipse.viatra2.core.IModelManager;
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.patternmatcher.ExecutionMode;
import org.eclipse.viatra2.gtasm.patternmatcher.ParameterMode;
import org.eclipse.viatra2.gtasm.patternmatcher.PatternCallSignature;
import org.eclipse.viatra2.gtasm.patternmatcher.PatternMatcherParameters;
import org.eclipse.viatra2.gtasm.patternmatcher.Scope;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.gtmatcher.exceptions.GTErrorStrings;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.gtmatcher.exceptions.GraphTransformationException;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.term.ITermHandler;
import org.eclipse.viatra2.gtasm.patternmatcher.patterns.IGTRuleMatcher;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.definitions.SymbolicRuleParameter;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.definitions.Variable;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.enums.ValueKind;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.GTPatternCall;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.GTRule;
import org.eclipse.viatra2.logger.Logger;
/**
* * The class encapsulates the basic methods to initialise a GTRule matcher
* @author Akos Horvath
*/
public abstract class GTRuleMatcher implements IGTRuleMatcher{
protected Logger logger;
protected IModelManager manager;
protected GTRule gtRule;
protected IExecutionEnvironment gtEnvironment = null;
protected ITermHandler handler;
// INITIALIZING methods for gt rule matching
/**
* Initialize the gtRule matcher
* @param logger the logger of the actual modelspace
* @param manager viatra model space manager
* @param gtRule the actual rule
* @param gtEnviroment the execution enviroment on which the rule communicates with the org.eclipse.viatra2.gtasm.interpreter.impl.interpreter
* @param handler term handler for the rule
*/
public GTRuleMatcher(Logger logger
, IModelManager manager
, GTRule gtRule
// , IGTRuleExecutionEnvironment gtEnviroment
//, TermHandler handler
) {
this.logger = logger;
this.manager = manager;
this.gtRule = gtRule;
//this.gtEnviroment = gtEnviroment;
//this.handler = handler;
}
//***************************************** INPUT parameter manipulation methods****************************
/**
* Initialize the Left hand side (matcher part) pattern of the gt rule and combinde the parameter mapping with the input parameters of the gt rule
* @param symSignatures mapping of the conatinment constraints of the input parameters
* @param symInputMapping mapping of the input parameters
* @param symQuantificationOrder the order of the forall and choose quantificated elements
* @return an inner class holding the inputmapping, the Signatures and the QuantifiationOrder
* @throws GTASMException
*/
protected PatternMatcherParameters initLHSPattern(PatternCallSignature[] symSignatures, Object[] symInputMapping, Integer[] symQuantificationOrder)
throws ViatraTransformationException
{
//******LHS of the GTRule
//create the PatternCall signature for the lhs call
PatternCallSignature[] lhsSignatures = new PatternCallSignature[gtRule.getPrecondition().getActualParameters().size()];
Object[] lhsInputMapping = new Object[gtRule.getPrecondition().getActualParameters().size()];
Integer[] lhsQuantification = new Integer[gtRule.getPrecondition().getActualParameters().size()];
int forallQuantificationNumber = 0;
//copy the symbolic parameter signatures and inputMapping to the lhs signature and Value arrays
for(int i = 0; i< symSignatures.length; i++)
{
SymbolicRuleParameter symParam = gtRule.getSymParameters().get(i);
if(symParam.getVariable().getReferences().size() > 0) //is the symParam in one of the patterns
{if(getActualParameterIndexofSymbolicParameter(gtRule.getPrecondition(), symParam) != -1)
{lhsSignatures[getActualParameterIndexofSymbolicParameter(gtRule.getPrecondition(), symParam)] = symSignatures[i];
lhsInputMapping[getActualParameterIndexofSymbolicParameter(gtRule.getPrecondition(), symParam)] = symInputMapping[i];
if(symQuantificationOrder != null)
{
// its a quantificated variable with a forall rule
//if(symSignatures[i].getExecutionMode().compareTo(ExecutionMode.MULTIPLE_RESULTS) == 0)
// {
lhsQuantification[getSymQuantificationIndex(i,symQuantificationOrder)] = getActualParameterIndexofSymbolicParameter(gtRule.getPrecondition(), symParam);
forallQuantificationNumber++;
// }
//else
// {lhsQuantification[getSymQuantificationIndex(i,symQuantificationOrder)] = getActualParameterIndexofSymbolicParameter(gtRule.getPrecondition(), symParam);
// }
}
}
}
else
logger.warning(symParam.getVariable().getName()+" is never used inside "+gtRule.getName()+".");
}
//fill up the lhs signature and InputMapping arrays with the corresponding values
for(int j=0; j < gtRule.getPrecondition().getActualParameters().size(); j++)
{
if(lhsSignatures[j] == null)
{lhsSignatures[j] = new PatternCallSignature();
lhsSignatures[j].setExecutionMode(ExecutionMode.SINGLE_RESULT);
lhsSignatures[j].setParameterMode(ParameterMode.OUTPUT);
lhsSignatures[j].setParameterScope(new Scope());
lhsInputMapping[j] = ValueKind.UNDEF_LITERAL;
if(symQuantificationOrder != null)
{
// its a quantificated variable with a forall rule
lhsQuantification[forallQuantificationNumber] = j;
forallQuantificationNumber++;
}
}
}
return new PatternMatcherParameters(lhsSignatures, lhsInputMapping, lhsQuantification);
}
/** Returns the order of the rule from the invocation
* @param symParamIndex the current index of the rule
* @param symQuantificationOrder Quantification order of the invocation
* @return the qunatification order of the symBolic parameter's index
* @throws GTASMException
*/
private int getSymQuantificationIndex(Integer symParamIndex, Integer[] symQuantificationOrder)
throws ViatraTransformationException{
for (int j = 0; j < symQuantificationOrder.length; j++) {
if(symQuantificationOrder[j].compareTo(symParamIndex) == 0) {
return j;
}
}
//there is an error in the input quantification order
String[] context = {(gtRule.getSymParameters().get(symParamIndex)).getVariable().getName(),
gtRule.getName()};
throw new GraphTransformationException(
GTErrorStrings.SYMBOLIC_QUANTIFICITION + gtRule.getName()
,context
,gtRule);
}
/** Returns the actual index of the Variable from the invocation context
* @param gtPattCall the gt rule invocation
* @param var a variable of the gt pattern call
* @return the index of the input variable
*/
protected int getActualParameterIndexof(GTPatternCall gtPattCall, Variable var){
for (Object obj :var.getReferences()) {
if (gtPattCall.getActualParameters().contains(obj)) {
return gtPattCall.getActualParameters().indexOf(obj);
}
}
return -1;
}
/** Returns the actual index of the Variable refernce's variable from the invocation context
* @param gtPatt the gt rule invocation context
* @param symParam the symbolic parameter of the invoked GT rule
* @return the index of the symbolic parameter in the GT rules
*/
protected int getActualParameterIndexofSymbolicParameter(GTPatternCall gtPattCall, SymbolicRuleParameter symParam){
return getActualParameterIndexof(gtPattCall, symParam.getVariable());
}
protected Object[] getOutPutSymbolicParatemerRules()
throws ViatraTransformationException {
Object[] values = new Object[gtRule.getSymParameters().size()];
for (int i = 0; i < gtRule.getSymParameters().size(); i++) {
SymbolicRuleParameter symParam =
gtRule.getSymParameters().get(i);
values[i] = gtEnvironment.getVariableValue(symParam.getVariable());
}
return values;
}
}