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;
}
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();
}
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());
}
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);
}
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));
}
Aggregations