public static ArrayList<String> getTextValue(Element ele, String tagName) { ArrayList<String> returnVal = new ArrayList<String>(); // NodeList nll = ele.getElementsByTagName("*"); NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { Element el = (Element) nl.item(i); returnVal.add(el.getFirstChild().getNodeValue()); } } return returnVal; }
static ArrayList<PVAR_INST_DEF> processXMLAction(DOMParser p, InputSource isrc, State state) throws Exception { try { // showInputSource(isrc); System.exit(1); // TODO p.parse(isrc); Element e = p.getDocument().getDocumentElement(); if (SHOW_XML) { System.out.println("Received action msg:"); printXMLNode(e); } if (!e.getNodeName().equals(ACTIONS)) { System.out.println("ERROR: NO ACTIONS NODE"); System.exit(1); return null; } NodeList nl = e.getElementsByTagName(ACTION); // System.out.println(nl); if (nl != null) { // && nl.getLength() > 0) { // TODO: Scott ArrayList<PVAR_INST_DEF> ds = new ArrayList<PVAR_INST_DEF>(); for (int i = 0; i < nl.getLength(); i++) { Element el = (Element) nl.item(i); String name = getTextValue(el, ACTION_NAME).get(0); ArrayList<String> args = getTextValue(el, ACTION_ARG); ArrayList<LCONST> lcArgs = new ArrayList<LCONST>(); for (String arg : args) { if (arg.startsWith("@")) lcArgs.add(new RDDL.ENUM_VAL(arg)); else lcArgs.add(new RDDL.OBJECT_VAL(arg)); } String pvalue = getTextValue(el, ACTION_VALUE).get(0); Object value = getValue(name, pvalue, state); PVAR_INST_DEF d = new PVAR_INST_DEF(name, value, lcArgs); ds.add(d); } return ds; } else return new ArrayList<PVAR_INST_DEF>(); // FYI: May be unreachable. -Scott // } else { // TODO: Removed by Scott, NOOP should not be handled differently // nl = e.getElementsByTagName(NOOP); // if ( nl != null && nl.getLength() > 0) { // ArrayList<PVAR_INST_DEF> ds = new ArrayList<PVAR_INST_DEF>(); // return ds; // } // } } catch (Exception e) { // TODO Auto-generated catch block System.out.println("FATAL SERVER ERROR:\n" + e); // t.printStackTrace(); throw e; // System.exit(1); } }
static boolean processXMLRoundRequest(DOMParser p, InputSource isrc) { try { p.parse(isrc); Element e = p.getDocument().getDocumentElement(); if (e.getNodeName().equals(ROUND_REQUEST)) { return true; } return false; } catch (SAXException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return false; }
static void processXMLSessionRequest(DOMParser p, InputSource isrc, Server server) { try { p.parse(isrc); Element e = p.getDocument().getDocumentElement(); if (e.getNodeName().equals(SESSION_REQUEST)) { server.requestedInstance = getTextValue(e, PROBLEM_NAME).get(0); server.clientName = getTextValue(e, CLIENT_NAME).get(0); NodeList nl = e.getElementsByTagName(NO_XML_HEADER); if (nl.getLength() > 0) { NO_XML_HEADING = true; } } return; } catch (SAXException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return; }
public static void addOneText(Document dom, Element p, String name, String value) { Element e = dom.createElement(name); Text text = dom.createTextNode(value); e.appendChild(text); p.appendChild(e); }
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; } }