Skip to content

Commit

Permalink
Was missing BusinessKnowledgeModel Converter and other Expression ... (
Browse files Browse the repository at this point in the history
…#5)

... converter fixes.
  • Loading branch information
tarilabs authored and etirelli committed Oct 10, 2016
1 parent c01edd5 commit 1dcf9c8
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected void assignChildElement(Object parent, String nodeName, Object child)

if (PARAMETER.equals(nodeName)) {
b.setParameter((InformationItem) child);
} else if (EXPRESSION.equals(nodeName)) {
} else if (child instanceof Expression) {
b.setExpression((Expression) child);
} else {
super.assignChildElement(parent, nodeName, child);
Expand All @@ -39,7 +39,7 @@ protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext
Binding b = (Binding) parent;

writeChildrenNode(writer, context, b.getParameter(), PARAMETER);
if (b.getExpression() != null) writeChildrenNode(writer, context, b.getExpression(), EXPRESSION);
if (b.getExpression() != null) writeChildrenNode(writer, context, b.getExpression(), MarshallingUtils.defineExpressionNodeName(b.getExpression()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.kie.dmn.backend.unmarshalling.v1_1.xstream;

import org.kie.dmn.feel.model.v1_1.AuthorityRequirement;
import org.kie.dmn.feel.model.v1_1.BusinessKnowledgeModel;
import org.kie.dmn.feel.model.v1_1.FunctionDefinition;
import org.kie.dmn.feel.model.v1_1.InformationItem;
import org.kie.dmn.feel.model.v1_1.KnowledgeRequirement;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class BusinessKnowledgeModelConverter extends DRGElementConverter {
public static final String ENCAPSULATED_LOGIC = "encapsulatedLogic";
public static final String VARIABLE = "variable";
public static final String KNOWLEDGE_REQUIREMENT = "knowledgeRequirement";
public static final String AUTHORITY_REQUIREMENT = "authorityRequirement";

@Override
protected void assignChildElement(Object parent, String nodeName, Object child) {
BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) parent;

if (ENCAPSULATED_LOGIC.equals(nodeName)) {
bkm.setEncapsulatedLogic((FunctionDefinition) child);
} else if (VARIABLE.equals(nodeName)) {
bkm.setVariable((InformationItem) child);
} else if (KNOWLEDGE_REQUIREMENT.equals(nodeName)) {
bkm.getKnowledgeRequirement().add((KnowledgeRequirement) child);
} else if (AUTHORITY_REQUIREMENT.equals(nodeName)) {
bkm.getAuthorityRequirement().add((AuthorityRequirement) child);
} else {
super.assignChildElement(parent, nodeName, child);
}
}

@Override
protected void assignAttributes(HierarchicalStreamReader reader, Object parent) {
super.assignAttributes(reader, parent);

// no attributes.
}

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
super.writeChildren(writer, context, parent);
BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) parent;

if (bkm.getEncapsulatedLogic() != null) writeChildrenNode(writer, context, bkm.getEncapsulatedLogic(), ENCAPSULATED_LOGIC);
if (bkm.getVariable() != null) writeChildrenNode(writer, context, bkm.getVariable(), VARIABLE);
for (KnowledgeRequirement i : bkm.getKnowledgeRequirement()) {
writeChildrenNode(writer, context, i, KNOWLEDGE_REQUIREMENT);
}
for (AuthorityRequirement a : bkm.getAuthorityRequirement()) {
writeChildrenNode(writer, context, a, AUTHORITY_REQUIREMENT);
}
}

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);

// no attributes.
}

public BusinessKnowledgeModelConverter(XStream xstream) {
super(xstream);
}

@Override
protected Object createModelObject() {
return new BusinessKnowledgeModel();
}

@Override
public boolean canConvert(Class clazz) {
return clazz.equals( BusinessKnowledgeModel.class );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected void assignChildElement(Object parent, String nodeName, Object child)

if (VARIABLE.equals(nodeName)) {
ce.setVariable((InformationItem) child);
} else if (EXPRESSION.equals(nodeName)) {
} else if (child instanceof Expression) {
ce.setExpression((Expression) child);
} else {
super.assignChildElement(parent, nodeName, child);
Expand All @@ -39,7 +39,7 @@ protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext
ContextEntry ce = (ContextEntry) parent;

if (ce.getVariable() != null) writeChildrenNode(writer, context, ce.getVariable(), VARIABLE);
writeChildrenNode(writer, context, ce.getExpression(), EXPRESSION);
writeChildrenNode(writer, context, ce.getExpression(), MarshallingUtils.defineExpressionNodeName(ce.getExpression()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,11 @@ protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext
}
if (dec.getExpression() != null) {
Expression e = dec.getExpression();
String nodeName = EXPRESSION;
if (e instanceof Context) {
nodeName = "context";
} else if (e instanceof DecisionTable) {
nodeName = "decisionTable";
} else if (e instanceof FunctionDefinition) {
nodeName = "functionDefinition";
} else if (e instanceof Invocation) {
nodeName = "invocation";
} else if (e instanceof LiteralExpression) {
nodeName = "literalExpression";
} else if (e instanceof Relation) {
nodeName = "relation";
}
writeChildrenNode(writer, context, dec.getExpression(), nodeName);
String nodeName = MarshallingUtils.defineExpressionNodeName(e);
writeChildrenNode(writer, context, e, nodeName);
}
}

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected void assignChildElement(Object parent, String nodeName, Object child)

if (FORMAL_PARAMETER.equals(nodeName)) {
fd.getFormalParameter().add((InformationItem) child);
} else if (EXPRESSION.equals(nodeName)) {
} else if (child instanceof Expression) {
fd.setExpression((Expression) child);
} else {
super.assignChildElement(parent, nodeName, child);
Expand All @@ -41,7 +41,7 @@ protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext
for (InformationItem fparam : fd.getFormalParameter()) {
writeChildrenNode(writer, context, fparam, FORMAL_PARAMETER);
}
if (fd.getExpression() != null) writeChildrenNode(writer, context, fd.getExpression(), EXPRESSION);
if (fd.getExpression() != null) writeChildrenNode(writer, context, fd.getExpression(), MarshallingUtils.defineExpressionNodeName(fd.getExpression()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class InvocationConverter extends ExpressionConverter {
protected void assignChildElement(Object parent, String nodeName, Object child) {
Invocation i = (Invocation) parent;

if (EXPRESSION.equals(nodeName)) {
if (child instanceof Expression) {
i.setExpression((Expression) child);
} else if (BINDING.equals(nodeName)) {
i.getBinding().add((Binding) child);
Expand All @@ -37,7 +37,7 @@ protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext
super.writeChildren(writer, context, parent);
Invocation i = (Invocation) parent;

if (i.getExpression() != null) writeChildrenNode(writer, context, i.getExpression(), EXPRESSION);
if (i.getExpression() != null) writeChildrenNode(writer, context, i.getExpression(), MarshallingUtils.defineExpressionNodeName(i.getExpression()));
for (Binding b : i.getBinding()) {
writeChildrenNode(writer, context, b, BINDING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

package org.kie.dmn.backend.unmarshalling.v1_1.xstream;

import org.kie.dmn.feel.model.v1_1.Context;
import org.kie.dmn.feel.model.v1_1.DecisionTable;
import org.kie.dmn.feel.model.v1_1.Expression;
import org.kie.dmn.feel.model.v1_1.FunctionDefinition;
import org.kie.dmn.feel.model.v1_1.Invocation;
import org.kie.dmn.feel.model.v1_1.LiteralExpression;
import org.kie.dmn.feel.model.v1_1.QName;
import org.kie.dmn.feel.model.v1_1.Relation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -50,4 +57,25 @@ public static String formatQName(QName qname) {
sb.append(qname.getLocalPart());
return sb.toString();
}

/**
* TODO missing what-if in the case of List..
*/
public static String defineExpressionNodeName(Expression e) {
String nodeName = "expression";
if (e instanceof Context) {
nodeName = "context";
} else if (e instanceof DecisionTable) {
nodeName = "decisionTable";
} else if (e instanceof FunctionDefinition) {
nodeName = "functionDefinition";
} else if (e instanceof Invocation) {
nodeName = "invocation";
} else if (e instanceof LiteralExpression) {
nodeName = "literalExpression";
} else if (e instanceof Relation) {
nodeName = "relation";
}
return nodeName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ private XStream newXStream() {
xStream.registerConverter(new AssociationConverter( xStream ) );
xStream.registerConverter(new AuthorityRequirementConverter( xStream ) );
xStream.registerConverter(new BindingConverter( xStream ) );
xStream.registerConverter(new BusinessKnowledgeModelConverter( xStream ) );
xStream.registerConverter(new ContextConverter( xStream ) );
xStream.registerConverter(new ContextEntryConverter( xStream ) );
xStream.registerConverter(new DecisionConverter( xStream ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public void testDish() throws Exception {
public void testCh11() throws Exception {
testRoundTrip("", "ch11example.xml");
}

@Ignore("A problem with the StaxDriver has still to be resolved.")
@Test
public void testFAILforMissingNamespaces() {
fail("PERFORM A MANUAL CHECK: does now the Stax driver do output the namespace for 'feel:' ?? ");
}

public void testRoundTrip(String subdir, String xmlfile) throws Exception {
XStreamUnmarshaller marshaller = new XStreamUnmarshaller();
Expand Down

0 comments on commit 1dcf9c8

Please sign in to comment.