PrologInterface.java
Go to the documentation of this file.
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 com.google.common.base.Joiner;
00010 
00011 import edu.tum.cs.ias.knowrob.owl.OWLThing;
00012 import edu.tum.cs.ias.knowrob.utils.ros.RosUtilities;
00013 import jpl.*;
00014 
00015 
00016 public class PrologInterface {
00017     
00018         private static boolean initialized = false;
00019 
00020 
00028     public static void initJPLProlog(String initPackage) {
00029         
00030         if(!initialized) {
00031 
00032                 try {
00033                         Vector<String> args= new Vector<String>(Arrays.asList(jpl.fli.Prolog.get_default_init_args()));
00034                         //            args.add( "-G256M" );
00035                         //args.add( "-q" );
00036                         args.add( "-nosignals" );
00037 
00038                         String rosprolog = RosUtilities.rospackFind("rosprolog");
00039                         jpl.fli.Prolog.set_default_init_args( args.toArray( new String[0] ) );
00040 
00041                         // load the appropriate startup file for this context
00042                         new jpl.Query("ensure_loaded('"+rosprolog+"/prolog/init.pl"+"')").oneSolution();
00043                         new jpl.Query("register_ros_package('"+initPackage+"')").oneSolution();
00044 
00045                         initialized = true;
00046                         
00047                 } catch(Exception e) {
00048                         e.printStackTrace();
00049                 }
00050         }
00051     }
00052         
00053         
00060         public static void initLocalProlog(String initFile) {
00061 
00062                 if(!PrologInterface.isInitialized()) {
00063                         try {
00064                                 Vector<String> args= new Vector<String>(Arrays.asList(jpl.fli.Prolog.get_default_init_args()));
00065                                 // args.add( "-G256M" );
00066                                 // args.add( "-q" );
00067                                 args.add( "-nosignals" );
00068                                 jpl.fli.Prolog.set_default_init_args( args.toArray( new String[0] ) );
00069 
00070                                 // load the appropriate startup file for this context
00071                                 new jpl.Query("ensure_loaded('"+initFile+"')").oneSolution();
00072 
00073                                 PrologInterface.setInitialized(true);
00074                                 
00075                         } catch(Exception e) {
00076                                 e.printStackTrace();
00077                         }
00078                 }
00079         }
00080     
00089         public static HashMap<String, Vector<String>> executeQuery(String query) {
00090                 
00091                 HashMap<String, Vector<String>> result = new HashMap< String, Vector<String> >();
00092                 @SuppressWarnings("rawtypes")
00093                 Hashtable[] solutions;
00094 
00095                 synchronized(jpl.Query.class) {
00096                 
00097                 Query q = new Query( "expand_goal(("+query+"),_9), call(_9)" );
00098 
00099                 if(!q.hasSolution())
00100                         return null;
00101                 
00102                 
00103                 solutions = q.allSolutions();
00104                 
00105                 if(solutions == null || solutions.length==0) // case: success, but no variable bindings
00106                         return result;
00107                 
00108                 for (Object key: solutions[0].keySet()) {
00109                         result.put(key.toString(), new Vector<String>());
00110                 }
00111                 
00112                 // Build the result
00113                 for (int i=0; i<solutions.length; i++) {
00114                         @SuppressWarnings("rawtypes")
00115                                 Hashtable solution = solutions[i];
00116                         for (Object key: solution.keySet()) {
00117                                 String keyStr = key.toString();
00118                                 if (!result.containsKey( keyStr )) {
00119 
00120                                         // previously unknown column, add result vector
00121                                         Vector<String> resultVector = new Vector<String>(); 
00122                                         resultVector.add( i, solution.get( key ).toString() );
00123                                         result.put(keyStr, resultVector);
00124 
00125                                 }
00126                                 // Put the solution into the correct vector
00127                                 Vector<String> resultVector = result.get( keyStr );
00128                                 resultVector.add( i, solution.get( key ).toString() );
00129                         }
00130                 }
00131                 }
00132                 // Generate the final QueryResult and return
00133                 return result;
00134         }
00135         
00142     public static ArrayList<String[]> dottedPairsToArrayList(String rest) {
00143         
00144         ArrayList<String[]> bindings = new ArrayList<String[]>();
00145         while(rest.length()>0) {
00146 
00147             String[] l = rest.split("'\\.'", 2);
00148             if((l[0].equals("")) || (l[0].equals("("))) {
00149                rest=l[1]; continue;
00150 
00151             } else {
00152                 if (l[0].length() > 2)                  
00153                         bindings.add(new String[]{l[0].substring(1, l[0].length()-2).split(", ")[0]});
00154                                 
00155                 if(l.length>1) {
00156                     rest=l[1];  continue;
00157                 } else break;
00158             }
00159             
00160         }
00161         return bindings;
00162       }
00163 
00164     
00174     public static String removeSingleQuotes(String str) {
00175         if(str.startsWith("'"))
00176             str = str.substring(1);
00177         
00178         if(str.endsWith("'"))
00179             str = str.substring(0, str.length()-1);
00180         return str;
00181     }
00182     
00183 
00194     public static String addSingleQuotes(String str) {
00195         return "'"+removeSingleQuotes(str)+"'";
00196     }
00197     
00198     
00207     public static String valueFromIRI(String iri) {
00208         String[] ks = iri.split("#");
00209            if(ks.length>1) {
00210                String res = ks[1].replaceAll("'", "");
00211                return res;
00212            }
00213            else return iri;
00214      }
00215     
00224         public static String prefixFromIRI(String iri) {
00225                 
00226         String[] ks = iri.split("#");
00227         if(ks.length>1) {
00228             String res = ks[0].replaceAll("'", "");
00229             return res;
00230         }
00231         else return iri;
00232         }
00233         
00242         public static String fileNameFromIRI(String iri) {
00243                 
00244         String[] ks = iri.split("/");
00245         if(ks.length>1) {
00246             String res = ks[ks.length-1].replaceAll("'", "");
00247             return res;
00248         }
00249         else return iri;
00250         }
00251         
00252     
00260     public static String stripLiteralType(String typedValue) {
00261 
00262         if(!typedValue.startsWith("literal("))
00263                 return typedValue;
00264         
00265         typedValue = typedValue.substring(13, typedValue.length()-2);
00266 
00267         // remove literal(type( and closing parentheses
00268         
00269         String[] parts = typedValue.split(",");
00270         String[] clean = Arrays.copyOfRange(parts, 1, parts.length);
00271         String cleanValue = Joiner.on(",").join(clean);
00272         
00273         if(cleanValue.startsWith(" "))
00274                 cleanValue = cleanValue.substring(1);
00275         
00276         return removeSingleQuotes(cleanValue);
00277     }
00278 
00279     
00286     public static String getLiteralType(String typedValue) {
00287 
00288         if(!typedValue.startsWith("literal("))
00289                 return null;
00290         
00291         typedValue = typedValue.substring(13, typedValue.length()-2);
00292 
00293         String[] parts = typedValue.split(",");
00294         String cleanValue = parts[0];
00295         
00296         if(cleanValue.startsWith(" "))
00297                 cleanValue = cleanValue.substring(1);
00298         
00299         return removeSingleQuotes(cleanValue);
00300     }
00301     
00302         
00308     public static boolean isInitialized() {
00309                 return initialized;
00310         }
00311 
00312 
00319         public static void setInitialized(boolean initialized) {
00320                 PrologInterface.initialized = initialized;
00321         }
00322     
00323 }
00324 


knowrob_common
Author(s): Moritz Tenorth
autogenerated on Mon Oct 6 2014 01:29:31