Java Code Examples for org.kie.internal.builder.KnowledgeBuilderFactory#newKnowledgeBuilderConfiguration()
The following examples show how to use
org.kie.internal.builder.KnowledgeBuilderFactory#newKnowledgeBuilderConfiguration() .
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: 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 2
Source File: FunctionsTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testFunctionCallingFunctionWithEclipse() throws Exception { KnowledgeBuilderConfiguration kbconf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); kbconf.setProperty( "drools.dialect.java.compiler", "ECLIPSE" ); KieBase kbase = loadKnowledgeBase( kbconf, "test_functionCallingFunction.drl" ); KieSession ksession = createKnowledgeSession(kbase); final List<Integer> list = new ArrayList<Integer>(); ksession.setGlobal( "results", list ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertEquals( 12, list.get( 0 ).intValue() ); }
Example 3
Source File: ParallelCompilationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public KieBase call() throws Exception { final Reader source = new InputStreamReader(ParallelCompilationTest.class.getResourceAsStream(DRL_FILE)); final Properties props = new Properties(); props.setProperty("drools.dialect.java.compiler", "JANINO"); props.setProperty("drools.dialect.java.compiler.lnglevel", "1.6"); KieBase result; final KnowledgeBuilderConfiguration configuration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(props, ParallelCompilationTest.class.getClass().getClassLoader()); final KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(configuration); Thread.sleep(Math.round(Math.random()*250)); Resource newReaderResource = ResourceFactory.newReaderResource(source); //synchronized (RuleUtil.class) { builder.add(newReaderResource, ResourceType.DRL); } result = builder.newKieBase(); return result; }
Example 4
Source File: ChangeSetTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testCustomClassLoader() throws Exception { // JBRULES-3630 String absolutePath = new File("file").getAbsolutePath(); URL url = ChangeSetTest.class.getResource(ChangeSetTest.class.getSimpleName() + ".class"); AtomicReference<File> jar = new AtomicReference<>(); assertTimeoutPreemptively(Duration.ofSeconds(10), () -> { File file = new File( url.toURI() ); while ( true ) { file = file.getParentFile(); File j = new File( file, "/src/test/resources/org/drools/compiler/compiler/xml/changeset/changeset.jar" ); if ( j.exists() ) { jar.set(j); break; } } }, "JAR not found in time."); ClassLoader classLoader = URLClassLoader.newInstance(new URL[]{jar.get().toURI().toURL()}, getClass().getClassLoader()); Resource changeSet = ResourceFactory.newClassPathResource("changeset1.xml", classLoader); KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, classLoader); KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf); builder.add(changeSet, ResourceType.CHANGE_SET); }
Example 5
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public Collection<KiePackage> loadKnowledgePackages(KnowledgeBuilderConfiguration kbuilderConf, PackageDescr descr) { if (kbuilderConf == null) { kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConf); kbuilder.add(ResourceFactory.newDescrResource(descr), ResourceType.DESCR); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } Collection<KiePackage> knowledgePackages = kbuilder.getKnowledgePackages(); return knowledgePackages; }
Example 6
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 7
Source File: FailureOnRemovalTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private KnowledgeBuilderConfiguration createKnowledgeBuilderConfiguration() { Properties properties = new Properties(); properties.setProperty( "drools.dialect.java.compiler", "JANINO" ); KnowledgeBuilderConfiguration kconf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration( properties, getClass().getClassLoader() ); kconf.setOption( DefaultDialectOption.get( "java" ) ); return kconf; }
Example 8
Source File: MarshallingTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * In this case we are dealing with facts which are not on the systems classpath. */ @Test public void testSerializabilityWithJarFacts() throws Exception { MapBackedClassLoader loader = new MapBackedClassLoader( this.getClass().getClassLoader() ); JarInputStream jis = new JarInputStream( this.getClass().getResourceAsStream( "/billasurf.jar" ) ); JarEntry entry = null; byte[] buf = new byte[1024]; int len = 0; while ( (entry = jis.getNextJarEntry()) != null ) { if ( !entry.isDirectory() ) { ByteArrayOutputStream out = new ByteArrayOutputStream(); while ( (len = jis.read( buf )) >= 0 ) { out.write( buf, 0, len ); } loader.addResource( entry.getName(), out.toByteArray() ); } } String drl = "package foo.bar \n" + "import com.billasurf.Board\n" + "rule 'MyGoodRule' \n dialect 'mvel' \n when " + " Board() " + "then \n" + " System.err.println(42); \n" + "end\n"; KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader); Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl); kpkgs = SerializationHelper.serializeObject( kpkgs, loader ); }
Example 9
Source File: DynamicRulesTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testClassLoaderSwitchsUsingConf() throws Exception { try { // Creates first class loader and use it to load fact classes ClassLoader loader1 = new SubvertedClassLoader( new URL[]{getClass().getResource( "/" )}, this.getClass().getClassLoader() ); Class cheeseClass = loader1.loadClass( "org.drools.compiler.Cheese" ); KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader1); KieBase kbase = loadKnowledgeBase( kbuilderConf, "test_Dynamic1.drl" ); KieSession wm = createKnowledgeSession(kbase); wm.insert( cheeseClass.newInstance() ); wm.fireAllRules(); // Creates second class loader and use it to load fact classes ClassLoader loader2 = new SubvertedClassLoader( new URL[]{getClass().getResource( "/" )}, this.getClass().getClassLoader() ); cheeseClass = loader2.loadClass( "org.drools.compiler.Cheese" ); kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader2); kbase = loadKnowledgeBase(kbuilderConf, "test_Dynamic1.drl"); wm = createKnowledgeSession(kbase); wm.insert(cheeseClass.newInstance()); wm.fireAllRules(); } catch ( ClassCastException cce ) { cce.printStackTrace(); fail( "No ClassCastException should be raised." ); } }
Example 10
Source File: PropertySpecificTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testWrongUasgeOfWatchAnnotationOnNonPropertySpecificClass() throws Exception { String rule = "package org.drools.compiler.integrationtests\n" + "dialect \"mvel\"\n" + "declare A\n" + " s : String\n" + "end\n" + "declare B\n" + " on : boolean\n" + " s : String\n" + "end\n" + "rule R1\n" + "when\n" + " A($s : s)\n" + " $b : B(s != $s) @watch( !s, on )\n" + "then\n" + " modify($b) { setS($s) }\n" + "end\n" + "rule R2\n" + "when\n" + " $b : B(on == false)\n" + "then\n" + " modify($b) { setOn(true) }\n" + "end\n"; // The compilation of the above ^ would turn error under the assumption Property Reactivity is NOT enabled by default. KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); conf.setOption(PropertySpecificOption.ALLOWED); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf); kbuilder.add( ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL ); assertTrue(kbuilder.hasErrors()); }
Example 11
Source File: PropertySpecificTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testClassReactive() throws Exception { String rule = "package org.drools.compiler.integrationtests\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "declare B\n" + " @classReactive\n" + " on : boolean\n" + " s : String\n" + "end\n" + "rule R1\n" + "when\n" + " $b : B(s == \"test\")\n" + "then\n" + " modify($b) { setOn(true) }\n" + " if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" + "end\n"; KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); config.setOption(PropertySpecificOption.ALWAYS); KieBase kbase = loadKnowledgeBaseFromString( config, rule ); KieSession ksession = kbase.newKieSession(); AtomicInteger counter = new AtomicInteger(0); ksession.setGlobal( "counter", counter ); FactType factTypeB = kbase.getFactType( "org.drools.compiler.integrationtests", "B" ); Object factB = factTypeB.newInstance(); factTypeB.set( factB, "s", "test" ); factTypeB.set( factB, "on", false ); ksession.insert( factB ); assertThatThrownBy(() -> ksession.fireAllRules()) .isInstanceOf(RuntimeException.class) .hasMessageContaining("Exception executing consequence for rule \"R1\""); }
Example 12
Source File: PropertySpecificTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testConfig() throws Exception { String rule = "package org.drools.compiler.integrationtests\n" + "declare A\n" + " i : int\n" + " j : int\n" + "end\n" + "rule R1\n" + "when\n" + " $a : A(i == 1)\n" + "then\n" + " modify($a) { setJ(2) };\n" + "end\n"; KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); config.setOption(PropertySpecificOption.ALWAYS); KieBase kbase = loadKnowledgeBaseFromString( config, rule ); KieSession ksession = kbase.newKieSession(); FactType typeA = kbase.getFactType( "org.drools.compiler.integrationtests", "A" ); Object a = typeA.newInstance(); typeA.set( a, "i", 1 ); ksession.insert( a ); int rules = ksession.fireAllRules(); assertEquals(1, rules); }
Example 13
Source File: JBRULESTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testGUVNOR578_2() throws Exception { final MapBackedClassLoader loader = new MapBackedClassLoader( this.getClass().getClassLoader() ); final JarInputStream jis = new JarInputStream( this.getClass().getResourceAsStream( "/primespoc.jar" ) ); JarEntry entry = null; final byte[] buf = new byte[1024]; int len = 0; while ( (entry = jis.getNextJarEntry()) != null ) { if ( !entry.isDirectory() ) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); while ( (len = jis.read( buf )) >= 0 ) { out.write( buf, 0, len ); } loader.addResource( entry.getName(), out.toByteArray() ); } } final List<JarInputStream> jarInputStreams = new ArrayList<JarInputStream>(); jarInputStreams.add(jis); final KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader); final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf); final String header = "import fr.gouv.agriculture.dag.agorha.business.primes.SousPeriodePrimeAgent\n"; kbuilder.add(ResourceFactory.newByteArrayResource(header.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); final String passingRule = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "SousPeriodePrimeAgent( echelle == \"abc\" )" + "then\n" + "end\n"; final String failingRule = "rule \"rule2\"\n" + "dialect \"mvel\"\n" + "when\n" + "SousPeriodePrimeAgent( quotiteRemuneration == 123 , echelle == \"abc\" )" + "then\n" + "end\n"; kbuilder.add(ResourceFactory.newByteArrayResource(passingRule.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); kbuilder.add(ResourceFactory.newByteArrayResource(failingRule.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); }
Example 14
Source File: KnowledgeBuilderConfigurationTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@BeforeEach public void setUp() throws Exception { config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(); }