Search in sources :

Example 36 with RuleImpl

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());
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 37 with RuleImpl

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[]);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 38 with RuleImpl

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);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 39 with RuleImpl

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]);
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 40 with RuleImpl

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;
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) ConsequenceException(org.drools.core.spi.ConsequenceException) LeftTuple(org.drools.core.reteoo.LeftTuple) InvalidRuleException(org.drools.core.rule.InvalidRuleException) IOException(java.io.IOException) ConsequenceException(org.drools.core.spi.ConsequenceException) IntrospectionException(java.beans.IntrospectionException)

Aggregations

RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)107 Test (org.junit.Test)51 Pattern (org.drools.core.rule.Pattern)30 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)28 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)21 WorkingMemory (org.drools.core.WorkingMemory)21 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)18 BuildContext (org.drools.core.reteoo.builder.BuildContext)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)16 Consequence (org.drools.core.spi.Consequence)15 GroupElement (org.drools.core.rule.GroupElement)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)12 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11