use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class AnnotationsTest method testCollectAnnotationsParsingAndBuilding.
@Test
public void testCollectAnnotationsParsingAndBuilding() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(new PackageDescr("org.drools"));
String ruleDrl = "package org.drools.compiler; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + " when " + " Collection() from collect ( " + " String() @Annot " + " ) " + " then " + "end " + "";
final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackage(pkgDescr.getName()).getRule("test collect with annotation");
List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
assertEquals(1, nested.size());
Map<String, AnnotationDefinition> annotations = ((Pattern) nested.get(0)).getAnnotations();
assertEquals(1, annotations.size());
assertNotNull(annotations.keySet().iterator().next());
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class AnnotationsTest method testTypedSimpleArrays.
@Test
public void testTypedSimpleArrays() {
String drl = "package org.drools.test; " + "import " + Simple.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Simple( numbers = { 1, 2, 3 } ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
Map<String, AnnotationDefinition> defs = p.getAnnotations();
assertEquals(1, defs.size());
AnnotationDefinition simple = defs.get(Simple.class.getName().replace("$", "."));
assertNotNull(simple);
Object val = simple.getPropertyValue("numbers");
assertTrue(val instanceof int[]);
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class AnnotationsTest method testAnnotationWithQualifiandClass.
@Test
public void testAnnotationWithQualifiandClass() {
String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Outer( klass = String.class, klasses = { String.class, Integer.class } ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
AnnotationDefinition adef = p.getAnnotations().get(Outer.class.getName().replace("$", "."));
assertEquals(String.class, adef.getPropertyValue("klass"));
assertEquals(Arrays.asList(new Class[] { String.class, Integer.class }), Arrays.asList((Class[]) adef.getPropertyValue("klasses")));
assertNotNull(adef);
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class DroolsObjectIOTest method testRuleStreamingWithCalendar.
@Test
public void testRuleStreamingWithCalendar() throws Exception {
// DROOLS-260
RuleImpl rule = new RuleImpl("test");
rule.setCalendars(new String[] { "mycalendar" });
byte[] buf = marshal(rule);
RuleImpl retrievedRule = (RuleImpl) unmarshal(buf);
assertNotNull(retrievedRule);
assertNotNull(retrievedRule.getCalendars());
assertEquals(1, retrievedRule.getCalendars().length);
assertEquals("mycalendar", retrievedRule.getCalendars()[0]);
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class BaseMannersTest method getContinueProcessing.
/**
* <pre>
* rule continue() {
* Context context;
* when {
* context : Context( state == Context.CHECK_DONE )
* } then {
* context.setState( Context.ASSIGN_SEATS );
* }
* }
* </pre>
* @return
* @throws InvalidRuleException
*/
private RuleImpl getContinueProcessing() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("continueProcessng");
// -----------
// context : Context( state == Context.CHECK_DONE )
// -----------
final Pattern contextPattern = new Pattern(0, this.contextType, "context");
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.CHECK_DONE));
rule.addPattern(contextPattern);
final Declaration contextDeclaration = rule.getDeclaration("context");
// ------------
// context.setName( Context.ASSIGN_SEATS );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
RuleImpl rule = drools.getRule();
LeftTuple tuple = (LeftTuple) drools.getTuple();
Context context = (Context) drools.get(contextDeclaration);
context.setState(Context.ASSIGN_SEATS);
drools.update(tuple.get(contextDeclaration), context);
// System.err.println( "continue processing" );
} catch (Exception e) {
e.printStackTrace();
throw new ConsequenceException(e);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
return rule;
}
Aggregations