| ILOG JRules User Guide > Testing Rules with Rule Scenario Manager > Reference > Rule Scenario Manager XML and Java Artifacts > Test Extractor, Operator, and Type Java Definitions |
Test Extractor, Operator, and Type Java Definitions |
PREVIOUS NEXT |
A conventional test is composed of two data components, the operands, and a functional component, the operator. In Rule Scenario Manager this structure is modelled by three interfaces:
IlrOperators represents the set of operators.
IlrExtractor represents the calculated value operand.
IlrTypeConverter represents the test value operand.
The test value and calculated value operands are separate because the sources of their information are different. The test value operand is taken as input in the Console or as retrieved from an XML test definition file. The calculated value is extracted from the result of a ruleset execution. Rule Scenario Manager provides a list of operators that can be used for basic types; they are available in the package ilog.rules.ras.type.
[IlrOperators interface] ilog.rules.ras.core.operator.IlrOperators
Operators must declare which types of extractors they can operate on.
[IlrExtractor interface] ilog.rules.ras.core.extractor.IlrExtractor
Extractors extract data from the result of a ruleset execution and model it in a form that can be processed by an operator.
[IlrTypeConverters interface] ilog.rules.ras.core.type.IlrTypeConverter
The type converter takes string data and creates an object that the operator can use.
On the execution of a ruleset, an IlrScenarioResponse instance is created. This object contains a set of methods that can be used to access the results of a ruleset execution.
The following is an example that shows the use of a date extractor and a set of operators that can be used on the resultant object. The extractor converts the String from the execution property and execution date (in ISO8601 format) and converts it into a java.util.Date type.
package ilog.rules.ras.examples;
import ilog.rules.ras.core.execution.IlrScenarioResponse;
import ilog.rules.ras.core.extractor.IlrExtractor;
import ilog.rules.ras.tools.IlrISO8601DateTool;
public class DateExtractor implements IlrExtractor {
public final static String NAME = "SAMPLE_DATE";
public String getName() {
return NAME;
}
public Object extractValue(IlrScenarioResponse report) {
String sDate = (String)report.getExecutionTraceProperties().get(IlrScenarioResponse.EXECUTION_DATE);
return IlrISO8601DateTool.fromString(sDate);
}
}
package ilog.rules.ras.examples;
import ilog.rules.ras.core.operator.IlrMalformedOperatorException;
import ilog.rules.ras.core.operator.IlrOperators;
import ilog.rules.ras.core.operator.IlrUnsupportedOperatorException;
import ilog.rules.ras.core.scenario.IlrConfiguration;
import ilog.rules.ras.core.type.IlrMalformedTypeException;
import ilog.rules.ras.tools.IlrSingletonPrecisionComparator;
import java.util.Date;
public class DateOperator implements IlrOperators {
public final static String EQUALS = "EQUALS";
public final static String NOT_EQUALS = "NOT_EQUALS";
public final static String GT = "GT";
public final static String NOT_GT = "NOT_GT";
public final static String LT = "LT";
public final static String NOT_LT = "NOT_LT";
public String[] acceptedOperators() {
return new String[]{
EQUALS,
NOT_EQUALS,
GT,
NOT_GT,
LT,
NOT_LT};
}
public String[] acceptedExtractorNames() {
return new String[]{DateExtractor.NAME};
}
public String getObjectType() {
return DateType.NAME;
}
public boolean doTest(IlrConfiguration configuration, String variableString,
Object testValue, String operator, Object computedValue, StringBuffer
actionSummary)
throws IlrMalformedOperatorException, IlrUnsupportedOperatorException {
IlrSingletonPrecisionComparator precision =
IlrSingletonPrecisionComparator.getInstance();
Date toBeTested = (Date)computedValue;
if( testValue instanceof String ){
try {
testValue = new DateType().toObject((String)testValue);
} catch (IlrMalformedTypeException e) {
IlrMalformedOperatorException ex = new
IlrMalformedOperatorException("Bad structured element.");
ex.initCause(e);
throw ex;
}
}
Date dateToTest = (Date)testValue;
if( operator.equals(EQUALS) ) {
return precision.equals(toBeTested, dateToTest);
} else if( operator.equals(NOT_EQUALS) ) {
return precision.notEquals(toBeTested, dateToTest);
} else if( operator.equals(GT) ) {
return precision.greaterThan(toBeTested, dateToTest);
} else if( operator.equals(NOT_GT) ) {
return precision.notGreaterThan(toBeTested, dateToTest);
} else if( operator.equals(LT) ) {
return precision.lessThan(toBeTested, dateToTest);
} else if( operator.equals(NOT_LT) ) {
return precision.notLessThan(toBeTested, dateToTest);
}
throw new IlrUnsupportedOperatorException("Can't use operator \""
+ operator + "\" in DateOperator.");
}
}
package ilog.rules.ras.examples;
import ilog.rules.ras.IlrLocalisedMessageCodes;
import ilog.rules.ras.IlrLocalisedMessageHelper;
import ilog.rules.ras.core.type.IlrMalformedTypeException;
import ilog.rules.ras.core.type.IlrTypeConverter;
import java.io.Serializable;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.xml.XppReader;
public class DateType implements IlrTypeConverter, Serializable {
private static final long serialVersionUID = 1L;
public static String NAME = "SAMPLE_DATE";
public String getName() {
return NAME;
}
public Object toObject(String xmlDescription) throws
IlrMalformedTypeException {
String stringValue = null;
try {
XppReader reader = new XppReader(new StringReader(xmlDescription));
stringValue = reader.getValue().trim();
} catch( StreamException e){
IlrMalformedTypeException ex = new
IlrMalformedTypeException(IlrLocalisedMessageHelper.getMessage
(IlrLocalisedMessageCodes.BAD_DATE_TYPE) + " " +
IlrLocalisedMessageHelper.getMessage
(IlrLocalisedMessageCodes.USE_THE_TEMPLATE,
new Object[]{ "yyyy/MM/dd HH:mm" }));
ex.initCause(e);
throw ex;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
return dateFormat.parse(stringValue);
} catch (ParseException e) {
IlrMalformedTypeException ex = new
IlrMalformedTypeException(e.getMessage() + " " +
IlrLocalisedMessageHelper.getMessage
(IlrLocalisedMessageCodes.USE_THE_TEMPLATE,
new Object[]{ "yyyy/MM/dd HH:mm" }));
ex.initCause(e);
throw ex;
}
}
public void fromObject(Object object, HierarchicalStreamWriter writer) throws
IlrMalformedTypeException {
if ( object == null ) return;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
writer.setValue(dateFormat.format((Date)object));
}
}
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |