1 package org.kuali.rice.krms.tree;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.rice.core.api.util.tree.Node;
5 import org.kuali.rice.core.api.util.tree.Tree;
6 import org.kuali.rice.krms.api.repository.LogicalOperator;
7 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinitionContract;
8 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
9 import org.kuali.rice.krms.api.repository.rule.RuleDefinitionContract;
10 import org.kuali.rice.krms.tree.node.CompareTreeNode;
11 import org.kuali.student.krms.naturallanguage.util.KsKrmsConstants;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16
17
18
19
20
21
22
23 public class RuleCompareTreeBuilder extends AbstractTreeBuilder{
24
25 private static final long serialVersionUID = 1L;
26
27 public Tree<CompareTreeNode, String> buildTree(RuleDefinitionContract original, RuleDefinitionContract compare) {
28 Tree<CompareTreeNode, String> myTree = new Tree<CompareTreeNode, String>();
29
30 Node<CompareTreeNode, String> rootNode = new Node<CompareTreeNode, String>();
31 rootNode.setNodeType("subruleElement");
32 rootNode.setData(new CompareTreeNode());
33 myTree.setRootElement(rootNode);
34
35 Node<CompareTreeNode, String> firstNode = new Node<CompareTreeNode, String>();
36 firstNode.setNodeType("subruleElement");
37 firstNode.setData(new CompareTreeNode());
38 rootNode.getChildren().add(firstNode);
39
40 if (original != null) {
41 addTreeNode(firstNode, original.getProposition(), compare.getProposition());
42 }
43
44
45 if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)){
46 Node<CompareTreeNode, String> childNode = firstNode.getChildren().get(0);
47 if(childNode.getData() != null){
48 CompareTreeNode compareTreeNode = childNode.getData();
49 compareTreeNode.setOriginal(compareTreeNode.getOriginal() + ":");
50 compareTreeNode.setCompared(compareTreeNode.getCompared() + ":");
51 }
52 }
53
54 return myTree;
55 }
56
57 public static Tree<CompareTreeNode, String> initCompareTree() {
58 Tree<CompareTreeNode, String> myTree = new Tree<CompareTreeNode, String>();
59
60 Node<CompareTreeNode, String> rootNode = new Node<CompareTreeNode, String>();
61 rootNode.setNodeType("subruleElement");
62 rootNode.setData(new CompareTreeNode());
63 myTree.setRootElement(rootNode);
64
65 Node<CompareTreeNode, String> firstNode = new Node<CompareTreeNode, String>();
66 firstNode.setNodeType("subruleElement");
67 firstNode.setData(new CompareTreeNode());
68 rootNode.getChildren().add(firstNode);
69
70 return myTree;
71 }
72
73 private void addTreeNode(Node<CompareTreeNode, String> currentNode, PropositionDefinitionContract original, PropositionDefinitionContract compared) {
74 if ((original == null) && (compared == null)) {
75 return;
76 }
77
78 Node<CompareTreeNode, String> newNode = new Node<CompareTreeNode, String>();
79 CompareTreeNode tNode = new CompareTreeNode(this.getDescription(original), this.getDescription(compared));
80 tNode.setOriginalItems(this.getListItems(original));
81 tNode.setComparedItems(this.getListItems(compared));
82 if (tNode.getOriginal().equals(tNode.getCompared())){
83 newNode.setNodeType("subruleElement");
84 } else {
85 newNode.setNodeType("subruleElement compareElement");
86 }
87
88 newNode.setData(tNode);
89 currentNode.getChildren().add(newNode);
90
91 this.addCompoundTreeNode(newNode, original, compared);
92 }
93
94 private void addCompoundTreeNode(Node<CompareTreeNode, String> newNode, PropositionDefinitionContract original, PropositionDefinitionContract compared) {
95
96
97 String originalOpCode = this.getOpCode(original);
98
99
100 String compareOpCode = this.getOpCode(compared);
101
102
103 List<? extends PropositionDefinitionContract> originalChildren = getChildPropositions(original);
104 List<? extends PropositionDefinitionContract> comparedChildren = getChildPropositions(compared);
105
106
107 int size = Math.max(originalChildren.size(), comparedChildren.size());
108
109 for (int i = 0; i < size; i++) {
110
111
112 PropositionDefinitionContract originalChild = null;
113 if (originalChildren.size() > i){
114 originalChild = originalChildren.get(i);
115 } else {
116 originalOpCode = " ";
117 }
118
119
120 PropositionDefinitionContract compareChild = null;
121 if (comparedChildren.size() > i){
122 compareChild = comparedChildren.get(i);
123 } else {
124 compareOpCode = " ";
125 }
126
127 if (i>0) {
128 this.addOperatorTreeNode(newNode, originalOpCode, compareOpCode);
129 }
130
131
132 addTreeNode(newNode, originalChild, compareChild);
133 }
134
135 }
136
137 private List<? extends PropositionDefinitionContract> getChildPropositions(PropositionDefinitionContract parent) {
138 List<? extends PropositionDefinitionContract> children;
139 if ((parent != null) && (parent.getCompoundComponents()!=null)){
140 children = parent.getCompoundComponents();
141 } else {
142 children = new ArrayList<PropositionDefinitionContract>();
143 }
144 return children;
145 }
146
147 private String getOpCode(PropositionDefinitionContract proposition){
148
149 String operatorCode = " ";
150 if (proposition != null){
151 if (LogicalOperator.AND.getCode().equalsIgnoreCase(proposition.getCompoundOpCode())) {
152 operatorCode = "AND";
153 } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(proposition.getCompoundOpCode())) {
154 operatorCode = "OR";
155 }
156 }
157 return operatorCode;
158 }
159
160 private void addOperatorTreeNode(Node<CompareTreeNode, String> newNode, String originial, String compared) {
161 Node<CompareTreeNode, String> opNode = new Node<CompareTreeNode, String>();
162 if (!originial.equals(compared)){
163 opNode.setNodeType("compareElement");
164 }
165 opNode.setData(new CompareTreeNode(originial, compared));
166 newNode.getChildren().add(opNode);
167 }
168
169 public List<String> getListItems(PropositionDefinitionContract propositionEditor) {
170 return null;
171 }
172
173 public String getNaturalLanguageUsageKey(){
174 return KsKrmsConstants.KRMS_NL_RULE_EDIT;
175 }
176
177 }