Search in sources :

Example 11 with Import

use of org.kie.dmn.model.api.Import in project kie-wb-common by kiegroup.

the class DMNMarshallerImportsHelperStandaloneImplTest method testGetItemDefinitionsWithNamespace.

@Test
public void testGetItemDefinitionsWithNamespace() {
    final Definitions definitions = mock(Definitions.class);
    final Import anImport = mock(Import.class);
    final ItemDefinition itemDefinition1 = new TItemDefinition();
    final ItemDefinition itemDefinition2 = new TItemDefinition();
    final ItemDefinition itemDefinition3 = new TItemDefinition();
    final ItemDefinition itemDefinition4 = new TItemDefinition();
    final ItemDefinition itemDefinition5 = new TItemDefinition();
    final List<ItemDefinition> itemDefinitions = asList(itemDefinition1, itemDefinition2, itemDefinition3, itemDefinition4, itemDefinition5);
    itemDefinition1.setName("tUUID");
    itemDefinition2.setName("tPerson");
    itemDefinition3.setName("id");
    itemDefinition4.setName("name");
    itemDefinition5.setName("age");
    itemDefinition1.setTypeRef(new QName("string"));
    itemDefinition2.setTypeRef(null);
    itemDefinition3.setTypeRef(new QName("tUUID"));
    itemDefinition4.setTypeRef(new QName("string"));
    itemDefinition5.setTypeRef(new QName("number"));
    when(anImport.getName()).thenReturn("model");
    when(definitions.getItemDefinition()).thenReturn(itemDefinitions);
    final List<ItemDefinition> actualItemDefinitions = helper.getItemDefinitionsWithNamespace(definitions, anImport);
    assertEquals(5, actualItemDefinitions.size());
    final ItemDefinition actualItemDefinition1 = actualItemDefinitions.get(0);
    assertEquals("model.tUUID", actualItemDefinition1.getName());
    assertEquals("string", actualItemDefinition1.getTypeRef().getLocalPart());
    final ItemDefinition actualItemDefinition2 = actualItemDefinitions.get(1);
    assertEquals("model.tPerson", actualItemDefinition2.getName());
    assertNull(actualItemDefinition2.getTypeRef());
    final ItemDefinition actualItemDefinition3 = actualItemDefinitions.get(2);
    assertEquals("model.id", actualItemDefinition3.getName());
    assertEquals("model.tUUID", actualItemDefinition3.getTypeRef().getLocalPart());
    final ItemDefinition actualItemDefinition4 = actualItemDefinitions.get(3);
    assertEquals("model.name", actualItemDefinition4.getName());
    assertEquals("string", actualItemDefinition4.getTypeRef().getLocalPart());
    final ItemDefinition actualItemDefinition5 = actualItemDefinitions.get(4);
    assertEquals("model.age", actualItemDefinition5.getName());
    assertEquals("number", actualItemDefinition5.getTypeRef().getLocalPart());
}
Also used : Import(org.kie.dmn.model.api.Import) QName(javax.xml.namespace.QName) Definitions(org.kie.dmn.model.api.Definitions) TItemDefinition(org.kie.dmn.model.v1_2.TItemDefinition) TItemDefinition(org.kie.dmn.model.v1_2.TItemDefinition) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) Test(org.junit.Test)

Example 12 with Import

use of org.kie.dmn.model.api.Import in project kie-wb-common by kiegroup.

the class DMNMarshallerImportsHelperStandaloneImplTest method testGetPMMLDocuments.

@Test
public void testGetPMMLDocuments() {
    final Path dmnModelPath = mock(Path.class);
    final Metadata metadata = mock(Metadata.class);
    final PMMLDocumentMetadata pmmlDocument = mock(PMMLDocumentMetadata.class);
    final Import import1 = mock(Import.class);
    final List<Import> imports = singletonList(import1);
    final Path path1 = mock(Path.class);
    final Path path2 = mock(Path.class);
    final List<Path> paths = asList(path1, path2);
    when(metadata.getPath()).thenReturn(dmnModelPath);
    when(import1.getLocationURI()).thenReturn("document1.pmml");
    when(pathsHelper.getRelativeURI(dmnModelPath, path1)).thenReturn("document1.pmml");
    when(pathsHelper.getRelativeURI(dmnModelPath, path2)).thenReturn("document2.pmml");
    when(pmmlDocumentFactory.getDocumentByPath(path1)).thenReturn(pmmlDocument);
    doReturn(paths).when(helper).getPMMLDocumentPaths(metadata);
    final Map<Import, PMMLDocumentMetadata> importDefinitions = helper.getPMMLDocuments(metadata, imports);
    assertEquals(1, importDefinitions.size());
    assertEquals(pmmlDocument, importDefinitions.get(import1));
}
Also used : Path(org.uberfire.backend.vfs.Path) Import(org.kie.dmn.model.api.Import) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) Test(org.junit.Test)

Example 13 with Import

use of org.kie.dmn.model.api.Import in project kie-wb-common by kiegroup.

the class DMNMarshallerImportsHelperStandaloneImplTest method testGetImportXML.

@Test
public void testGetImportXML() throws java.io.IOException {
    final String xml1 = "<some xml/>";
    final String xml2 = "<some other xml/>";
    final Metadata metadata = mock(Metadata.class);
    final Import import1 = mock(Import.class);
    final Path path1 = makePath("../file1.dmn");
    final Path path2 = makePath("../file2.dmn");
    // Mock loading of XML files in Project
    final InputStream inputStream1 = mock(InputStream.class);
    final InputStream inputStream2 = mock(InputStream.class);
    final InputStreamReader inputStreamReader1 = mock(InputStreamReader.class);
    final InputStreamReader inputStreamReader2 = mock(InputStreamReader.class);
    final List<Path> paths = asList(path1, path2);
    when(pathsHelper.getDMNModelsPaths(any())).thenReturn(paths);
    when(dmnIOHelper.isAsString(inputStream1)).thenReturn(xml1);
    when(dmnIOHelper.isAsString(inputStream2)).thenReturn(xml2);
    doReturn(Optional.of(inputStream1)).when(helper).loadPath(path1);
    doReturn(Optional.of(inputStream2)).when(helper).loadPath(path2);
    doReturn(inputStreamReader1).when(helper).toInputStreamReader(inputStream1);
    doReturn(inputStreamReader2).when(helper).toInputStreamReader(inputStream2);
    // Mock retrieval of Definitions from XML files in Project
    final StringReader stringReader1 = mock(StringReader.class);
    final StringReader stringReader2 = mock(StringReader.class);
    final Definitions definitions1 = mock(Definitions.class);
    final Definitions definitions2 = mock(Definitions.class);
    doReturn(stringReader1).when(helper).toStringReader(xml1);
    doReturn(stringReader2).when(helper).toStringReader(xml2);
    when(marshaller.unmarshal(stringReader1)).thenReturn(definitions1);
    when(marshaller.unmarshal(stringReader2)).thenReturn(definitions2);
    when(import1.getNamespace()).thenReturn("://namespace1");
    when(definitions1.getNamespace()).thenReturn("://namespace1");
    when(definitions2.getNamespace()).thenReturn("://namespace2-not-imported");
    final List<Import> imports = Collections.singletonList(import1);
    final Map<Import, String> importXML = helper.getImportXML(metadata, imports);
    assertEquals(1, importXML.size());
    assertEquals(xml1, importXML.get(import1));
}
Also used : Path(org.uberfire.backend.vfs.Path) Import(org.kie.dmn.model.api.Import) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Definitions(org.kie.dmn.model.api.Definitions) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 14 with Import

use of org.kie.dmn.model.api.Import in project drools by kiegroup.

the class DMNImportPMMLInfo method from.

public static Either<Exception, DMNImportPMMLInfo> from(InputStream is, DMNCompilerConfigurationImpl cc, DMNModelImpl model, Import i) {
    try {
        final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(is);
        PMMLHeaderInfo h = PMMLInfo.pmmlToHeaderInfo(pmml, pmml.getHeader());
        for (DataField df : pmml.getDataDictionary().getDataFields()) {
            String dfName = df.getName().getValue();
            BuiltInType ft = getBuiltInTypeByDataType(df.getDataType());
            List<FEELProfile> helperFEELProfiles = cc.getFeelProfiles();
            DMNFEELHelper feel = new DMNFEELHelper(cc.getRootClassLoader(), helperFEELProfiles);
            List<UnaryTest> av = new ArrayList<>();
            if (df.getValues() != null && !df.getValues().isEmpty() && ft != BuiltInType.UNKNOWN) {
                final BuiltInType feelType = ft;
                String lov = df.getValues().stream().map(Value::getValue).map(o -> feelType == BuiltInType.STRING ? "\"" + o.toString() + "\"" : o.toString()).collect(Collectors.joining(","));
                av = feel.evaluateUnaryTests(lov, Collections.emptyMap());
            } else if (df.getIntervals() != null && !df.getIntervals().isEmpty() && ft != BuiltInType.UNKNOWN) {
                for (Interval interval : df.getIntervals()) {
                    String utString = null;
                    switch(interval.getClosure()) {
                        case CLOSED_CLOSED:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case CLOSED_OPEN:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                        case OPEN_CLOSED:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case OPEN_OPEN:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                    }
                    List<UnaryTest> ut = feel.evaluateUnaryTests(utString, Collections.emptyMap());
                    av.addAll(ut);
                }
            }
            DMNType type = new SimpleTypeImpl(i.getNamespace(), dfName, null, false, av, model.getTypeRegistry().resolveType(model.getDefinitions().getURIFEEL(), ft.getName()), ft);
            model.getTypeRegistry().registerType(type);
        }
        pmml.getModels().stream().forEach(m -> registerOutputFieldType(m, model, i));
        List<DMNPMMLModelInfo> models = pmml.getModels().stream().map(m -> PMMLInfo.pmmlToModelInfo(m)).map(proto -> DMNPMMLModelInfo.from(proto, model, i)).collect(Collectors.toList());
        DMNImportPMMLInfo info = new DMNImportPMMLInfo(i, models, h);
        return Either.ofRight(info);
    } catch (Throwable e) {
        return Either.ofLeft(new Exception("Unable to process DMNImportPMMLInfo", e));
    }
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) Interval(org.dmg.pmml.Interval) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) LoggerFactory(org.slf4j.LoggerFactory) Either(org.kie.dmn.feel.util.Either) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) Map(java.util.Map) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) PMML(org.dmg.pmml.PMML) DMNCompilerConfigurationImpl(org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl) Logger(org.slf4j.Logger) Collection(java.util.Collection) DataType(org.dmg.pmml.DataType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Import(org.kie.dmn.model.api.Import) Collectors(java.util.stream.Collectors) DataField(org.dmg.pmml.DataField) List(java.util.List) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Collections(java.util.Collections) InputStream(java.io.InputStream) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) ArrayList(java.util.ArrayList) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DataField(org.dmg.pmml.DataField) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) PMML(org.dmg.pmml.PMML) ArrayList(java.util.ArrayList) List(java.util.List) Interval(org.dmg.pmml.Interval) DMNType(org.kie.dmn.api.core.DMNType)

Example 15 with Import

use of org.kie.dmn.model.api.Import in project drools by kiegroup.

the class ImportDMNResolverUtilTest method testLocateInNSnoModelNameWithAlias2.

@Test
public void testLocateInNSnoModelNameWithAlias2() {
    final Import i = makeImport("nsA", "boh", null);
    final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3"));
    final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity());
    assertTrue(result.isLeft());
}
Also used : TImport(org.kie.dmn.model.v1_1.TImport) Import(org.kie.dmn.model.api.Import) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Aggregations

Import (org.kie.dmn.model.api.Import)42 Test (org.junit.Test)22 QName (javax.xml.namespace.QName)19 TImport (org.kie.dmn.model.v1_1.TImport)15 Definitions (org.kie.dmn.model.api.Definitions)13 HashMap (java.util.HashMap)7 DRGElement (org.kie.dmn.model.api.DRGElement)7 ItemDefinition (org.kie.dmn.model.api.ItemDefinition)6 ArrayList (java.util.ArrayList)5 BusinessKnowledgeModel (org.kie.dmn.model.api.BusinessKnowledgeModel)5 Decision (org.kie.dmn.model.api.Decision)5 PMMLDocumentMetadata (org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata)5 Artifact (org.kie.dmn.model.api.Artifact)4 Association (org.kie.dmn.model.api.Association)4 BusinessContextElement (org.kie.dmn.model.api.BusinessContextElement)4 ElementCollection (org.kie.dmn.model.api.ElementCollection)4 InputData (org.kie.dmn.model.api.InputData)4 KnowledgeSource (org.kie.dmn.model.api.KnowledgeSource)4 OrganizationUnit (org.kie.dmn.model.api.OrganizationUnit)4 PerformanceIndicator (org.kie.dmn.model.api.PerformanceIndicator)4