Java Code Examples for org.kie.internal.builder.KnowledgeBuilder#getKnowledgePackages()
The following examples show how to use
org.kie.internal.builder.KnowledgeBuilder#getKnowledgePackages() .
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: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testAddKPackageCollection() throws Exception { String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick @role(event) end\n" + "rule R1 when\n" + " StockTick()\n" + "then\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( rule.getBytes() ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); assertEquals( 2, kpkgs.size() ); byte[] skpkg = DroolsStreamUtils.streamOut( kpkgs ); kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( skpkg ), ResourceType.PKG ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); kpkgs = kbuilder.getKnowledgePackages(); assertEquals( 2, kpkgs.size() ); }
Example 2
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public Collection<KiePackage> loadKnowledgePackages( KnowledgeBuilderConfiguration kbuilderConf, boolean serialize, String... classPathResources) { if (kbuilderConf == null) { kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConf); for (String classPathResource : classPathResources) { kbuilder.add(ResourceFactory.newClassPathResource(classPathResource, getClass()), ResourceType.DRL); } if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } Collection<KiePackage> knowledgePackages = null; if ( serialize ) { try { knowledgePackages = SerializationHelper.serializeObject(kbuilder.getKnowledgePackages(), ((KnowledgeBuilderConfigurationImpl)kbuilderConf).getClassLoader() ); } catch (Exception e) { throw new RuntimeException(e); } } else { knowledgePackages = kbuilder.getKnowledgePackages(); } return knowledgePackages; }
Example 3
Source File: SerializedPackageMergeTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private StatelessKieSession getSession(boolean serialize) throws Exception { InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); for ( String drl : DRLs ) { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newInputStreamResource(getClass().getResourceAsStream(drl)), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); Collection<KiePackage> newCollection = null; if ( serialize ) { newCollection = new ArrayList<KiePackage>(); for( KiePackage kpkg : kpkgs) { kpkg = SerializationHelper.serializeObject(kpkg); newCollection.add( kpkg ); } } else { newCollection = kpkgs; } kbase.addPackages( newCollection ); } return kbase.newStatelessKieSession(); }
Example 4
Source File: AbstractKieModule.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public KnowledgePackagesBuildResult buildKnowledgePackages(KieBaseModelImpl kBaseModel, KieProject kieProject, ResultsImpl messages) { Collection<KiePackage> pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName()); if ( pkgs == null ) { KnowledgeBuilder kbuilder = kieProject.buildKnowledgePackages(kBaseModel, messages); if ( kbuilder.hasErrors() ) { // Messages already populated by the buildKnowlegePackages return new KnowledgePackagesBuildResult(true, pkgs); } pkgs = kbuilder.getKnowledgePackages(); } return new KnowledgePackagesBuildResult(false, pkgs); }
Example 5
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testAddPackageArray() throws Exception { String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick @role(event) end\n" + "rule R1 when\n" + " StockTick()\n" + "then\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); assertEquals( 2, kpkgs.size() ); }
Example 6
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testAddKPackageSingle() throws Exception { String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "rule R1 when\n" + " StockTick()\n" + "then\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( rule.getBytes() ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); assertEquals( 2, kpkgs.size() ); KiePackage kpkg = kpkgs.iterator().next(); byte[] skpkg = DroolsStreamUtils.streamOut( kpkg ); kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( skpkg ), ResourceType.PKG ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); kpkgs = kbuilder.getKnowledgePackages(); assertEquals( 1, kpkgs.size() ); kpkg = kpkgs.iterator().next(); assertEquals( 1, kpkg.getRules().size() ); }
Example 7
Source File: StatelessSessionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private StatelessKieSession getSession2(final Resource resource) throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( resource, ResourceType.DRL ); if (kbuilder.hasErrors() ) { System.out.println( kbuilder.getErrors() ); } assertFalse( kbuilder.hasErrors() ); final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages(); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( pkgs ); kbase = SerializationHelper.serializeObject( kbase ); final StatelessKieSession session = kbase.newStatelessKieSession(); session.setGlobal( "list", this.list ); session.setGlobal( "cheesery", this.cheesery ); return session; }
Example 8
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testLogicalInsertionsLoop() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_LogicalInsertionsLoop.drl", getClass() ), ResourceType.DRL ); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kpkgs ); kbase = SerializationHelper.serializeObject(kbase); final KieSession session = createKnowledgeSession(kbase); try { final List l = new ArrayList(); final Person a = new Person( "a" ); session.setGlobal( "a", a ); session.setGlobal( "l", l ); session.fireAllRules(); Collection< ? > list = session.getObjects( new ClassObjectFilter( a.getClass() ) ); assertEquals(0, list.size(), "a still asserted."); assertEquals(10, l.size(), "Rule has not fired (looped) expected number of times"); } finally { session.dispose(); } }
Example 9
Source File: SerializedPackageMergeTwoSteps1Test.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testBuildAndSerializePackagesInTwoSteps1() { String str1 = "package com.sample.packageA\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R1 when\n" + " $p : Person( name == \"John\" )\n" + "then\n" + " list.add($p);" + "end\n"; String str2 = "package com.sample.packageB\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + " $p : Person()\n" + "then\n" + " list.add($p);" + "end\n"; // Create 2 knowledgePackages separately KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder1.add( ResourceFactory.newByteArrayResource( str1.getBytes() ), ResourceType.DRL ); Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages(); // serialize the first package to a file writeKnowledgePackage(knowledgePackages1, BINPKG[0]); KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder2.add( ResourceFactory.newByteArrayResource( str2.getBytes() ), ResourceType.DRL ); Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages(); // serialize the second package to a file writeKnowledgePackage(knowledgePackages2, BINPKG[1]); }
Example 10
Source File: DslTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private void checkDSLExpanderTest(KnowledgeBuilder kbuilder) throws IOException, ClassNotFoundException { assertFalse( kbuilder.hasErrors() ); final String err = kbuilder.getErrors().toString(); assertEquals( "", err ); assertEquals( 0, kbuilder.getErrors().size() ); // the compiled package final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages(); assertEquals( 2, pkgs.size() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( pkgs ); kbase = SerializationHelper.serializeObject(kbase); KieSession session = createKnowledgeSession(kbase); session.insert( new Person( "Bob", "http://foo.bar" ) ); session.insert( new Cheese( "stilton", 42 ) ); final List messages = new ArrayList(); session.setGlobal( "messages", messages ); session.fireAllRules(); assertEquals( 1, messages.size() ); }
Example 11
Source File: DslTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test @Disabled("antlr cannot parse correctly if the file ends with a comment without a further line break") public void testEmptyDSL() throws Exception { // FIXME etirelli / mic_hat not sure what to do with this? final String DSL = "# This is an empty dsl file."; // gives antlr <EOF> error KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_expander.dsl", getClass() ), ResourceType.DSL ); kbuilder.add( ResourceFactory.newReaderResource( new StringReader( DSL) ) , ResourceType.DSLR ); assertFalse( kbuilder.hasErrors() ); // trying to expand Cheese() pattern // Check errors final String err = kbuilder.getErrors().toString(); assertEquals( "", err ); assertEquals( 0, kbuilder.getErrors().size() ); // the compiled package Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages(); assertEquals( 0, pkgs.size() ); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( pkgs ); kbase = SerializationHelper.serializeObject(kbase); KieSession session = createKnowledgeSession(kbase); pkgs = SerializationHelper.serializeObject(pkgs); assertNull( pkgs ); }
Example 12
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public Collection<KiePackage> loadKnowledgePackagesFromString(KnowledgeBuilderConfiguration kbuilderConf, String... content) { if (kbuilderConf == null) { kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConf); for (String r : content) { kbuilder.add(ResourceFactory.newByteArrayResource(r.getBytes()),ResourceType.DRL); } if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } Collection<KiePackage> knowledgePackages = kbuilder.getKnowledgePackages(); return knowledgePackages; }
Example 13
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testKnowledgeProviderWithProcesses() { KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); String str = ""; str += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; str += "<process xmlns=\"http://drools.org/drools-5.0/process\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\" xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\" "; str += " type=\"RuleFlow\" name=\"flow1\" id=\"0\" package-name=\"org.test1\" >"; str += " <header/>\n"; str += " <nodes><start id=\"1\" name=\"Start\" /><end id=\"2\" name=\"End\" /></nodes>\n"; str += " <connections><connection from=\"1\" to=\"2\"/></connections>"; str += "</process>"; builder.add(ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRF ); str = ""; str += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; str += "<process xmlns=\"http://drools.org/drools-5.0/process\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\" xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\" "; str += " type=\"RuleFlow\" name=\"flow2\" id=\"0\" package-name=\"org.test2\" >"; str += " <header/>\n"; str += " <nodes><start id=\"1\" name=\"Start\" /><end id=\"2\" name=\"End\" /></nodes>\n"; str += " <connections><connection from=\"1\" to=\"2\"/></connections>"; str += "</process>"; builder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRF ); Collection<KiePackage> pkgs = builder.getKnowledgePackages(); assertNotNull( pkgs ); assertEquals( 2, pkgs.size() ); KiePackage test1 = getKnowledgePackage(pkgs, "org.test1" ); Collection<Process> processes = test1.getProcesses(); assertEquals( 1, processes.size() ); Process process = getProcess( processes, "flow1" ); assertEquals("flow1", process.getName() ); KiePackage test2 = getKnowledgePackage(pkgs, "org.test2" ); processes = test2.getProcesses(); assertEquals( 1, processes.size() ); process = getProcess( processes, "flow2" ); assertEquals("flow2", process.getName() ); }
Example 14
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testLogicalInsertions() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_LogicalInsertions.drl", getClass() ), ResourceType.DRL ); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kpkgs ); kbase = SerializationHelper.serializeObject(kbase); KieSession session = createKnowledgeSession(kbase); try { final List list = new ArrayList(); session.setGlobal( "list", list ); final Cheese brie = new Cheese( "brie", 12 ); FactHandle brieHandle = session.insert( brie ); final Cheese provolone = new Cheese( "provolone", 12 ); FactHandle provoloneHandle = session.insert( provolone ); session.fireAllRules(); session = getSerialisedStatefulKnowledgeSession( session, true ); System.out.println( list ); assertEquals(3, list.size()); assertEquals(3, session.getObjects().size()); brieHandle = getFactHandle( brieHandle, session ); session.delete( brieHandle ); session = getSerialisedStatefulKnowledgeSession( session, true ); assertEquals(2, session.getObjects().size()); provoloneHandle = getFactHandle( provoloneHandle, session ); session.delete( provoloneHandle ); session.fireAllRules(); assertEquals(0, session.getObjects().size()); } finally { session.dispose(); } }
Example 15
Source File: ProcessWorkItemTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testWorkItemImmediateCompletion() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); Reader source = new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + " <header>\n" + " <variables>\n" + " <variable name=\"UserName\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>John Doe</value>\n" + " </variable>\n" + " <variable name=\"Person\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\" />\n" + " </variable>\n" + " <variable name=\"MyObject\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </variable>\n" + " <variable name=\"Number\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.IntegerDataType\" />\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"HumanTask\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>#{UserName}</value>\n" + " </parameter>\n" + " <parameter name=\"Content\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>#{Person.name}</value>\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something</value>\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Attachment\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " <mapping type=\"in\" from=\"MyObject\" to=\"Attachment\" />" + " <mapping type=\"in\" from=\"Person.name\" to=\"Comment\" />" + " <mapping type=\"out\" from=\"Result\" to=\"MyObject\" />" + " <mapping type=\"out\" from=\"Result.length()\" to=\"Number\" />" + " </workItem>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>"); kbuilder.add( ResourceFactory.newReaderResource( source ), ResourceType.DRF ); Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages(); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kpkgs ); KieSession ksession = kbase.newKieSession(); ImmediateTestWorkItemHandler handler = new ImmediateTestWorkItemHandler(); ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler); ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("UserName", "John Doe"); Person person = new Person(); person.setName("John Doe"); parameters.put("Person", person); WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.actions", parameters); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); }
Example 16
Source File: FailureOnRemovalTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
private Collection<KiePackage> compileRule(String name) throws DroolsParserException, IOException { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(createKnowledgeBuilderConfiguration()); String drl = getDrl( name ); Reader drlReader = new StringReader( drl ); kbuilder.add( ResourceFactory.newReaderResource( drlReader ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); return kbuilder.getKnowledgePackages(); }
Example 17
Source File: KnowledgeBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testKnowledgeProviderWithRules() { KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); String str = ""; str += "package org.drools.compiler.test1\n"; str += "rule rule1\n"; str += "when\n"; str += "then\n"; str += "end\n\n"; str += "rule rule2\n"; str += "when\n"; str += "then\n"; str += "end\n"; builder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL ); str = "package org.drools.compiler.test2\n"; str += "rule rule3\n"; str += "when\n"; str += "then\n"; str += "end\n\n"; str += "rule rule4\n"; str += "when\n"; str += "then\n"; str += "end\n"; builder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), ResourceType.DRL ); Collection<KiePackage> pkgs = builder.getKnowledgePackages(); assertNotNull( pkgs ); assertEquals( 2, pkgs.size() ); KiePackage test1 = getKnowledgePackage(pkgs, "org.drools.compiler.test1" ); Collection<Rule> rules = test1.getRules(); assertEquals( 2, rules.size() ); Rule rule = getRule( rules, "rule1" ); assertEquals("rule1", rule.getName() ); rule = getRule( rules, "rule2" ); assertEquals( "rule2", rule.getName() ); KiePackage test2 = getKnowledgePackage(pkgs, "org.drools.compiler.test2" ); rules = test2.getRules(); assertEquals( 2, rules.size() ); rule = getRule( rules, "rule3" ); assertEquals("rule3", rule.getName() ); rule = getRule( rules, "rule4" ); assertEquals( "rule4", rule.getName() ); }
Example 18
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDeclareFieldArray() { String str1 = "" + "package org.drools " + "declare Test end " + "declare Pet " + " owners : Owner[] " + " twoDimArray : Foo[][] " + " friends : Pet[] " + " ages : int[] " + "end " + "declare Owner " + " name : String " + "end " + "declare Foo end " + ""; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newByteArrayResource( str1.getBytes() ), ResourceType.DRL ); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } for( KiePackage kp : kbuilder.getKnowledgePackages() ) { if ( kp.getName().equals( "org.drools" ) ) { Collection<FactType> types = kp.getFactTypes(); for ( FactType type : types ) { if ( "org.drools.Pet".equals( type.getName() ) ) { assertEquals( 4, type.getFields().size() ); FactField owners = type.getField( "owners" ); assertTrue( owners != null && owners.getType().getSimpleName().equals( "Owner[]" ) && owners.getType().isArray() ); FactField twoDim = type.getField( "twoDimArray" ); assertTrue( twoDim != null && twoDim.getType().getSimpleName().equals( "Foo[][]" ) && twoDim.getType().isArray() ); FactField friends = type.getField( "friends" ); assertTrue( friends != null && friends.getType().getSimpleName().equals( "Pet[]" ) && friends.getType().isArray() ); FactField ages = type.getField( "ages" ); assertTrue( ages != null && ages.getType().getSimpleName().equals( "int[]" ) && ages.getType().isArray() ); } } } } }
Example 19
Source File: DslTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testDSLWithIndividualConstraintMappings() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "test_dslWithIndividualConstraints.dsl", getClass() ), ResourceType.DSL ); kbuilder.add( ResourceFactory.newClassPathResource( "test_dslWithIndividualConstraints.dslr", getClass() ) , ResourceType.DSLR ); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } // Check errors final String err = kbuilder.getErrors().toString(); assertEquals( "", err ); assertEquals(0, kbuilder.getErrors().size()); // the compiled package final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages(); assertEquals(1, pkgs.size()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(pkgs); kbase = SerializationHelper.serializeObject(kbase); KieSession session = createKnowledgeSession(kbase); List results = new ArrayList(); session.setGlobal("results", results); Cheese cheese = new Cheese( "stilton", 42 ); session.insert(cheese); // wm = SerializationHelper.serializeObject(wm); session.fireAllRules(); // should have fired assertEquals( 1, results.size() ); assertEquals(cheese, results.get(0)); }
Example 20
Source File: AbstractKieModule.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Override public Collection<KiePackage> getKnowledgePackagesForKieBase(String kieBaseName) { KnowledgeBuilder kbuilder = kBuilders.get(kieBaseName); return kbuilder != null ? kbuilder.getKnowledgePackages() : null; }