use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testTraitModifyCore2.
@Test
public void testTraitModifyCore2() {
String s1 = "package test; " + "import org.drools.core.factmodel.traits.*; " + "" + "declare trait Student @propertyReactive name : String end " + "declare trait Worker @propertyReactive name : String end " + "declare trait StudentWorker extends Student, Worker @propertyReactive name : String end " + "declare trait StudentWorker2 extends StudentWorker @propertyReactive name : String end " + "declare trait Assistant extends Student, Worker @propertyReactive name : String end " + "declare Person @Traitable @propertyReactive name : String end " + "" + "rule \"Init\" " + "when " + "then " + " Person p = new Person( \"john\" ); " + " insert( p ); " + "end " + "" + "rule \"Don\" " + "when " + " $p : Person( name == \"john\" ) " + "then " + " System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON WORKER \" + $p ); " + " don( $p, Worker.class ); " + " System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON STUDWORKER-2 \" + $p ); " + " don( $p, StudentWorker2.class ); " + " System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON ASSISTANT \" + $p ); " + " don( $p, Assistant.class ); " + "end " + "" + "rule \"Log S\" " + "when " + " $t : Student() @watch( name ) " + "then " + " System.err.println( \"@@Student >> \" + $t ); " + "end " + "rule \"Log W\" " + "when " + " $t : Worker() @watch( name ) " + "then " + " System.err.println( \"@@Worker >> \" + $t ); " + "end " + "rule \"Log SW\" " + "when " + " $t : StudentWorker() @watch( name ) " + "then " + " System.err.println( \"@@StudentWorker >> \" + $t ); " + "end " + "rule \"Log RA\" " + "when " + " $t : Assistant() @watch( name ) " + "then " + " System.err.println( \"@@Assistant >> \" + $t ); " + "end " + "rule \"Log Px\" " + "salience -1 " + "when " + " $p : Person() @watch( name ) " + "then " + " System.err.println( \"Poor Core Person >> \" + $p ); " + "end " + "" + "rule \"Mod\" " + "salience -10 " + "when " + " String( this == \"go\" ) " + " $p : Student( name == \"john\" ) " + "then " + " System.out.println( \" ------------------------------------------------------------------------------ \" + $p ); " + " modify ( $p ) { setName( \"alan\" ); } " + "end " + "";
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());
KieSession ksession = kbase.newKieSession();
int k = ksession.fireAllRules();
assertEquals(7, k);
ksession.insert("go");
k = ksession.fireAllRules();
assertEquals(6, k);
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testTMSConsistencyWithNonTraitableBeans.
@Test(timeout = 10000)
public void testTMSConsistencyWithNonTraitableBeans() {
String s1 = "package org.drools.test;\n" + "import org.drools.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + " insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + " age : int\n" + " name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + " $p : Person( )\n" + "then\n" + " don( $p, Student.class, true );\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();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
TraitFactory.setMode(mode, ksession.getKieBase());
ksession.fireAllRules();
FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
InternalFactHandle h = ((InternalFactHandle) personHandle);
ObjectTypeConfigurationRegistry reg = h.getEntryPoint().getObjectTypeConfigurationRegistry();
ObjectTypeConf conf = reg.getObjectTypeConf(h.getEntryPoint().getEntryPoint(), ((InternalFactHandle) personHandle).getObject());
assertTrue(conf.isTMSEnabled());
ksession.dispose();
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testTraitEncoding.
@Test(timeout = 10000)
public void testTraitEncoding() {
String s1 = "package org.drools.core.factmodel.traits;\n" + "declare trait A 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 trait F extends C end\n" + "declare trait G extends D end\n" + "declare trait H extends D end\n" + "declare trait I extends E end\n" + "declare trait J extends F end\n" + "declare trait K extends G, H end\n" + "declare trait L extends G, H end\n" + "declare trait M extends I, J end\n" + "declare trait N extends K, L end\n" + "" + "rule \"donOneThing\"\n" + "when\n" + " $x : Entity()\n" + "then\n" + " don( $x, A.class );\n" + "end\n" + "" + "rule \"donManyThing\"\n" + "when\n" + " String( this == \"y\" ) \n" + " $x : Entity()\n" + "then\n" + " don( $x, B.class );\n" + " don( $x, D.class );\n" + " don( $x, F.class );\n" + " don( $x, E.class );\n" + " don( $x, I.class );\n" + " don( $x, K.class );\n" + " don( $x, J.class );\n" + " don( $x, C.class );\n" + " don( $x, H.class );\n" + " don( $x, G.class );\n" + " don( $x, L.class );\n" + " don( $x, M.class );\n" + " don( $x, N.class );\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();
TraitFactory.setMode(mode, kbase);
kbase.addPackages(kbuilder.getKnowledgePackages());
TraitRegistry tr = ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().getTraitRegistry();
System.out.println(tr.getHierarchy());
Entity ent = new Entity("x");
KieSession ksession = kbase.newKieSession();
ksession.insert(ent);
ksession.fireAllRules();
assertEquals(1, ent.getMostSpecificTraits().size());
ksession.insert("y");
ksession.fireAllRules();
System.out.println(ent.getMostSpecificTraits());
assertEquals(2, ent.getMostSpecificTraits().size());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testTraitModifyCore2a.
@Test(timeout = 10000)
public void testTraitModifyCore2a() {
String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "global java.util.List list; \n" + "" + "declare trait Student @propertyReactive name : String end\n" + "declare trait Worker @propertyReactive name : String end\n" + "declare trait StudentWorker extends Student, Worker @propertyReactive name : String end\n" + "declare trait Assistant extends Student, Worker @propertyReactive name : String end\n" + "declare Person @Traitable @propertyReactive name : String end\n" + "" + "rule \"Init\" \n" + "when \n" + "then \n" + " Person p = new Person( \"john\" ); \n" + " insert( p ); \n" + "end \n" + "" + "rule \"Don\" \n" + "when \n" + " $p : Person( name == \"john\" ) \n" + "then \n" + " System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON WORKER \" + $p ); \n" + " don( $p, Worker.class ); \n" + " System.out.println( \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DON STUDWORKER \" + $p ); \n" + " don( $p, StudentWorker.class ); \n" + "end \n" + "" + "rule \"Log W\" \n" + "when \n" + " $t : Worker( this isA StudentWorker ) @watch( name ) \n" + "then \n" + " System.out.println( \"@@Worker >> \" + $t ); \n" + " list.add( true ); \n" + "end \n" + "rule \"Log SW\" \n" + "when \n" + " $t : StudentWorker() @watch( name ) \n" + "then \n" + " System.out.println( \"@@StudentWorker >> \" + $t ); \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());
KieSession ksession = kbase.newKieSession();
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
int k = ksession.fireAllRules();
assertTrue(list.contains(true));
assertEquals(1, list.size());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class PMML4Compiler method buildOutputResource.
private Resource buildOutputResource(Resource resource, String theory) {
ByteArrayResource byteArrayResource = new ByteArrayResource(theory.getBytes(IoUtils.UTF8_CHARSET));
byteArrayResource.setResourceType(ResourceType.PMML);
if (resource.getSourcePath() != null) {
String originalPath = resource.getSourcePath();
int start = originalPath.lastIndexOf(File.separator);
byteArrayResource.setSourcePath("generated-sources/" + originalPath.substring(start) + ".pmml");
} else {
byteArrayResource.setSourcePath("generated-sources/" + helper.getContext() + ".pmml");
}
return byteArrayResource;
}
Aggregations