Search in sources :

Example 1 with InstanceGiven

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

the class DMNScenarioRunnerHelper method defineInputValues.

/**
 * It returns a {@link Map} which contains the actual data in the DMN Executable Builder (BC) or DMN Context (Kogito)
 * Typically, the Map contains a pair with the <b>Fact Name</b> as a Key and its <b>Object</b> as value
 * (another Map containing the fact properties)
 * (eg.   "Driver": {
 *              "Name": "string"
 *         }
 * )
 * In case of a Imported Fact, i.e. a Decision or a Input node imported from an external DMN file, the Map contains
 * the <b>Fact prefix as a Key</b>, which is the name of the imported DMN document, and another Map as value which
 * contains all the Imported Fact with that prefix.
 * (eg.   "imp" : {
 *              "Violation": {
 *                  "Code": "string"
 *              }
 *        }
 * )
 * If the the same fact is present in both Background and Given list, the Given one will override the background one.
 * @param backgroundData,
 * @param givenData
 * @return
 */
protected Map<String, Object> defineInputValues(List<InstanceGiven> backgroundData, List<InstanceGiven> givenData) {
    List<InstanceGiven> inputData = new ArrayList<>();
    inputData.addAll(backgroundData);
    inputData.addAll(givenData);
    Map<String, Object> inputValues = new HashMap<>();
    Map<String, Map<String, Object>> importedInputValues = new HashMap<>();
    for (InstanceGiven input : inputData) {
        String factName = input.getFactIdentifier().getName();
        String importPrefix = input.getFactIdentifier().getImportPrefix();
        if (importPrefix != null && !importPrefix.isEmpty()) {
            String importedFactName = factName.replaceFirst(importPrefix + ".", "");
            Map<String, Object> groupedFacts = importedInputValues.computeIfAbsent(importPrefix, k -> new HashMap<>());
            Object value = groupedFacts.containsKey(importedFactName) ? mergeValues(groupedFacts.get(importedFactName), input.getValue()) : input.getValue();
            importedInputValues.get(importPrefix).put(importedFactName, value);
        } else {
            Object value = inputValues.containsKey(factName) ? mergeValues(inputValues.get(factName), input.getValue()) : input.getValue();
            inputValues.put(factName, value);
        }
    }
    importedInputValues.forEach(inputValues::put);
    return inputValues;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with InstanceGiven

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

the class RuleScenarioRunnerHelper method verifyConditions.

@Override
protected void verifyConditions(ScesimModelDescriptor scesimModelDescriptor, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, Map<String, Object> requestContext) {
    for (InstanceGiven input : scenarioRunnerData.getGivens()) {
        FactIdentifier factIdentifier = input.getFactIdentifier();
        List<ScenarioExpect> assertionOnFact = scenarioRunnerData.getExpects().stream().filter(elem -> !elem.isNewFact()).filter(elem -> Objects.equals(elem.getFactIdentifier(), factIdentifier)).collect(toList());
        // check if this fact has something to check
        if (assertionOnFact.isEmpty()) {
            continue;
        }
        getScenarioResultsFromGivenFacts(scesimModelDescriptor, assertionOnFact, input, expressionEvaluatorFactory).forEach(scenarioRunnerData::addResult);
    }
}
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) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier)

Example 3 with InstanceGiven

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

the class RuleScenarioRunnerHelperTest method executeScenario.

@Test
public void executeScenario() {
    ArgumentCaptor<Object> insertCaptor = ArgumentCaptor.forClass(Object.class);
    ScenarioRunnerData scenarioRunnerData = new ScenarioRunnerData();
    scenarioRunnerData.addBackground(new InstanceGiven(personFactIdentifier, new Person()));
    scenarioRunnerData.addBackground(new InstanceGiven(disputeFactIdentifier, new Dispute()));
    scenarioRunnerData.addGiven(new InstanceGiven(personFactIdentifier, new Person()));
    FactMappingValue factMappingValue = new FactMappingValue(personFactIdentifier, firstNameExpectedExpressionIdentifier, NAME);
    scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), false));
    scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), true));
    int inputObjects = scenarioRunnerData.getBackgrounds().size() + scenarioRunnerData.getGivens().size();
    String ruleFlowGroup = "ruleFlowGroup";
    settings.setRuleFlowGroup(ruleFlowGroup);
    runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings);
    verify(ruleScenarioExecutableBuilderMock, times(1)).setActiveRuleFlowGroup(ruleFlowGroup);
    verify(ruleScenarioExecutableBuilderMock, times(inputObjects)).insert(insertCaptor.capture());
    for (Object value : insertCaptor.getAllValues()) {
        assertTrue(value instanceof Person || value instanceof Dispute);
    }
    verify(ruleScenarioExecutableBuilderMock, times(1)).addInternalCondition(eq(Person.class), any(), any());
    verify(ruleScenarioExecutableBuilderMock, times(1)).run();
    assertEquals(1, scenarioRunnerData.getResults().size());
    // test not rule error
    settings.setType(ScenarioSimulationModel.Type.DMN);
    assertThatThrownBy(() -> runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings)).isInstanceOf(ScenarioException.class).hasMessageStartingWith("Impossible to run");
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Dispute(org.drools.scenariosimulation.backend.model.Dispute) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 4 with InstanceGiven

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

the class AbstractRunnerHelper method extractGivenValues.

protected List<InstanceGiven> extractGivenValues(ScesimModelDescriptor scesimModelDescriptor, List<FactMappingValue> factMappingValues, ClassLoader classLoader, ExpressionEvaluatorFactory expressionEvaluatorFactory) {
    List<InstanceGiven> instanceGiven = new ArrayList<>();
    Map<FactIdentifier, List<FactMappingValue>> groupByFactIdentifier = groupByFactIdentifierAndFilter(factMappingValues, FactMappingType.GIVEN);
    boolean hasError = false;
    for (Map.Entry<FactIdentifier, List<FactMappingValue>> entry : groupByFactIdentifier.entrySet()) {
        try {
            FactIdentifier factIdentifier = entry.getKey();
            // for each fact, create a map of path to fields and values to set
            Map<List<String>, Object> paramsForBean = getParamsForBean(scesimModelDescriptor, factIdentifier, entry.getValue(), expressionEvaluatorFactory);
            Object bean = createObject(getDirectMapping(paramsForBean), factIdentifier.getClassName(), paramsForBean, classLoader);
            instanceGiven.add(new InstanceGiven(factIdentifier, bean));
        } catch (Exception e) {
            String errorMessage = e.getMessage() != null ? e.getMessage() : e.getClass().getCanonicalName();
            logger.error("Error in GIVEN data " + entry.getKey() + ": " + errorMessage, e);
            hasError = true;
        }
    }
    if (hasError) {
        throw new ScenarioException("Error in GIVEN data");
    }
    return instanceGiven;
}
Also used : ArrayList(java.util.ArrayList) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) HashMap(java.util.HashMap) Map(java.util.Map) ScenarioSimulationSharedUtils.isCollectionOrMap(org.drools.scenariosimulation.api.utils.ScenarioSimulationSharedUtils.isCollectionOrMap)

Example 5 with InstanceGiven

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

the class RuleScenarioRunnerHelperTest method getScenarioResultsTest.

@Test
public void getScenarioResultsTest() {
    List<InstanceGiven> scenario1Inputs = runnerHelper.extractGivenValues(simulation.getScesimModelDescriptor(), scenario1.getUnmodifiableFactMappingValues(), classLoader, expressionEvaluatorFactory);
    List<ScenarioExpect> scenario1Outputs = runnerHelper.extractExpectedValues(scenario1.getUnmodifiableFactMappingValues());
    assertTrue(scenario1Inputs.size() > 0);
    InstanceGiven input1 = scenario1Inputs.get(0);
    scenario1Outputs = scenario1Outputs.stream().filter(elem -> elem.getFactIdentifier().equals(input1.getFactIdentifier())).collect(toList());
    List<ScenarioResult> scenario1Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario1Outputs, input1, expressionEvaluatorFactory);
    assertEquals(1, scenario1Results.size());
    assertEquals(FactMappingValueStatus.SUCCESS, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
    List<InstanceGiven> scenario2Inputs = runnerHelper.extractGivenValues(simulation.getScesimModelDescriptor(), scenario2.getUnmodifiableFactMappingValues(), classLoader, expressionEvaluatorFactory);
    List<ScenarioExpect> scenario2Outputs = runnerHelper.extractExpectedValues(scenario2.getUnmodifiableFactMappingValues());
    assertTrue(scenario2Inputs.size() > 0);
    InstanceGiven input2 = scenario2Inputs.get(0);
    scenario2Outputs = scenario2Outputs.stream().filter(elem -> elem.getFactIdentifier().equals(input2.getFactIdentifier())).collect(toList());
    List<ScenarioResult> scenario2Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario2Outputs, input2, expressionEvaluatorFactory);
    assertEquals(1, scenario2Results.size());
    assertEquals(FactMappingValueStatus.SUCCESS, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
    List<ScenarioExpect> newFact = singletonList(new ScenarioExpect(personFactIdentifier, emptyList(), true));
    List<ScenarioResult> scenario2NoResults = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), newFact, input2, expressionEvaluatorFactory);
    assertEquals(0, scenario2NoResults.size());
    Person person = new Person();
    person.setFirstName("ANOTHER STRING");
    InstanceGiven newInput = new InstanceGiven(personFactIdentifier, person);
    List<ScenarioResult> scenario3Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario1Outputs, newInput, expressionEvaluatorFactory);
    assertEquals(FactMappingValueStatus.FAILED_WITH_ERROR, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
    assertEquals(1, scenario3Results.size());
    assertEquals(person.getFirstName(), scenario3Results.get(0).getResultValue().get());
    assertEquals("NAME", scenario3Results.get(0).getFactMappingValue().getRawValue());
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Aggregations

InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)9 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)4 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)4 ScenarioRunnerData (org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData)4 HashMap (java.util.HashMap)3 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)3 Person (org.drools.scenariosimulation.backend.model.Person)3 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 Dispute (org.drools.scenariosimulation.backend.model.Dispute)2 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)2 AbstractMap (java.util.AbstractMap)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1