Search in sources :

Example 66 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class KnowledgeBuilderTest method testAddPackageArray.

@Test
public void testAddPackageArray() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick @role(event) end\n" + "rule R1 when\n" + "   StockTick()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, kpkgs.size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) Test(org.junit.Test)

Example 67 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DslTest method testDSLWithIndividualConstraintMappings.

@Test
public void testDSLWithIndividualConstraintMappings() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_dslWithIndividualConstraints.dsl", getClass()), ResourceType.DSL);
    kbuilder.add(ResourceFactory.newClassPathResource("test_dslWithIndividualConstraints.dslr", getClass()), ResourceType.DSLR);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    // Check errors
    final String err = kbuilder.getErrors().toString();
    assertEquals("", err);
    assertEquals(0, kbuilder.getErrors().size());
    // the compiled package
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    assertEquals(1, pkgs.size());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession session = createKnowledgeSession(kbase);
    List results = new ArrayList();
    session.setGlobal("results", results);
    Cheese cheese = new Cheese("stilton", 42);
    session.insert(cheese);
    // wm  = SerializationHelper.serializeObject(wm);
    session.fireAllRules();
    // should have fired
    assertEquals(1, results.size());
    assertEquals(cheese, results.get(0));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 68 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DslTest method testWithExpanderMore.

@Test
public void testWithExpanderMore() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_expander.dsl", getClass()), ResourceType.DSL);
    kbuilder.add(ResourceFactory.newClassPathResource("rule_with_expander_dsl_more.dslr", getClass()), ResourceType.DSLR);
    assertFalse(kbuilder.hasErrors());
    // Check errors
    final String err = kbuilder.getErrors().toString();
    assertEquals("", err);
    assertEquals(0, kbuilder.getErrors().size());
    // the compiled package
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, pkgs.size());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession session = createKnowledgeSession(kbase);
    session.insert(new Person("rage"));
    session.insert(new Cheese("cheddar", 15));
    final List messages = new ArrayList();
    session.setGlobal("messages", messages);
    // wm  = SerializationHelper.serializeObject(wm);
    session.fireAllRules();
    // should have NONE, as both conditions should be false.
    assertEquals(0, messages.size());
    session.insert(new Person("fire"));
    session.fireAllRules();
    // still no firings
    assertEquals(0, messages.size());
    session.insert(new Cheese("brie", 15));
    session.fireAllRules();
    // YOUR FIRED
    assertEquals(1, messages.size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 69 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testDynamicNotNode.

@Test(timeout = 10000)
public void testDynamicNotNode() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_CollectDynamicRules1.drl");
    kbase = SerializationHelper.serializeObject(kbase);
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    KieSession ksession = kbase.newKieSession(null, env);
    List<?> results = new ArrayList<Object>();
    ksession.setGlobal("results", results);
    final Cheese a = new Cheese("stilton", 10);
    final Cheese b = new Cheese("stilton", 15);
    final Cheese c = new Cheese("stilton", 20);
    ksession.insert(a);
    ksession.insert(b);
    ksession.insert(c);
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicNotNode.drl"));
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    results = (List) ksession.getGlobal("results");
    ksession.fireAllRules();
    assertEquals(0, results.size());
    kbase.removeKiePackage("org.drools.compiler");
    ksession.retract(ksession.getFactHandle(b));
    kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicNotNode.drl"));
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    results = (List<?>) ksession.getGlobal("results");
    ksession.fireAllRules();
    assertEquals(1, results.size());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 70 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testDynamicRuleRemovalsUnusedWorkingMemory.

@Test(timeout = 10000)
public void testDynamicRuleRemovalsUnusedWorkingMemory() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_Dynamic1.drl", "test_Dynamic2.drl", "test_Dynamic3.drl", "test_Dynamic4.drl"));
    KieSession workingMemory = createKnowledgeSession(kbase);
    assertEquals(2, kbase.getKiePackages().size());
    KiePackage knowledgePackage = null;
    for (KiePackage pkg : kbase.getKiePackages()) {
        if (pkg.getName().equals("org.drools.compiler.test")) {
            knowledgePackage = pkg;
            break;
        }
    }
    assertEquals(5, knowledgePackage.getRules().size());
    kbase.removeRule("org.drools.compiler.test", "Who likes Stilton");
    assertEquals(4, knowledgePackage.getRules().size());
    kbase.removeRule("org.drools.compiler.test", "like cheese");
    assertEquals(3, knowledgePackage.getRules().size());
    kbase.removeKiePackage("org.drools.compiler.test");
    assertEquals(1, kbase.getKiePackages().size());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7