Ejemplo n.º 1
0
 static String createXMLRoundInit(int round, int numRounds, double timeUsed, double timeAllowed) {
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   try {
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document dom = db.newDocument();
     Element rootEle = dom.createElement(ROUND_INIT);
     dom.appendChild(rootEle);
     addOneText(dom, rootEle, ROUND_NUM, round + "");
     addOneText(dom, rootEle, ROUND_LEFT, (numRounds - round) + "");
     addOneText(dom, rootEle, TIME_LEFT, (timeAllowed - timeUsed) + "");
     return Client.serialize(dom);
   } catch (Exception e) {
     System.out.println(e);
     return null;
   }
 }
Ejemplo n.º 2
0
 static String createXMLSessionInit(int numRounds, double timeAllowed, Server server) {
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   try {
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document dom = db.newDocument();
     Element rootEle = dom.createElement(SESSION_INIT);
     dom.appendChild(rootEle);
     addOneText(dom, rootEle, SESSION_ID, server.id + "");
     addOneText(dom, rootEle, NUM_ROUNDS, numRounds + "");
     addOneText(dom, rootEle, TIME_ALLOWED, timeAllowed + "");
     return Client.serialize(dom);
   } catch (Exception e) {
     System.out.println(e);
     return null;
   }
 }
Ejemplo n.º 3
0
 static String createXMLRoundEnd(int round, double reward, int turnsUsed, double timeUsed) {
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   try {
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document dom = db.newDocument();
     Element rootEle = dom.createElement(ROUND_END);
     dom.appendChild(rootEle);
     addOneText(dom, rootEle, ROUND_NUM, round + "");
     addOneText(dom, rootEle, ROUND_REWARD, reward + "");
     addOneText(dom, rootEle, TURNS_USED, turnsUsed + "");
     addOneText(dom, rootEle, TIME_USED, timeUsed + "");
     return Client.serialize(dom);
   } catch (Exception e) {
     System.out.println(e);
     return null;
   }
 }
Ejemplo n.º 4
0
 static String createXMLSessionEnd(
     double reward, int roundsUsed, double timeUsed, String clientName, int sessionId) {
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   try {
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document dom = db.newDocument();
     Element rootEle = dom.createElement(SESSION_END);
     dom.appendChild(rootEle);
     addOneText(dom, rootEle, TOTAL_REWARD, reward + "");
     addOneText(dom, rootEle, ROUNDS_USED, roundsUsed + "");
     addOneText(dom, rootEle, TIME_USED, timeUsed + "");
     addOneText(dom, rootEle, CLIENT_NAME, clientName + "");
     addOneText(dom, rootEle, SESSION_ID, sessionId + "");
     return Client.serialize(dom);
   } catch (Exception e) {
     System.out.println(e);
     return null;
   }
 }
Ejemplo n.º 5
0
  static String createXMLTurn(
      State state,
      int turn,
      DOMAIN domain,
      HashMap<PVAR_NAME, HashMap<ArrayList<LCONST>, Object>> observStore)
      throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document dom = db.newDocument();
      Element rootEle = dom.createElement(TURN);
      dom.appendChild(rootEle);
      Element turnNum = dom.createElement(TURN_NUM);
      Text textTurnNum = dom.createTextNode(turn + "");
      turnNum.appendChild(textTurnNum);
      rootEle.appendChild(turnNum);

      // System.out.println("PO: " + domain._bPartiallyObserved);
      if (!domain._bPartiallyObserved || observStore != null) {
        for (PVAR_NAME pn :
            (domain._bPartiallyObserved ? observStore.keySet() : state._state.keySet())) {
          // System.out.println(turn + " check2 Partial Observ " + pn +" : "+
          // domain._bPartiallyObserved);

          // No problem to overwrite observations, only ever read from
          if (domain._bPartiallyObserved && observStore != null)
            state._observ.put(pn, observStore.get(pn));

          ArrayList<ArrayList<LCONST>> gfluents = state.generateAtoms(pn);
          for (ArrayList<LCONST> gfluent : gfluents) {
            // for ( Map.Entry<ArrayList<LCONST>,Object> gfluent :
            //	(domain._bPartiallyObserved
            //			? observStore.get(pn).entrySet()
            //					: state._state.get(pn).entrySet())) {
            Element ofEle = dom.createElement(OBSERVED_FLUENT);
            rootEle.appendChild(ofEle);
            Element pName = dom.createElement(FLUENT_NAME);
            Text pTextName = dom.createTextNode(pn.toString());
            pName.appendChild(pTextName);
            ofEle.appendChild(pName);
            for (LCONST lc : gfluent) {
              Element pArg = dom.createElement(FLUENT_ARG);
              Text pTextArg = dom.createTextNode(lc.toString());
              pArg.appendChild(pTextArg);
              ofEle.appendChild(pArg);
            }
            Element pValue = dom.createElement(FLUENT_VALUE);
            Object value = state.getPVariableAssign(pn, gfluent);
            if (value == null) {
              System.out.println("STATE:\n" + state);
              throw new Exception("ERROR: Could not retrieve value for " + pn + gfluent.toString());
            }

            Text pTextValue = dom.createTextNode(value.toString());
            pValue.appendChild(pTextValue);
            ofEle.appendChild(pValue);
          }
        }
      } else {
        // No observations (first turn of POMDP)
        Element ofEle = dom.createElement(NULL_OBSERVATIONS);
        rootEle.appendChild(ofEle);
      }
      if (SHOW_XML) {
        printXMLNode(dom);
        System.out.println();
        System.out.flush();
      }
      return (Client.serialize(dom));

    } catch (Exception e) {
      System.out.println("FATAL SERVER EXCEPTION: " + e);
      e.printStackTrace();
      throw e;
      // System.exit(1);
      // return null;
    }
  }