@Test public void testDeepExt() throws Exception { KieSession ksession = genSession("test_Extends.drl"); FactType LTstudent = ksession.getKieBase().getFactType("defaultpkg","LongTermStudent"); Constructor constructor = LTstudent.getFactClass().getConstructor(String.class,int.class,String.class,String.class,int.class); assertNotNull(constructor); Object ls1 = constructor.newInstance("John",18,"Skool","C1245",4); Object ls2 = constructor.newInstance("John",33,"Skool","C1421",4); assertEquals(ls1,ls2); ksession.dispose(); }
@Test public void testGeneratedBeansMVEL() throws IllegalAccessException, InstantiationException { final KieBase kbase = loadKnowledgeBase("test_GeneratedBeansMVEL.drl"); // Retrieve the generated fact type final FactType pf = kbase.getFactType("mortgages", "Applicant"); final FactType af = kbase.getFactType("mortgages", "LoanApplication"); final Object person = pf.newInstance(); pf.set(person, "creditRating", "OK"); final Object application = af.newInstance(); final KieSession ksession = createKnowledgeSession(kbase); ksession.insert(person); ksession.insert(application); ksession.fireAllRules(); }
ksession.setGlobal( "counter", counter ); FactType factTypeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" ); Object factA = factTypeA.newInstance(); factTypeA.set( factA, "x", 0 ); ksession.insert(factA); FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" ); Object factB = factTypeB.newInstance(); ksession.insert(factB); FactType factTypeC = kbase.getFactType( "org.drools.compiler.integrationtests", "C" ); Object factC = factTypeC.newInstance(); factTypeC.set( factC, "y", 0 ); ksession.insert(factC); FactType factTypeD = kbase.getFactType( "org.drools.compiler.integrationtests", "D" ); Object factD = factTypeD.newInstance(); ksession.insert(factD); ksession.fireAllRules(); } finally { assertEquals(2, factTypeC.get(factC, "y")); ksession.dispose();
@Test public void testExtendsDump() { PackageDescrBuilder pkgd = DescrFactory.newPackage(); pkgd.name( "org.test" ) .newDeclare().type().name( "Foo" ) .newField( "id" ).type( "int" ).end() .end() .newDeclare().type().name( "Bar" ).superType( "Foo" ) .newField( "val" ).type( "int" ).initialValue( "42" ).end() .end(); String drl = new DrlDumper().dump( pkgd.getDescr() ); KieBase kb = loadKnowledgeBaseFromString( drl ); FactType bar = kb.getFactType( "org.test", "Bar" ); try { Object x = bar.newInstance(); assertEquals( 42, bar.get( x, "val" ) ); bar.set( x, "id", 1 ); assertEquals( 1, bar.get( x, "id" ) ); } catch ( Exception e ) { fail( e.getMessage() ); } }
@Test( expected = UnsupportedOperationException.class ) public void testPreventReflectionAPIsOnJavaClasses() { String drl = "package org.test; " + // existing java class "declare org.drools.compiler.Person " + " @role(event) " + "end \n" + ""; KieBuilder kieBuilder = build(drl); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase(); FactType type = kieBase.getFactType( "org.drools.compiler", "Person" ); }
@Test public void testRedefineDefaults() throws Exception { //Test Base Fact Type KieSession ksession = genSession("test_Extends.drl"); FactType person = ksession.getKieBase().getFactType("defaultpkg","Person"); FactType student = ksession.getKieBase().getFactType("defaultpkg","Student"); Object p = person.newInstance(); Object s = student.newInstance(); assertNotNull(p); assertNotNull(s); assertEquals( 99, person.get(p,"age") ); assertEquals( 18, person.get(s,"age") ); ksession.dispose(); }
@Test public void testClassReactive() throws Exception { String rule = "package org.drools.compiler.integrationtests\n" + ksession.setGlobal( "counter", counter ); FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" ); Object factB = factTypeB.newInstance(); factTypeB.set( factB, "s", "test" ); ksession.insert( factB ); Assertions.assertThatThrownBy(() -> ksession.fireAllRules()) .isInstanceOf(RuntimeException.class) .hasMessageContaining("Exception executing consequence for rule \"R1\"");
@Test public void testUnknownField() throws InstantiationException, IllegalAccessException { // DROOLS-546 String drl = "package org.test; " + "declare Pet" + " " + "end \n"; KieBuilder kieBuilder = build(drl); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase(); FactType factType = kieBase.getFactType("org.test", "Pet"); Object instance = factType.newInstance(); factType.get(instance, "unknownField"); factType.set(instance, "unknownField", "myValue"); }
ksession.setGlobal( "counter", counter ); FactType factTypeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" ); Object factA = factTypeA.newInstance(); factTypeA.set( factA, "x", 0 ); ksession.insert(factA); FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" ); Object factB = factTypeB.newInstance(); ksession.insert(factB); FactType factTypeC = kbase.getFactType( "org.drools.compiler.integrationtests", "C" ); Object factC = factTypeC.newInstance(); factTypeC.set( factC, "y", 0 ); ksession.fireAllRules(); } finally { assertEquals(2, factTypeC.get(factC, "y")); ksession.dispose();
@Test public void testExtends() throws Exception { //Test Base Fact Type KieSession ksession = genSession("test_Extends.drl"); FactType person = ksession.getKieBase().getFactType("defaultpkg","Person"); FactType eqPair = ksession.getKieBase().getFactType("defaultpkg","EqualityPair"); Object p = person.newInstance(); assertNotNull(p); ksession.insert("Populate"); ksession.fireAllRules(); // A Rule will generate 3 Persons, one with default constructor, two with field constructor // and 3 Students, which extends Person. One with default, one with field constructor assertEquals(6, ksession.getObjects(new ClassObjectFilter(person.getFactClass())).size()); assertEquals(1+4+1+4,ksession.getObjects(new ClassObjectFilter(eqPair.getFactClass())).size()); ksession.dispose(); }
@Test public void testExtendsAcrossFiles() throws Exception { KieSession ksession = new KieHelper() .addResource( ResourceFactory.newClassPathResource( "test_Ext1.drl", getClass() ), ResourceType.DRL ) .addResource( ResourceFactory.newClassPathResource( "test_Ext2.drl", getClass() ), ResourceType.DRL ) .addResource( ResourceFactory.newClassPathResource( "test_Ext3.drl", getClass() ), ResourceType.DRL ) .addResource( ResourceFactory.newClassPathResource( "test_Ext4.drl", getClass() ), ResourceType.DRL ) .build().newKieSession(); FactType person = ksession.getKieBase().getFactType("org.drools.compiler.ext.test","Person"); assertNotNull(person); FactType student = ksession.getKieBase().getFactType("org.drools.compiler.ext.test","Student"); assertNotNull(student); FactType worker = ksession.getKieBase().getFactType("org.drools.compiler.anothertest","Worker"); assertNotNull(worker); FactType ltss = ksession.getKieBase().getFactType("defaultpkg","SubLTStudent"); assertNotNull(ltss); Constructor ctor = worker.getFactClass().getConstructor(String.class,int.class,String.class, double.class, int.class); assertNotNull(ctor); Object w = ctor.newInstance("Adam",20,"Carpenter",150.0,40); System.out.println(w); assertEquals("Adam",worker.get(w,"name")); ksession.fireAllRules(); }
@Test public void testExtendsLegacy() throws Exception { KieSession ksession = genSession("test_ExtLegacy.drl",0); FactType leg = ksession.getKieBase().getFactType("org.drools.compiler","BetterLegacy"); assertNotNull(leg); Object b = leg.newInstance(); assertEquals(3.3,leg.get(b,"doubleField")); assertNull(leg.get(b,"objField")); assertEquals(245,leg.get(b,"intField")); assertEquals("XX",leg.get(b,"strField")); assertEquals(true,leg.get(b,"prop")); assertEquals("Hello",leg.get(b,"oneMoreField")); System.out.println(b); }
@Test public void testBoxedFieldInit() throws Exception { KieSession ksession = genSession("test_ExtFieldInit.drl"); FactType test = ksession.getKieBase().getFactType("org.drools.compiler","MyBoxBean"); Object x = test.newInstance(); assertNotNull(x); assertEquals(12,test.get(x,"fieldint")); assertEquals(3.23,test.get(x,"fielddbl")); assertEquals(1.2f,test.get(x,"fieldflt")); short sht = 2; assertEquals(sht,test.get(x,"fieldsht")); byte byt = 1; assertEquals(byt,test.get(x,"fieldbyt")); assertEquals(true,test.get(x,"fieldbln")); assertEquals('x',test.get(x,"fieldchr")); assertEquals(9999L,test.get(x,"fieldlng")); System.out.println(x); }
@Test public void testCoercionOfStringValueWithoutQuotes() throws Exception { // JBRULES-3080 final String str = "package org.drools.compiler.test; \n" + "declare A\n" + " field : String\n" + "end\n" + "rule R when\n" + " A( field == 12 )\n" + "then\n" + "end\n"; final KieBase kbase = loadKnowledgeBaseFromString(str); final KieSession ksession = kbase.newKieSession(); final FactType typeA = kbase.getFactType("org.drools.compiler.test", "A"); final Object a = typeA.newInstance(); typeA.set(a, "field", "12"); ksession.insert(a); assertEquals(1, ksession.fireAllRules()); }
@Test public void testAbductiveLogicWithSelectiveConstructorArgs() { String droolsSource = session.setGlobal( "list", list ); session.fireAllRules(); FactType type = session.getKieBase().getFactType( "org.drools.abductive.test", "Foo" ); for ( Object o : session.getObjects() ) { if ( type.getFactClass().isInstance( o ) ) { assertEquals( "id_0", type.get( o, "id" ) ); assertEquals( "name_test", type.get( o, "name" ) ); assertEquals( 0.0, type.get( o, "value" ) );
@Test public void testGeneratedBeansSerializable() throws Exception { final KieBase kbase = loadKnowledgeBase("test_GeneratedBeansSerializable.drl"); final FactType cheeseFact = kbase.getFactType("org.drools.generatedbeans", "Cheese"); assertTrue("Generated beans must be serializable", Serializable.class.isAssignableFrom(cheeseFact.getFactClass())); final Object cheese = cheeseFact.newInstance(); cheeseFact.set(cheese, "type", "stilton"); final Object cheese2 = cheeseFact.newInstance(); cheeseFact.set(cheese2, "type", "brie"); final KieSession ksession = createKnowledgeSession(kbase); final List<Number> results = new ArrayList<>(); ksession.setGlobal("results", results); ksession.insert(cheese); ksession.insert(cheese2); ksession.fireAllRules(); assertEquals(1, results.size()); assertEquals(2, results.get(0).intValue()); } }
@Test public void testAbducedReturnBinding() { String droolsSource = session.insert( 11 ); session.fireAllRules(); FactType foo = session.getKieBase().getFactType( "org.drools.abductive.test", "Foo" ); for ( Object k : map.keySet() ) { Object val = map.get( k ); assertSame( foo.getFactClass(), val.getClass() ); assertEquals( k, foo.get( val, "id" ) );
@Test(timeout = 10000 ) public void testDefeatOutcomePosNeg() { KieSession kSession = getSession( "org/drools/compiler/beliefsystem/defeasible/negDefeatPos.drl" ); ArrayList list = new ArrayList(); kSession.setGlobal( "list", list ); kSession.fireAllRules(); TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint( "DEFAULT" )).getTruthMaintenanceSystem(); FactType Xtype = kSession.getKieBase().getFactType( "org.drools.defeasible", "X" ); ObjectHashMap keys = tms.getEqualityKeyMap(); Iterator iter = keys.iterator(); ObjectHashMap.ObjectEntry entry; while ( ( entry = ( ObjectHashMap.ObjectEntry) iter.next() ) != null ) { EqualityKey key = (EqualityKey) entry.getValue(); Class factClass = key.getFactHandle().getObject().getClass(); if ( factClass == Xtype.getFactClass() ) { checkStatus( key, 1, DefeasibilityStatus.DEFEASIBLY ); } else { fail( "Unrecognized object has been logically justified : " + factClass ); } } assertEquals( 2, kSession.getObjects().size() ); assertEquals( 1, getNegativeObjects(kSession).size() ); assertEquals( 1, list.size() ); assertTrue( list.contains( "-1" ) ); }
@Test public void testGeneratedMethods() throws Exception { KieSession ksession = genSession("test_Extends.drl"); FactType student = ksession.getKieBase().getFactType("defaultpkg","Student"); assertEquals("Student( name=John, age=18, school=Skool )", s1.toString()); assertEquals("Student( name=John, age=25, school=Skool )", s2.toString()); assertEquals(s1.hashCode(),s2.hashCode()); assertTrue((Boolean) equals.invoke(s1,s2)); assertTrue(s1.equals(s2));
@Test(timeout = 10000 ) public void testDefeasibleEntailmentWithStrictOverride() { KieSession kSession = getSession( "org/drools/compiler/beliefsystem/defeasible/strictOverride.drl" ); kSession.fireAllRules(); TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint( "DEFAULT" )).getTruthMaintenanceSystem(); FactType Ctype = kSession.getKieBase().getFactType( "org.drools.defeasible", "C" ); FactType Xtype = kSession.getKieBase().getFactType( "org.drools.defeasible", "X" ); ObjectHashMap keys = tms.getEqualityKeyMap(); Iterator iter = keys.iterator(); ObjectHashMap.ObjectEntry entry; while ( ( entry = ( ObjectHashMap.ObjectEntry) iter.next() ) != null ) { EqualityKey key = (EqualityKey) entry.getValue(); Class factClass = key.getFactHandle().getObject().getClass(); if ( factClass == Ctype.getFactClass() ) { checkStatus( key, 1, DefeasibilityStatus.DEFINITELY ); } else if ( factClass == Xtype.getFactClass() ) { checkStatus( key, 1, DefeasibilityStatus.DEFINITELY ); } else { fail( "Unrecognized object has been logically justified : " + factClass ); } } assertEquals( 5, kSession.getObjects().size() ); }