initial load
[DTRules.git] / DTRules / src / main / java / com / dtrules / decisiontables / BalanceTable.java
blob01ae6fce45df56f0d2b81af8bb0117c4c0d21b0a
1 /*
2 * Copyright 2004-2007 MTBJ, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.dtrules.decisiontables;
18 import com.dtrules.infrastructure.RulesException;
19 import com.dtrules.session.IRSession;
21 /**
22 * Builds a balanced decision table from the given decision table.
23 * @author paul snow
24 * Mar 5, 2007
27 public class BalanceTable {
29 private int maxRow=0,maxCol=0;
30 private int maxARow=0;
31 private String ctable[][] = new String[32][10240];
32 private String atable[][] = new String[32][10240];
34 final RDecisionTable dt;
35 public BalanceTable(RDecisionTable dt) throws RulesException{
36 this.dt = dt;
39 RDecisionTable balancedTable (IRSession s) throws RulesException{
40 if(!dt.isCompiled())dt.build();
41 if(!dt.isCompiled()){
42 throw new RulesException("DecisionTableError","balancedTable()","Malformed Decision Table");
44 RDecisionTable btable = (RDecisionTable) dt.clone(s);
45 ctable = btable.conditiontable;
46 atable = btable.actiontable;
47 filltable(0,0,btable.decisiontree);
48 btable.setType(RDecisionTable.Type.BALANCED);
49 btable.build();
50 return btable;
53 public String getPrintableTable(){
54 if(dt.decisiontree==null)return "empty table";
55 filltable(0, 0, dt.decisiontree);
56 StringBuffer buff = new StringBuffer();
57 buff.append("Number of Columns: "+maxCol+"\r\n\r\n");
58 String spacer = " ";
59 if(maxCol < 25) spacer = " ";
62 for(int i=0;i<maxRow;i++){
63 String row = (i+1)+"";
64 row = " ".substring(row.length())+row+spacer;
65 buff.append(row);
66 for(int j=0;j<maxCol; j++){
67 if(ctable[i][j]==null)ctable[i][j]="-";
68 buff.append(ctable[i][j]);
69 buff.append(spacer);
71 buff.append("\r\n");
73 buff.append("\n");
74 for(int i=0;i<=maxARow;i++){
75 String row = (i+1)+"";
76 row = " ".substring(row.length())+row+spacer;
77 buff.append(row);
78 for(int j=0;j<maxCol; j++){
79 if(atable[i][j]==null)atable[i][j]=" ";
80 buff.append(atable[i][j]);
81 buff.append(spacer);
83 buff.append("\r\n");
85 buff.append("\r\n");
86 return buff.toString();
90 private int filltable(int row, int col, DTNode node){
92 if(node.getClass()==CNode.class){
93 int ncol;
94 CNode cnode = (CNode) node;
96 if(cnode.conditionNumber!=row){
97 ncol = filltable(row+1,col, node);
98 for(int i=col;i<ncol;i++)ctable[row][i]="-";
99 return ncol;
102 ncol = filltable(row+1,col,cnode.iftrue);
103 for(int i=col;i<ncol;i++)ctable[row][i]="y";
104 col = ncol;
105 ncol = filltable(row+1,col,cnode.iffalse);
106 for(int i=col;i<ncol;i++)ctable[row][i]="n";
107 col = ncol;
108 }else{
109 ctable[row][col]="-";
110 ANode anode = (ANode)node;
111 for(int i=0;i<anode.anumbers.size();i++){
112 int index = anode.anumbers.get(i).intValue();
113 atable[index][col]="x";
114 if(maxARow<index) maxARow = index;
116 col++;
118 maxRow = maxRow<row?row:maxRow;
119 maxCol = maxCol<col?col:maxCol;
120 return col;