2 * Copyright 2004-2007 MTBJ, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
;
22 * Builds a balanced decision table from the given decision table.
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
{
39 RDecisionTable
balancedTable (IRSession s
) throws RulesException
{
40 if(!dt
.isCompiled())dt
.build();
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
);
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");
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
;
66 for(int j
=0;j
<maxCol
; j
++){
67 if(ctable
[i
][j
]==null)ctable
[i
][j
]="-";
68 buff
.append(ctable
[i
][j
]);
74 for(int i
=0;i
<=maxARow
;i
++){
75 String row
= (i
+1)+"";
76 row
= " ".substring(row
.length())+row
+spacer
;
78 for(int j
=0;j
<maxCol
; j
++){
79 if(atable
[i
][j
]==null)atable
[i
][j
]=" ";
80 buff
.append(atable
[i
][j
]);
86 return buff
.toString();
90 private int filltable(int row
, int col
, DTNode node
){
92 if(node
.getClass()==CNode
.class){
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
]="-";
102 ncol
= filltable(row
+1,col
,cnode
.iftrue
);
103 for(int i
=col
;i
<ncol
;i
++)ctable
[row
][i
]="y";
105 ncol
= filltable(row
+1,col
,cnode
.iffalse
);
106 for(int i
=col
;i
<ncol
;i
++)ctable
[row
][i
]="n";
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
;
118 maxRow
= maxRow
<row?row
:maxRow
;
119 maxCol
= maxCol
<col?col
:maxCol
;