Search in sources :

Example 26 with KnowledgeBuilder

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

the class RHSTest method testRHSClone.

@Test
public void testRHSClone() {
    // JBRULES-3539
    final String str = "import java.util.Map;\n" + "dialect \"mvel\"\n" + "rule \"RHSClone\"\n" + "when\n" + "   Map($valOne : this['keyOne'] !=null)\n" + "then\n" + "   System.out.println( $valOne.clone() );\n" + "end\n";
    final KnowledgeBuilderConfigurationImpl pkgBuilderCfg = new KnowledgeBuilderConfigurationImpl();
    final MVELDialectConfiguration mvelConf = (MVELDialectConfiguration) pkgBuilderCfg.getDialectConfiguration("mvel");
    mvelConf.setStrict(false);
    mvelConf.setLangLevel(5);
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pkgBuilderCfg);
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    final KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (final KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        fail("Could not parse knowledge");
    }
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) MVELDialectConfiguration(org.drools.compiler.rule.builder.dialect.mvel.MVELDialectConfiguration) Test(org.junit.Test)

Example 27 with KnowledgeBuilder

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

the class AddRemoveRulesTest method testAddRemoveDeletingFact.

@Test
public void testAddRemoveDeletingFact() {
    // DROOLS-328
    final String drl = "\n" + "rule B\n" + "  when\n" + "    Boolean()\n" + "    Float()\n" + "  then\n" + "  end\n" + "\n" + "";
    final KnowledgeBuilder kbuilder = TestUtil.createKnowledgeBuilder(null, drl);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // Create kSession and initialize it
    final KieSession kSession = kbase.newKieSession();
    final FactHandle fh = kSession.insert(new Float(0.0f));
    kSession.fireAllRules();
    ((InternalKnowledgeBase) kSession.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    kSession.delete(fh);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 28 with KnowledgeBuilder

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

the class AddRemoveRulesTest method loadRule.

private boolean loadRule(final String rule) {
    String prefix = getPrefix();
    prefix += rule;
    final KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(ResourceFactory.newReaderResource(new StringReader(prefix)), ResourceType.DRL);
    final Collection<KiePackage> pkgs = this.buildKnowledge(builder);
    this.addKnowledgeToBase(pkgs);
    return true;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader)

Example 29 with KnowledgeBuilder

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

the class AddRemoveRulesTest method testAddRemoveWithPartialSharing.

@Test
public void testAddRemoveWithPartialSharing() {
    final String drl = "package org.drools.test; \n" + "\n" + "declare A end \n" + "declare B end \n" + "declare C end \n" + "declare D end \n" + "" + "rule Init \n" + "  when\n" + "  then\n" + "    insert( new A() ); \n" + "    insert( new B() ); \n" + "  end\n" + "" + "rule One\n" + "  when\n" + "    A()\n" + "    B()\n" + "    C()\n" + "  then\n" + "  end\n" + "\n" + "rule Two\n" + "  when\n" + "    A()\n" + "    B()\n" + "    D()\n" + "  then\n" + "  end\n" + "\n" + "";
    final KnowledgeBuilder kbuilder = TestUtil.createKnowledgeBuilder(null, drl);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // Create kSession and initialize it
    final KieSession kSession = kbase.newKieSession();
    kSession.fireAllRules();
    kSession.getKieBase().removeRule("org.drools.test", "Two");
    kSession.fireAllRules();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 30 with KnowledgeBuilder

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

the class QueryTest3 method setUp.

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    String text = "";
    text += "package org.drools.integrationtests\n";
    text += "import " + QueryTest3.Bar.class.getCanonicalName() + "\n";
    text += "import " + QueryTest3.Foo.class.getCanonicalName() + "\n";
    text += "import " + QueryTest3.Foo2.class.getCanonicalName() + "\n";
    text += "query \"testDifferent\"\n";
    text += "    foo : Foo();\n";
    text += "    bar : Bar(id == foo.id)\n";
    text += "end\n";
    text += "query \"testSame\"\n";
    text += "    foo : Foo();\n";
    text += "    foo2 : Foo(id == foo.id);\n";
    text += "end\n";
    text += "query \"testExtends\"\n";
    text += "    foo : Foo();\n";
    text += "    foo2 : Foo2(id == foo.id);\n";
    text += "end\n";
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(ResourceFactory.newByteArrayResource(text.getBytes()), ResourceType.DRL);
    assertFalse(knowledgeBuilder.hasErrors());
    knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(knowledgeBuilder.getKnowledgePackages());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Before(org.junit.Before)

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