Search in sources :

Example 1 with EvaluatorDefinition

use of org.drools.core.base.evaluators.EvaluatorDefinition in project drools by kiegroup.

the class MVELConstraintBuilder method getEvaluator.

public Evaluator getEvaluator(final RuleBuildContext context, final BaseDescr descr, final ValueType valueType, final String evaluatorString, final boolean isNegated, final String parameters, final EvaluatorDefinition.Target left, final EvaluatorDefinition.Target right) {
    final EvaluatorDefinition def = context.getConfiguration().getEvaluatorRegistry().getEvaluatorDefinition(evaluatorString);
    if (def == null) {
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the Evaluator for ID '" + evaluatorString + "'"));
        return null;
    }
    final Evaluator evaluator = def.getEvaluator(valueType, evaluatorString, isNegated, parameters, left, right);
    if (evaluator == null) {
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Evaluator '" + (isNegated ? "not " : "") + evaluatorString + "' does not support type '" + valueType));
    }
    return evaluator;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) Evaluator(org.drools.core.spi.Evaluator)

Example 2 with EvaluatorDefinition

use of org.drools.core.base.evaluators.EvaluatorDefinition in project drools by kiegroup.

the class TemporalEvaluatorFactoryTest method runEvaluatorTest.

private void runEvaluatorTest(final Object[][] data, final ValueType valueType) {
    final InternalReadAccessor extractor = new MockExtractor();
    for (int i = 0; i < data.length; i++) {
        final Object[] row = data[i];
        boolean isNegated = ((String) row[1]).startsWith("not ");
        // System.out.println((String) row[1]);
        String evaluatorStr = isNegated ? ((String) row[1]).substring(4) : (String) row[1];
        boolean isConstrained = evaluatorStr.endsWith("]");
        String parameters = null;
        if (isConstrained) {
            parameters = evaluatorStr.split("\\[")[1];
            evaluatorStr = evaluatorStr.split("\\[")[0];
            parameters = parameters.split("\\]")[0];
        }
        EvaluatorDefinition evalDef = registry.getEvaluatorDefinition(evaluatorStr);
        assertNotNull(evalDef);
        final Evaluator evaluator = evalDef.getEvaluator(valueType, evaluatorStr, isNegated, parameters);
        // System.out.println(evaluator);
        checkEvaluatorMethodWith2Extractors(valueType, extractor, row, evaluator);
        checkEvaluatorMethodCachedRight(valueType, extractor, row, evaluator);
        checkEvaluatorMethodCachedLeft(valueType, extractor, row, evaluator);
        checkEvaluatorMethodWithFieldValue(valueType, extractor, row, evaluator);
        assertEquals(valueType, evaluator.getValueType());
    }
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) DuringEvaluatorDefinition(org.drools.core.base.evaluators.DuringEvaluatorDefinition) CoincidesEvaluatorDefinition(org.drools.core.base.evaluators.CoincidesEvaluatorDefinition) BeforeEvaluatorDefinition(org.drools.core.base.evaluators.BeforeEvaluatorDefinition) MetByEvaluatorDefinition(org.drools.core.base.evaluators.MetByEvaluatorDefinition) AfterEvaluatorDefinition(org.drools.core.base.evaluators.AfterEvaluatorDefinition) EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) MeetsEvaluatorDefinition(org.drools.core.base.evaluators.MeetsEvaluatorDefinition) Evaluator(org.drools.core.spi.Evaluator) DisconnectedWorkingMemoryEntryPoint(org.drools.core.common.DisconnectedWorkingMemoryEntryPoint)

Example 3 with EvaluatorDefinition

use of org.drools.core.base.evaluators.EvaluatorDefinition in project drools by kiegroup.

the class KnowledgeBuilderConfigurationImpl method getProperty.

public String getProperty(String name) {
    name = name.trim();
    if (StringUtils.isEmpty(name)) {
        return null;
    }
    if (name.equals(DefaultDialectOption.PROPERTY_NAME)) {
        return getDefaultDialect();
    } else if (name.equals(DefaultPackageNameOption.PROPERTY_NAME)) {
        return getDefaultPackageName();
    } else if (name.startsWith(AccumulateFunctionOption.PROPERTY_NAME)) {
        int index = AccumulateFunctionOption.PROPERTY_NAME.length();
        AccumulateFunction function = this.accumulateFunctions.get(name.substring(index));
        return function != null ? function.getClass().getName() : null;
    } else if (name.startsWith(EvaluatorOption.PROPERTY_NAME)) {
        String key = name.substring(name.lastIndexOf('.') + 1);
        EvaluatorDefinition evalDef = this.evaluatorRegistry.getEvaluatorDefinition(key);
        return evalDef != null ? evalDef.getClass().getName() : null;
    } else if (name.equals(DumpDirOption.PROPERTY_NAME)) {
        return this.dumpDirectory != null ? this.dumpDirectory.toString() : null;
    } else if (name.equals(ProcessStringEscapesOption.PROPERTY_NAME)) {
        return String.valueOf(isProcessStringEscapes());
    } else if (name.equals(ClassLoaderCacheOption.PROPERTY_NAME)) {
        return String.valueOf(isClassLoaderCacheEnabled());
    } else if (name.startsWith(KBuilderSeverityOption.PROPERTY_NAME)) {
        String key = name.substring(name.lastIndexOf('.') + 1);
        ResultSeverity severity = this.severityMap.get(key);
        return severity.toString();
    } else if (name.equals(LanguageLevelOption.PROPERTY_NAME)) {
        return "" + getLanguageLevel();
    }
    return null;
}
Also used : EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ResultSeverity(org.kie.internal.builder.ResultSeverity)

Example 4 with EvaluatorDefinition

use of org.drools.core.base.evaluators.EvaluatorDefinition in project drools by kiegroup.

the class KnowledgeBuilderConfigurationTest method testEvaluatorConfiguration.

@Test
public void testEvaluatorConfiguration() {
    // in this use case, the application already has the instance of the evaluator definition
    EvaluatorDefinition afterDef = new AfterEvaluatorDefinition();
    assertNotNull(afterDef);
    // creating the option and storing in a local var just to make test easier
    EvaluatorOption option = EvaluatorOption.get("after", afterDef);
    // wiring the evaluator definition using the type safe method
    config.setOption(option);
    // checking the type safe getOption() method
    assertEquals(option, config.getOption(EvaluatorOption.class, "after"));
    // checking string conversion
    assertEquals("after", config.getOption(EvaluatorOption.class, "after").getName());
    assertEquals(afterDef, config.getOption(EvaluatorOption.class, "after").getEvaluatorDefinition());
    // checking the string based getProperty() method
    assertEquals(AfterEvaluatorDefinition.class.getName(), config.getProperty(EvaluatorOption.PROPERTY_NAME + "after"));
    // wiring the evaluator definition using the string based setProperty() method
    config.setProperty(EvaluatorOption.PROPERTY_NAME + "before", BeforeEvaluatorDefinition.class.getName());
    BeforeEvaluatorDefinition beforeDef = new BeforeEvaluatorDefinition();
    // checking the type safe getOption() method
    assertEquals(EvaluatorOption.get("before", beforeDef), config.getOption(EvaluatorOption.class, "before"));
    // checking string conversion
    assertEquals("before", config.getOption(EvaluatorOption.class, "before").getName());
    assertEquals(beforeDef.getClass().getName(), config.getOption(EvaluatorOption.class, "before").getEvaluatorDefinition().getClass().getName());
    // checking the string based getProperty() method
    assertEquals(beforeDef.getClass().getName(), config.getProperty(EvaluatorOption.PROPERTY_NAME + "before"));
}
Also used : BeforeEvaluatorDefinition(org.drools.core.base.evaluators.BeforeEvaluatorDefinition) AfterEvaluatorDefinition(org.drools.core.base.evaluators.AfterEvaluatorDefinition) BeforeEvaluatorDefinition(org.drools.core.base.evaluators.BeforeEvaluatorDefinition) AfterEvaluatorDefinition(org.drools.core.base.evaluators.AfterEvaluatorDefinition) EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) EvaluatorOption(org.kie.internal.builder.conf.EvaluatorOption) Test(org.junit.Test)

Aggregations

EvaluatorDefinition (org.drools.core.base.evaluators.EvaluatorDefinition)4 AfterEvaluatorDefinition (org.drools.core.base.evaluators.AfterEvaluatorDefinition)2 BeforeEvaluatorDefinition (org.drools.core.base.evaluators.BeforeEvaluatorDefinition)2 Evaluator (org.drools.core.spi.Evaluator)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 CoincidesEvaluatorDefinition (org.drools.core.base.evaluators.CoincidesEvaluatorDefinition)1 DuringEvaluatorDefinition (org.drools.core.base.evaluators.DuringEvaluatorDefinition)1 MeetsEvaluatorDefinition (org.drools.core.base.evaluators.MeetsEvaluatorDefinition)1 MetByEvaluatorDefinition (org.drools.core.base.evaluators.MetByEvaluatorDefinition)1 DisconnectedWorkingMemoryEntryPoint (org.drools.core.common.DisconnectedWorkingMemoryEntryPoint)1 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)1 Test (org.junit.Test)1 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)1 ResultSeverity (org.kie.internal.builder.ResultSeverity)1 EvaluatorOption (org.kie.internal.builder.conf.EvaluatorOption)1