Search in sources :

Example 1 with AverageAccumulateFunction

use of org.drools.core.base.accumulators.AverageAccumulateFunction in project drools by kiegroup.

the class KnowledgeBuilderConfigurationTest method testAccumulateFunctionConfiguration.

@Test
public void testAccumulateFunctionConfiguration() {
    Set<String> keySet = new HashSet<String>();
    // in this use case, the application already has the instance of the accumulate function
    AccumulateFunction function = new AverageAccumulateFunction();
    // creating the option and storing in a local var just to make test easier
    AccumulateFunctionOption option = AccumulateFunctionOption.get("avg", function);
    // wiring the accumulate function using the type safe method
    config.setOption(option);
    // checking the type safe getOption() method
    assertEquals(option, config.getOption(AccumulateFunctionOption.class, "avg"));
    // checking string conversion
    assertEquals("avg", config.getOption(AccumulateFunctionOption.class, "avg").getName());
    assertEquals(function, config.getOption(AccumulateFunctionOption.class, "avg").getFunction());
    // checking the string based getProperty() method
    assertEquals(AverageAccumulateFunction.class.getName(), config.getProperty(AccumulateFunctionOption.PROPERTY_NAME + "avg"));
    // check the key set
    keySet.add("avg");
    assertTrue(config.getOptionKeys(AccumulateFunctionOption.class).contains("avg"));
    // wiring the accumulate function using the string based setProperty() method
    config.setProperty(AccumulateFunctionOption.PROPERTY_NAME + "maximum", MaxAccumulateFunction.class.getName());
    MaxAccumulateFunction max = new MaxAccumulateFunction();
    // checking the type safe getOption() method
    assertEquals(AccumulateFunctionOption.get("maximum", max), config.getOption(AccumulateFunctionOption.class, "maximum"));
    // checking string conversion
    assertEquals("maximum", config.getOption(AccumulateFunctionOption.class, "maximum").getName());
    assertEquals(max.getClass().getName(), config.getOption(AccumulateFunctionOption.class, "maximum").getFunction().getClass().getName());
    // checking the string based getProperty() method
    assertEquals(MaxAccumulateFunction.class.getName(), config.getProperty(AccumulateFunctionOption.PROPERTY_NAME + "maximum"));
    keySet.add("avg");
    // wiring the inner class accumulate function using the string based setProperty() method
    config.setProperty(AccumulateFunctionOption.PROPERTY_NAME + "inner", InnerAccumulateFuncion.class.getName());
    InnerAccumulateFuncion inner = new InnerAccumulateFuncion();
    // checking the type safe getOption() method
    assertEquals(AccumulateFunctionOption.get("inner", inner), config.getOption(AccumulateFunctionOption.class, "inner"));
    // checking string conversion
    assertEquals("inner", config.getOption(AccumulateFunctionOption.class, "inner").getName());
    assertEquals(inner.getClass().getName(), config.getOption(AccumulateFunctionOption.class, "inner").getFunction().getClass().getName());
    // checking the string based getProperty() method
    assertEquals(InnerAccumulateFuncion.class.getName(), config.getProperty(AccumulateFunctionOption.PROPERTY_NAME + "inner"));
    keySet.add("avg");
    assertTrue(config.getOptionKeys(AccumulateFunctionOption.class).containsAll(keySet));
// for( String key: config.getOptionKeys(AccumulateFunctionOption.class ) ){
// System.out.println( key + "->" + config.getOption(AccumulateFunctionOption.class, key).getClass().getName() );
// }
}
Also used : MaxAccumulateFunction(org.drools.core.base.accumulators.MaxAccumulateFunction) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) MaxAccumulateFunction(org.drools.core.base.accumulators.MaxAccumulateFunction) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) AccumulateFunctionOption(org.kie.internal.builder.conf.AccumulateFunctionOption) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HashSet (java.util.HashSet)1 AverageAccumulateFunction (org.drools.core.base.accumulators.AverageAccumulateFunction)1 MaxAccumulateFunction (org.drools.core.base.accumulators.MaxAccumulateFunction)1 Test (org.junit.Test)1 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)1 AccumulateFunctionOption (org.kie.internal.builder.conf.AccumulateFunctionOption)1