initial load
[DTRules.git] / DTRules / src / main / java / com / dtrules / decisiontables / CNode.java
blob5ee3af9dd30969f02d732692caa91ac5cf270ff9
1 /*
2 * $Id$
3 *
4 * Copyright 2004-2007 MTBJ, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
19 package com.dtrules.decisiontables;
21 import com.dtrules.infrastructure.RulesException;
22 import com.dtrules.interpreter.IRObject;
23 import com.dtrules.session.DTState;
24 /**
25 * The Condition Node evaluates conditions within a Decision
26 * table.
27 * @author Paul Snow
30 public class CNode implements DTNode {
32 final int column; //Column that created this node
33 final int conditionNumber; //NOPMD
34 final IRObject condition;
35 final RDecisionTable dtable; // Pointer back to the Decision Table for debug purposes
37 DTNode iftrue = null;
38 DTNode iffalse = null;
40 /**
41 * Clone this CNode and all the CNodes referenced by this CNode
43 public CNode cloneDTNode (){
44 CNode newCNode = new CNode(dtable,column,conditionNumber,condition);
45 newCNode.iftrue = iftrue == null ? null : iftrue.cloneDTNode();
46 newCNode.iffalse = iffalse == null ? null : iffalse.cloneDTNode();
47 return newCNode;
50 public int getRow(){ return conditionNumber; }
51 /**
52 * Two CNodes are equal if their true paths and their
53 * false paths are the same.
54 * And those CommonANodes have to be equal.
56 public boolean equalsNode(DTNode node) {
57 if(node.getClass().equals(CNode.class)){
58 CNode cnode = (CNode)node;
59 return(cnode.iffalse.equalsNode(iffalse) &&
60 cnode.iftrue.equalsNode(iftrue));
61 }else{
62 ANode me = getCommonANode(); // Get this CNode's commonANode.
63 if(me==null)return false; // If none exists, it can't be equal!
64 ANode him = getCommonANode(); // Get the other DTNode's commonANode
65 if(him==null)return false; // If none exists, it can't be equal!
66 return me.equalsNode(him); // Return true if this node matches that node!
70 /**
71 * To have a CommonANode, every path through the CNode (i.e. both
72 * the true path and the false path) has to have the same commonANode.
73 * So both iftrue and iffalse have to have a commonANode, and those have
74 * to match.
76 public ANode getCommonANode() {
77 ANode trueSide = iftrue.getCommonANode(); // Does the true side have a CommonANode
78 if(trueSide==null)return null; // Nope? false!
79 ANode falseSide = iffalse.getCommonANode(); // Does the false side have a CommonANode?
80 if(falseSide==null)return null; // Nope? false!
81 if(trueSide.equalsNode(falseSide))return trueSide; // If they match, I just have to return one of them!
82 return null; // If they don't match, I have to return false!
87 CNode(RDecisionTable dt, int column, int conditionNumber, IRObject condition){
88 this.column = column;
89 this.conditionNumber = conditionNumber;
90 this.condition = condition;
91 this.dtable = dt;
94 public int countColumns(){
95 int t = iftrue.countColumns();
96 int f = iffalse.countColumns();
97 return t+f;
100 public void execute(DTState state) throws RulesException{
101 boolean result;
102 try {
103 result = state.evaluateCondition(condition);
104 } catch (RulesException e) {
105 e.setSection("Condition",conditionNumber+1);
106 throw e;
108 if(state.testState(DTState.TRACE)){
109 if(state.testState(DTState.VERBOSE)){
110 state.traceTagBegin("Condition", "n='"+conditionNumber+"'"+"r='"+(result?"Y'":"N'"));
111 state.traceInfo("Formal", null,dtable.getConditions()[conditionNumber]);
112 state.traceTagEnd("Condition", "");
113 }else{
114 state.traceInfo("Condition", "n='"+conditionNumber+"'"+"r='"+(result?"Y'":"N'"));
117 if(result){
118 iftrue.execute(state);
119 }else{
120 iffalse.execute(state);
124 public Coordinate validate() {
125 if(iftrue == null || iffalse == null){
126 return new Coordinate(conditionNumber,column);
128 Coordinate result = iffalse.validate();
129 if(result!=null){
130 return result;
133 return iftrue.validate();
136 public String toString(){
137 return "Condition Number "+(conditionNumber+1);