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());
}
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));
}
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));
}
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));
}
}
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());
}
Aggregations