PropertySpecificOption.ALLOWED.toString()); kieFileSystem.writeKModuleXML(kmodel.toXML());
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; }
/** * This is a factory method for this DefaultDialect configuration. * The factory method is a best practice for the case where the * actual object construction is changed in the future. * * @param name the name of the dialect to be configured as default * * @return the actual type safe default dialect configuration. */ public static DefaultDialectOption get( String name ) { return new DefaultDialectOption( name ); }
EvaluatorOption option = EvaluatorOption.get( "after", afterDef ); config.getOption( EvaluatorOption.class, "after" ).getName() ); assertEquals( afterDef, config.getOption( EvaluatorOption.class, "after" ).getEvaluatorDefinition() ); assertEquals( EvaluatorOption.get( "before", beforeDef ), config.getOption( EvaluatorOption.class, "before" ) ); config.getOption( EvaluatorOption.class, "before" ).getName() ); assertEquals( beforeDef.getClass().getName(), config.getOption( EvaluatorOption.class, "before" ).getEvaluatorDefinition().getClass().getName() );
@Test public void testImportedAnnotation() { String str = "package org.simple \n" + "import " + Xyz.class.getCanonicalName() + " \n" + "@Xyz rule yyy \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem() .write( "src/main/resources/r1.drl", str ) .writeKModuleXML(ks.newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString()) .toXML()); Results results = ks.newKieBuilder( kfs ).buildAll().getResults(); assertEquals(0, results.getMessages().size()); }
AccumulateFunctionOption option = AccumulateFunctionOption.get( "avg", function ); config.getOption( AccumulateFunctionOption.class, "avg" ).getName() ); assertEquals( function, config.getOption( AccumulateFunctionOption.class, "avg" ).getFunction() ); assertEquals( AccumulateFunctionOption.get( "maximum", max ), config.getOption( AccumulateFunctionOption.class, "maximum" ) ); config.getOption( AccumulateFunctionOption.class, "maximum" ).getName() ); assertEquals( max.getClass().getName(), config.getOption( AccumulateFunctionOption.class, "maximum" ).getFunction().getClass().getName() ); assertEquals( AccumulateFunctionOption.get( "inner", inner ), config.getOption( AccumulateFunctionOption.class, "inner" ) ); config.getOption( AccumulateFunctionOption.class, "inner" ).getName() ); assertEquals( inner.getClass().getName(), config.getOption( AccumulateFunctionOption.class, "inner" ).getFunction().getClass().getName() );
@Test public void testDefaultDialectConfiguration() { // setting the default dialect using the type safe method config.setOption( DefaultDialectOption.get( "mvel" ) ); // checking the type safe getOption() method assertEquals( DefaultDialectOption.get( "mvel" ), config.getOption( DefaultDialectOption.class ) ); // checking string conversion assertEquals( "mvel", config.getOption( DefaultDialectOption.class ).getName() ); // checking the string based getProperty() method assertEquals( "mvel", config.getProperty( DefaultDialectOption.PROPERTY_NAME ) ); // setting the default dialect using the string based setProperty() method config.setProperty( DefaultDialectOption.PROPERTY_NAME, "java" ); // checking the type safe getOption() method assertEquals( DefaultDialectOption.get( "java" ), config.getOption( DefaultDialectOption.class ) ); assertEquals( "DefaultDialectOption( name=java )", config.getOption( DefaultDialectOption.class ).toString() ); // checking string conversion assertEquals( "java", config.getOption( DefaultDialectOption.class ).getName() ); // checking the string based getProperty() method assertEquals( "java", config.getProperty( DefaultDialectOption.PROPERTY_NAME ) ); }
@Test public void testDefaultPackageNameConfiguration() { // setting the default dialect using the type safe method config.setOption( DefaultPackageNameOption.get( "org.drools.compiler.test" ) ); // checking the type safe getOption() method assertEquals( DefaultPackageNameOption.get( "org.drools.compiler.test" ), config.getOption( DefaultPackageNameOption.class ) ); // checking string conversion assertEquals( "org.drools.compiler.test", config.getOption( DefaultPackageNameOption.class ).getPackageName() ); // checking the string based getProperty() method assertEquals( "org.drools.compiler.test", config.getProperty( DefaultPackageNameOption.PROPERTY_NAME ) ); // setting the default dialect using the string based setProperty() method config.setProperty( DefaultPackageNameOption.PROPERTY_NAME, "org.drools" ); // checking the type safe getOption() method assertEquals( DefaultPackageNameOption.get( "org.drools" ), config.getOption( DefaultPackageNameOption.class ) ); // checking string conversion assertEquals( "org.drools", config.getOption( DefaultPackageNameOption.class ).getPackageName() ); // checking the string based getProperty() method assertEquals( "org.drools", config.getProperty( DefaultPackageNameOption.PROPERTY_NAME ) ); }
@Test public void testDumpDirectoryConfiguration() { File dumpDir = new File("target"); // setting the dump directory using the type safe method config.setOption( DumpDirOption.get( dumpDir ) ); // checking the type safe getOption() method assertEquals( DumpDirOption.get( dumpDir ), config.getOption( DumpDirOption.class ) ); // checking string conversion assertEquals( dumpDir, config.getOption( DumpDirOption.class ).getDirectory() ); // checking the string based getProperty() method assertEquals( dumpDir.toString(), config.getProperty( DumpDirOption.PROPERTY_NAME ) ); // setting the dump dir using the string based setProperty() method dumpDir = new File( System.getProperty( "java.io.tmpdir" ) ); config.setProperty( DumpDirOption.PROPERTY_NAME, System.getProperty( "java.io.tmpdir" ) ); // checking the type safe getOption() method assertEquals( DumpDirOption.get( dumpDir ), config.getOption( DumpDirOption.class ) ); // checking string conversion assertEquals( dumpDir, config.getOption( DumpDirOption.class ).getDirectory() ); // checking the string based getProperty() method assertEquals( dumpDir.toString(), config.getProperty( DumpDirOption.PROPERTY_NAME ) ); }
@Test public void testResultSeverity() { System.setProperty("drools.kbuilder.severity." + DuplicateFunction.KEY, "ERROR"); KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl(); assertEquals(cfg.getOptionKeys(KBuilderSeverityOption.class).size(), 1); assertEquals(cfg.getOption(KBuilderSeverityOption.class, DuplicateFunction.KEY).getSeverity(), ResultSeverity.ERROR); }
/** * This is a factory method for this ParallelRulesBuildThresholdOption configuration. The factory method is a best practice for the case * where the actual object construction is changed in the future. * * @param vale * the value of the parallel rules build threshold to be configured * * @return the actual type safe parallel rules build threshold configuration. */ public static ParallelRulesBuildThresholdOption get(final int parallelRulesBuildThreshold) { return new ParallelRulesBuildThresholdOption(parallelRulesBuildThreshold); }
/** * This is a factory method for this DefaultPackageName configuration. * The factory method is a best practice for the case where the * actual object construction is changed in the future. * * @param packageName the name of the package to be configured as default * * @return the actual type safe default package name configuration. */ public static DefaultPackageNameOption get( String packageName ) { return new DefaultPackageNameOption( packageName ); }
@Test public void testStirctWatchWithoutQuotes() { String str = "package com.sample;\n" + "import " + MyClass.class.getCanonicalName() + ";\n" + "rule R1 when\n" + " @Watch( !value ) $m : MyClass( value < 10 )\n" + "then \n" + " modify( $m ) { setValue( $m.getValue()+1 ) };\n" + "end\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem() .write( "src/main/resources/r1.drl", str ) .writeKModuleXML(ks.newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString()) .toXML()); Results results = ks.newKieBuilder( kfs ).buildAll().getResults(); assertEquals(1, results.getMessages().size()); }
@Test public void testResultSeverityNonExistingValueDefaultToInfo() { System.setProperty("drools.kbuilder.severity." + DuplicateFunction.KEY, "FOO"); KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl(); assertEquals(cfg.getOptionKeys(KBuilderSeverityOption.class).size(), 1); assertEquals(cfg.getOption(KBuilderSeverityOption.class, DuplicateFunction.KEY).getSeverity(), ResultSeverity.INFO); }
@Test public void testJavaSqlTimestamp() { String str = "package " + Message.class.getPackage().getName() + "\n" + "@Role( Role.Type.EVENT ) @Timestamp( \"startTime\" ) @Duration( \"duration\" )\n" + "declare " + Message.class.getCanonicalName() + "\n" + "end\n"; KieSession ksession = new KieHelper() .setKieModuleModel(KieServices.Factory.get().newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())) .addContent(str, ResourceType.DRL) .build() .newKieSession(); try { Message msg = new Message(); msg.setStartTime( new Timestamp( 10000 ) ); msg.setDuration( 1000l ); EventFactHandle efh = (EventFactHandle) ksession.insert( msg ); assertEquals( 10000, efh.getStartTimestamp() ); assertEquals( 1000, efh.getDuration() ); } finally { ksession.dispose(); } }
@Test public void testUnknownAnnotation() { String str = "package org.simple \n" + "@Xyz rule yyy \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem() .write( "src/main/resources/r1.drl", str ) .writeKModuleXML(ks.newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString()) .toXML()); Results results = ks.newKieBuilder( kfs ).buildAll().getResults(); assertEquals(1, results.getMessages().size()); }
.setKieModuleModel(KieServices.Factory.get().newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())) .addContent(str, ResourceType.DRL) .build();
@Test public void testLanguageLevelConfiguration() { // setting the language level using the type safe method config.setOption( LanguageLevelOption.DRL5 ); // checking the type safe getOption() method assertEquals( LanguageLevelOption.DRL5, config.getOption( LanguageLevelOption.class ) ); // checking string conversion assertEquals( LanguageLevelOption.DRL5, config.getOption( LanguageLevelOption.class ) ); // checking the string based getProperty() method assertEquals( "DRL5", config.getProperty( LanguageLevelOption.PROPERTY_NAME ) ); // setting the default dialect using the string based setProperty() method config.setProperty( LanguageLevelOption.PROPERTY_NAME, "DRL6" ); // checking the type safe getOption() method assertEquals(LanguageLevelOption.DRL6, config.getOption(LanguageLevelOption.class)); assertEquals( "DRL6", config.getOption( LanguageLevelOption.class ).toString() ); // checking string conversion assertEquals( LanguageLevelOption.DRL6, config.getOption( LanguageLevelOption.class ) ); // checking the string based getProperty() method assertEquals( "DRL6", config.getProperty( LanguageLevelOption.PROPERTY_NAME ) ); }
.setKieModuleModel(ks.newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())) .addContent(str, ResourceType.DRL) .build()
@Test public void testWatch() { String str = "package com.sample;\n" + "import " + MyClass.class.getCanonicalName() + ";\n" + "rule R1 when\n" + " @Watch( \"!value\" ) $m : MyClass( value < 10 )\n" + "then \n" + " modify( $m ) { setValue( $m.getValue()+1 ) };\n" + "end\n"; KieSession ksession = new KieHelper() .setKieModuleModel(KieServices.Factory.get().newKieModuleModel() .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString())) .addContent(str, ResourceType.DRL) .build() .newKieSession(); try { MyClass myClass = new MyClass("test", 1); ksession.insert(myClass); ksession.fireAllRules(); assertEquals(2, myClass.getValue()); } finally { ksession.dispose(); } }