use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class KnowledgePackageMetaDataTest method testMetaData.
@Test
public void testMetaData() {
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
fail(kBuilder.getErrors().toString());
}
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kBuilder.getKnowledgePackages());
KiePackage pack = kBase.getPackage("org.drools.compiler.test.definitions");
assertNotNull(pack);
assertEquals(2, pack.getFunctionNames().size());
assertTrue(pack.getFunctionNames().contains("fun1"));
assertTrue(pack.getFunctionNames().contains("fun2"));
assertEquals(2, pack.getGlobalVariables().size());
GlobalImpl g1 = new GlobalImpl("N", "java.lang.Integer");
GlobalImpl g2 = new GlobalImpl("list", "java.util.List");
assertTrue(pack.getGlobalVariables().contains(g1));
assertTrue(pack.getGlobalVariables().contains(g2));
assertEquals(2, pack.getFactTypes().size());
FactType type;
for (int j = 0; j < 2; j++) {
type = pack.getFactTypes().iterator().next();
if (type.getName().equals("org.drools.compiler.test.definitions.Person")) {
assertEquals(2, type.getFields().size());
} else if (type.getName().equals("org.drools.compiler.test.definitions.Foo")) {
assertEquals("org.drools.compiler.test.definitions.Person", type.getSuperClass());
FactField fld = type.getField("bar");
assertEquals(2, fld.getIndex());
assertEquals(String.class, fld.getType());
} else {
fail("Unexpected fact type " + type);
}
}
assertEquals(2, pack.getQueries().size());
for (Query q : pack.getQueries()) {
assertTrue(q.getName().equals("qry1") || q.getName().equals("qry2"));
}
assertEquals(4, pack.getRules().size());
assertTrue(pack.getRules().containsAll(pack.getQueries()));
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method traitsInBatchExecution.
// BZ #748752
@Test(timeout = 10000)
public void traitsInBatchExecution() {
String str = "package org.jboss.qa.brms.traits\n" + "import org.drools.compiler.Person;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "global java.util.List list;" + "" + "declare Person \n" + " @Traitable \n" + "end \n" + "" + "declare trait Student\n" + " school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + " when\n" + " $student : Person( age < 26 )\n" + " then\n" + " Student s = don( $student, Student.class );\n" + " s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + " when\n" + " student : Person( this isA Student )\n" + " then" + " list.add( 1 );\n" + " System.out.println(\"Person is a student: \" + student);\n" + "end\n" + "\n" + "rule \"print school\"\n" + " when\n" + " Student( $school : school )\n" + " then\n" + " list.add( 2 );\n" + " System.out.println(\"Student is attending \" + $school);\n" + "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new RuntimeException(kbuilder.getErrors().toString());
}
List list = new ArrayList();
KieBase kbase = kbuilder.newKieBase();
TraitFactory.setMode(mode, kbase);
StatelessKieSession ksession = kbase.newStatelessKieSession();
ksession.setGlobal("list", list);
List<Command<?>> commands = new ArrayList<Command<?>>();
Person student = new Person("student", 18);
commands.add(CommandFactory.newInsert(student));
System.out.println("Starting execution...");
commands.add(CommandFactory.newFireAllRules());
ksession.execute(CommandFactory.newBatchExecution(commands));
System.out.println("Finished...");
assertEquals(2, list.size());
assertTrue(list.contains(1));
assertTrue(list.contains(2));
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testShedThing.
@Test(timeout = 10000)
public void testShedThing() {
String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + " insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + " $x : Core( id == 0 )\n" + "then\n" + " don( $x, A.class );\n" + " don( $x, B.class );\n" + " don( $x, C.class );\n" + " don( $x, D.class );\n" + " don( $x, E.class );\n" + "end\n" + "\n" + "\n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + " $g : String( this == \"go\" ) \n" + " $x : Core( id == 0 ) \n" + "then \n" + " shed( $x, Thing.class ); " + " delete( $g ); \n\n" + "end \n" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
// not relevant
TraitFactory.setMode(mode, kbase);
kbase.addPackages(kbuilder.getKnowledgePackages());
List list = new ArrayList();
KieSession ksession = kbase.newKieSession();
ksession.setGlobal("list", list);
ksession.fireAllRules();
ksession.insert("go");
ksession.fireAllRules();
for (Object o : ksession.getObjects()) {
System.out.println(o);
}
assertEquals(1, ksession.getObjects().size());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class PositionalTest method testPositional.
@Test
public void testPositional() {
String drl = "import " + Man.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;" + "\n" + "rule \"To be or not to be\"\n" + "when\n" + " $m : Man( \"john\" , 18 , $w ; )\n" + "then\n" + " list.add($w); " + "end";
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.out.println(knowledgeBuilder.getErrors().toString());
assertFalse(knowledgeBuilder.hasErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession kSession = createKnowledgeSession(kBase);
java.util.ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
kSession.insert(new Man("john", 18, 84.2));
kSession.fireAllRules();
assertTrue(list.contains(84.2));
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class DescrBuilderTest method testDumperFromPkg.
@Test
public void testDumperFromPkg() {
// DROOLS-109
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
System.out.println(drl);
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
RuleImpl r = ((RuleImpl) rule);
assertEquals(2, r.getLhs().getChildren().size());
Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
RuleConditionElement arg1 = iter.next();
assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
assertEquals(2, ((GroupElement) arg1).getChildren().size());
RuleConditionElement arg2 = iter.next();
assertTrue(arg2 instanceof Pattern);
}
Aggregations