screen.getTxtName().setEnabled(true);
screen.getTxtDescription().setText(node.getDescription());
screen.getTxtName().setText(node.getName());
final JTable table;
final PotentialTable potTab;
final int variables;
/* Check if the node represents a numeric attribute */
if (node.getStatesSize() == 0) {
Node parent = node.getParents().get(0);
int numClasses = parent.getStatesSize();
double[] mean = node.getMean();
double[] stdDev = node.getStandardDeviation();
table = new JTable(3, numClasses + 1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setTableHeader(null);
/* First column */
table.setValueAt(parent.getName(), 0, 0);
table.setValueAt(resource.getString("mean"), 1, 0);
table.setValueAt(resource.getString("stdDev"), 2, 0);
/* Other columns */
for (int i = 0; i < numClasses; i++) {
table.setValueAt(parent.getStateAt(i), 0, i + 1);
table.setValueAt(mean[i], 1, i + 1);
table.setValueAt(stdDev[i], 2, i + 1);
}
return table;
}
if (node instanceof IRandomVariable) {
potTab = (PotentialTable)((IRandomVariable) node).getProbabilityFunction();
int states = 1;
variables = potTab.variableCount();
// calculate the number of states by multiplying the number of
// states that each father (variables) has. Where variable 0 is the
// node itself. That is why it starts at 1.
/*
* Ex: states = 2 * 2;
*
* |------------------------------------------------------| | Father
* 2 | State 1 | State 2 |
* |--------------|-------------------|-------------------| | Father
* 1 | State 1 | State 2 | State 1 | State 2 |
* |------------------------------------------------------| | Node
* State 1 | 1 | 1 | 1 | 1 | | Node State 2 | 0 | 0 | 0 | 0 |
*
*/
states = potTab.tableSize() / node.getStatesSize();
/*
* for (int count = 1; count < variables; count++) { states *=
* potTab.getVariableAt(count).getStatesSize(); }
*/
// the number of rows is the number of states the node has plus the
// number of fathers (variables - 1, because one of the variables
// is the node itself).
int rows = node.getStatesSize() + variables - 1;
// the number of columns is the number of states that we calculate
// before plus one that is the column where the fathers names and
// the states of the node itself will be placed.
int columns = states + 1;
table = new JTable(rows, columns);
// put the name of the states of the node in the first column
// starting in the (variables - 1)th row (number of fathers). That
// is because on the rows before that there will be placed the
// name of the fathers.
for (int k = variables - 1, l = 0; k < table.getRowCount(); k++, l++) {
table.setValueAt(node.getStateAt(l), k, 0);
}
// put the name of the father and its states' name in the right
// place.
for (int k = variables - 1, l = 0; k >= 1; k--, l++) {
Node variable = (Node)potTab.getVariableAt(k);
// the number of states is the multiplication of the number of
// states of the other fathers above this one.
states /= variable.getStatesSize();
// put the name of the father in the first column.
table.setValueAt(variable.getName(), l, 0);
// put the name of the states of this father in the lth row
// and ith column, repeating the name if necessary (for each
// state of the father above).
for (int i = 0; i < table.getColumnCount() - 1; i++) {
table.setValueAt(variable.getStateAt((i / states)
% variable.getStatesSize()), l, i + 1);
}
}
// now states is the number of states that the node has.
states = node.getStatesSize();
// put the values of the probabilistic table in the jth row and ith
// column, picking up the values in a double collection in potTab.
for (int i = 1, k = 0; i < table.getColumnCount(); i++, k += states) {
for (int j = variables - 1, l = 0; j < table.getRowCount(); j++, l++) {
table.setValueAt("" + df.format(potTab.getValue(k + l)), j,
i);
}
}
} else {
// decision
// the number of rows in this case is the number of states of the
// node and the number of columns is always 1.
// int rows = node.getStatesSize();
// int columns = 1;
// there is no potential table and the number of variables is the
// number of parents this node has.
potTab = null;
variables = node.getParents().size();
table = new JTable(node.getStatesSize(), 1);
// put the name of each state in the first and only column.
for (int i = 0; i < node.getStatesSize(); i++) {
table.setValueAt(node.getStateAt(i), i, 0);
}
}
table.setTableHeader(null);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (e.getLastRow() < variables - 1) {
return;
}
if (e.getColumn() == 0) {
if (!table.getValueAt(e.getLastRow(), e.getColumn())
.equals("")) {
node.setStateAt(table.getValueAt(e.getLastRow(),
e.getColumn()).toString(), e.getLastRow()
- (table.getRowCount() - node.getStatesSize()));
}
} else {
String temp = table.getValueAt(e.getLastRow(),
e.getColumn()).toString().replace(',', '.');
try {
float valor = Float.parseFloat(temp);
potTab.setValue((e.getColumn() - 1) * node.getStatesSize() + e.getLastRow(), valor);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,
resource.getString("error"),
resource.getString("realNumberError"),
JOptionPane.ERROR_MESSAGE);
table.revalidate();
table.setValueAt(""
+ potTab.getValue((e.getColumn() - 1) * node.getStatesSize() + e.getLastRow()),
e.getLastRow(), e.getColumn());
}
}
}
});