Java Code Examples for org.drools.core.impl.InternalKnowledgeBase#getFactType()
The following examples show how to use
org.drools.core.impl.InternalKnowledgeBase#getFactType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMultipleInheritanceWithPosition1() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 5, sw.getFields().size() ); assertEquals( "field0", sw.getFields().get(0).getName() ); assertEquals( "field1", sw.getFields().get(1).getName() ); assertEquals( "mfield1", sw.getFields().get(2).getName() ); assertEquals( "field2", sw.getFields().get(3).getName() ); assertEquals( "mfield0", sw.getFields().get(4).getName() ); }
Example 2
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMultipleInheritanceWithPosition2() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 5, sw.getFields().size() ); assertEquals( "mfield0", sw.getFields().get(0).getName() ); assertEquals( "field0", sw.getFields().get(1).getName() ); assertEquals( "mfield1", sw.getFields().get(2).getName() ); assertEquals( "field1", sw.getFields().get(3).getName() ); assertEquals( "field2", sw.getFields().get(4).getName() ); }
Example 3
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMultipleInheritanceWithPosition3() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(0)\n" + " field1 : int = 101 @position(1)\n" + " field2 : int = 102 @position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 5, sw.getFields().size() ); assertEquals( "field0", sw.getFields().get(0).getName() ); assertEquals( "field2", sw.getFields().get(1).getName() ); assertEquals( "field1", sw.getFields().get(2).getName() ); assertEquals( "mfield1", sw.getFields().get(3).getName() ); assertEquals( "mfield0", sw.getFields().get(4).getName() ); }
Example 4
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMultipleInheritanceWithPosition4() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(1)\n" + " field1 : int = 101 @position(0)\n" + " field2 : int = 102 @position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 5, sw.getFields().size() ); assertEquals( "field1", sw.getFields().get(0).getName() ); assertEquals( "field2", sw.getFields().get(1).getName() ); assertEquals( "mfield0", sw.getFields().get(2).getName() ); assertEquals( "field0", sw.getFields().get(3).getName() ); assertEquals( "mfield1", sw.getFields().get(4).getName() ); }
Example 5
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMultipleInheritanceWithPosition5() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(2)\n" + " field1 : int = 101 @position(1)\n" + " field2 : int = 102 @position(8)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(7)\n" + " mfield1 : int = 201 @position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 5, sw.getFields().size() ); assertEquals( "field1", sw.getFields().get(0).getName() ); assertEquals( "field0", sw.getFields().get(1).getName() ); assertEquals( "mfield1", sw.getFields().get(2).getName() ); assertEquals( "mfield0", sw.getFields().get(3).getName() ); assertEquals( "field2", sw.getFields().get(4).getName() ); }
Example 6
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testCompositeKnowledgeBuilder() throws Exception { String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " $fieldA : FactA( $fieldB : fieldB )\n" + " FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + "end"; String declarationA = "package org.drools.compiler.test\n" + "declare FactA\n" + " fieldB: FactB\n" + "end\n"; String declarationB = "package org.drools.compiler.test\n" + "declare FactB\n" + " fieldA: FactA\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( rule.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( declarationB.getBytes() ), ResourceType.DRL ); assertTrue( kbuilder.hasErrors() ); kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( declarationB.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( rule.getBytes() ), ResourceType.DRL ); assertTrue( kbuilder.hasErrors() ); KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder2.batch() .add( ResourceFactory.newByteArrayResource( rule.getBytes() ), ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ), ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource(declarationB.getBytes()), ResourceType.DRL ) .build(); if ( kbuilder2.hasErrors() ) { fail( kbuilder2.getErrors().toString() ); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder2.getKnowledgePackages() ); KieSession ksession = kbase.newKieSession(); FactType aType = kbase.getFactType( "org.drools.compiler.test", "FactA" ); Object a = aType.newInstance(); FactType bType = kbase.getFactType( "org.drools.compiler.test", "FactB" ); Object b = bType.newInstance(); aType.set( a, "fieldB", b ); bType.set( b, "fieldA", a ); // JBRULES-3683 - check that the recurisive type declaration doesn't cause a StackOverflowError a.toString(); b.toString(); ksession.insert( a ); ksession.insert( b ); int rules = ksession.fireAllRules(); assertEquals( 1, rules ); }
Example 7
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDifferentPackages() throws Exception { String rule = "package org.drools.compiler.test.rule\n" + "import org.drools.compiler.testA.FactA\n" + "import org.drools.compiler.testB.FactB\n" + "rule R1 when\n" + " $fieldA : FactA( $fieldB : fieldB, bigint == 1 )\n" + " FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + " list.add(\"OK\");" + "end"; String declarationA = "package org.drools.compiler.testA\n" + "import org.drools.compiler.testB.FactB\n" + "import java.math.BigInteger\n" + "declare FactA\n" + " fieldB: FactB\n" + " bigint: BigInteger\n" + "end\n"; String declarationB = "package org.drools.compiler.testB\n" + "import org.drools.compiler.testA.FactA\n" + "global java.util.List list\n" + "declare FactB\n" + " fieldA: FactA\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.batch() .type( ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource( rule.getBytes() ) ) .add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ) ) .add( ResourceFactory.newByteArrayResource( declarationB.getBytes() ) ) .build(); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } String declarationC = "package org.drools.compiler.testA\n" + "declare FactC\n" + " field : UnknownClass\n" + "end\n"; kbuilder.add( ResourceFactory.newByteArrayResource( declarationC.getBytes() ), ResourceType.DRL ); assertTrue( kbuilder.hasErrors() ); kbuilder.undo(); assertFalse( kbuilder.hasErrors() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactType aType = kbase.getFactType( "org.drools.compiler.testA", "FactA" ); Object a = aType.newInstance(); FactType bType = kbase.getFactType( "org.drools.compiler.testB", "FactB" ); Object b = bType.newInstance(); aType.set( a, "fieldB", b ); aType.set( a, "bigint", new BigInteger( "1" ) ); bType.set( b, "fieldA", a ); ksession.insert( a ); ksession.insert( b ); int rules = ksession.fireAllRules(); assertEquals( 1, rules ); assertEquals( "OK", list.get( 0 ) ); }
Example 8
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test @Disabled("All the classes generated by type declarations are now defined in the ProjectClassLoader") public void testUndoTypeDeclaration() throws Exception { String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.test.FactA\n" + "import org.drools.compiler.test.FactB\n" + "rule R1 when\n" + " FactA( i == 1 )\n" + " FactB( i == 1 )\n" + "then\n" + " list.add(\"OK\");" + "end\n"; String declarationA = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "declare FactA\n" + " j : int\n" + "end\n"; String declarationB = "package org.drools.compiler.test\n" + "declare FactB\n" + " i : int\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( declarationB.getBytes() ), ResourceType.DRL ); assertFalse( kbuilder.hasErrors() ); kbuilder.batch() .type( ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource( rule.getBytes() ) ) .add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ) ) .build(); assertTrue( kbuilder.hasErrors() ); kbuilder.undo(); assertFalse( kbuilder.hasErrors() ); declarationA = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "declare FactA\n" + " i : int\n" + "end\n"; kbuilder.batch() .type( ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource( rule.getBytes() ) ) .add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ) ) .build(); assertFalse( kbuilder.hasErrors() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactType aType = kbase.getFactType( "org.drools.compiler.test", "FactA" ); Object a = aType.newInstance(); aType.set( a, "i", 1 ); FactType bType = kbase.getFactType( "org.drools.compiler.test", "FactB" ); Object b = bType.newInstance(); bType.set( b, "i", 1 ); ksession.insert( a ); ksession.insert( b ); int rules = ksession.fireAllRules(); assertEquals( 1, rules ); assertEquals( "OK", list.get( 0 ) ); }
Example 9
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testUndoRule() throws Exception { String rule = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "import org.drools.compiler.test.FactA\n" + "import org.drools.compiler.test.FactB\n" + "rule R1 when\n" + " FactA( j == 1 )\n" + " FactB( i == 1 )\n" + "then\n" + " list.add(\"OK\");" + "end\n"; String declarationA = "package org.drools.compiler.test\n" + "declare FactA\n" + " i : int\n" + "end\n"; String declarationB = "package org.drools.compiler.test\n" + "declare FactB\n" + " i : int\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( declarationB.getBytes() ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ), ResourceType.DRL ); assertFalse( kbuilder.hasErrors() ); kbuilder.batch() .type( ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource( rule.getBytes() ) ) .build(); assertTrue( kbuilder.hasErrors() ); kbuilder.undo(); assertFalse( kbuilder.hasErrors() ); rule = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "import org.drools.compiler.test.FactA\n" + "import org.drools.compiler.test.FactB\n" + "rule R1 when\n" + " FactA( i == 1 )\n" + " FactB( i == 1 )\n" + "then\n" + " list.add(\"OK\");" + "end\n"; kbuilder.batch() .type( ResourceType.DRL ) .add( ResourceFactory.newByteArrayResource( rule.getBytes() ) ) .add( ResourceFactory.newByteArrayResource( declarationA.getBytes() ) ) .build(); assertFalse( kbuilder.hasErrors() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactType aType = kbase.getFactType( "org.drools.compiler.test", "FactA" ); Object a = aType.newInstance(); aType.set( a, "i", 1 ); FactType bType = kbase.getFactType( "org.drools.compiler.test", "FactB" ); Object b = bType.newInstance(); bType.set( b, "i", 1 ); ksession.insert( a ); ksession.insert( b ); int rules = ksession.fireAllRules(); assertEquals( 1, rules ); assertEquals( "OK", list.get( 0 ) ); }
Example 10
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testExtendOverride() { String drl = "package test.beans;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "\n" + "global List ans;" + "\n" + "\n" + "declare ArrayList\n" + "end\n" + "" + "declare Bean extends ArrayList\n" + " fld : int = 4 \n" + " myField : String = \"xxx\" \n" + "end\n" + "\n" + "declare Bean2 extends Bean\n" + " moref : double\n" + " myField : String\n" + "end\n" + "rule \"Init\"\n" + "when\n" + "then\n" + " Bean b = new Bean2();\n" + " ans.add(b);" + " System.out.println(b);\t\n" + "end\n" ; 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()); KieSession ksession = createKnowledgeSession(kbase); List out = new ArrayList(); ksession.setGlobal("ans",out); ksession.fireAllRules(); Object x = out.get(0); FactType type = kbase.getFactType("test.beans","Bean2"); assertEquals(4, type.get( x, "fld") ); assertEquals("xxx", type.get( x, "myField") ); assertEquals(0.0, type.get( x, "moref") ); assertTrue( x instanceof ArrayList ); }
Example 11
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testMultipleInheritanceWithPosition6() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 //@position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 6, sw.getFields().size() ); assertEquals( "field0", sw.getFields().get(0).getName() ); assertEquals( "field1", sw.getFields().get(1).getName() ); assertEquals( "field2", sw.getFields().get(2).getName() ); assertEquals( "field3", sw.getFields().get(3).getName() ); assertEquals( "mfield0", sw.getFields().get(4).getName() ); assertEquals( "mfield1", sw.getFields().get(5).getName() ); }
Example 12
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testMultipleInheritanceWithPosition7() throws Exception { // DROOLS-249 String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(0)\n" + " field1 : int = 101 @position(1)\n" + " field2 : int = 102 @position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 @position(2)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end"; KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kBuilder.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL ); if ( kBuilder.hasErrors() ) { System.err.println( kBuilder.getErrors() ); } assertFalse( kBuilder.hasErrors() ); InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(); knowledgeBase.addPackages( kBuilder.getKnowledgePackages() ); FactType sw = knowledgeBase.getFactType( "org.drools.test", "MultiInhPosTrait" ); assertEquals( 6, sw.getFields().size() ); assertEquals( "field0", sw.getFields().get(0).getName() ); assertEquals( "field2", sw.getFields().get(1).getName() ); assertEquals( "mfield0", sw.getFields().get(2).getName() ); assertEquals( "field1", sw.getFields().get(3).getName() ); assertEquals( "field3", sw.getFields().get(4).getName() ); assertEquals( "mfield1", sw.getFields().get(5).getName() ); }
Example 13
Source File: DescrBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDeclare() throws InstantiationException, IllegalAccessException { PackageDescr pkg = DescrFactory.newPackage() .name( "org.beans" ) // declare .newDeclare() .type().name( "StockTick" ) .newAnnotation( "role" ).value( "event" ).end() .newAnnotation( "author" ).value( "bob" ).end() .newField( "symbol" ).type( "String" ).end() .newField( "price" ).type( "double" ).end() .end() .newDeclare() .enumerative().name( "Planets" ) .newAnnotation( "kind" ).value( "enum" ).end() .newEnumLiteral( "earth" ).constructorArg( "6.0" ).constructorArg( "10.0" ).end() .newEnumLiteral( "jupiter" ).constructorArg( "44.0" ).constructorArg( "50.0" ).end() .newField( "mass" ).type( "double" ).end() .newField( "radius" ).type( "double" ).end() .end() .getDescr(); assertEquals( 1, pkg.getTypeDeclarations().size() ); assertEquals( 1, pkg.getEnumDeclarations().size() ); KiePackage kpkg = compilePkgDescr( pkg ); assertEquals( "org.beans", kpkg.getName() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( Collections.singletonList( kpkg ) ); FactType stType = kbase.getFactType( "org.beans", "StockTick" ); assertNotNull( stType ); Object st = stType.newInstance(); stType.set( st, "symbol", "RHT" ); stType.set( st, "price", 10 ); assertEquals( "RHT", stType.get( st, "symbol" ) ); //stType.getAnnotation("author"); TODO: implement support for this }
Example 14
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testTypeDeclarationMetadata() { String str = ""; str += "package org.drools.compiler.test; \n" + "import org.drools.compiler.compiler.TypeDeclarationTest.KlassAnnotation; \n" + "import org.drools.compiler.compiler.TypeDeclarationTest.FieldAnnotation; \n" + "import org.drools.compiler.Person\n" + "\n" + "declare Bean \n" + "@role(event) \n" + "@expires( 1s ) \n" + "@KlassAnnotation( \"klass\" )" + "" + " name : String @key @FieldAnnotation( prop = \"fld\" )\n" + "end \n" + "declare Person @role(event) end"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL ); System.err.println( kbuilder.getErrors() ); assertFalse(kbuilder.hasErrors()); InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase(); kBase.addPackages( kbuilder.getKnowledgePackages() ); FactType bean = kBase.getFactType( "org.drools.compiler.test", "Bean" ); FactType pers = kBase.getFactType( "org.drools", "Person" ); assertEquals( "org.drools.compiler.test.Bean", bean.getName() ); assertEquals( "Bean", bean.getSimpleName() ); assertEquals( "org.drools.compiler.test", bean.getPackageName() ); assertEquals( 3, bean.getClassAnnotations().size() ); Annotation ann = bean.getClassAnnotations().get( 0 ); if (!ann.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation")) { ann = bean.getClassAnnotations().get( 1 ); } if (!ann.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation")) { ann = bean.getClassAnnotations().get( 2 ); } assertEquals( "org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation", ann.getName() ); assertEquals( "klass", ann.getPropertyValue( "value" ) ); assertEquals( String.class, ann.getPropertyType( "value" ) ); assertEquals( 2, bean.getMetaData().size() ); assertEquals( "event", bean.getMetaData().get( "role" ) ); FactField field = bean.getField( "name" ); assertNotNull( field ); assertEquals( 2, field.getFieldAnnotations().size() ); Annotation fnn = field.getFieldAnnotations().get( 0 ); if (!fnn.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$FieldAnnotation")) { fnn = field.getFieldAnnotations().get( 1 ); } assertEquals( "org.drools.compiler.compiler.TypeDeclarationTest$FieldAnnotation", fnn.getName() ); assertEquals( "fld", fnn.getPropertyValue( "prop" ) ); assertEquals( String.class, fnn.getPropertyType( "prop" ) ); assertEquals( 1, field.getMetaData().size() ); assertTrue( field.getMetaData().containsKey( "key" ) ); }