Skip to content

Commit

Permalink
XIVY-12538 migrate to process format 11.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-rew committed Nov 9, 2023
1 parent ebc4413 commit c60a003
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 133 deletions.
131 changes: 6 additions & 125 deletions dmn-decision-table-demo/processes/TaxCalculation.p.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema" : "https://json-schema.axonivy.com/process/11.2.1/process.json",
"$schema" : "https://json-schema.axonivy.com/process/11.2.2/process.json",
"id" : "15C82A89FA49E8E6",
"config" : {
"data" : "com.axonivy.ivy.supplements.rule.beans.demos.Data"
Expand Down Expand Up @@ -35,130 +35,11 @@
"impl" : "DecisionActivity",
"name" : "evaluate taxes with decision table",
"config" : {
"userConfig" : [
"{",
" \"conditionColumns\" : [ {",
" \"attributeName\" : \"in.yearlyIncomeDollars\",",
" \"type\" : \"Number\"",
" }, {",
" \"attributeName\" : \"in.yearlyIncomeDollars\",",
" \"type\" : \"Number\"",
" }, {",
" \"attributeName\" : \"in.person.gender\",",
" \"type\" : \"String\"",
" } ],",
" \"actionColumns\" : [ {",
" \"attributeName\" : \"out.taxrate\",",
" \"type\" : \"Number\"",
" } ],",
" \"rows\" : [ {",
" \"cells\" : [ {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL_OR_GREATER\",",
" \"arguments\" : [ \"0\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"LESS\",",
" \"arguments\" : [ \"50000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"NO_CONDITION\",",
" \"arguments\" : [ ]",
" }",
" }, {",
" \"valueCell\" : {",
" \"value\" : \"0\"",
" }",
" } ]",
" }, {",
" \"cells\" : [ {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL_OR_GREATER\",",
" \"arguments\" : [ \"50000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"LESS\",",
" \"arguments\" : [ \"250000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL\",",
" \"arguments\" : [ \"male\" ]",
" }",
" }, {",
" \"valueCell\" : {",
" \"value\" : \"15\"",
" }",
" } ]",
" }, {",
" \"cells\" : [ {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL_OR_GREATER\",",
" \"arguments\" : [ \"250000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"NO_CONDITION\",",
" \"arguments\" : [ ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL\",",
" \"arguments\" : [ \"male\" ]",
" }",
" }, {",
" \"valueCell\" : {",
" \"value\" : \"30\"",
" }",
" } ]",
" }, {",
" \"cells\" : [ {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL_OR_GREATER\",",
" \"arguments\" : [ \"50000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"LESS\",",
" \"arguments\" : [ \"250000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL\",",
" \"arguments\" : [ \"female\" ]",
" }",
" }, {",
" \"valueCell\" : {",
" \"value\" : \"10\"",
" }",
" } ]",
" }, {",
" \"cells\" : [ {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL_OR_GREATER\",",
" \"arguments\" : [ \"250000\" ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"NO_CONDITION\",",
" \"arguments\" : [ ]",
" }",
" }, {",
" \"conditionCell\" : {",
" \"operator\" : \"EQUAL\",",
" \"arguments\" : [ \"female\" ]",
" }",
" }, {",
" \"valueCell\" : {",
" \"value\" : \"20\"",
" }",
" } ]",
" } ]",
"}"
]
"userConfig" : {
"rows" : "[{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"0\"]}},{\"conditionCell\":{\"operator\":\"LESS\",\"arguments\":[\"50000\"]}},{\"conditionCell\":{\"operator\":\"NO_CONDITION\",\"arguments\":[]}},{\"valueCell\":{\"value\":\"0\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"50000\"]}},{\"conditionCell\":{\"operator\":\"LESS\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"male\"]}},{\"valueCell\":{\"value\":\"15\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"NO_CONDITION\",\"arguments\":[]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"male\"]}},{\"valueCell\":{\"value\":\"30\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"50000\"]}},{\"conditionCell\":{\"operator\":\"LESS\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"female\"]}},{\"valueCell\":{\"value\":\"10\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"NO_CONDITION\",\"arguments\":[]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"female\"]}},{\"valueCell\":{\"value\":\"20\"}}]}]",
"actionColumns" : "[{\"attributeName\":\"out.taxrate\",\"type\":\"Number\"}]",
"conditionColumns" : "[{\"attributeName\":\"in.yearlyIncomeDollars\",\"type\":\"Number\"},{\"attributeName\":\"in.yearlyIncomeDollars\",\"type\":\"Number\"},{\"attributeName\":\"in.person.gender\",\"type\":\"String\"}]"
}
},
"visual" : {
"at" : { "x" : 664, "y" : 112 },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.axonivy.ivy.process.element.rule.model;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class RulesModelSerialization {
private static final ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -19,8 +24,14 @@ public class RulesModelSerialization {

private RulesModelSerialization() {}

public static String serialize(RulesModel model) throws JsonProcessingException {
return objectMapper.writeValueAsString(model);
public static Map<String, String> serialize(RulesModel model) throws JsonProcessingException {
var json = objectMapper.writeValueAsString(model);
var tree = (ObjectNode) objectMapper.readValue(json, JsonNode.class);
Map<String, String> conf = new HashMap<>();
tree.fields().forEachRemaining(et -> {
conf.put(et.getKey(), et.getValue().toString());
});
return conf;
}

public static RulesModel deserialize(String jsonModel)
Expand All @@ -31,4 +42,16 @@ public static RulesModel deserialize(String jsonModel)
return objectMapper.readValue(jsonModel, RulesModel.class);
}

public static RulesModel deserialize(Map<String, String> userConf) throws Exception {
var obj = JsonNodeFactory.instance.objectNode();
for(var et : userConf.entrySet()) {
obj.set(et.getKey(), read(et.getValue()));
}
return objectMapper.readValue(obj.toString(), RulesModel.class);
}

private static JsonNode read(String value) throws JsonMappingException, JsonProcessingException {
return objectMapper.readValue(value, JsonNode.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.axonivy.ivy.process.element.rule.ui;

import java.util.List;
import java.util.Map;

import com.axonivy.ivy.process.element.rule.model.Row;
import com.axonivy.ivy.process.element.rule.model.RulesModel;
Expand Down Expand Up @@ -42,18 +43,19 @@ private void updateRows(List<Row> rows) {
}

private RulesModel loadRulesModel() {
Map<String, String> conf = element.getUserConfig().configs();
try {
return RulesModelSerialization.deserialize(element.getUserConfig().getRawValue());
return RulesModelSerialization.deserialize(conf);
} catch (Exception ex) {
LOGGER.error("Failed to load decision table config", ex);
LOGGER.error("Failed to load decision table config "+conf, ex);
return new RulesModel();
}
}

public void storeRulesModel() {
try {
String json = RulesModelSerialization.serialize(rules);
element.setUserConfig(new UserConfig(json));
Map<String, String> conf = RulesModelSerialization.serialize(rules);
element.setUserConfig(new UserConfig(conf));
} catch (JsonProcessingException ex) {
throw new RuntimeException("Failed to store decision table config", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void serialization() throws Exception {
rowModel.addCell(new ValueCell("8.5"));
model.addRow(rowModel);

String json = RulesModelSerialization.serialize(model);
RulesModel deserializedModel = RulesModelSerialization.deserialize(json);
var conf = RulesModelSerialization.serialize(model);
RulesModel deserializedModel = RulesModelSerialization.deserialize(conf);

assertEquals(model, deserializedModel);
}
Expand Down

0 comments on commit c60a003

Please sign in to comment.