Search in sources :

Example 11 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class ConsequenceTest method testConsequenceBuilderException.

@Test
public void testConsequenceBuilderException() throws Exception {
    final KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(ResourceFactory.newClassPathResource("test_ConsequenceBuilderException.drl", getClass()), ResourceType.DRL);
    assertTrue(builder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 12 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class ConsequenceTest method testMVELConsequenceUsingFactConstructors.

// following test depends on MVEL: http://jira.codehaus.org/browse/MVEL-212
@Test
public void testMVELConsequenceUsingFactConstructors() throws Exception {
    String drl = "";
    drl += "package test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "global " + KieSession.class.getCanonicalName() + " ksession\n";
    drl += "rule test dialect 'mvel'\n";
    drl += "when\n";
    drl += "    $person:Person( name == 'mark' )\n";
    drl += "then\n";
    drl += "    // below constructor for Person does not exist\n";
    drl += "    Person p = new Person( 'bob', 30, 555 )\n";
    drl += "    ksession.update(ksession.getFactHandle($person), new Person('bob', 30, 999, 453, 534, 534, 32))\n";
    drl += "end\n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 13 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class ImportsTest method testMissingImports.

@Test
public void testMissingImports() {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_missing_import.drl", getClass()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 14 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class TestUtil method buildSessionInSteps.

public static KieSession buildSessionInSteps(final boolean reuseKieBaseWhenAddingRules, final String... drls) {
    if (drls == null || drls.length == 0) {
        return KnowledgeBaseFactory.newKnowledgeBase().newKieSession();
    } else {
        String drl = drls[0];
        final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, drl);
        final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addPackages(kbuilder.getKnowledgePackages());
        final KieSession kSession = kbase.newKieSession();
        for (int i = 1; i < drls.length; i++) {
            drl = drls[i];
            final KnowledgeBuilder kbuilder2;
            if (reuseKieBaseWhenAddingRules) {
                kbuilder2 = createKnowledgeBuilder(kSession.getKieBase(), drl);
            } else {
                kbuilder2 = createKnowledgeBuilder(null, drl);
            }
            ((InternalKnowledgeBase) kSession.getKieBase()).addPackages(kbuilder2.getKnowledgePackages());
        }
        return kSession;
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 15 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class RuleExtensionTest method testRuleExtendsNonexistingRule.

@Test
public void testRuleExtendsNonexistingRule() {
    // DROOLS-100
    String str = "package org.drools.test;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Base\"\n" + "when\n" + "  String( this == \"go\" )\n" + "then\n" + "end\n" + "\n" + "rule \"ExtYes\" extends \"Bas\"\n" + "when\n" + "then\n" + "  list.add( 1 );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
    kbuilder.getErrors().iterator().next().toString().contains("Base");
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Aggregations

KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)376 Test (org.junit.Test)302 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)231 KieSession (org.kie.api.runtime.KieSession)149 ArrayList (java.util.ArrayList)112 List (java.util.List)65 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)52 KiePackage (org.kie.api.definition.KiePackage)39 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)38 FactHandle (org.kie.api.runtime.rule.FactHandle)36 FactType (org.kie.api.definition.type.FactType)27 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)24 Person (org.drools.compiler.Person)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)17 StringReader (java.io.StringReader)15 Resource (org.kie.api.io.Resource)15