Search in sources :

Example 46 with KiePackage

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

the class CommonTestMethodBase method loadKnowledgePackages.

public Collection<KiePackage> loadKnowledgePackages(KnowledgeBuilderConfiguration kbuilderConf, PackageDescr descr) {
    if (kbuilderConf == null) {
        kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    }
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConf);
    kbuilder.add(ResourceFactory.newDescrResource(descr), ResourceType.DESCR);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    Collection<KiePackage> knowledgePackages = kbuilder.getKnowledgePackages();
    return knowledgePackages;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage)

Example 47 with KiePackage

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

the class KnowledgeSessionTest method testKnowledgeProviderWithRules.

@Test
public void testKnowledgeProviderWithRules() {
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    String str = "";
    str += "package org.drools.compiler.test1\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "then\n";
    str += "end\n\n";
    str += "rule rule2\n";
    str += "when\n";
    str += "then\n";
    str += "end\n";
    builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    str = "package org.drools.compiler.test2\n";
    str += "rule rule3\n";
    str += "when\n";
    str += "then\n";
    str += "end\n\n";
    str += "rule rule4\n";
    str += "when\n";
    str += "then\n";
    str += "end\n";
    builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    Collection<KiePackage> pkgs = builder.getKnowledgePackages();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) Test(org.junit.Test)

Example 48 with KiePackage

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

the class QueryBuilderTest method testQuery.

@Test
public void testQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$type");
    packageDescr.addRule(queryDescr);
    final AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == $type"));
    // Another query, no parameters
    QueryDescr queryDescr2 = new QueryDescr("query2");
    packageDescr.addRule(queryDescr2);
    AndDescr lhs2 = new AndDescr();
    queryDescr2.setLhs(lhs2);
    PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
    lhs2.addDescr(pattern2);
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
    final KieSession session = kbase.newKieSession();
    session.insert(new Cheese("stilton", 15));
    QueryResults results = session.getQueryResults("query1", "stilton");
    assertEquals(1, results.size());
    Object object = results.iterator().next().get("stilton");
    assertEquals(new Cheese("stilton", 15), object);
    results = session.getQueryResults("query1", new Object[] { "cheddar" });
    assertEquals(0, results.size());
    session.insert(new Cheese("dolcelatte", 20));
    results = session.getQueryResults("query2", new Object[] {});
    assertEquals(2, results.size());
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 49 with KiePackage

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

the class BackwardChainingTest method testDynamicRulesWithSharing.

// @Test(timeout = 10000)
@Test()
public void testDynamicRulesWithSharing() throws IOException, ClassNotFoundException {
    String str = "" + "package org.drools.compiler.test1  \n" + "\n" + "declare Location\n" + "    thing : String \n" + "    location : String \n" + "end" + "\n" + "declare Edible\n" + "   thing : String\n" + "end" + "\n" + "query whereFood( String x, String y ) \n" + "    Location(x, y;) Edible(x;) \n" + "end\n" + "\n" + "rule init when\n" + "then\n" + "        \n" + "        insert( new Location(\"apple\", \"kitchen\") );\n" + "        insert( new Location(\"crackers\", \"kitchen\") );\n" + "        insert( new Location(\"broccoli\", \"kitchen\") );\n" + "        insert( new Location(\"computer\", \"office\") );\n" + "        insert( new Edible(\"apple\") );\n" + "        insert( new Edible(\"crackers\") );\n" + "end\n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    str = "" + "package org.drools.compiler.test2  \n" + "import org.drools.compiler.test1.*\n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "\n" + "rule look2 when\n" + "     $place : String() // just here to give a OTN lookup point\n" + "     whereFood(thing, $place;)\n" + "then\n" + "      list.add( \"2:\" + thing );\n" + "end\n";
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    str = "" + "package org.drools.compiler.test3  \n" + "import org.drools.compiler.test1.*\n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "\n" + "rule look3 when\n" + "     $place : String() // just here to give a OTN lookup point\n" + "     whereFood(thing, $place;)\n" + "then\n" + "      list.add( \"3:\" + thing );\n" + "end\n";
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    str = "" + "package org.drools.compiler.test4  \n" + "import org.drools.compiler.test1.*\n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "\n" + "rule look4 when\n" + "     $place : String() // just here to give a OTN lookup point\n" + "     whereFood(thing, $place;)\n" + "then\n" + "      list.add( \"4:\" + thing );\n" + "end\n";
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    Map<String, KiePackage> pkgs = new HashMap<String, KiePackage>();
    for (KiePackage pkg : kbuilder.getKnowledgePackages()) {
        pkgs.put(pkg.getName(), pkg);
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(new KiePackage[] { pkgs.get("org.drools.compiler.test1"), pkgs.get("org.drools.compiler.test2") }));
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession ksession = createKnowledgeSession(kbase);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    ksession.setGlobal("list", list);
    FactHandle fh = ksession.insert("kitchen");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertContains(new String[] { "2:crackers", "2:apple" }, list);
    list.clear();
    kbase.addPackages(Arrays.asList(new KiePackage[] { pkgs.get("org.drools.compiler.test3") }));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertContains(new String[] { "3:crackers", "3:apple" }, list);
    list.clear();
    kbase.addPackages(Arrays.asList(new KiePackage[] { pkgs.get("org.drools.compiler.test4") }));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertContains(new String[] { "4:crackers", "4:apple" }, list);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) HashMap(java.util.HashMap) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Map(java.util.Map) HashMap(java.util.HashMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 50 with KiePackage

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

the class CepEspTest method testPackageSerializationWithEvents.

@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testPackageSerializationWithEvents() throws Exception {
    // read in the source
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
    // get the package
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, pkgs.size());
    // serialize the package
    byte[] serializedPkg = DroolsStreamUtils.streamOut(pkgs);
    pkgs = (Collection<KiePackage>) DroolsStreamUtils.streamIn(serializedPkg);
    // create the kbase
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    // create the session
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    KieSession session = createKnowledgeSession(kbase, conf);
    final List<StockTick> results = new ArrayList<StockTick>();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(tick2, results.get(0));
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) 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