Search in sources :

Example 11 with FactMappingValue

use of org.drools.scenariosimulation.api.model.FactMappingValue in project drools-wb by kiegroup.

the class ScenarioCsvImportExport method exportData.

public String exportData(AbstractScesimModel<? extends AbstractScesimData> scesimModel) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    List<FactMapping> factMappings = scesimModel.getScesimModelDescriptor().getUnmodifiableFactMappings();
    CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT);
    generateHeader(factMappings, printer);
    for (AbstractScesimData scesimData : scesimModel.getUnmodifiableData()) {
        List<Object> values = new ArrayList<>();
        for (FactMapping factMapping : factMappings) {
            Optional<FactMappingValue> factMappingValue = scesimData.getFactMappingValue(factMapping.getFactIdentifier(), factMapping.getExpressionIdentifier());
            values.add(factMappingValue.map(FactMappingValue::getRawValue).orElse(""));
        }
        printer.printRecord(values.toArray());
    }
    printer.close();
    return stringBuilder.toString();
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) AbstractScesimData(org.drools.scenariosimulation.api.model.AbstractScesimData) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ArrayList(java.util.ArrayList)

Example 12 with FactMappingValue

use of org.drools.scenariosimulation.api.model.FactMappingValue in project drools-wb by kiegroup.

the class ScenarioSimulationEditorPresenterTest method getImportCallback.

@Test
public void getImportCallback() {
    List<AbstractScesimModel> toTest = Arrays.asList(new Simulation(), new Background());
    for (AbstractScesimModel abstractScesimModel : toTest) {
        FactMapping factMapping = abstractScesimModel.getScesimModelDescriptor().addFactMapping(FactIdentifier.EMPTY, ExpressionIdentifier.create("empty", FactMappingType.GIVEN));
        FactMappingValue toBeRemoved = abstractScesimModel.addData().addOrUpdateMappingValue(factMapping.getFactIdentifier(), factMapping.getExpressionIdentifier(), "toBeRemoved");
        presenterSpy.getImportCallBack().callback(abstractScesimModel);
        verify(presenterSpy, times(1)).cleanReadOnlyColumn(eq(abstractScesimModel));
        assertNull(toBeRemoved.getRawValue());
        reset(presenterSpy);
    }
}
Also used : FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) Background(org.drools.scenariosimulation.api.model.Background) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) AbstractScesimModel(org.drools.scenariosimulation.api.model.AbstractScesimModel) Test(org.junit.Test)

Example 13 with FactMappingValue

use of org.drools.scenariosimulation.api.model.FactMappingValue in project drools by kiegroup.

the class DMNScenarioRunnerHelperTest method executeScenario.

@Test
public void executeScenario() {
    ArgumentCaptor<Object> setValueCaptor = ArgumentCaptor.forClass(Object.class);
    ArgumentCaptor<String> setKeyCaptor = ArgumentCaptor.forClass(String.class);
    FactIdentifier bookFactIdentifier = FactIdentifier.create("Book", "Book");
    FactIdentifier importedPersonFactIdentifier = FactIdentifier.create(IMPORTED_PREFIX + ".Person", IMPORTED_PREFIX + ".Person", IMPORTED_PREFIX);
    FactIdentifier importedDisputeFactIdentifier = FactIdentifier.create(IMPORTED_PREFIX + ".Dispute", IMPORTED_PREFIX + ".Dispute", IMPORTED_PREFIX);
    FactIdentifier importedBookFactIdentifier = FactIdentifier.create(IMPORTED_PREFIX + ".Book", IMPORTED_PREFIX + ".Book", IMPORTED_PREFIX);
    FactIdentifier importedWrBookFactIdentifier = FactIdentifier.create(IMPORTED_PREFIX + ".wr.Book", IMPORTED_PREFIX + ".wr.Book", IMPORTED_PREFIX);
    AbstractMap.SimpleEntry<String, Object> backgroundDisputeFactData = new AbstractMap.SimpleEntry<>("description", "Nice");
    AbstractMap.SimpleEntry<String, Object> backgroundPersonFactData = new AbstractMap.SimpleEntry<>("name", "Carl");
    AbstractMap.SimpleEntry<String, Object> backgroundPersonFactData2 = new AbstractMap.SimpleEntry<>("age", 2);
    AbstractMap.SimpleEntry<String, Object> backgroundImportedDisputeFactData = new AbstractMap.SimpleEntry<>("description", "Bad");
    AbstractMap.SimpleEntry<String, Object> backgroundImportedPersonFactData = new AbstractMap.SimpleEntry<>("name", "Max");
    AbstractMap.SimpleEntry<String, Object> backgroundImportedPersonFactData2 = new AbstractMap.SimpleEntry<>("age", 34);
    AbstractMap.SimpleEntry<String, Object> givenPersonFactData = new AbstractMap.SimpleEntry<>("surname", "Brown");
    AbstractMap.SimpleEntry<String, Object> givenPersonFactData2 = new AbstractMap.SimpleEntry<>("age", 23);
    AbstractMap.SimpleEntry<String, Object> givenBookFactData = new AbstractMap.SimpleEntry<>("Author", "Resey Rema");
    AbstractMap.SimpleEntry<String, Object> givenBookFactData2 = new AbstractMap.SimpleEntry<>("Name", "The mighty Test Scenario!");
    AbstractMap.SimpleEntry<String, Object> givenImportedBookFactData = new AbstractMap.SimpleEntry<>("Author", "Mr Y");
    AbstractMap.SimpleEntry<String, Object> givenImportedBookFactData2 = new AbstractMap.SimpleEntry<>("Title", "The awesome Test Scenario!");
    AbstractMap.SimpleEntry<String, Object> givenImportedPersonFactData = new AbstractMap.SimpleEntry<>("surname", "White");
    AbstractMap.SimpleEntry<String, Object> givenImportedPersonFactData2 = new AbstractMap.SimpleEntry<>("age", 67);
    AbstractMap.SimpleEntry<String, Object> givenImportedWrBookFactData = new AbstractMap.SimpleEntry<>("Title", "I hate name with multi dots");
    ScenarioRunnerData scenarioRunnerData = new ScenarioRunnerData();
    scenarioRunnerData.addBackground(new InstanceGiven(disputeFactIdentifier, instantiateMap(backgroundDisputeFactData)));
    scenarioRunnerData.addBackground(new InstanceGiven(personFactIdentifier, instantiateMap(backgroundPersonFactData, backgroundPersonFactData2)));
    scenarioRunnerData.addBackground(new InstanceGiven(importedPersonFactIdentifier, instantiateMap(backgroundImportedPersonFactData, backgroundImportedPersonFactData2)));
    scenarioRunnerData.addBackground(new InstanceGiven(importedDisputeFactIdentifier, instantiateMap(backgroundImportedDisputeFactData)));
    scenarioRunnerData.addGiven(new InstanceGiven(personFactIdentifier, instantiateMap(givenPersonFactData, givenPersonFactData2)));
    scenarioRunnerData.addGiven(new InstanceGiven(importedPersonFactIdentifier, instantiateMap(givenImportedPersonFactData, givenImportedPersonFactData2)));
    scenarioRunnerData.addGiven(new InstanceGiven(bookFactIdentifier, instantiateMap(givenBookFactData, givenBookFactData2)));
    scenarioRunnerData.addGiven(new InstanceGiven(importedBookFactIdentifier, instantiateMap(givenImportedBookFactData, givenImportedBookFactData2)));
    scenarioRunnerData.addGiven(new InstanceGiven(importedWrBookFactIdentifier, instantiateMap(givenImportedWrBookFactData)));
    FactMappingValue factMappingValue = new FactMappingValue(personFactIdentifier, firstNameExpectedExpressionIdentifier, NAME);
    scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), false));
    scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), true));
    List<String> expectedInputDataToLoad = asList(personFactIdentifier.getName(), disputeFactIdentifier.getName(), bookFactIdentifier.getName(), IMPORTED_PREFIX);
    int inputObjects = expectedInputDataToLoad.size();
    runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings);
    verify(dmnScenarioExecutableBuilderMock, times(1)).setActiveModel(DMN_FILE_PATH);
    verify(dmnScenarioExecutableBuilderMock, times(inputObjects)).setValue(setKeyCaptor.capture(), setValueCaptor.capture());
    assertTrue(setKeyCaptor.getAllValues().containsAll(expectedInputDataToLoad));
    for (int i = 0; i < inputObjects; i++) {
        String key = setKeyCaptor.getAllValues().get(i);
        Map<String, Object> value = (Map<String, Object>) setValueCaptor.getAllValues().get(i);
        if (personFactIdentifier.getName().equals(key)) {
            assertEquals(backgroundPersonFactData.getValue(), value.get(backgroundPersonFactData.getKey()));
            assertNotEquals(backgroundPersonFactData2.getValue(), value.get(backgroundPersonFactData2.getKey()));
            assertEquals(givenPersonFactData.getValue(), value.get(givenPersonFactData.getKey()));
            assertEquals(givenPersonFactData2.getValue(), value.get(givenPersonFactData2.getKey()));
            assertEquals(3, value.size());
        } else if (disputeFactIdentifier.getName().equals(key)) {
            assertEquals(backgroundDisputeFactData.getValue(), value.get(backgroundDisputeFactData.getKey()));
            assertEquals(1, value.size());
        } else if (bookFactIdentifier.getName().equals(key)) {
            assertEquals(givenBookFactData.getValue(), value.get(givenBookFactData.getKey()));
            assertEquals(givenBookFactData2.getValue(), value.get(givenBookFactData2.getKey()));
            assertEquals(2, value.size());
        } else if (IMPORTED_PREFIX.equals(key)) {
            Map<String, Object> subValueDispute = (Map<String, Object>) value.get("Dispute");
            assertEquals(backgroundImportedDisputeFactData.getValue(), subValueDispute.get(backgroundImportedDisputeFactData.getKey()));
            assertEquals(1, subValueDispute.size());
            Map<String, Object> subValueBook = (Map<String, Object>) value.get("Book");
            assertEquals(givenImportedBookFactData.getValue(), subValueBook.get(givenImportedBookFactData.getKey()));
            assertEquals(givenImportedBookFactData2.getValue(), subValueBook.get(givenImportedBookFactData2.getKey()));
            assertEquals(2, subValueBook.size());
            Map<String, Object> subValuePerson = (Map<String, Object>) value.get("Person");
            assertEquals(backgroundImportedPersonFactData.getValue(), subValuePerson.get(backgroundImportedPersonFactData.getKey()));
            assertNotEquals(backgroundImportedPersonFactData2.getValue(), subValuePerson.get(backgroundImportedPersonFactData2.getKey()));
            assertEquals(givenImportedPersonFactData.getValue(), subValuePerson.get(givenImportedPersonFactData.getKey()));
            assertEquals(givenImportedPersonFactData2.getValue(), subValuePerson.get(givenImportedPersonFactData2.getKey()));
            assertEquals(3, subValuePerson.size());
            Map<String, Object> subValueWrBook = (Map<String, Object>) value.get("wr.Book");
            assertEquals(givenImportedWrBookFactData.getValue(), subValueWrBook.get(givenImportedWrBookFactData.getKey()));
            assertEquals(1, subValueWrBook.size());
            assertEquals(4, value.size());
        } else {
            fail("Unexpected key: " + key);
        }
    }
    verify(dmnScenarioExecutableBuilderMock, times(1)).run();
    // test not rule error
    settings.setType(ScenarioSimulationModel.Type.RULE);
    assertThatThrownBy(() -> runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings)).isInstanceOf(ScenarioException.class).hasMessageStartingWith("Impossible to run");
}
Also used : FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Mockito.anyString(org.mockito.Mockito.anyString) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) AbstractMap(java.util.AbstractMap) ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Test(org.junit.Test)

Example 14 with FactMappingValue

use of org.drools.scenariosimulation.api.model.FactMappingValue in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method extractBackgroundValues.

@Test
public void extractBackgroundValues() {
    // TEST 0 - empty background
    Background emptyBackground = new Background();
    List<InstanceGiven> emptyBackgroundGivens = runnerHelper.extractBackgroundValues(emptyBackground, classLoader, expressionEvaluatorFactory);
    assertEquals(0, emptyBackgroundGivens.size());
    emptyBackground.addData();
    emptyBackgroundGivens = runnerHelper.extractBackgroundValues(emptyBackground, classLoader, expressionEvaluatorFactory);
    assertEquals(0, emptyBackgroundGivens.size());
    // TEST 1 - background correct
    List<InstanceGiven> backgroundGivens = runnerHelper.extractBackgroundValues(this.background, classLoader, expressionEvaluatorFactory);
    assertEquals(3, backgroundGivens.size());
    for (InstanceGiven backgroundGiven : backgroundGivens) {
        if (backgroundGiven.getFactIdentifier().equals(personFactIdentifier)) {
            assertEquals(personFactIdentifier, backgroundGiven.getFactIdentifier());
            Person person = (Person) backgroundGiven.getValue();
            assertEquals(NAME, person.getFirstName());
        } else if (backgroundGiven.getFactIdentifier().equals(disputeFactIdentifier)) {
            assertEquals(disputeFactIdentifier, backgroundGiven.getFactIdentifier());
            Dispute dispute = (Dispute) backgroundGiven.getValue();
            double parsedAmount = Double.parseDouble(AMOUNT);
            assertEquals(parsedAmount, dispute.getAmount(), 0.1);
        } else {
            fail();
        }
    }
    // TEST 2 - broken background
    String notValid = "notValid";
    FactMappingValue notValid1 = backgroundData1.addOrUpdateMappingValue(disputeFactIdentifier, amountGivenExpressionIdentifier, notValid);
    FactMappingValue notValid2 = backgroundData2.addOrUpdateMappingValue(disputeFactIdentifier, amountGivenExpressionIdentifier, notValid);
    assertThatThrownBy(() -> runnerHelper.extractBackgroundValues(this.background, classLoader, expressionEvaluatorFactory)).isInstanceOf(ScenarioException.class).hasMessage("Error in BACKGROUND data");
    assertEquals(FactMappingValueStatus.FAILED_WITH_EXCEPTION, notValid1.getStatus());
    assertTrue(notValid1.getExceptionMessage().startsWith("Impossible to parse"));
    assertEquals(FactMappingValueStatus.FAILED_WITH_EXCEPTION, notValid2.getStatus());
    assertTrue(notValid2.getExceptionMessage().startsWith("Impossible to parse"));
}
Also used : Background(org.drools.scenariosimulation.api.model.Background) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Dispute(org.drools.scenariosimulation.backend.model.Dispute) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 15 with FactMappingValue

use of org.drools.scenariosimulation.api.model.FactMappingValue in project drools by kiegroup.

the class ExpressionEvaluatorFactoryTest method getOrCreate.

@Test
public void getOrCreate() {
    FactMappingValue simpleFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, "10");
    FactMappingValue objectFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, "10");
    FactMappingValue mvelFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, MVEL_ESCAPE_SYMBOL + " 10");
    FactMappingValue mvelWithSpacesFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, "     " + MVEL_ESCAPE_SYMBOL + " 10");
    FactMappingValue mvelCollectionExpressionFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, new TextNode(MVEL_ESCAPE_SYMBOL + " 10").textValue());
    FactMappingValue mvelCollectionExpressionWithSpacesFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, new TextNode("     " + MVEL_ESCAPE_SYMBOL + " 10").textValue());
    FactMappingValue mvelCollectionExpressionWitoutMVELEscapeSymbolFMV = new FactMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, new TextNode(" 10").textValue());
    ExpressionEvaluatorFactory ruleEvaluatorFactory = ExpressionEvaluatorFactory.create(classLoader, ScenarioSimulationModel.Type.RULE);
    ExpressionEvaluatorFactory dmnEvaluatorFactory = ExpressionEvaluatorFactory.create(classLoader, ScenarioSimulationModel.Type.DMN);
    assertTrue(ruleEvaluatorFactory.getOrCreate(simpleFMV) instanceof BaseExpressionEvaluator);
    assertTrue(ruleEvaluatorFactory.getOrCreate(objectFMV) instanceof BaseExpressionEvaluator);
    assertTrue(ruleEvaluatorFactory.getOrCreate(mvelFMV) instanceof MVELExpressionEvaluator);
    assertTrue(ruleEvaluatorFactory.getOrCreate(mvelWithSpacesFMV) instanceof MVELExpressionEvaluator);
    assertTrue(ruleEvaluatorFactory.getOrCreate(mvelCollectionExpressionFMV) instanceof MVELExpressionEvaluator);
    assertTrue(ruleEvaluatorFactory.getOrCreate(mvelCollectionExpressionWithSpacesFMV) instanceof MVELExpressionEvaluator);
    assertTrue(ruleEvaluatorFactory.getOrCreate(mvelCollectionExpressionWitoutMVELEscapeSymbolFMV) instanceof BaseExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(simpleFMV) instanceof DMNFeelExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(objectFMV) instanceof DMNFeelExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(mvelFMV) instanceof DMNFeelExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(mvelWithSpacesFMV) instanceof DMNFeelExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(mvelCollectionExpressionFMV) instanceof DMNFeelExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(mvelCollectionExpressionWithSpacesFMV) instanceof DMNFeelExpressionEvaluator);
    assertTrue(dmnEvaluatorFactory.getOrCreate(mvelCollectionExpressionWitoutMVELEscapeSymbolFMV) instanceof DMNFeelExpressionEvaluator);
}
Also used : FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) TextNode(com.fasterxml.jackson.databind.node.TextNode) Test(org.junit.Test)

Aggregations

FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)24 Test (org.junit.Test)15 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)9 ArrayList (java.util.ArrayList)7 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)7 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)7 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)7 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)7 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)6 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)6 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)5 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Background (org.drools.scenariosimulation.api.model.Background)4 ScenarioRunnerData (org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData)4 Collectors.toList (java.util.stream.Collectors.toList)3 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)3 ExpressionEvaluatorResult (org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult)3