Search in sources :

Example 1 with EvaluatorDefinition

use of org.drools.compiler.rule.builder.EvaluatorDefinition in project drools by kiegroup.

the class CustomOperatorSpec method addOperatorArgument.

@Override
protected Operator addOperatorArgument(RuleContext context, MethodCallExpr methodCallExpr, String opName) {
    EvaluatorDefinition evalDef = context.getEvaluatorDefinition(opName);
    if (evalDef == null) {
        throw new RuntimeException("Unknown custom operator: " + opName);
    }
    String arg = "new " + CustomOperatorWrapper.class.getCanonicalName() + "( new " + evalDef.getClass().getCanonicalName() + "().getEvaluator(" + ValueType.class.getCanonicalName() + ".OBJECT_TYPE, \"" + opName + "\", false, null), \"" + opName + "\")";
    methodCallExpr.addArgument(arg);
    return null;
}
Also used : EvaluatorDefinition(org.drools.compiler.rule.builder.EvaluatorDefinition)

Example 2 with EvaluatorDefinition

use of org.drools.compiler.rule.builder.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) PatternBuilder.registerDescrBuildError(org.drools.compiler.rule.builder.PatternBuilder.registerDescrBuildError) AfterEvaluatorDefinition(org.drools.mvel.evaluators.AfterEvaluatorDefinition) DuringEvaluatorDefinition(org.drools.mvel.evaluators.DuringEvaluatorDefinition) IncludesEvaluatorDefinition(org.drools.mvel.evaluators.IncludesEvaluatorDefinition) StartedByEvaluatorDefinition(org.drools.mvel.evaluators.StartedByEvaluatorDefinition) BeforeEvaluatorDefinition(org.drools.mvel.evaluators.BeforeEvaluatorDefinition) MetByEvaluatorDefinition(org.drools.mvel.evaluators.MetByEvaluatorDefinition) MeetsEvaluatorDefinition(org.drools.mvel.evaluators.MeetsEvaluatorDefinition) FinishesEvaluatorDefinition(org.drools.mvel.evaluators.FinishesEvaluatorDefinition) OverlappedByEvaluatorDefinition(org.drools.mvel.evaluators.OverlappedByEvaluatorDefinition) StartsEvaluatorDefinition(org.drools.mvel.evaluators.StartsEvaluatorDefinition) CoincidesEvaluatorDefinition(org.drools.mvel.evaluators.CoincidesEvaluatorDefinition) OverlapsEvaluatorDefinition(org.drools.mvel.evaluators.OverlapsEvaluatorDefinition) StrEvaluatorDefinition(org.drools.mvel.evaluators.StrEvaluatorDefinition) EvaluatorDefinition(org.drools.compiler.rule.builder.EvaluatorDefinition) FinishedByEvaluatorDefinition(org.drools.mvel.evaluators.FinishedByEvaluatorDefinition) MvelEvaluator.createMvelEvaluator(org.drools.mvel.expr.MvelEvaluator.createMvelEvaluator) MvelEvaluator(org.drools.mvel.expr.MvelEvaluator) ReteEvaluator(org.drools.core.common.ReteEvaluator) Evaluator(org.drools.core.spi.Evaluator)

Example 3 with EvaluatorDefinition

use of org.drools.compiler.rule.builder.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 MvelEvaluator evaluator = (MvelEvaluator) evalDef.getEvaluator(valueType, evaluatorStr, isNegated, parameters);
        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) EvaluatorDefinition(org.drools.compiler.rule.builder.EvaluatorDefinition) DisconnectedWorkingMemoryEntryPoint(org.drools.kiesession.entrypoints.DisconnectedWorkingMemoryEntryPoint)

Example 4 with EvaluatorDefinition

use of org.drools.compiler.rule.builder.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.mvel.evaluators.BeforeEvaluatorDefinition) AfterEvaluatorDefinition(org.drools.mvel.evaluators.AfterEvaluatorDefinition) AfterEvaluatorDefinition(org.drools.mvel.evaluators.AfterEvaluatorDefinition) BeforeEvaluatorDefinition(org.drools.mvel.evaluators.BeforeEvaluatorDefinition) EvaluatorDefinition(org.drools.compiler.rule.builder.EvaluatorDefinition) EvaluatorOption(org.kie.internal.builder.conf.EvaluatorOption) Test(org.junit.Test)

Example 5 with EvaluatorDefinition

use of org.drools.compiler.rule.builder.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.equals(TrimCellsInDTableOption.PROPERTY_NAME)) {
        return String.valueOf(isTrimCellsInDTable());
    } else if (name.equals(GroupDRLsInKieBasesByFolderOption.PROPERTY_NAME)) {
        return String.valueOf(isGroupDRLsInKieBasesByFolder());
    } 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();
    } else if (name.equals(ParallelRulesBuildThresholdOption.PROPERTY_NAME)) {
        return String.valueOf(getParallelRulesBuildThreshold());
    } else if (name.equals(ExternaliseCanonicalModelLambdaOption.PROPERTY_NAME)) {
        return String.valueOf(isExternaliseCanonicalModelLambda());
    } else if (name.equals(ParallelLambdaExternalizationOption.PROPERTY_NAME)) {
        return String.valueOf(isParallelLambdaExternalization());
    }
    return null;
}
Also used : EvaluatorDefinition(org.drools.compiler.rule.builder.EvaluatorDefinition) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ResultSeverity(org.kie.internal.builder.ResultSeverity)

Aggregations

EvaluatorDefinition (org.drools.compiler.rule.builder.EvaluatorDefinition)6 AfterEvaluatorDefinition (org.drools.mvel.evaluators.AfterEvaluatorDefinition)2 BeforeEvaluatorDefinition (org.drools.mvel.evaluators.BeforeEvaluatorDefinition)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 PatternBuilder.registerDescrBuildError (org.drools.compiler.rule.builder.PatternBuilder.registerDescrBuildError)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 Evaluator (org.drools.core.spi.Evaluator)1 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)1 DisconnectedWorkingMemoryEntryPoint (org.drools.kiesession.entrypoints.DisconnectedWorkingMemoryEntryPoint)1 CoincidesEvaluatorDefinition (org.drools.mvel.evaluators.CoincidesEvaluatorDefinition)1 DuringEvaluatorDefinition (org.drools.mvel.evaluators.DuringEvaluatorDefinition)1 FinishedByEvaluatorDefinition (org.drools.mvel.evaluators.FinishedByEvaluatorDefinition)1 FinishesEvaluatorDefinition (org.drools.mvel.evaluators.FinishesEvaluatorDefinition)1 IncludesEvaluatorDefinition (org.drools.mvel.evaluators.IncludesEvaluatorDefinition)1 MeetsEvaluatorDefinition (org.drools.mvel.evaluators.MeetsEvaluatorDefinition)1 MetByEvaluatorDefinition (org.drools.mvel.evaluators.MetByEvaluatorDefinition)1 OverlappedByEvaluatorDefinition (org.drools.mvel.evaluators.OverlappedByEvaluatorDefinition)1 OverlapsEvaluatorDefinition (org.drools.mvel.evaluators.OverlapsEvaluatorDefinition)1 StartedByEvaluatorDefinition (org.drools.mvel.evaluators.StartedByEvaluatorDefinition)1 StartsEvaluatorDefinition (org.drools.mvel.evaluators.StartsEvaluatorDefinition)1