private Object newCancelFact(KieSession ksession, boolean cancel) { FactType type = ksession.getKieBase().getFactType("org.jboss.qa.brms.agendafilter", "CancelFact"); Object instance = null; try { instance = type.newInstance(); type.set(instance, "cancel", cancel); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } return instance; }
@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(); }
private void insertMessageFromTypeDeclaration(KieSession ksession) throws InstantiationException, IllegalAccessException { FactType messageType = ksession.getKieBase().getFactType("org.drools.test", "Message"); Object message = messageType.newInstance(); messageType.set(message, "message", "Hello World"); ksession.insert(message); }
@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(timeout=10000) public void testWrapperEmpty() { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", "Imp" ); TraitableBean imp = (TraitableBean) impClass.newInstance(); assertFalse( wrapper.isEmpty() ); studentClass.set( proxy, "name", "john" ); assertFalse( wrapper.isEmpty() ); studentClass.set( proxy, "name", null ); assertFalse( wrapper.isEmpty() ); studentClass.set( proxy, "age", 32 );
@Test(timeout=10000) public void testTraitWrapGetAndSet() { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", "Imp" ); TraitableBean imp = (TraitableBean) impClass.newInstance(); Class trait = kb.getFactType( "org.drools.compiler.trait.test", "Student" ).getFactClass(); assertEquals( 4, wrapper.size() ); assertEquals( 2, virtualFields.size() ); assertEquals( "john", wrapper.get( "name" ) ); assertEquals( "xyz",
@Test public void testDeclaredFactAndFunction() throws Exception { String rule = "package com.jboss.qa;\n"; Object address = addressFact.newInstance(); session.insert( address ); session.fireAllRules();
@Test public void testGeneratedBeans1() throws Exception { KnowledgeBase kbase = loadKnowledgeBase("test_GeneratedBeans.drl"); Object cheese = cheeseFact.newInstance(); cheeseFact.set( cheese, "type", "stilton" ); "Person" ); Object ps = personType.newInstance(); personType.set( ps, "age", 42 ); Object cg = cheeseFact.newInstance(); ksession.setGlobal( "cg", cg ); Object person = personFact.newInstance();
@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 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 testDeclareAndFrom() throws Exception { KnowledgeBase kbase = loadKnowledgeBase( "test_DeclareWithFrom.drl" ); FactType profileType = kbase.getFactType( "org.drools.compiler", "Profile" ); StatefulKnowledgeSession ksession = createKnowledgeSession( kbase ); Object profile = profileType.newInstance(); Map<String, Integer> map = new HashMap<String, Integer>(); map.put( "internet", Integer.valueOf( 2 ) ); profileType.set( profile, "pageFreq", map ); ksession.insert( profile ); ksession.fireAllRules(); ksession.dispose(); }
@Test(timeout=10000) public void testInternalComponents1( ) { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", "Imp" ); TraitableBean imp = (TraitableBean) impClass.newInstance(); FactType traitClass = kb.getFactType( "org.drools.compiler.trait.test", "Student" ); assertTrue( coreProperties instanceof HashMap ); } else { assertEquals( "org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName() );
@Test public void testPackageNameOfTheBeast() throws Exception { Object myDeclaredFactInstance = myDeclaredFactType.newInstance(); knowledgeSession.insert( myDeclaredFactInstance );
private Object createDRLStock(final String name, final Double value) throws IllegalAccessException, InstantiationException { final FactType stockType = kbase.getFactType(DRL_PACKAGE_NAME, DRL_FACT_NAME); final Object stock = stockType.newInstance(); stockType.set(stock, "name", name); stockType.set(stock, "value", value); return stock; } }
@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 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 testGeneratedBeansMVEL() throws Exception { KnowledgeBase kbase = loadKnowledgeBase( "test_GeneratedBeansMVEL.drl" ); // Retrieve the generated fact type FactType pf = kbase.getFactType( "mortgages", "Applicant" ); FactType af = kbase.getFactType( "mortgages", "LoanApplication" ); Object person = pf.newInstance(); pf.set( person, "creditRating", "OK" ); Object application = af.newInstance(); StatefulKnowledgeSession ksession = createKnowledgeSession( kbase ); ksession.insert( person ); ksession.insert( application ); ksession.fireAllRules(); }
@Test public void testAccumulatesExpireVsCancel() throws Exception { ksession.insert( ft.newInstance() ); ksession.fireAllRules(); ksession.insert( ft.newInstance() ); ksession.fireAllRules(); ksession.insert( ft.newInstance() ); ksession.fireAllRules();
@Test public void testPropSpecOnBetaNode() throws Exception { String rule = "package org.drools.compiler.integrationtests\n" + KieSession ksession = kbase.newKieSession(); FactType typeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" ); FactType typeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" ); Object a1 = typeA.newInstance(); typeA.set( a1, "i", 1 ); ksession.insert( a1 ); Object a2 = typeA.newInstance(); typeA.set( a2, "i", 2 ); ksession.insert( a2 ); Object b1 = typeB.newInstance(); typeB.set( b1, "i", 1 ); typeB.set( b1, "j", 1 ); ksession.insert( b1 ); int rules = ksession.fireAllRules(); assertEquals(3, rules);
@Test public void testFieldInit() throws Exception { KieSession ksession = genSession("test_ExtFieldInit.drl"); FactType test = ksession.getKieBase().getFactType("org.drools.compiler", "MyBean3"); Object x = test.newInstance(); assertNotNull(x); assertEquals(12,test.get(x,"fieldint")); assertEquals("xyz",test.get(x,"fieldstr")); assertEquals(3.23,test.get(x,"fielddbl")); assertEquals(0,test.get(x,"field0")); assertEquals(0.0f,test.get(x,"field1")); assertEquals(1.2f,test.get(x,"fieldflt")); short sht = 2; assertEquals(sht,test.get(x,"fieldsht")); assertEquals(0,test.get(x,"field2")); 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); }