use of org.kie.dmn.model.api.Definitions in project tck by dmn-tck.
the class DroolsTCKTest method beforeTestCases.
@Override
public void beforeTestCases(TestSuiteContext context, TestCases testCases, URL modelURL, Collection<? extends URL> additionalModels) {
logger.info("Creating runtime for model: {} and additional models {}\n", modelURL, additionalModels);
DroolsContext ctx = (DroolsContext) context;
ctx.runtime = createRuntime(modelURL, additionalModels);
if (ctx.runtime.getModels().isEmpty()) {
throw new RuntimeException("Unable to load model for URL '" + modelURL + "'");
}
try {
Definitions mainModelXML = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(new FileReader(modelURL.getFile()));
ctx.dmnmodel = ctx.runtime.getModel(mainModelXML.getNamespace(), mainModelXML.getName());
} catch (FileNotFoundException e) {
throw new RuntimeException("Unable to locate file for model for URL '" + modelURL + "'");
}
}
use of org.kie.dmn.model.api.Definitions in project kie-wb-common by kiegroup.
the class ImportConverter method createWBImport.
private static Import createWBImport(final org.kie.dmn.model.api.Import dmn, final Definitions definitions, final PMMLDocumentMetadata pmmlDocument) {
final LocationURI locationURI = new LocationURI(dmn.getLocationURI());
if (Objects.equals(DMNImportTypes.DMN, determineImportType(dmn.getImportType()))) {
final ImportDMN result = new ImportDMN(dmn.getNamespace(), locationURI, dmn.getImportType());
result.setDrgElementsCount(countDefinitionElement(definitions, () -> d -> d.getDrgElement().size()));
result.setItemDefinitionsCount(countDefinitionElement(definitions, () -> d -> d.getItemDefinition().size()));
return result;
} else if (Objects.equals(DMNImportTypes.PMML, determineImportType(dmn.getImportType()))) {
final ImportPMML result = new ImportPMML(dmn.getNamespace(), locationURI, dmn.getImportType());
result.setModelCount(pmmlDocument.getModels().size());
return result;
} else {
return new Import(dmn.getNamespace(), locationURI, dmn.getImportType());
}
}
use of org.kie.dmn.model.api.Definitions in project kie-wb-common by kiegroup.
the class DMNMarshallerImportsHelperStandaloneImpl method getImportXML.
@Override
public Map<Import, String> getImportXML(final Metadata metadata, final List<Import> imports) {
final Map<Import, String> importXML = new HashMap<>();
if (imports.size() > 0) {
for (final String xml : getOtherDMNDiagramsXML(metadata)) {
try (final StringReader sr = toStringReader(xml)) {
final Definitions definitions = marshaller.unmarshal(sr);
findImportByDefinitions(definitions, imports).ifPresent(anImport -> {
importXML.put(anImport, xml);
});
}
}
}
return importXML;
}
use of org.kie.dmn.model.api.Definitions in project kie-wb-common by kiegroup.
the class DMNMarshallerStandaloneTest method test_diamond.
@Test
public void test_diamond() throws IOException {
// round trip test
roundTripUnmarshalThenMarshalUnmarshal(this.getClass().getResourceAsStream("/diamondDMN12.dmn"), this::checkDiamondGraph);
// additionally, check the marshalled is still DMN executable as expected
DMNMarshallerStandalone m = getDMNMarshaller();
Graph<?, ?> g = m.unmarshall(createMetadata(), this.getClass().getResourceAsStream("/diamondDMN12.dmn"));
DiagramImpl diagram = createDiagram();
diagram.setGraph(g);
String mString = m.marshall(diagram);
final KieServices ks = KieServices.Factory.get();
final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test_diamond", "1.0"), ks.getResources().newByteArrayResource(mString.getBytes()).setTargetPath("src/main/resources/diamond.dmn"));
final DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
Assert.assertNotNull(runtime);
DMNModel diamondModel = runtime.getModel("http://www.trisotech.com/definitions/_8afa6c24-55c8-43cf-8a02-fdde7fc5d1f2", "three decisions in a diamond shape");
DMNContext dmnContext = runtime.newContext();
dmnContext.set("My Name", "John Doe");
DMNResult dmnResult = runtime.evaluateAll(diamondModel, dmnContext);
assertFalse(dmnResult.getMessages().toString(), dmnResult.hasErrors());
DMNContext result = dmnResult.getContext();
assertEquals("Hello, John Doe.", result.get("My Decision"));
// additionally, check DMN DD/DI
org.kie.dmn.api.marshalling.DMNMarshaller dmnMarshaller = DMNMarshallerFactory.newDefaultMarshaller();
Definitions definitions = dmnMarshaller.unmarshal(mString);
org.kie.dmn.model.api.dmndi.DMNDiagram ddRoot = (org.kie.dmn.model.api.dmndi.DMNDiagram) definitions.getDMNDI().getDMNDiagram().get(0);
DMNShape myname = findShapeByDMNI(ddRoot, "_4cd17e52-6253-41d6-820d-5824bf5197f3");
assertBounds(500, 500, 100, 50, myname.getBounds());
assertColor(255, 255, 255, ((DMNStyle) myname.getStyle()).getFillColor());
assertColor(0, 0, 0, ((DMNStyle) myname.getStyle()).getStrokeColor());
assertDMNStyle("Open Sans", 24, 255, 0, 0, (DMNStyle) myname.getStyle());
DMNShape prefix = findShapeByDMNI(ddRoot, "_e920f38a-293c-41b8-adb3-69d0dc184fab");
assertBounds(300, 400, 100, 50, prefix.getBounds());
assertColor(0, 253, 25, ((DMNStyle) prefix.getStyle()).getFillColor());
assertColor(253, 0, 0, ((DMNStyle) prefix.getStyle()).getStrokeColor());
assertDMNStyle("Times New Roman", 8, 70, 60, 50, (DMNStyle) prefix.getStyle());
DMNShape postfix = findShapeByDMNI(ddRoot, "_f49f9c34-29d5-4e72-91d2-f4f92117c8da");
assertBounds(700, 400, 100, 50, postfix.getBounds());
assertColor(247, 255, 0, ((DMNStyle) postfix.getStyle()).getFillColor());
assertColor(0, 51, 255, ((DMNStyle) postfix.getStyle()).getStrokeColor());
assertDMNStyle("Arial", 10, 50, 60, 70, (DMNStyle) postfix.getStyle());
DMNShape mydecision = findShapeByDMNI(ddRoot, "_9b061fc3-8109-42e2-9fe4-fc39c90b654e");
assertBounds(487.5, 275, 125, 75, mydecision.getBounds());
assertColor(255, 255, 255, ((DMNStyle) mydecision.getStyle()).getFillColor());
assertColor(0, 0, 0, ((DMNStyle) mydecision.getStyle()).getStrokeColor());
assertDMNStyle("Monospaced", 32, 55, 66, 77, (DMNStyle) mydecision.getStyle());
}
use of org.kie.dmn.model.api.Definitions in project kie-wb-common by kiegroup.
the class DMNMarshallerStandaloneTest method roundTripUnmarshalMarshalThenUnmarshalDMNexpectingErrors.
private ErrorsAndDMNModelAsSerialized roundTripUnmarshalMarshalThenUnmarshalDMNexpectingErrors(InputStream dmnXmlInputStream) throws IOException {
String xml = null;
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(dmnXmlInputStream))) {
xml = buffer.lines().collect(Collectors.joining("\n"));
} catch (Exception e) {
throw new RuntimeException("test utility method roundTripUnmarshalMarshalThenUnmarshalDMN failed to read XML content.", e);
}
LOG.debug("ORIGINAL xml:\n{}\n", xml);
final List<Message> messages0 = kieBuilderMessagesUsingDMNXML(xml);
assertTrue("The DMN XML content did NOT result in compilation errors and this test method expected errors to be detected. If this was intentional use test method roundTripUnmarshalMarshalThenUnmarshalDMN", messages0.stream().filter(m -> m.getLevel().equals(Message.Level.ERROR)).count() > 0);
DMNMarshallerStandalone m = getDMNMarshaller();
// first unmarshal from DMN XML to Stunner DMN Graph
@SuppressWarnings("unchecked") Graph<?, Node<?, ?>> g = m.unmarshall(createMetadata(), new ReaderInputStream(new StringReader(xml)));
// round trip to Stunner DMN Graph back to DMN XML
DiagramImpl diagram = createDiagram();
diagram.setGraph(g);
String mString = m.marshall(diagram);
LOG.debug("MARSHALLED ROUNDTRIP RESULTING xml:\n{}\n", mString);
// now unmarshal once more, from the marshalled just done above, into a DMNRuntime
final List<Message> result = kieBuilderMessagesUsingDMNXML(mString);
assertTrue("The DMN XML content did NOT result in compilation errors and this test method expected errors to be detected. If this was intentional use test method roundTripUnmarshalMarshalThenUnmarshalDMN", messages0.stream().filter(msg -> msg.getLevel().equals(Message.Level.ERROR)).count() > 0);
Definitions definitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(mString);
return new ErrorsAndDMNModelAsSerialized(result, definitions);
}
Aggregations