use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class BaseLeftTuplesBuilder method insert.
public T insert(Object... objects) {
this.testStagedInsert = true;
if (objects == null) {
objects = new Object[0];
}
for (int i = 0; i < objects.length; i++) {
if (!(objects[i] instanceof Pair)) {
Object o1 = objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
leftTuples.addInsert(leftTuple);
} else {
Pair p = (Pair) objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
leftTuples.addInsert(leftTuple2);
}
}
return (T) this;
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class BaseLeftTuplesBuilder method delete.
public T delete(Object... objects) {
this.testStagedDelete = true;
if (objects == null) {
objects = new Object[0];
}
for (int i = 0; i < objects.length; i++) {
if (!(objects[i] instanceof Pair)) {
Object o1 = objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
leftTuples.addDelete(leftTuple);
} else {
Pair p = (Pair) objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
leftTuples.addDelete(leftTuple2);
}
}
return (T) this;
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class PhreakLiaNodeTest method test.
@Test
public void test() {
String str = "package org.drools.compiler.test\n" + "\n" + "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "\n" + "rule r1 \n" + " when \n" + " $a : A( object == 1 )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n" + "rule r2 \n" + " when \n" + " $a : A( object == 2 )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n " + "rule r3 \n" + " when \n" + " $a : A( object == 2 )\n" + " $b : B( )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n " + "rule r4 \n" + " when \n" + " $a : A( object == 3 )\n" + " then \n" + " System.out.println( $a ); \n" + "end \n";
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (builder.hasErrors()) {
throw new RuntimeException(builder.getErrors().toString());
}
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(builder.getKnowledgePackages());
KieSession ksession = knowledgeBase.newKieSession();
InternalFactHandle fhB = (InternalFactHandle) ksession.insert(B.b(1));
InternalFactHandle fhA = (InternalFactHandle) ksession.insert(A.a(1));
ksession.fireAllRules();
System.out.println("---1---");
// ksession.update( fhA, a(1) );
// ksession.fireAllRules();
// System.out.println( "---2---" );
//
ksession.update(fhA, A.a(2));
ksession.fireAllRules();
System.out.println("---3---");
ksession.update(fhA, A.a(2));
ksession.fireAllRules();
System.out.println("---4---");
ksession.update(fhA, A.a(3));
ksession.fireAllRules();
System.out.println("---5---");
ksession.update(fhB, B.b(1));
ksession.update(fhA, A.a(3));
ksession.fireAllRules();
// ksession.update( fhA, a(1) );
// ksession.fireAllRules();
//
// ksession.update( fhA, a(1) );
// ksession.fireAllRules();
ksession.dispose();
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedMultipleSharesRemoveFirst.
@Test
public void testPopulatedMultipleSharesRemoveFirst() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) D() E()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) wm.insert(new A(1));
InternalFactHandle fh2 = (InternalFactHandle) wm.insert(new A(2));
InternalFactHandle fh3 = (InternalFactHandle) wm.insert(new A(2));
InternalFactHandle fh4 = (InternalFactHandle) wm.insert(new A(3));
InternalFactHandle fh5 = (InternalFactHandle) wm.insert(new B(1));
InternalFactHandle fh6 = (InternalFactHandle) wm.insert(new B(2));
InternalFactHandle fh7 = (InternalFactHandle) wm.insert(new C(1));
InternalFactHandle fh8 = (InternalFactHandle) wm.insert(new C(2));
InternalFactHandle fh9 = (InternalFactHandle) wm.insert(new D(1));
InternalFactHandle fh10 = (InternalFactHandle) wm.insert(new E(1));
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(2;) D() E()\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(3;) B(1;) B(2;) C(2;) D() E()\n"));
wm.fireAllRules();
assertEquals(5, list.size());
kbase1.removeRule("org.kie", "r1");
list.clear();
wm.update(fh1, fh1.getObject());
wm.update(fh2, fh2.getObject());
wm.update(fh3, fh3.getObject());
wm.update(fh4, fh4.getObject());
wm.update(fh5, fh5.getObject());
wm.update(fh6, fh6.getObject());
wm.update(fh7, fh7.getObject());
wm.update(fh8, fh8.getObject());
wm.update(fh9, fh9.getObject());
wm.update(fh10, fh10.getObject());
wm.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedSharedLiaNodeNoBeta.
@Test
public void testPopulatedSharedLiaNodeNoBeta() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
A a1 = new A(1);
InternalFactHandle fh1 = (InternalFactHandle) wm.insert(a1);
A a2 = new A(2);
InternalFactHandle fh2 = (InternalFactHandle) wm.insert(a2);
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " a : A()\n"));
wm.fireAllRules();
assertEquals(4, list.size());
kbase1.removeRule("org.kie", "r1");
kbase1.removeRule("org.kie", "r2");
list.clear();
assertNull(fh1.getFirstLeftTuple());
assertNull(fh2.getFirstLeftTuple());
wm.update(fh1, a1);
wm.update(fh2, a2);
wm.fireAllRules();
assertEquals(0, list.size());
}
Aggregations