Search in sources :

Example 86 with KiePackage

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

the class TemplatesTest method testManyRules.

private void testManyRules(final String drl, final int expectedRulesCount) {
    final Resource drlResource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
    drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
    Collection<KiePackage> pkgs = kbase.getKiePackages();
    Assertions.assertThat(pkgs.size()).isEqualTo(1);
    KiePackage pkg = pkgs.iterator().next();
    // check of generated rules size from template
    Assertions.assertThat(pkg.getRules().size()).isEqualTo(expectedRulesCount);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader)

Example 87 with KiePackage

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

the class TemplatesTest method testCorrectnessCheck.

private void testCorrectnessCheck(final String drl) {
    final Resource drlResource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
    drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
    final KieSession session = kbase.newKieSession();
    final List<String> list = new ArrayList<String>();
    session.setGlobal("list", list);
    session.insert(new Vegetable("tomato", 350, 8, 3, Taste.GOOD));
    session.insert(new Vegetable("tomato", 150, 8, 3, Taste.BAD));
    session.insert(new Vegetable("tomato", 350, 8, 7, Taste.GOOD));
    session.insert(new Vegetable("tomato", 1000, 8, 6, Taste.EXCELENT));
    session.insert(new Vegetable("cucumber", 1500, 19, 5, Taste.EXCELENT));
    session.insert(new Vegetable("cucumber", 1500, 21, 5, Taste.EXCELENT));
    session.insert(new Vegetable("carrot", 1000, 8, 6, Taste.EXCELENT));
    session.insert(new Vegetable("carrot", 200, 8, 1, Taste.HORRIBLE));
    session.insert(new Vegetable("onion", 500, 7, 4, Taste.EXCELENT));
    session.fireAllRules();
    // check of size of satisfying items
    Assertions.assertThat(list.size()).isEqualTo(4);
    final Collection<KiePackage> pkgs = kbase.getKiePackages();
    Assertions.assertThat(pkgs.size()).isEqualTo(1);
    final KiePackage pkg = pkgs.iterator().next();
    // check of generated rules size from template
    Assertions.assertThat(pkg.getRules().size()).isEqualTo(3);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession)

Example 88 with KiePackage

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

the class IncrementalCompilationTest method createAndDeployAndTest.

public static void createAndDeployAndTest(KieContainer kc, String version, String header, String drls, String... ruleNames) {
    if (ruleNames == null) {
        ruleNames = new String[0];
    }
    KieServices ks = KieServices.Factory.get();
    StringBuilder sbuilder = new StringBuilder();
    sbuilder.append(header);
    sbuilder.append(drls);
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", version);
    KieModule km = createAndDeployJar(ks, releaseId1, sbuilder.toString());
    kc.updateToVersion(km.getReleaseId());
    KiePackage kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
    assertEquals(ruleNames.length, kpkg.getRules().size());
    Map<String, Rule> rules = rulestoMap(kpkg.getRules());
    int i = 0;
    for (String ruleName : ruleNames) {
        assertEquals(ruleName, i++, ((RuleImpl) rules.get(ruleName)).getLoadOrder());
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Rule(org.kie.api.definition.rule.Rule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule)

Example 89 with KiePackage

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

the class RemoveRuleTest method testRemoveBigRule.

@Test
public void testRemoveBigRule() throws Exception {
    // JBRULES-3496
    final String str = "package org.drools.compiler.test\n" + "\n" + "declare SimpleFact\n" + "   patientSpaceId : String\n" + "   block : int\n" + "end\n" + "\n" + "declare SimpleMembership\n" + "   patientSpaceId : String\n" + "   listId : String\n" + "end\n" + "\n" + "declare SimplePatient\n" + "   spaceId : String\n" + "end\n" + "\n" + "rule \"RTR - 47146 retract\"\n" + "agenda-group \"list membership\"\n" + "when\n" + "   $listMembership0 : SimpleMembership( $listMembershipPatientSpaceIdRoot : patientSpaceId, ( listId != null && listId == \"47146\" ) )\n" + "   not ( $patient0 : SimplePatient( $patientSpaceIdRoot : spaceId, spaceId != null && spaceId == $listMembershipPatientSpaceIdRoot ) \n" + "       and ( ( " + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 1 )\n" + "         ) or ( " + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 2 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId  == $patientSpaceIdRoot, block == 3 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 4 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 5 )\n" + "       ) ) and ( ( " + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 6 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 7 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 8 )\n" + "       ) ) and ( ( " + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 9 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 10 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 11 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 12 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 13 )\n" + "         ) or ( (" + "            SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 14 )\n" + "           ) and (" + "              SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 15 )\n" + "         ) ) or ( ( " + "            SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 16 )\n" + "           ) and ( " + "             SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 17 )\n" + "         ) ) or ( ( " + "             SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 18 )\n" + "           ) and (" + "             SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 19 )\n" + "         ) ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 20 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 21 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 22 )\n" + "         ) or ( ( " + "             SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 23 )\n" + "         ) and (" + "             SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 24 )\n" + "     ) ) ) and ( ( " + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 25 )\n" + "         ) or (" + "           SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 26 )\n" + "     ) ) )\n" + "then\n" + "end\n";
    final Collection<KiePackage> kpgs = loadKnowledgePackagesFromString(str);
    assertEquals(1, kpgs.size());
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    kbase.addPackages(kpgs);
    kbase.removeKiePackage(kpgs.iterator().next().getName());
    final EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
    for (final ObjectTypeNode otn : epn.getObjectTypeNodes().values()) {
        final ObjectSink[] sinks = otn.getObjectSinkPropagator().getSinks();
        if (sinks.length > 0) {
            fail(otn + " has sinks " + Arrays.toString(sinks));
        }
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) KiePackage(org.kie.api.definition.KiePackage) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSink(org.drools.core.reteoo.ObjectSink) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 90 with KiePackage

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

the class MarshallingTest method testSerializeAddRemove_NoClassDefFoundError.

/*
     * I have tried both the scenarios
     * 1. Remove a rule from a pkg.
     * 2. Remove a pkg
     *
     * But both cases after inserting associated data points (i.e data points which are used to fire/activate the removed rule)
     * session.fireAllRules() is throwing NoClassDefFoundError
     */
@Test
public void testSerializeAddRemove_NoClassDefFoundError() throws Exception {
    // Create a rulebase, a session, and test it
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    List results = new ArrayList();
    KieSession session = kBase.newKieSession();
    session.setGlobal("results", results);
    InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(stilton1.getObject(), results.get(0));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(3, results.size());
    assertEquals(stilton2.getObject(), results.get(0));
    assertEquals(bob2.getObject(), results.get(1));
    assertEquals(bob1.getObject(), results.get(2));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    // CASE 1: remove rule
    kBase.removeRule("org.drools.compiler.test", "like stilton");
    InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(bob4.getObject(), results.get(0));
    assertEquals(bob3.getObject(), results.get(1));
    // now recreate the rulebase, deserialize the session and test it
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    // CASE 2: remove pkg
    kBase.removeKiePackage("org.drools.compiler.test");
    InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(bob6.getObject(), results.get(0));
    assertEquals(bob5.getObject(), results.get(1));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    session.setGlobal("results", results);
    InternalFactHandle stilton5 = (InternalFactHandle) session.insert(new Cheese("stilton", 30));
    InternalFactHandle brie5 = (InternalFactHandle) session.insert(new Cheese("brie", 30));
    InternalFactHandle bob7 = (InternalFactHandle) session.insert(new Person("bob", 30));
    InternalFactHandle bob8 = (InternalFactHandle) session.insert(new Person("bob", 40));
    session.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(bob8.getObject(), results.get(0));
    assertEquals(bob7.getObject(), results.get(1));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) 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