// 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();
header = this.tempTable.getFirstTrueClause(map);
// note that default (else) expression should allways return true!
// System.out.println(header + ": took " + (new Date().getTime() - time) + " in loop " + loop++ + " for values " + map);
}
// the "{" is only necessary if there is any if clause
if (cpsIfClause.length() > 0) {
cps += "{ ";
}
// fill cps with something like "{NormalDist( 5,1 ) } " or "{ true:1.0; false:0.0; }"
ArrayList<Entity> possibleValues = new ArrayList<Entity>(this.getSSBNNode().getActualValues());
if ((this.getNode() instanceof ContinuousResidentNode)
|| possibleValues == null
|| possibleValues.size() <= 0) {
// this should be a continuous expression, like "{NormalDist( 5,1 ) } "
// trace how many cells we have (we are expecting only 1)
if (header.getCellList() == null || header.getCellList().size() <= 0) {
throw new MEBNException("Temporary table " + header + " has no cell. This is either a wrong script or compiler bug.");
} else if (header.getCellList().size() > 1) {
try {
Debug.println(getClass(), getSSBNNode() + " is supposed to be continuous, but we found states: " + header.getCellList());
} catch (Throwable t) {
t.printStackTrace();
}
}
// use the first possible value and concatenate as-is
cps += header.getCellList().get(0).getProbabilityValue() + ";";
} else {
// this is a discrete expression, like "{ true:1.0; false:0.0; }"
// extract the value to set
for (TempTableProbabilityCell cell : header.getCellList()) {
if (cell.getPossibleValue() != null) {
cps += cell.getPossibleValue().getName() + ":" + cell.getProbabilityValue() + ";";
} else {
cps += cell.getProbabilityValue() + ";";
try {
Debug.println(getClass(), "The header " + header + " contains a cell with no possible value: " + cell);
} catch (Throwable t) {
t.printStackTrace();
}
break;
}
}
}
// the "}" is only necessary if there is any if clause
if (cpsIfClause.length() > 0) {
cps += " } ";
}
} while (!isLastCombination);
// rollback MFrag settings
for (SSBNNode parent : parentToPreviousMFragMap.keySet()) {
try{
parent.turnArgumentsForMFrag(parentToPreviousMFragMap.get(parent));
} catch (Exception e) {
Debug.println(this.getClass(), parent.toString(), e);
}
}
// the ssbn algorithm should use the returned value and set the node with correct cpt (cps).
// However, since all algorithms are not doing so, we are setting the cps for a given node here