$search
00001 /* 00002 * Copyright (c) 2010, Moritz Tenorth 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 package edu.tum.cs.ias.knowrob.json_prolog; 00031 00032 import java.util.HashMap; 00033 import java.util.Vector; 00034 00035 import jpl.Term; 00036 00037 import edu.tum.cs.ias.knowrob.json_prolog.JSONQuery.InvalidJSONQuery; 00038 00039 import net.sf.json.JSONArray; 00040 import net.sf.json.JSONObject; 00041 import net.sf.json.util.JSONUtils; 00042 00043 00050 public class PrologBindings { 00051 00052 00053 private HashMap<String, PrologValue> bdgs_; 00054 00055 00056 public PrologBindings() { 00057 bdgs_ = new HashMap<String, PrologValue>(); 00058 } 00059 00066 static public PrologBindings parseJSONBindings(String json_bdgs) { 00067 00068 PrologBindings res = new PrologBindings(); 00069 if(json_bdgs!=null) { 00070 00071 JSONObject json_obj = JSONObject.fromObject(json_bdgs); 00072 for(Object k : json_obj.keySet()) { 00073 00074 Object bdg = json_obj.get(k.toString()); 00075 00076 try { 00077 00078 PrologValue r = new PrologValue(); 00079 00080 if(JSONUtils.isArray(bdg)) { 00081 00082 Term[] objs = JSONQuery.decodeJSONArray(JSONArray.fromObject(bdg)); 00083 00084 Vector<PrologValue> values = new Vector<PrologValue>(); 00085 for(Term o : objs) { 00086 values.add(new PrologValue(o.toString())); 00087 } 00088 r = new PrologValue(values); 00089 00090 } else if(JSONUtils.isObject(bdg)) { 00091 Term obj = JSONQuery.decodeJSONValue(JSONObject.fromObject(bdg)); 00092 r = new PrologValue(obj.toString()); 00093 00094 } else if(JSONUtils.isString(bdg)) { 00095 r = new PrologValue(bdg.toString()); 00096 00097 } else if(JSONUtils.isNumber(bdg)) { 00098 r = new PrologValue(Double.valueOf(bdg.toString())); 00099 00100 } 00101 res.bdgs_.put(k.toString(), r); 00102 00103 } catch (InvalidJSONQuery e) { 00104 e.printStackTrace(); 00105 } 00106 00107 } 00108 00109 } 00110 return res; 00111 } 00112 00113 00114 public HashMap<String, PrologValue> getBdgs_() { 00115 return bdgs_; 00116 } 00117 00118 00119 public void setBdgs_(HashMap<String, PrologValue> bdgs) { 00120 bdgs_ = bdgs; 00121 } 00122 00123 00124 class VariableUnbound extends Exception { 00125 00126 private static final long serialVersionUID = -5329179598962011281L; 00127 00128 public VariableUnbound(String var_name) { 00129 super(var_name); 00130 } 00131 }; 00132 00133 00134 class JSONParseError extends Exception { 00135 00136 private static final long serialVersionUID = 6704618653787680364L; 00137 00138 public JSONParseError(String msg) { 00139 super(msg); 00140 } 00141 }; 00142 00143 00144 }