* @param knowledgeBase
* @return
*/
protected SSBN initialization(List<Query> queryList, KnowledgeBase knowledgeBase){
SSBN ssbn = new SSBN();
if (!isLogEnabled()) {
ssbn.setLogManager(null);
}
MultiEntityBayesianNetwork mebn = null;
//log
ISSBNLogManager logManager = ssbn.getLogManager();
if (logManager != null) {
logManager.printBox1Bar();
logManager.printBox1(getResourceForHybridAlgorithm().getString("001_Title"));
logManager.printBox1("Queries:");
for(int i = 0; i < queryList.size(); i++){
logManager.printBox1(" (" + i + ") " + queryList.get(i).toString());
}
logManager.printBox1(getResourceForHybridAlgorithm().getString("002_Algorithm") + ": " +
getResourceForHybridAlgorithm().getString("002_001_LaskeyAlgorithm"));
logManager.printBox1Bar();
logManager.skipLine();
logManager.printText(null, false, getResourceForHybridAlgorithm().getString("003_Step1_Initialization"));
logManager.skipLine();
}
ssbn.setKnowledgeBase(knowledgeBase);
//We assume that all the queries is referent to the same MEBN
mebn = queryList.get(0).getMebn();
//Parameters:
IdentationLevel in = new IdentationLevel(null);
//Add queries to the list of nodes
IdentationLevel in1 = new IdentationLevel(in);
if (logManager != null) {
logManager.printText(in1, true,
getResourceForHybridAlgorithm().getString("011_BuildingSSBNForQueries"));
}
for(Query query: queryList){
SimpleSSBNNode ssbnNode = SimpleSSBNNode.getInstance(query.getResidentNode());
query.setSSBNNode(ssbnNode);
for(OVInstance argument : query.getArguments()){
ssbnNode.setEntityForOv(argument.getOv(), argument.getEntity());
}
ssbnNode.setFinished(false);
ssbn.addSSBNNodeIfItDontAdded(ssbnNode);
ssbn.addQueryToTheQueryList(query);
if (logManager != null) {
logManager.printText(in1, false, " - " + ssbnNode);
}
}
if (logManager != null) {
logManager.skipLine();
}
//Add findings to the list of nodes
in1 = new IdentationLevel(in);
if (logManager != null) {
logManager.printText(in1, true,
getResourceForHybridAlgorithm().getString("012_BuildingSSBNForFindings"));
}
for(MFrag mFrag: mebn.getMFragList()){
for(ResidentNode residentNode: mFrag.getResidentNodeList()){
for(RandomVariableFinding finding: residentNode.getRandomVariableFindingList()){
if (logManager != null) {
logManager.printText(in1, false, " - " + finding);
}
ObjectEntityInstance arguments[] = finding.getArguments();
List<OVInstance> ovInstanceList = new ArrayList<OVInstance>();
for(int i = 0; i < arguments.length ; i++){
OrdinaryVariable ov = residentNode.getArgumentNumber(i + 1).getOVariable();
LiteralEntityInstance lei = LiteralEntityInstance.getInstance(arguments[i].getName() , ov.getValueType());
ovInstanceList.add(OVInstance.getInstance(ov, lei));
}
SimpleSSBNNode ssbnNode = SimpleSSBNNode.getInstance(residentNode);
for(OVInstance argument : ovInstanceList){
ssbnNode.setEntityForOv(argument.getOv(), argument.getEntity());
}
ssbnNode = ssbn.addSSBNNodeIfItDontAdded(ssbnNode);
ssbnNode.setState(finding.getState());
ssbnNode.setFinished(false);
ssbn.addFindingToTheFindingList(ssbnNode);
}
}
}
if (logManager != null) {
logManager.skipLine();
logManager.printText(null, false, getResourceForHybridAlgorithm().getString("010_StepFinished"));
logManager.skipLine();
logManager.printSectionSeparation();
}
// add continuous nodes to the list of initial nodes
if (isToAddAllContinuousNodes()) {
for(MFrag mfrag: mebn.getMFragList()){
// we are creating one continuous SSBN node per each possible combination of arguments in a continuous resident node
for (Node node : mfrag.getNodes()) {
if (node instanceof ContinuousResidentNode) {
ContinuousResidentNode continuousResidentNode = (ContinuousResidentNode) node;
if (continuousResidentNode.getOrdinaryVariableList() == null
|| continuousResidentNode.getOrdinaryVariableList().isEmpty()) {
continue; // go to next node
}
// prepare all possible combinations of arguments. The ovs are synchronized by index
boolean hasAtLeastOneCombination = true; // true if all Lists in combinator were filled
List<List<LiteralEntityInstance>> combinator = new ArrayList<List<LiteralEntityInstance>>();
for (OrdinaryVariable ov : continuousResidentNode.getOrdinaryVariableList()) {
List<LiteralEntityInstance> contentOfCombinator = new ArrayList<LiteralEntityInstance>();
// get all values for ov
List<String> ovValues = knowledgeBase.getEntityByType(ov.getValueType().getName());
if (ovValues.isEmpty()) {
// this iterator cannot be filled, because ov has no possible values
hasAtLeastOneCombination = false;
break;
}
// add all possible values for ov
for (String ovValue : ovValues) {
contentOfCombinator.add(LiteralEntityInstance.getInstance(ovValue, ov.getValueType()));
}
combinator.add(contentOfCombinator);
}
// do not add node if there is some ov with no possible value (that means, no combination can be created)
if (!hasAtLeastOneCombination) {
continue; // go to next continuous node
}
// list of indexes of combinator
List<Integer> indexes = new ArrayList<Integer>(combinator.size());
for (int i = 0; i < combinator.size(); i++) {
// Note that at this point, each list in combinator has at least 1 element, because hasAtLeastOneCombination == true (so, 0 is a valid index)
indexes.add(0);
}
/*
* iterate on combinator using indexes.
* E.g.: combinator = {{a,b},{1,2}};
* then indexes must iterate like:
* {0,0} (means (a,1));
* {1,0} (means (b,1));
* {0,1} (means (a,2));
* {1,1} (means (b,2));
*
*/
while (hasAtLeastOneCombination) {
// create a continuous ssbn node for each possible combination of combinator
SimpleSSBNNode ssbnNode = SimpleSSBNNode.getInstance(continuousResidentNode);
ssbnNode.setFinished(false);
// fill arguments of ssbnNode
for (int i = 0; i < continuousResidentNode.getOrdinaryVariableList().size(); i++) {
ssbnNode.setEntityForOv(
continuousResidentNode.getOrdinaryVariableList().get(i),
combinator.get(i).get(indexes.get(i))
);
}
if (logManager != null) {
logManager.printText(in1, false, " - " + ssbnNode);
}
ssbn.addSSBNNodeIfItDontAdded(ssbnNode);
// update indexes and check condition to end loop (it ends when all possible combinations were visited)
for (int i = 0; i < combinator.size(); i++) {
// OBS. combinator, indexes and continuousResidentNode.getOrdinaryVariableList() have same size and are synchronized.
Integer newIndex = indexes.get(i) + 1; // obtain current step + 1