Search in sources :

Example 21 with PackageDataModelOracle

use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools-wb by kiegroup.

the class GuidedDecisionTableTest method testValueLists.

@Test
public void testValueLists() {
    final GuidedDecisionTable52 model = new GuidedDecisionTable52();
    final PackageDataModelOracle loader = PackageDataModelOracleBuilder.newPackageOracleBuilder(new RawMVELEvaluator()).addEnum("Driver", "name", new String[] { "bob", "michael" }).addEnum("Person", "rating", new String[] { "1", "2" }).build();
    // Emulate server-to-client conversions
    final AsyncPackageDataModelOracle oracle = getOracle();
    final PackageDataModelOracleBaselinePayload dataModel = new PackageDataModelOracleBaselinePayload();
    dataModel.setModelFields(loader.getModuleModelFields());
    dataModel.setWorkbenchEnumDefinitions(loader.getPackageWorkbenchDefinitions());
    populateDataModelOracle(mock(Path.class), model, oracle, dataModel);
    final ColumnUtilities utils = new ColumnUtilities(model, oracle);
    final Map<String, String> currentValueMap = new HashMap<>();
    // add cols for LHS
    final ConditionCol52 c1 = new ConditionCol52();
    final Pattern52 p1 = new Pattern52();
    p1.setBoundName("c1");
    p1.setFactType("Driver");
    c1.setFactField("name");
    c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    p1.getChildColumns().add(c1);
    model.getConditions().add(p1);
    final ConditionCol52 c1_ = new ConditionCol52();
    final Pattern52 p1_ = new Pattern52();
    p1_.setBoundName("c1");
    p1_.setFactType("Driver");
    c1_.setFactField("name");
    p1_.getChildColumns().add(c1_);
    c1_.setConstraintValueType(BaseSingleFieldConstraint.TYPE_RET_VALUE);
    model.getConditions().add(p1_);
    final ConditionCol52 c1__ = new ConditionCol52();
    c1__.setFactField("sex");
    p1_.getChildColumns().add(c1__);
    c1__.setConstraintValueType(BaseSingleFieldConstraint.TYPE_RET_VALUE);
    c1__.setValueList("Male,Female");
    model.getConditions().add(p1_);
    final ConditionCol52 c1___ = new ConditionCol52();
    final Pattern52 p1__ = new Pattern52();
    p1__.setBoundName("c1");
    p1__.setFactType("Driver");
    c1___.setFactField("name");
    c1___.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    c1___.setValueList("one,two,three");
    p1__.getChildColumns().add(c1___);
    model.getConditions().add(p1__);
    final ConditionCol52 c2 = new ConditionCol52();
    final Pattern52 p2 = new Pattern52();
    p2.setBoundName("c2");
    p2.setFactType("Driver");
    c2.setFactField("nothing");
    c2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    p2.getChildColumns().add(c2);
    model.getConditions().add(p2);
    final ActionSetFieldCol52 asf = new ActionSetFieldCol52();
    asf.setBoundName("c1");
    asf.setFactField("name");
    model.getActionCols().add(asf);
    final ActionInsertFactCol52 ins = new ActionInsertFactCol52();
    ins.setBoundName("x");
    ins.setFactField("rating");
    ins.setFactType("Person");
    model.getActionCols().add(ins);
    final ActionInsertFactCol52 ins_ = new ActionInsertFactCol52();
    ins_.setBoundName("x");
    ins_.setFactField("rating");
    ins_.setFactType("Person");
    ins_.setValueList("one,two,three");
    model.getActionCols().add(ins_);
    final ActionSetFieldCol52 asf_ = new ActionSetFieldCol52();
    asf_.setBoundName("c1");
    asf_.setFactField("goo");
    model.getActionCols().add(asf_);
    final ActionSetFieldCol52 asf__ = new ActionSetFieldCol52();
    asf__.setBoundName("c1");
    asf__.setType("String");
    asf__.setFactField("goo");
    asf__.setValueList("one,two,three");
    model.getActionCols().add(asf__);
    assertTrue(oracle.hasEnums(p1.getFactType(), c1.getFactField()));
    assertFalse(utils.hasValueList(c1));
    String[] r = oracle.getEnums(p1.getFactType(), c1.getFactField(), currentValueMap).getFixedList();
    assertEquals(2, r.length);
    assertEquals("bob", r[0]);
    assertEquals("michael", r[1]);
    assertTrue(oracle.hasEnums(p1_.getFactType(), c1_.getFactField()));
    assertFalse(utils.hasValueList(c1_));
    r = oracle.getEnums(p1_.getFactType(), c1_.getFactField(), currentValueMap).getFixedList();
    assertEquals(2, r.length);
    assertEquals("bob", r[0]);
    assertEquals("michael", r[1]);
    assertFalse(oracle.hasEnums(p1_.getFactType(), c1__.getFactField()));
    assertTrue(utils.hasValueList(c1__));
    r = utils.getValueList(c1__);
    assertEquals(2, r.length);
    assertEquals("Male", r[0]);
    assertEquals("Female", r[1]);
    assertTrue(oracle.hasEnums(p1__.getFactType(), c1___.getFactField()));
    assertTrue(utils.hasValueList(c1___));
    r = utils.getValueList(c1___);
    assertEquals(3, r.length);
    assertEquals("one", r[0]);
    assertEquals("two", r[1]);
    assertEquals("three", r[2]);
    assertEquals(0, utils.getValueList(c2).length);
    assertTrue(oracle.hasEnums(p1.getFactType(), asf.getFactField()));
    assertFalse(utils.hasValueList(asf));
    r = oracle.getEnums(p1.getFactType(), asf.getFactField(), currentValueMap).getFixedList();
    assertEquals(2, r.length);
    assertEquals("bob", r[0]);
    assertEquals("michael", r[1]);
    assertTrue(oracle.hasEnums(ins.getFactType(), ins.getFactField()));
    assertFalse(utils.hasValueList(ins));
    r = oracle.getEnums(ins.getFactType(), ins.getFactField(), currentValueMap).getFixedList();
    assertEquals(2, r.length);
    assertEquals("1", r[0]);
    assertEquals("2", r[1]);
    assertTrue(oracle.hasEnums(ins_.getFactType(), ins_.getFactField()));
    assertTrue(utils.hasValueList(ins_));
    r = utils.getValueList(ins_);
    assertEquals(3, r.length);
    assertEquals("one", r[0]);
    assertEquals("two", r[1]);
    assertEquals("three", r[2]);
    assertEquals(0, utils.getValueList(asf_).length);
    assertFalse(oracle.hasEnums(p1.getFactType(), asf__.getFactField()));
    assertTrue(utils.hasValueList(asf__));
    r = utils.getValueList(asf__);
    assertEquals(3, r.length);
    assertEquals("one", r[0]);
    assertEquals("two", r[1]);
    assertEquals("three", r[2]);
    AttributeCol52 at = new AttributeCol52();
    at.setAttribute("no-loop");
    model.getAttributeCols().add(at);
    r = utils.getValueList(at);
    assertEquals(2, r.length);
    assertEquals("true", r[0]);
    assertEquals("false", r[1]);
    at.setAttribute("enabled");
    assertEquals(2, utils.getValueList(at).length);
    at.setAttribute("salience");
    assertEquals(0, utils.getValueList(at).length);
}
Also used : AsyncPackageDataModelOracle(org.kie.workbench.common.widgets.client.datamodel.AsyncPackageDataModelOracle) Path(org.uberfire.backend.vfs.Path) ColumnUtilities(org.drools.workbench.screens.guided.dtable.client.widget.table.utilities.ColumnUtilities) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) HashMap(java.util.HashMap) ActionSetFieldCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionSetFieldCol52) AttributeCol52(org.drools.workbench.models.guided.dtable.shared.model.AttributeCol52) ConditionCol52(org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) RawMVELEvaluator(org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) AsyncPackageDataModelOracle(org.kie.workbench.common.widgets.client.datamodel.AsyncPackageDataModelOracle) Test(org.junit.Test)

Example 22 with PackageDataModelOracle

use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools-wb by kiegroup.

the class GuidedRuleEditorServiceImpl method constructContent.

@Override
protected GuidedEditorContent constructContent(Path path, Overview overview) {
    final RuleModel model = load(path);
    final PackageDataModelOracle oracle = dataModelService.getDataModel(path);
    final PackageDataModelOracleBaselinePayload dataModel = new PackageDataModelOracleBaselinePayload();
    // Get FQCN's used by model
    final GuidedRuleModelVisitor visitor = new GuidedRuleModelVisitor(model);
    final Set<String> consumedFQCNs = visitor.getConsumedModelClasses();
    // Get FQCN's used by Globals
    consumedFQCNs.addAll(oracle.getPackageGlobals().values());
    // Get FQCN's of collections defined in project settings
    // they can be used in From Collect expressions
    consumedFQCNs.addAll(oracle.getModuleCollectionTypes().entrySet().stream().filter(entry -> entry.getValue()).map(entry -> entry.getKey()).collect(Collectors.toSet()));
    DataModelOracleUtilities.populateDataModel(oracle, dataModel, consumedFQCNs);
    // Signal opening to interested parties
    resourceOpenedEvent.fire(new ResourceOpenedEvent(path, safeSessionInfo));
    return new GuidedEditorContent(model, overview, dataModel);
}
Also used : PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) GuidedEditorContent(org.drools.workbench.screens.guided.rule.model.GuidedEditorContent) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ValidationMessage(org.guvnor.common.services.shared.validation.model.ValidationMessage) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) Metadata(org.guvnor.common.services.shared.metadata.model.Metadata) RuleModelDRLPersistenceImpl(org.drools.workbench.models.commons.backend.rule.RuleModelDRLPersistenceImpl) ArrayList(java.util.ArrayList) Package(org.guvnor.common.services.project.model.Package) Paths(org.uberfire.backend.server.util.Paths) Inject(javax.inject.Inject) ExceptionUtilities(org.guvnor.common.services.backend.exceptions.ExceptionUtilities) CommentedOptionFactory(org.guvnor.common.services.backend.util.CommentedOptionFactory) Overview(org.guvnor.common.services.shared.metadata.model.Overview) Service(org.jboss.errai.bus.server.annotations.Service) SafeSessionInfo(org.guvnor.common.services.backend.config.SafeSessionInfo) SessionInfo(org.uberfire.rpc.SessionInfo) GuidedRuleDSLRResourceTypeDefinition(org.drools.workbench.screens.guided.rule.type.GuidedRuleDSLRResourceTypeDefinition) Named(javax.inject.Named) KieService(org.kie.workbench.common.services.backend.service.KieService) RenameService(org.uberfire.ext.editor.commons.service.RenameService) Instance(javax.enterprise.inject.Instance) GuidedRuleDRLResourceTypeDefinition(org.drools.workbench.screens.guided.rule.type.GuidedRuleDRLResourceTypeDefinition) Path(org.uberfire.backend.vfs.Path) DeleteService(org.uberfire.ext.editor.commons.service.DeleteService) Event(javax.enterprise.event.Event) DataModelService(org.kie.workbench.common.services.datamodel.backend.server.service.DataModelService) SaveAndRenameServiceImpl(org.uberfire.ext.editor.commons.backend.service.SaveAndRenameServiceImpl) RuleModelIActionPersistenceExtension(org.drools.workbench.models.commons.backend.rule.RuleModelIActionPersistenceExtension) Collection(java.util.Collection) DataModelOracleUtilities(org.kie.workbench.common.services.datamodel.backend.server.DataModelOracleUtilities) Set(java.util.Set) Import(org.kie.soup.project.datamodel.imports.Import) Collectors(java.util.stream.Collectors) ResourceOpenedEvent(org.uberfire.workbench.events.ResourceOpenedEvent) GenericValidator(org.guvnor.common.services.backend.validation.GenericValidator) List(java.util.List) GuidedRuleEditorService(org.drools.workbench.screens.guided.rule.service.GuidedRuleEditorService) PostConstruct(javax.annotation.PostConstruct) IOService(org.uberfire.io.IOService) ApplicationScoped(javax.enterprise.context.ApplicationScoped) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException) CopyService(org.uberfire.ext.editor.commons.service.CopyService) PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) GuidedEditorContent(org.drools.workbench.screens.guided.rule.model.GuidedEditorContent) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) ResourceOpenedEvent(org.uberfire.workbench.events.ResourceOpenedEvent) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel)

Example 23 with PackageDataModelOracle

use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools-wb by kiegroup.

the class GuidedScoreCardEditorServiceImpl method constructContent.

@Override
protected ScoreCardModelContent constructContent(Path path, Overview overview) {
    final ScoreCardModel model = load(path);
    final PackageDataModelOracle oracle = dataModelService.getDataModel(path);
    final PackageDataModelOracleBaselinePayload dataModel = new PackageDataModelOracleBaselinePayload();
    final GuidedScoreCardModelVisitor visitor = new GuidedScoreCardModelVisitor(model);
    DataModelOracleUtilities.populateDataModel(oracle, dataModel, visitor.getConsumedModelClasses());
    // Signal opening to interested parties
    resourceOpenedEvent.fire(new ResourceOpenedEvent(path, safeSessionInfo));
    return new ScoreCardModelContent(model, overview, dataModel);
}
Also used : ScoreCardModelContent(org.drools.workbench.screens.guided.scorecard.model.ScoreCardModelContent) ScoreCardModel(org.drools.workbench.models.guided.scorecard.shared.ScoreCardModel) PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) ResourceOpenedEvent(org.uberfire.workbench.events.ResourceOpenedEvent)

Example 24 with PackageDataModelOracle

use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools-wb by kiegroup.

the class GuidedRuleTemplateEditorServiceImplTest method checkConstructContentPopulateProjectCollectionTypes.

@Test
public void checkConstructContentPopulateProjectCollectionTypes() {
    final Path path = mock(Path.class);
    final Overview overview = mock(Overview.class);
    final PackageDataModelOracle oracle = mock(PackageDataModelOracle.class);
    when(path.toURI()).thenReturn("default://project/src/main/resources/mypackage/rule.template");
    when(dataModelService.getDataModel(any())).thenReturn(oracle);
    when(oracle.getPackageGlobals()).thenReturn(new HashMap<String, String>() {

        {
            put("number", "java.lang.Number");
            put("collection", "java.util.Collection");
        }
    });
    when(oracle.getModuleCollectionTypes()).thenReturn(new HashMap<String, Boolean>() {

        {
            put("java.util.List", true);
            put("java.util.Set", true);
            put("java.util.Collection", true);
            put("java.util.UnknownCollection", false);
        }
    });
    final GuidedTemplateEditorContent content = service.constructContent(path, overview);
    assertEquals(3, content.getDataModel().getCollectionTypes().size());
    assertTrue(content.getDataModel().getCollectionTypes().containsKey("java.util.Collection"));
    assertTrue(content.getDataModel().getCollectionTypes().containsKey("java.util.List"));
    assertTrue(content.getDataModel().getCollectionTypes().containsKey("java.util.Set"));
}
Also used : Path(org.uberfire.backend.vfs.Path) GuidedTemplateEditorContent(org.drools.workbench.screens.guided.template.model.GuidedTemplateEditorContent) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) Overview(org.guvnor.common.services.shared.metadata.model.Overview) Test(org.junit.Test)

Example 25 with PackageDataModelOracle

use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testActionSetFieldValueWithDSL.

@Test
public void testActionSetFieldValueWithDSL() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        final String dsl = "There is an Applicant";
        final String dslDefinition = "[when]" + dsl + "=$a : Applicant();";
        final RuleModel m = new RuleModel();
        m.name = "r0";
        final DSLSentence sen = new DSLSentence();
        sen.setDefinition(dsl);
        m.addLhsItem(sen);
        final ActionInsertFact aif = new ActionInsertFact("Applicant");
        aif.addFieldValue(new ActionFieldValue("applicationDate", "13-Mar-2017", DataType.TYPE_DATE));
        m.addRhsItem(aif);
        final String expected = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "then\n" + ">java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + ">Applicant fact0 = new Applicant();\n" + ">fact0.setApplicationDate( sdf.parse(\"13-Mar-2017\") );\n" + ">insert( fact0 );\n" + "end";
        final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
        assertEqualsIgnoreWhitespace(expected, actual);
        PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
        final RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(actual, Collections.emptyList(), dmo, dslDefinition);
        assertNotNull(m2);
        // LHS
        assertEquals(1, m.lhs.length);
        DSLSentence dslSentence = (DSLSentence) m.lhs[0];
        assertEquals(dsl, dslSentence.getDefinition());
        assertEquals(0, dslSentence.getValues().size());
        // RHS
        assertEquals(1, m2.rhs.length);
        assertTrue(m2.rhs[0] instanceof ActionInsertFact);
        final ActionInsertFact a = (ActionInsertFact) m2.rhs[0];
        assertEquals("Applicant", a.getFactType());
        assertEquals(1, a.getFieldValues().length);
        final ActionFieldValue afv = a.getFieldValues()[0];
        assertEquals("applicationDate", afv.getField());
        assertEquals("13-Mar-2017", afv.getValue());
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Aggregations

PackageDataModelOracle (org.kie.soup.project.datamodel.oracle.PackageDataModelOracle)40 Test (org.junit.Test)24 PackageDataModelOracleBaselinePayload (org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload)19 Path (org.uberfire.backend.vfs.Path)18 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)12 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)12 RawMVELEvaluator (org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator)12 ConditionCol52 (org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52)11 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)11 AsyncPackageDataModelOracle (org.kie.workbench.common.widgets.client.datamodel.AsyncPackageDataModelOracle)11 ModuleDataModelOracle (org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle)10 LimitedEntryConditionCol52 (org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryConditionCol52)9 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)9 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)8 ResourceOpenedEvent (org.uberfire.workbench.events.ResourceOpenedEvent)8 ActionInsertFactCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52)7 ActionSetFieldCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionSetFieldCol52)7 DTCellValue52 (org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52)7 LimitedEntryActionInsertFactCol52 (org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryActionInsertFactCol52)6 LimitedEntryActionSetFieldCol52 (org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryActionSetFieldCol52)6