Package unbbayes.prs.mebn.ssbn

Examples of unbbayes.prs.mebn.ssbn.SSBNNode


   
    //1 Create all the nodes with its states
   
    for(SimpleSSBNNode simple: simpleSSBNNodeList){
     
      SSBNNode ssbnNode = (SSBNNode)this.getNodeTranslator().translate(simple, pn);
      correspondencyMap.put(simple, ssbnNode);
      listSSBNNodes.add(ssbnNode);
     
      //Arguments.
      for(int i = 0; i < simple.getOvArray().length; i++){
        OVInstance ovInstance = OVInstance.getInstance(
            simple.getOvArray()[i],  simple.getEntityArray()[i]);
        ssbnNode.addArgument(ovInstance);
      }
     
      //Finding.
      if(simple.isFinding()){
        ssbnNode.setValue(simple.getState());
      }
     
     
      //Default distribution
      if(simple.isDefaultDistribution()){
        ssbnNode.setUsingDefaultCPT(true);
      }
     
      ssbnNode.setPermanent(true);
     
      //The values of the ordinary variables are different dependeing on what MFrag we are dealing
     
      // lets deal first at resident node's MFrag
      OrdinaryVariable[] residentOvArray = ssbnNode.getResident().getOrdinaryVariableList().toArray(
                              new OrdinaryVariable[ssbnNode.getResident().getOrdinaryVariableList().size()]
                          );
     
      List<OVInstance> argumentsForResidentMFrag = new ArrayList<OVInstance>();
      for(int i = 0; i < residentOvArray.length; i++){
        OVInstance ovInstance = OVInstance.getInstance(residentOvArray[i], simple.getEntityArray()[i]);
        argumentsForResidentMFrag.add(ovInstance);
      }
     
      ssbnNode.addArgumentsForMFrag(
          ssbnNode.getResident().getMFrag(),
          argumentsForResidentMFrag);
     
     
      // lets map OVs of every input node pointing to current SSBNNode
      for(InputNode inputNode: simple.getResidentNode().getInputInstanceFromList()){
        OrdinaryVariable[] ovArray =
          inputNode.getResidentNodePointer().getOrdinaryVariableArray();
       
        List<OVInstance> argumentsForMFrag = new ArrayList<OVInstance>();
        for(int i = 0; i < ovArray.length; i++){
          OVInstance ovInstance = OVInstance.getInstance(ovArray[i], simple.getEntityArray()[i]);
          argumentsForMFrag.add(ovInstance);
        }
       
        ssbnNode.addArgumentsForMFrag(
            inputNode.getMFrag(),
            argumentsForMFrag);
      }
     
      //Treat the context node father
      List<SimpleContextNodeFatherSSBNNode> simpleContextNodeList =
        simple.getContextParents();
     
      if(simpleContextNodeList.size() > 0){
        if(simpleContextNodeList.size() > 1){
          throw new ImplementationRestrictionException(
              ImplementationRestrictionException.MORE_THAN_ONE_CTXT_NODE_SEARCH);
        }else{
          //We have only one context node father
          ContextNode contextNode = simpleContextNodeList.get(0).getContextNode();
          ContextFatherSSBNNode contextFather = mapContextNode.get(contextNode);
          if(contextFather == null){
            contextFather = new ContextFatherSSBNNode(pn, contextNode);
           
            List<LiteralEntityInstance> possibleValueList = new ArrayList<LiteralEntityInstance>();
            for(String entity: simpleContextNodeList.get(0).getPossibleValues()){
              possibleValueList.add(LiteralEntityInstance.getInstance(entity, simpleContextNodeList.get(0).getOvProblematic().getValueType()));
            }
           
            for(LiteralEntityInstance lei: possibleValueList){
              contextFather.addPossibleValue(lei);
            }
           
            contextFather.setOvProblematic(simpleContextNodeList.get(0).getOvProblematic());
            mapContextNode.put(contextNode, contextFather);
          }
         
          try {
            ssbnNode.setContextFatherSSBNNode(contextFather);
          } catch (InvalidParentException e) {
            //This exception don't occur in this case...
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
          }
         
        }
       
      }
      if (simple.isNodeInAVirualChain()) {
        // change the name of chain nodes
        // TODO figure out a better way to avoid conflict
        ssbnNode.getProbNode().setName("Chain"+ simple.getStepsForChainNodeToReachMainNode() + "_" + ssbnNode.getProbNode().getName());
      }
      simple.setProbNode(ssbnNode.getProbNode());
    }
   
    //Create the parent structure
   
    for(SimpleSSBNNode simple: simpleSSBNNodeList){
     
//      if(simple.getParents().size()==0){
//        if(simple.getChildNodes().size()==0){
//          continue; //This node is out of the network.
//        }
//      }
     
      SSBNNode ssbnNode = correspondencyMap.get(simple);
     
      for(SimpleSSBNNode parent: simple.getParents()){
        SSBNNode parentSSBNNode = correspondencyMap.get(parent);
        ssbnNode.addParent(parentSSBNNode, false);
      }
     
    }
   
View Full Code Here


     
      // 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
View Full Code Here

    ssbn.setNetwork(probabilisticNetwork);
   
    speedOBJ1T2 = new ProbabilisticNode();
    speedOBJ1T2.setName(residentSpeed.getName() + "_OBJ1_T2");
    probabilisticNetwork.addNode(speedOBJ1T2);
    SSBNNode ssbnNodeSpeedOBJ1T2 = SSBNNode.getInstance(probabilisticNetwork, residentSpeed, speedOBJ1T2);
    ssbn.getSsbnNodeList().add(ssbnNodeSpeedOBJ1T2);
    ssbnNodeSpeedOBJ1T2.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
    ssbnNodeSpeedOBJ1T2.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("t"), "T2");
   
    // Speed(OBJ1, T0),
//    ProbabilisticNode speedOBJ1T0 = new ProbabilisticNode();
//    speedOBJ1T0.setName(residentSpeed.getName() + "_OBJ1_T0");
//    probabilisticNetwork.addNode(speedOBJ1T0);
//    SSBNNode ssbnNodeSpeedOBJ1T0 = SSBNNode.getInstance(probabilisticNetwork, residentSpeed, speedOBJ1T0);
//    ssbn.getSsbnNodeList().add(ssbnNodeSpeedOBJ1T0);
//    ssbnNodeSpeedOBJ1T0.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
//    ssbnNodeSpeedOBJ1T0.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("tPrev"), "T0");
//    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeSpeedOBJ1T0, false);  // this should also add the edge
   
    // Speed(OBJ1, T1),
    ProbabilisticNode speedOBJ1T1 = new ProbabilisticNode();
    speedOBJ1T1.setName(residentSpeed.getName() + "_OBJ1_T1");
    probabilisticNetwork.addNode(speedOBJ1T1);
    SSBNNode ssbnNodeSpeedOBJ1T1 = SSBNNode.getInstance(probabilisticNetwork, residentSpeed, speedOBJ1T1);
    ssbn.getSsbnNodeList().add(ssbnNodeSpeedOBJ1T1);
    ssbnNodeSpeedOBJ1T1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
    ssbnNodeSpeedOBJ1T1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("tPrev"), "T1");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeSpeedOBJ1T1, false)// this should also add the edge
   
    // TerrainType(RGN1),
    ResidentNode terrainTypeResident = mebn.getDomainResidentNode("TerrainType");
    ProbabilisticNode terrainTypeRGN1 = new ProbabilisticNode();
    terrainTypeRGN1.setName(terrainTypeResident.getName() + "_RGN1");
    probabilisticNetwork.addNode(terrainTypeRGN1);
    SSBNNode ssbnNodeTerrainTypeRGN1 = SSBNNode.getInstance(probabilisticNetwork, terrainTypeResident, terrainTypeRGN1);
    ssbn.getSsbnNodeList().add(ssbnNodeTerrainTypeRGN1);
    ssbnNodeTerrainTypeRGN1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("rgn"), "RGN1");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeTerrainTypeRGN1,false)// this should also add the edge
   
    // TerrainType(RGN2),
    ProbabilisticNode terrainTypeRGN2 = new ProbabilisticNode();
    terrainTypeRGN2.setName(terrainTypeResident.getName() + "_RGN2");
    probabilisticNetwork.addNode(terrainTypeRGN2);
    SSBNNode ssbnNodeTerrainTypeRGN2 = SSBNNode.getInstance(probabilisticNetwork, terrainTypeResident, terrainTypeRGN2);
    ssbn.getSsbnNodeList().add(ssbnNodeTerrainTypeRGN2);
    ssbnNodeTerrainTypeRGN2.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("rgn"), "RGN2");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeTerrainTypeRGN2,false)// this should also add the edge
   
    // TerrainType(RGN3),
//    ProbabilisticNode terrainTypeRGN3 = new ProbabilisticNode();
//    terrainTypeRGN3.setName(terrainTypeResident.getName() + "_RGN3");
//    probabilisticNetwork.addNode(terrainTypeRGN3);
//    SSBNNode ssbnNodeTerrainTypeRGN3 = SSBNNode.getInstance(probabilisticNetwork, terrainTypeResident, terrainTypeRGN3);
//    ssbn.getSsbnNodeList().add(ssbnNodeTerrainTypeRGN3);
//    ssbnNodeTerrainTypeRGN3.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("rgn"), "RGN3");
//    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeTerrainTypeRGN3,false);  // this should also add the edge
   
    // ObjectType(OBJ1)
    ResidentNode objectTypeResident = mebn.getDomainResidentNode("ObjectType");
    ProbabilisticNode objectTypeOBJ1 = new ProbabilisticNode();
    objectTypeOBJ1.setName(objectTypeResident.getName() + "_OBJ1");
    probabilisticNetwork.addNode(objectTypeOBJ1);
    SSBNNode ssbnNodeObjectTypeOBJ1 = SSBNNode.getInstance(probabilisticNetwork, objectTypeResident, objectTypeOBJ1);
    ssbn.getSsbnNodeList().add(ssbnNodeObjectTypeOBJ1);
    ssbnNodeObjectTypeOBJ1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeObjectTypeOBJ1,false)// this should also add the edge
   
    try {
      tableParser = (ContinuousResidentNodeLPDCompiler)ContinuousResidentNodeLPDCompiler.newInstance(residentSpeed, ssbnNodeSpeedOBJ1T2);
    } catch (ClassCastException e) {
View Full Code Here

TOP

Related Classes of unbbayes.prs.mebn.ssbn.SSBNNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.