try {
parents = new ArrayList<SSBNNode>(this.getSSBNNode().getParents());
} catch (NullPointerException e) {
parents = new ArrayList<SSBNNode>();
} catch (Exception e) {
throw new InconsistentTableSemanticsException(e);
}
Map<String, List<EntityAndArguments>> map = null; // parameter of boolean expression evaluation method
// this iterators helps us combine parents' possible values
// e.g. (True,Alpha), (True,Beta), (False,Alpha), (False,Beta).
List<Iterator<Entity>> valueCombinationIterators = new ArrayList<Iterator<Entity>>();
for (SSBNNode parentNode : parents) {
if (parentNode.getActualValues() == null || parentNode.getActualValues().isEmpty()) {
// add a dummy value just to ensure index are synchronized
List<Entity> temp = new ArrayList<Entity>();
temp.add(new NullEntity());
valueCombinationIterators.add(temp.iterator());
} else {
valueCombinationIterators.add(parentNode.getActualValues().iterator());
}
}
// saves the current values of the iterators
List<Entity> currentIteratorValue = new ArrayList<Entity>();
for (Iterator it : valueCombinationIterators) {
currentIteratorValue.add((Entity)it.next());
}
// initialize cps to something like "defineState( Continuous ,1, 2, 3, 4, 5, 6);p( C0 | C1 ) = "
String cps = " defineState( ";
if (this.getNode() instanceof ContinuousResidentNode) {
cps += "Continuous";
if (isToAddStatesToContinuousResidentNodes()) {
for (int i = 0 ; i < getSSBNNode().getProbNode().getStatesSize(); i++ ) {
cps += ", ";
cps += getSSBNNode().getProbNode().getStateAt(i);
}
}
} else {
cps += "Discrete";
for (int i = 0 ; i < getSSBNNode().getProbNode().getStatesSize(); i++ ) {
cps += ", ";
cps += getSSBNNode().getProbNode().getStateAt(i);
}
}
cps += ");p( " + getSSBNNode().getProbNode().getName();
if (getSSBNNode().getProbNode().getParentNodes() != null
&& !getSSBNNode().getProbNode().getParentNodes().isEmpty()) {
// add condition nodes if exist
cps += " | ";
for (INode parent : getSSBNNode().getProbNode().getParentNodes()) {
cps += parent.getName() + " , ";
}
cps = cps.substring(0, cps.lastIndexOf(" , ")); // remove last comma
}
cps += " ) = ";
// start running at the probabilistic table and filling its cells
//List<Entity> entityList = null;
// for( int i = 0; i < this.cpt.tableSize(); i += this.getSSBNNode().getProbNode().getStatesSize()) {
boolean isLastCombination = true; // indicates that the valueCombinationIterators has hasNext() == false for all contents. if set to false, the loop ends
// try {
// System.gc();
// } catch (Throwable t) {
// t.printStackTrace();
// }
boolean isItTheFisrtIf = true;
do {
// clears and initializes map
map = new HashMap<String, List<EntityAndArguments>>();
for (SSBNNode node : parents) {
if (!map.containsKey(node.getResident().getName())) {
map.put(node.getResident().getName(), new ArrayList<EntityAndArguments>());
}
}
// fill cps with something like "if( P1 == true && P2 == true )"
String cpsIfClause = ""; // content of if-clause. If there is no discrete parent, this content is empty
// fill map at this loop. Note that parents.size, currentIteratorValue.size, and valueCombinationiterators are the same
for (int j = 0; j < parents.size(); j++) {
SSBNNode parent = parents.get(j);
ResidentNode parentResident = parent.getResident();
// ignore continuous parent
if (!(parentResident instanceof ContinuousResidentNode)) {
Entity val = currentIteratorValue.get(j);
if (cpsIfClause.length() > 0) {
// there are other boolean expressions. Combine using &&
cpsIfClause += " && ";
}
cpsIfClause += parent.getProbNode().getName() + " == " + val.getName();
map.get(parentResident.getName()).add(new EntityAndArguments(val,new ArrayList<OVInstance>(parent.getArguments())));
}
}
if (cpsIfClause.length() > 0) {
// only start if-clause if there are discrete parents
if (!isItTheFisrtIf) {
cps += "else ";
}
cps += "if( " + cpsIfClause + ")";
isItTheFisrtIf = false;
}
isLastCombination = true; // reset flag before testing conditions
// updates iterators and check if this is the last combination
for (int j = 0; j < valueCombinationIterators.size(); j++) {
if (valueCombinationIterators.get(j).hasNext()) {
// if has next, then update current value and exits loop
currentIteratorValue.set(j, valueCombinationIterators.get(j).next());
isLastCombination = false;
break;
} else {
// else, reset the iterator (and current value) until exit loop
valueCombinationIterators.set(j, parents.get(j).getActualValues().iterator());
if (valueCombinationIterators.get(j).hasNext()) {
currentIteratorValue.set(j, valueCombinationIterators.get(j).next());
}
}
}
// prepare to extract which column to verify
TempTableHeaderCell header = null;
// if default distro, then use the default header...
// also, if no parents are declared, use default distro...
boolean thereAreNoParents = false;
if(this.getSSBNNode().getParents() == null) {
thereAreNoParents = true;
} else if (this.getSSBNNode().getParents().size() == 0) {
thereAreNoParents = true;
}
if (thereAreNoParents || this.getSSBNNode().isUsingDefaultCPT()) {
// we assume the default distro is the last block on pseudocode
header = this.tempTable.getDefaultClause();
// let's just check if this header is really declaring a default distro... Just in case...
if (!header.isDefault) {
throw new InconsistentTableSemanticsException();
}
} else {
// if not default, look for the column to verify
// the first expression to return true is the one we want
// long time = new Date().getTime();