package modTransf.rules.core;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import modTransf.engine.RuleContext;
import modTransf.engine.SimpleRuleContext;
import modTransf.engine.Arguments;
import modTransf.engine.TransformationException;
import java.util.Map;
import java.util.HashMap;
import modTransf.model.ModelHelper;
import modTransf.engine.EngineException;
import modTransf.engine.Rule;
import modTransf.engine.ParameterDescriptor;
/**
* Test case for SimpleRuleContext.
*/
public class RuleCallActionTest extends TransformationTestCase
{
public RuleCallActionTest(String name)
{
super(name);
}
/**
* Initialize the context for a new test.
* This method is called befor each test
* @throws IOException
*/
public void setUp()
throws IOException
{
super.setUp();
}
/**
* Tear down the test.
* This method is called after each test.
*/
public void tearDown()
{
super.tearDown();
}
/**
* Check the internal SimpleRule.
*/
public void testSimpleRule()
{
Rule rule = new SimpleRule( "aRule", "in, out" );
assertEquals("parameter desc count", 2, rule.getParameterDescriptors().size() );
assertEquals("args[0]-->in", true,
((ParameterDescriptor)rule.getParameterDescriptors().get(0)).isIn(context) );
assertEquals("args[0]-->out", false,
((ParameterDescriptor)rule.getParameterDescriptors().get(0)).isOut(context) );
assertEquals("args[1]-->in", false,
((ParameterDescriptor)rule.getParameterDescriptors().get(1)).isIn(context) );
assertEquals("args[1]-->out", true,
((ParameterDescriptor)rule.getParameterDescriptors().get(1)).isOut(context) );
}
/**
* Test.
*/
public void testParameterRegistration()
{
RuleCallAction call = new RuleCallAction( "aRule" );
call.addParameterExpression( new SimpleVariableParameterExpression("p1") );
call.addParameterExpression( new SimpleVariableParameterExpression("p2") );
assertEquals("parameter count", 2, call.getParameterExpressions().size() );
}
/**
* Check that the rule is called.
*/
public void testExecuteRuleIsCalled()
throws TransformationException, EngineException
{
String ruleName = "rule1";
// Create a transformation and register a rule.
setUpTransformation();
transformation.getRuleSet().addRule( new SimpleRule(ruleName, "in, out") );
// Create a call with two args.
RuleCallAction call = new RuleCallAction( ruleName );
call.addParameterExpression( new SimpleVariableParameterExpression("p1") );
call.addParameterExpression( new SimpleVariableParameterExpression("p2") );
call.engineStart(context);
call.execute( null, context);
assertTrue("trace", methodCallsTrace.remove(ruleName+".execute()"));
assertEquals("trace", 0, methodCallsTrace.size());
}
/**
* Check the argument passed to the called rule.
*/
public void testExecuteProvidedArgs()
throws TransformationException, EngineException
{
String ruleName = "rule1";
// Create a transformation and register a rule.
setUpTransformation();
transformation.getRuleSet().addRule( new SimpleRule(ruleName, "in, out") );
// put parameter in context
context.setAttribute( attr0Name, attr0);
context.setAttribute( attr1Name, attr1);
// Create a call with two args.
RuleCallAction call = new RuleCallAction( ruleName );
call.addParameterExpression( new SimpleVariableParameterExpression(attr0Name) );
call.addParameterExpression( new SimpleVariableParameterExpression(attr1Name) );
// Execute the call
call.engineStart(context);
call.execute( null, context);
assertTrue("trace", methodCallsTrace.remove(ruleName+".execute()"));
assertEquals("trace", 0, methodCallsTrace.size());
// Check the arguments provided to the called rule
Arguments args = (Arguments)objectTrace.get( ruleName+".execute().args");
assertEquals("provided args ", attr0, args.get(0));
assertEquals("provided args ", null, args.get(1)); // out args are nullified
}
/**
* Check the argument returned by the called rule.
*/
public void testExecuteReturnedArgs()
throws TransformationException, EngineException
{
String ruleName = "rule1";
// Create a transformation and register a rule.
setUpTransformation();
transformation.getRuleSet().addRule( new RulePushingAttribute(ruleName, "in, out") );
// put parameter in context
context.setAttribute( attr0Name, attr0);
context.setAttribute( attr1Name, null);
// Create a call with two args.
RuleCallAction call = new RuleCallAction( ruleName );
call.addParameterExpression( new SimpleVariableParameterExpression(attr0Name) );
call.addParameterExpression( new SimpleVariableParameterExpression(attr1Name) );
// Execute the call
call.engineStart(context);
call.execute( null, context);
// Check the arguments returned by the called rule
// attr1 is returned by the called rule and set by the call under
// the name of the specified args
assertEquals("returned args[1]", attr1, context.getAttribute(attr1Name, RuleContext.LOCAL_SCOPE));
}
/**
*
* <p>Titre : ModTransf V3</p>
* <p>Description : </p>
* <p>Copyright : Copyright (c) 2005</p>
* <p>Soci�t� : </p>
* @author Cedric Dumoulin
* @version 3.0
*/
public class RulePushingAttribute extends SimpleRule {
String name;
List parameterDescriptors = new ArrayList();
RulePushingAttribute( String name )
{
super(name);
}
RulePushingAttribute( String name, String argsDirection )
{
super(name, argsDirection);
}
/**
* execute
*
* @param args Arguments
* @param context RuleContext
* @return boolean
*/
public boolean execute(Arguments args, RuleContext context)
{
super.execute(args, context);
args.set(0, attr0 );
args.set(1, attr1 );
return true;
}
}
}