Search in sources :

Example 6 with ScenarioResult

use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult in project drools by kiegroup.

the class ConditionFilterTest method acceptTest.

@Test
public void acceptTest() {
    Function<Object, ValueWrapper> alwaysMatchFunction = ValueWrapper::of;
    FactMappingValue factMappingValue = new FactMappingValue(FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION, "Test");
    ScenarioResult scenarioResult = new ScenarioResult(factMappingValue);
    ConditionFilter conditionFilter = new ConditionFilter(singletonList(new FactCheckerHandle(String.class, alwaysMatchFunction, scenarioResult)));
    Assert.assertFalse(conditionFilter.accept(1));
    Assert.assertTrue(conditionFilter.accept("String"));
    Function<Object, ValueWrapper> alwaysNotMatchFunction = object -> ValueWrapper.errorWithValidValue(null, null);
    ConditionFilter conditionFilterFail = new ConditionFilter(singletonList(new FactCheckerHandle(String.class, alwaysNotMatchFunction, scenarioResult)));
    Assert.assertFalse(conditionFilterFail.accept("String"));
}
Also used : ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Test(org.junit.Test) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) Assert(org.junit.Assert) Function(java.util.function.Function) Collections.singletonList(java.util.Collections.singletonList) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) Test(org.junit.Test)

Example 7 with ScenarioResult

use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult in project drools by kiegroup.

the class RuleStatelessScenarioExecutableBuilderTest method generateCommands.

@Test
public void generateCommands() {
    FactMappingValue emptyFMV = new FactMappingValue(FactIdentifier.EMPTY, ExpressionIdentifier.DESCRIPTION, null);
    RuleStatelessScenarioExecutableBuilder builder = new RuleStatelessScenarioExecutableBuilder(null, null);
    Command<ExecutionResults> batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertFalse(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertFalse(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
    builder.setActiveAgendaGroup("test");
    batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertFalse(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
    builder.insert(new Object());
    batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertTrue(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
    builder.addInternalCondition(String.class, obj -> null, new ScenarioResult(emptyFMV, null));
    batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertTrue(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertTrue(verifyCommand(batchCommand, ValidateFactCommand.class));
}
Also used : InsertElementsCommand(org.drools.core.command.runtime.rule.InsertElementsCommand) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ExecutionResults(org.kie.api.runtime.ExecutionResults) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) AgendaGroupSetFocusCommand(org.drools.core.command.runtime.rule.AgendaGroupSetFocusCommand) Test(org.junit.Test)

Example 8 with ScenarioResult

use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult in project drools by kiegroup.

the class RuleScenarioRunnerHelper method getScenarioResultsFromGivenFacts.

protected List<ScenarioResult> getScenarioResultsFromGivenFacts(ScesimModelDescriptor scesimModelDescriptor, List<ScenarioExpect> scenarioOutputsPerFact, InstanceGiven input, ExpressionEvaluatorFactory expressionEvaluatorFactory) {
    Object factInstance = input.getValue();
    List<ScenarioResult> scenarioResults = new ArrayList<>();
    for (ScenarioExpect scenarioExpect : scenarioOutputsPerFact) {
        if (scenarioExpect.isNewFact()) {
            continue;
        }
        for (FactMappingValue expectedResult : scenarioExpect.getExpectedResult()) {
            ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(expectedResult);
            ScenarioResult scenarioResult = fillResult(expectedResult, () -> createExtractorFunction(expressionEvaluator, expectedResult, scesimModelDescriptor).apply(factInstance), expressionEvaluator);
            scenarioResults.add(scenarioResult);
        }
    }
    return scenarioResults;
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ArrayList(java.util.ArrayList) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)

Example 9 with ScenarioResult

use of org.drools.scenariosimulation.backend.runner.model.ScenarioResult in project drools by kiegroup.

the class RuleScenarioRunnerHelper method executeScenario.

@Override
protected Map<String, Object> executeScenario(KieContainer kieContainer, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, ScesimModelDescriptor scesimModelDescriptor, Settings settings) {
    if (!Type.RULE.equals(settings.getType())) {
        throw new ScenarioException("Impossible to run a not-RULE simulation with RULE runner");
    }
    RuleScenarioExecutableBuilder ruleScenarioExecutableBuilder = createBuilderWrapper(kieContainer, settings);
    if (settings.getRuleFlowGroup() != null) {
        ruleScenarioExecutableBuilder.setActiveRuleFlowGroup(settings.getRuleFlowGroup());
    }
    loadInputData(scenarioRunnerData.getBackgrounds(), ruleScenarioExecutableBuilder);
    loadInputData(scenarioRunnerData.getGivens(), ruleScenarioExecutableBuilder);
    // all new facts should be verified internally to the working memory
    scenarioRunnerData.getExpects().stream().filter(ScenarioExpect::isNewFact).flatMap(output -> output.getExpectedResult().stream().map(ScenarioResult::new)).forEach(scenarioResult -> {
        Class<?> clazz = ScenarioBeanUtil.loadClass(scenarioResult.getFactIdentifier().getClassName(), kieContainer.getClassLoader());
        ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(scenarioResult.getFactMappingValue());
        scenarioRunnerData.addResult(scenarioResult);
        ruleScenarioExecutableBuilder.addInternalCondition(clazz, createExtractorFunction(expressionEvaluator, scenarioResult.getFactMappingValue(), scesimModelDescriptor), scenarioResult);
    });
    return ruleScenarioExecutableBuilder.run();
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) RuleScenarioExecutableBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder) ScenarioResultMetadata(org.drools.scenariosimulation.backend.runner.model.ScenarioResultMetadata) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ExpressionElement(org.drools.scenariosimulation.api.model.ExpressionElement) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScenarioBeanUtil.fillBean(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.fillBean) Map(java.util.Map) COVERAGE_LISTENER(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.COVERAGE_LISTENER) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) RuleScenarioExecutableBuilder.createBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.createBuilder) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) ScenarioBeanWrapper(org.drools.scenariosimulation.backend.util.ScenarioBeanWrapper) CoverageAgendaListener(org.drools.scenariosimulation.backend.fluent.CoverageAgendaListener) Settings(org.drools.scenariosimulation.api.model.Settings) ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ScenarioBeanUtil(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil) Set(java.util.Set) ConstantsHolder(org.drools.scenariosimulation.api.utils.ConstantsHolder) KieContainer(org.kie.api.runtime.KieContainer) Objects(java.util.Objects) Type(org.drools.scenariosimulation.api.model.ScenarioSimulationModel.Type) ExpressionEvaluatorFactory(org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorFactory) List(java.util.List) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) Collectors.toList(java.util.stream.Collectors.toList) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) RULES_AVAILABLE(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.RULES_AVAILABLE) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) RuleScenarioExecutableBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder)

Aggregations

ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)9 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)6 Test (org.junit.Test)5 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)4 ArrayList (java.util.ArrayList)3 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)3 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)3 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)3 Function (java.util.function.Function)2 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)2 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)2 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)2 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)2 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1