use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class DefinitionsConverter method assignChildElement.
@Override
protected void assignChildElement(Object parent, String nodeName, Object child) {
Definitions def = (Definitions) parent;
if (IMPORT.equals(nodeName)) {
def.getImport().add((Import) child);
} else if (ITEM_DEFINITION.equals(nodeName)) {
def.getItemDefinition().add((ItemDefinition) child);
} else if (child instanceof DRGElement) {
def.getDrgElement().add((DRGElement) child);
} else if (child instanceof Artifact) {
def.getArtifact().add((Artifact) child);
} else if (ELEMENT_COLLECTION.equals(nodeName)) {
def.getElementCollection().add((ElementCollection) child);
} else if (child instanceof BusinessContextElement) {
def.getBusinessContextElement().add((BusinessContextElement) child);
} else if (child instanceof DMNDI) {
DMNDI dmndi = (DMNDI) child;
dmndi.normalize();
def.setDMNDI(dmndi);
} else {
super.assignChildElement(def, nodeName, child);
}
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class DefinitionsConverter method writeAttributes.
@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
Definitions def = (Definitions) parent;
if (def.getExpressionLanguage() != null)
writer.addAttribute(EXPRESSION_LANGUAGE, def.getExpressionLanguage());
if (def.getTypeLanguage() != null)
writer.addAttribute(TYPE_LANGUAGE, def.getTypeLanguage());
if (def.getNamespace() != null)
writer.addAttribute(NAMESPACE, def.getNamespace());
if (def.getExporter() != null)
writer.addAttribute(EXPORTER, def.getExporter());
if (def.getExporterVersion() != null)
writer.addAttribute(EXPORTER_VERSION, def.getExporterVersion());
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class XStreamMarshaller method unmarshal.
@Override
public Definitions unmarshal(String xml) {
try (Reader firstStringReader = new StringReader(xml);
Reader secondStringReader = new StringReader(xml)) {
DMN_VERSION inferDMNVersion = inferDMNVersion(firstStringReader);
Definitions result;
switch(inferDMNVersion) {
case DMN_v1_1:
result = xstream11.unmarshal(secondStringReader);
break;
case DMN_v1_2:
result = xstream12.unmarshal(secondStringReader);
break;
case DMN_v1_3:
result = xstream13.unmarshal(secondStringReader);
break;
case DMN_v1_4:
result = xstream14.unmarshal(secondStringReader);
break;
case UNKNOWN:
default:
result = xstream14.unmarshal(secondStringReader);
break;
}
return result;
} catch (Exception e) {
logger.error("Error unmarshalling DMN model from reader.", e);
}
return null;
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class DMNXMLLoaderTest method testLoadingDefinitions.
@Test
public void testLoadingDefinitions() {
final DMNMarshaller DMNMarshaller = DMNMarshallerFactory.newDefaultMarshaller();
final InputStream is = this.getClass().getResourceAsStream("0001-input-data-string.dmn");
final InputStreamReader isr = new InputStreamReader(is);
final Definitions def = DMNMarshaller.unmarshal(isr);
assertThat(def, not(nullValue()));
assertThat(def.getName(), is("0001-input-data-string"));
assertThat(def.getId(), is("_0001-input-data-string"));
assertThat(def.getNamespace(), is("https://github.com/agilepro/dmn-tck"));
assertThat(def.getDrgElement().size(), is(2));
assertThat(def.getDrgElement().get(0), is(instanceOf(Decision.class)));
Decision dec = (Decision) def.getDrgElement().get(0);
assertThat(dec.getName(), is("Greeting Message"));
assertThat(dec.getId(), is("d_GreetingMessage"));
assertThat(dec.getVariable().getName(), is("Greeting Message"));
assertThat(dec.getVariable().getTypeRef().getPrefix(), is("feel"));
assertThat(dec.getVariable().getTypeRef().getLocalPart(), is("string"));
assertThat(dec.getVariable().getTypeRef().getNamespaceURI(), is(XMLConstants.NULL_NS_URI));
assertThat(dec.getInformationRequirement().size(), is(1));
assertThat(dec.getInformationRequirement().get(0).getRequiredInput().getHref(), is("#i_FullName"));
assertThat(dec.getExpression(), is(instanceOf(LiteralExpression.class)));
LiteralExpression le = (LiteralExpression) dec.getExpression();
assertThat(le.getText(), is("\"Hello \" + Full Name"));
InputData idata = (InputData) def.getDrgElement().get(1);
assertThat(idata.getId(), is("i_FullName"));
assertThat(idata.getName(), is("Full Name"));
assertThat(idata.getVariable().getName(), is("Full Name"));
assertThat(idata.getVariable().getTypeRef().getPrefix(), is("feel"));
assertThat(idata.getVariable().getTypeRef().getLocalPart(), is("string"));
assertThat(idata.getVariable().getTypeRef().getNamespaceURI(), is(XMLConstants.NULL_NS_URI));
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class UnmarshalMarshalTest method testRoundTrip.
public void testRoundTrip(String subdir, String xmlfile, DMNMarshaller marshaller, Source schemaSource) throws Exception {
File baseOutputDir = new File("target/test-xmlunit/");
File testClassesBaseDir = new File("target/test-classes/");
File inputXMLFile = new File(testClassesBaseDir, subdir + xmlfile);
FileInputStream fis = new FileInputStream(inputXMLFile);
Definitions unmarshal = marshaller.unmarshal(new InputStreamReader(fis));
Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
v.setSchemaSource(schemaSource);
ValidationResult validateInputResult = v.validateInstance(new StreamSource(inputXMLFile));
if (!validateInputResult.isValid()) {
for (ValidationProblem p : validateInputResult.getProblems()) {
LOG.error("{}", p);
}
}
assertTrue(validateInputResult.isValid());
final File subdirFile = new File(baseOutputDir, subdir);
if (!subdirFile.mkdirs()) {
LOG.warn("mkdirs() failed for File: ", subdirFile.getAbsolutePath());
}
FileOutputStream sourceFos = new FileOutputStream(new File(baseOutputDir, subdir + "a." + xmlfile));
Files.copy(new File(testClassesBaseDir, subdir + xmlfile).toPath(), sourceFos);
sourceFos.flush();
sourceFos.close();
LOG.debug("{}", marshaller.marshal(unmarshal));
File outputXMLFile = new File(baseOutputDir, subdir + "b." + xmlfile);
try (FileWriter targetFos = new FileWriter(outputXMLFile)) {
marshaller.marshal(unmarshal, targetFos);
}
// Should also validate output XML:
ValidationResult validateOutputResult = v.validateInstance(new StreamSource(outputXMLFile));
if (!validateOutputResult.isValid()) {
for (ValidationProblem p : validateOutputResult.getProblems()) {
LOG.error("{}", p);
}
}
assertTrue(validateOutputResult.isValid());
LOG.debug("\n---\nDefault XMLUnit comparison:");
Source control = Input.fromFile(inputXMLFile).build();
Source test = Input.fromFile(outputXMLFile).build();
Diff allDiffsSimilarAndDifferent = DiffBuilder.compare(control).withTest(test).build();
allDiffsSimilarAndDifferent.getDifferences().forEach(m -> LOG.debug("{}", m));
LOG.info("XMLUnit comparison with customized similarity for defaults:");
// in the following a manual DifferenceEvaluator is needed until XMLUnit is configured for properly parsing the XSD linked inside the XML,
// in order to detect the optional+defaultvalue attributes of xml element which might be implicit in source-test, and explicit in test-serialized.
/*
* $ grep -Eo "<xsd:attribute name=\\\"([^\\\"]*)\\\" type=\\\"([^\\\"]*)\\\" use=\\\"optional\\\" default=\\\"([^\\\"])*\\\"" dmn.xsd
<xsd:attribute name="expressionLanguage" type="xsd:anyURI" use="optional" default="http://www.omg.org/spec/FEEL/20140401"
<xsd:attribute name="typeLanguage" type="xsd:anyURI" use="optional" default="http://www.omg.org/spec/FEEL/20140401"
<xsd:attribute name="isCollection" type="xsd:boolean" use="optional" default="false"
<xsd:attribute name="hitPolicy" type="tHitPolicy" use="optional" default="UNIQUE"
<xsd:attribute name="preferredOrientation" type="tDecisionTableOrientation" use="optional" default="Rule-as-Row"
DMNv1.2:
<xsd:attribute name="kind" type="tFunctionKind" default="FEEL"/>
<xsd:attribute name="textFormat" type="xsd:string" default="text/plain"/>
<xsd:attribute name="associationDirection" type="tAssociationDirection" default="None"/>
DMNDIv1.2:
<xsd:attribute name="isCollapsed" type="xsd:boolean" use="optional" default="false"/>
*/
Set<QName> attrWhichCanDefault = new HashSet<QName>();
attrWhichCanDefault.addAll(Arrays.asList(new QName[] { new QName("expressionLanguage"), new QName("typeLanguage"), new QName("isCollection"), new QName("hitPolicy"), new QName("preferredOrientation"), new QName("kind"), new QName("textFormat"), new QName("associationDirection"), new QName("isCollapsed") }));
Set<String> nodeHavingDefaultableAttr = new HashSet<>();
nodeHavingDefaultableAttr.addAll(Arrays.asList(new String[] { "definitions", "decisionTable", "itemDefinition", "itemComponent", "encapsulatedLogic", "textAnnotation", "association", "DMNShape" }));
Diff checkSimilar = DiffBuilder.compare(control).withTest(test).withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, ((comparison, outcome) -> {
if (outcome == ComparisonResult.DIFFERENT && comparison.getType() == ComparisonType.ELEMENT_NUM_ATTRIBUTES) {
if (comparison.getControlDetails().getTarget().getNodeName().equals(comparison.getTestDetails().getTarget().getNodeName()) && nodeHavingDefaultableAttr.contains(safeStripDMNPRefix(comparison.getControlDetails().getTarget()))) {
return ComparisonResult.SIMILAR;
}
}
// DMNDI/DMNDiagram#documentation is actually deserialized escaped with newlines as by the XML JDK infra.
if (outcome == ComparisonResult.DIFFERENT && comparison.getType() == ComparisonType.ATTR_VALUE) {
if (comparison.getControlDetails().getTarget().getNodeName().equals(comparison.getTestDetails().getTarget().getNodeName()) && comparison.getControlDetails().getTarget().getNodeType() == Node.ATTRIBUTE_NODE && comparison.getControlDetails().getTarget().getLocalName().equals("documentation")) {
return ComparisonResult.SIMILAR;
}
}
if (outcome == ComparisonResult.DIFFERENT && comparison.getType() == ComparisonType.ATTR_NAME_LOOKUP) {
boolean testIsDefaulableAttribute = false;
QName whichDefaultableAttr = null;
if (comparison.getControlDetails().getValue() == null && attrWhichCanDefault.contains(comparison.getTestDetails().getValue())) {
for (QName a : attrWhichCanDefault) {
boolean check = comparison.getTestDetails().getXPath().endsWith("@" + a);
if (check) {
testIsDefaulableAttribute = true;
whichDefaultableAttr = a;
continue;
}
}
}
if (testIsDefaulableAttribute) {
if (comparison.getTestDetails().getXPath().equals(comparison.getControlDetails().getXPath() + "/@" + whichDefaultableAttr)) {
// TODO missing to check the explicited option attribute has value set to the actual default value.
return ComparisonResult.SIMILAR;
}
}
}
return outcome;
}))).ignoreWhitespace().checkForSimilar().build();
checkSimilar.getDifferences().forEach(m -> LOG.error("{}", m));
if (!checkSimilar.getDifferences().iterator().hasNext()) {
LOG.info("[ EMPTY - no diffs using customized similarity ]");
}
assertFalse("XML are NOT similar: " + checkSimilar.toString(), checkSimilar.hasDifferences());
}
Aggregations