Search in sources :

Example 6 with Import

use of org.kie.soup.project.datamodel.imports.Import in project drools by kiegroup.

the class ScenarioRunnerTest method testCollection.

@Test
public void testCollection() throws Exception {
    KieSession ksession = getKieSession("test_rules2.drl");
    ScenarioRunner run = new ScenarioRunner(ksession);
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheesery"));
    run.run(scenario);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) KieSession(org.kie.api.runtime.KieSession) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 7 with Import

use of org.kie.soup.project.datamodel.imports.Import in project drools by kiegroup.

the class ScenarioRunnerTest method testVerifyFactsWithEnum.

@Test
public void testVerifyFactsWithEnum() throws Exception {
    FieldData fieldData = new FieldData("cheeseType", "CheeseType.CHEDDAR");
    fieldData.setNature(FieldData.TYPE_ENUM);
    FactData cheeseFactData = new FactData("Cheese", "c1", Arrays.<Field>asList(fieldData), false);
    FieldData cheeseType = new FieldData("cheeseType", "CheeseType.CHEDDAR");
    cheeseType.setNature(FieldData.TYPE_ENUM);
    FactData f1 = new FactData("Cheese", "f1", Arrays.<Field>asList(cheeseType), false);
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.CheeseType"));
    scenario.getFixtures().add(cheeseFactData);
    scenario.getFixtures().add(f1);
    ScenarioRunner runner = new ScenarioRunner(ksession);
    VerifyFact vf = new VerifyFact();
    vf.setName("f1");
    VerifyField verifyField = new VerifyField("cheeseType", "CheeseType.CHEDDAR", "==");
    verifyField.setNature(VerifyField.TYPE_ENUM);
    vf.setFieldValues(ls(verifyField));
    scenario.getFixtures().add(vf);
    runner.run(scenario);
    for (VerifyField field : vf.getFieldValues()) {
        assertTrue(field.getSuccessResult());
    }
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 8 with Import

use of org.kie.soup.project.datamodel.imports.Import in project drools by kiegroup.

the class ScenarioRunnerTest method testVerifyFacts.

@Test
public void testVerifyFacts() throws Exception {
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Person"));
    ScenarioRunner runner = new ScenarioRunner(ksession);
    scenario.getFixtures().add(new FactData("Cheese", "f1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false));
    scenario.getFixtures().add(new FactData("Person", "f2", Arrays.<Field>asList(new FieldData("name", "michael"), new FieldData("age", "33")), false));
    // test all true
    VerifyFact verifyCheddar = new VerifyFact();
    verifyCheddar.setName("f1");
    verifyCheddar.setFieldValues(asList(new VerifyField("type", "cheddar", "=="), new VerifyField("price", "42", "==")));
    scenario.getFixtures().add(verifyCheddar);
    VerifyFact michaelVerifyFact = new VerifyFact();
    michaelVerifyFact.setName("f2");
    michaelVerifyFact.setFieldValues(asList(new VerifyField("name", "michael", "=="), new VerifyField("age", "33", "==")));
    scenario.getFixtures().add(michaelVerifyFact);
    // test one false
    VerifyFact markVerifyFact = new VerifyFact();
    markVerifyFact.setName("f2");
    markVerifyFact.setFieldValues(asList(new VerifyField("name", "mark", "=="), new VerifyField("age", "33", "==")));
    scenario.getFixtures().add(markVerifyFact);
    // test 2 false
    VerifyFact mark2VerifyFact = new VerifyFact();
    mark2VerifyFact.setName("f2");
    mark2VerifyFact.setFieldValues(asList(new VerifyField("name", "mark", "=="), new VerifyField("age", "32", "==")));
    scenario.getFixtures().add(mark2VerifyFact);
    runner.run(scenario);
    for (VerifyField verifyField : verifyCheddar.getFieldValues()) {
        assertTrue(verifyField.getSuccessResult());
    }
    for (VerifyField verifyField : michaelVerifyFact.getFieldValues()) {
        assertTrue(verifyField.getSuccessResult());
    }
    assertFalse((markVerifyFact.getFieldValues().get(0)).getSuccessResult());
    assertTrue((markVerifyFact.getFieldValues().get(1)).getSuccessResult());
    assertEquals("michael", markVerifyFact.getFieldValues().get(0).getActualResult());
    assertEquals("mark", markVerifyFact.getFieldValues().get(0).getExpected());
    assertFalse((mark2VerifyFact.getFieldValues().get(0)).getSuccessResult());
    assertFalse((mark2VerifyFact.getFieldValues().get(1)).getSuccessResult());
    assertEquals("michael", mark2VerifyFact.getFieldValues().get(0).getActualResult());
    assertEquals("mark", mark2VerifyFact.getFieldValues().get(0).getExpected());
    assertEquals("33", mark2VerifyFact.getFieldValues().get(1).getActualResult());
    assertEquals("32", mark2VerifyFact.getFieldValues().get(1).getExpected());
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) Field(org.drools.workbench.models.testscenarios.shared.Field) Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 9 with Import

use of org.kie.soup.project.datamodel.imports.Import in project drools by kiegroup.

the class GuidedScoreCardDRLPersistenceTest method testModelWithImportsAndFactNameDuplicatingExplicitImport.

@Test
public void testModelWithImportsAndFactNameDuplicatingExplicitImport() {
    final ScoreCardModel model = new ScoreCardModel();
    model.setName("test");
    model.setPackageName("org.drools.workbench.models.guided.scorecard.backend");
    model.getImports().addImport(new Import("org.smurf.Pupa"));
    model.setFactName("org.smurf.Pupa");
    final String drl = GuidedScoreCardDRLPersistence.marshal(model);
    assertNotNull(drl);
    assertTrue(drl.contains("package org.drools.workbench.models.guided.scorecard.backend"));
    assertEquals(12, StringUtil.countMatches(drl, "rule \""));
    assertEquals(3, StringUtil.countMatches(drl, "import "));
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) ScoreCardModel(org.drools.workbench.models.guided.scorecard.shared.ScoreCardModel) Test(org.junit.Test)

Example 10 with Import

use of org.kie.soup.project.datamodel.imports.Import in project drools by kiegroup.

the class GuidedScoreCardDRLPersistence method createPMMLDocument.

private static PMML createPMMLDocument(final ScoreCardModel model) {
    final Scorecard pmmlScorecard = ScorecardPMMLUtils.createScorecard();
    final Output output = new Output();
    final Characteristics characteristics = new Characteristics();
    final MiningSchema miningSchema = new MiningSchema();
    Extension extension = new Extension();
    extension.setName(PMMLExtensionNames.EXTERNAL_CLASS);
    extension.setValue(model.getFactName());
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    String agendaGroup = model.getAgendaGroup();
    if (!StringUtils.isEmpty(agendaGroup)) {
        extension = new Extension();
        extension.setName(PMMLExtensionNames.AGENDA_GROUP);
        extension.setValue(agendaGroup);
        pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    }
    String ruleFlowGroup = model.getRuleFlowGroup();
    if (!StringUtils.isEmpty(ruleFlowGroup)) {
        extension = new Extension();
        extension.setName(PMMLExtensionNames.RULEFLOW_GROUP);
        extension.setValue(agendaGroup);
        pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    }
    extension = new Extension();
    extension.setName(PMMLExtensionNames.MODEL_IMPORTS);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    List<String> imports = new ArrayList<String>();
    StringBuilder importBuilder = new StringBuilder();
    for (Import imp : model.getImports().getImports()) {
        if (!imports.contains(imp.getType())) {
            imports.add(imp.getType());
            importBuilder.append(imp.getType()).append(",");
        }
    }
    extension.setValue(importBuilder.toString());
    extension = new Extension();
    extension.setName(ScorecardPMMLExtensionNames.SCORECARD_RESULTANT_SCORE_FIELD);
    extension.setValue(model.getFieldName());
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    extension = new Extension();
    extension.setName(PMMLExtensionNames.MODEL_PACKAGE);
    String pkgName = model.getPackageName();
    extension.setValue(!(pkgName == null || pkgName.isEmpty()) ? pkgName : null);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(extension);
    final String modelName = convertToJavaIdentifier(model.getName());
    pmmlScorecard.setModelName(modelName);
    pmmlScorecard.setInitialScore(model.getInitialScore());
    pmmlScorecard.setUseReasonCodes(model.isUseReasonCodes());
    if (model.isUseReasonCodes()) {
        pmmlScorecard.setBaselineScore(model.getBaselineScore());
        pmmlScorecard.setReasonCodeAlgorithm(model.getReasonCodesAlgorithm());
    }
    for (final org.drools.workbench.models.guided.scorecard.shared.Characteristic characteristic : model.getCharacteristics()) {
        final Characteristic _characteristic = new Characteristic();
        characteristics.getCharacteristics().add(_characteristic);
        extension = new Extension();
        extension.setName(PMMLExtensionNames.EXTERNAL_CLASS);
        extension.setValue(characteristic.getFact());
        _characteristic.getExtensions().add(extension);
        extension = new Extension();
        extension.setName(ScorecardPMMLExtensionNames.CHARACTERTISTIC_DATATYPE);
        if ("string".equalsIgnoreCase(characteristic.getDataType())) {
            extension.setValue(XLSKeywords.DATATYPE_TEXT);
        } else if ("int".equalsIgnoreCase(characteristic.getDataType()) || "double".equalsIgnoreCase(characteristic.getDataType())) {
            extension.setValue(XLSKeywords.DATATYPE_NUMBER);
        } else if ("boolean".equalsIgnoreCase(characteristic.getDataType())) {
            extension.setValue(XLSKeywords.DATATYPE_BOOLEAN);
        } else {
            System.out.println(">>>> Found unknown data type :: " + characteristic.getDataType());
        }
        _characteristic.getExtensions().add(extension);
        _characteristic.setBaselineScore(characteristic.getBaselineScore());
        if (model.isUseReasonCodes()) {
            _characteristic.setReasonCode(characteristic.getReasonCode());
        }
        _characteristic.setName(characteristic.getName());
        final MiningField miningField = new MiningField();
        miningField.setName(characteristic.getField());
        miningField.setUsageType(FIELDUSAGETYPE.ACTIVE);
        miningField.setInvalidValueTreatment(INVALIDVALUETREATMENTMETHOD.RETURN_INVALID);
        miningSchema.getMiningFields().add(miningField);
        extension = new Extension();
        extension.setName(PMMLExtensionNames.EXTERNAL_CLASS);
        extension.setValue(characteristic.getFact());
        miningField.getExtensions().add(extension);
        for (final org.drools.workbench.models.guided.scorecard.shared.Attribute attribute : characteristic.getAttributes()) {
            final Attribute _attribute = new Attribute();
            _characteristic.getAttributes().add(_attribute);
            extension = new Extension();
            extension.setName(ScorecardPMMLExtensionNames.CHARACTERTISTIC_FIELD);
            extension.setValue(characteristic.getField());
            _attribute.getExtensions().add(extension);
            if (model.isUseReasonCodes()) {
                _attribute.setReasonCode(attribute.getReasonCode());
            }
            _attribute.setPartialScore(attribute.getPartialScore());
            final String operator = attribute.getOperator();
            final String dataType = characteristic.getDataType();
            String predicateResolver;
            if ("boolean".equalsIgnoreCase(dataType)) {
                predicateResolver = operator.toUpperCase();
            } else if ("String".equalsIgnoreCase(dataType)) {
                if (operator.contains("=")) {
                    predicateResolver = operator + attribute.getValue();
                } else {
                    predicateResolver = attribute.getValue() + ",";
                }
            } else {
                if (NUMERIC_OPERATORS.contains(operator)) {
                    predicateResolver = operator + " " + attribute.getValue();
                } else {
                    predicateResolver = attribute.getValue().replace(",", "-");
                }
            }
            extension = new Extension();
            extension.setName("predicateResolver");
            extension.setValue(predicateResolver);
            _attribute.getExtensions().add(extension);
        }
    }
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(miningSchema);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(output);
    pmmlScorecard.getExtensionsAndCharacteristicsAndMiningSchemas().add(characteristics);
    return new ScorecardPMMLGenerator().generateDocument(pmmlScorecard);
}
Also used : MiningField(org.dmg.pmml.pmml_4_2.descr.MiningField) Import(org.kie.soup.project.datamodel.imports.Import) Attribute(org.dmg.pmml.pmml_4_2.descr.Attribute) Characteristic(org.dmg.pmml.pmml_4_2.descr.Characteristic) ArrayList(java.util.ArrayList) Extension(org.dmg.pmml.pmml_4_2.descr.Extension) MiningSchema(org.dmg.pmml.pmml_4_2.descr.MiningSchema) Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) Output(org.dmg.pmml.pmml_4_2.descr.Output) ScorecardPMMLGenerator(org.drools.scorecards.pmml.ScorecardPMMLGenerator) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard)

Aggregations

Import (org.kie.soup.project.datamodel.imports.Import)103 Test (org.junit.Test)79 Imports (org.kie.soup.project.datamodel.imports.Imports)26 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)25 Scenario (org.drools.workbench.models.testscenarios.shared.Scenario)14 ArrayList (java.util.ArrayList)12 Path (org.uberfire.java.nio.file.Path)12 Query (org.apache.lucene.search.Query)11 FactData (org.drools.workbench.models.testscenarios.shared.FactData)11 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)11 BaseIndexingTest (org.kie.workbench.common.services.refactoring.backend.server.BaseIndexingTest)11 SingleTermQueryBuilder (org.kie.workbench.common.services.refactoring.backend.server.query.builder.SingleTermQueryBuilder)11 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)10 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)9 KieSession (org.kie.api.runtime.KieSession)9 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)9 ValueReferenceIndexTerm (org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueReferenceIndexTerm)9 Path (org.uberfire.backend.vfs.Path)9 ProjectImports (org.guvnor.common.services.project.model.ProjectImports)8 Analyzer (org.drools.verifier.core.main.Analyzer)7