00001 package edu.tum.cs.ias.knowrob.prolog;
00002
00003 import java.util.ArrayList;
00004 import java.util.Arrays;
00005 import java.util.HashMap;
00006 import java.util.Hashtable;
00007 import java.util.Vector;
00008
00009 import edu.tum.cs.ias.knowrob.utils.ros.RosUtilities;
00010 import jpl.*;
00011
00012
00013 public class PrologInterface {
00014
00022 public static void initJPLProlog(String initPackage) {
00023 try {
00024 Vector<String> args= new Vector<String>(Arrays.asList(jpl.fli.Prolog.get_default_init_args()));
00025 args.add( "-G256M" );
00026
00027 args.add( "-nosignals" );
00028
00029 String rosprolog = RosUtilities.rospackFind("rosprolog");
00030 System.err.println(rosprolog+"/prolog/init.pl");
00031 jpl.fli.Prolog.set_default_init_args( args.toArray( new String[0] ) );
00032
00033
00034 new jpl.Query("ensure_loaded('"+rosprolog+"/prolog/init.pl"+"')").oneSolution();
00035 new jpl.Query("register_ros_package('"+initPackage+"')").oneSolution();
00036
00037 } catch(Exception e) {
00038 e.printStackTrace();
00039 }
00040 }
00041
00042
00050 public static HashMap<String, Vector<String>> executeQuery(String query) {
00051
00052 HashMap<String, Vector<String>> result = new HashMap< String, Vector<String> >();
00053 Hashtable[] solutions;
00054
00055 synchronized(jpl.Query.class) {
00056
00057 Query q = new Query( "expand_goal(("+query+"),_9), call(_9)" );
00058
00059 if(!q.hasSolution())
00060 return new HashMap<String, Vector<String>>();
00061
00062
00063 solutions = q.allSolutions();
00064 for (Object key: solutions[0].keySet()) {
00065 result.put(key.toString(), new Vector<String>());
00066 }
00067
00068
00069 for (int i=0; i<solutions.length; i++) {
00070 Hashtable solution = solutions[i];
00071 for (Object key: solution.keySet()) {
00072 String keyStr = key.toString();
00073 if (!result.containsKey( keyStr )) {
00074
00075
00076 Vector<String> resultVector = new Vector<String>();
00077 resultVector.add( i, solution.get( key ).toString() );
00078 result.put(keyStr, resultVector);
00079
00080 }
00081
00082 Vector<String> resultVector = result.get( keyStr );
00083 resultVector.add( i, solution.get( key ).toString() );
00084 }
00085 }
00086 }
00087
00088 return result;
00089 }
00090
00097 public static ArrayList<String[]> dottedPairsToArrayList(String rest) {
00098
00099 ArrayList<String[]> bindings = new ArrayList<String[]>();
00100 while(rest.length()>0) {
00101 String[] l = rest.split("'\\.'", 2);
00102
00103 if((l[0].equals("")) || (l[0].equals("("))) {
00104 rest=l[1]; continue;
00105
00106 } else {
00107 bindings.add(new String[]{l[0].substring(1, l[0].length()-2).split(", ")[0]});
00108
00109 if(l.length>1) {
00110 rest=l[1]; continue;
00111 } else break;
00112 }
00113
00114 }
00115 return bindings;
00116 }
00117
00118
00128 public static String removeSingleQuotes(String str) {
00129 if(str.startsWith("'"))
00130 str = str.substring(1);
00131
00132 if(str.endsWith("'"))
00133 str = str.substring(0, str.length()-1);
00134 return str;
00135 }
00136
00137
00147 public static String addSingleQuotes(String str) {
00148 return "'"+removeSingleQuotes(str)+"'";
00149 }
00150
00151
00159 public static String valueFromIRI(String iri) {
00160 String[] ks = iri.split("#");
00161 if(ks.length>1) {
00162 String res = ks[1].replaceAll("'", "");
00163 return res;
00164 }
00165 else return iri;
00166 }
00167
00168 }