From: <seb...@us...> - 2012-03-18 21:44:37
|
Revision: 3615 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3615&view=rev Author: sebastianwtr Date: 2012-03-18 21:44:30 +0000 (Sun, 18 Mar 2012) Log Message: ----------- [tbsl exploration] improved some functions Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/Index_utils.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java Added Paths: ----------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java Removed Paths: ------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/GetRessourcePropertys.java Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/Index_utils.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/Index_utils.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/Index_utils.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -2,6 +2,8 @@ import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; public class Index_utils { @@ -15,7 +17,7 @@ */ public static ArrayList<String> searchIndex(String string, int fall, SQLiteIndex myindex) throws SQLException{ - String originalString=string; + string=string.replace("_", " "); string=string.replace("-", " "); string=string.replace(".", " "); @@ -54,8 +56,8 @@ public static ArrayList<String> searchIndexForResource(String string, SQLiteIndex myindex) throws SQLException{ - - string=string.replace("_", " "); + HashMap<String,Float> hm = new HashMap<String,Float>(); +string=string.replace("_", " "); string=string.replace("-", " "); string=string.replace(".", " "); String result=null; @@ -82,6 +84,15 @@ } public static ArrayList<String> searchIndexForProperty(String string, SQLiteIndex myindex) throws SQLException{ + HashMap<String,Float> hm = new HashMap<String,Float>(); + + // adding or set elements in Map by put method key and value pair + /* + * + * // autoboxing takes care of that. +map.put(23, 2.5f); +map.put(64, 4.83f); + */ string=string.replace("_", " "); string=string.replace("-", " "); @@ -91,14 +102,17 @@ result=myindex.getPropertyURI(string.toLowerCase()); if(result!=null){ result_List.add(result); + hm.put(result, 1.0f); } else{ result_List.add("http://dbpedia.org/ontology/"+string.toLowerCase().replace(" ", "_")); + hm.put(result, 0.0f); } return result_List; + //return hm; } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -19,7 +19,7 @@ public class SQLiteIndex { private Connection conn; StanfordLemmatizer lemma; - double lim_levensthein=0.84; + double lim_levensthein=0.88; public SQLiteIndex() throws ClassNotFoundException, SQLException { // TODO Auto-generated constructor stub Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -28,8 +28,8 @@ import net.didion.jwnl.data.POS; import org.dllearner.algorithm.tbsl.exploration.Index.SQLiteIndex; -import org.dllearner.algorithm.tbsl.exploration.Utils.GetRessourcePropertys; import org.dllearner.algorithm.tbsl.exploration.Utils.Levenshtein; +import org.dllearner.algorithm.tbsl.exploration.Utils.ServerUtil; import org.dllearner.algorithm.tbsl.nlp.StanfordLemmatizer; import org.dllearner.algorithm.tbsl.nlp.WordNet; import org.dllearner.algorithm.tbsl.sparql.BasicQueryTemplate; @@ -582,11 +582,11 @@ if(tmpcounter <=number_of_x_used){ HashMap<String,String> propertiesleft = new HashMap<String, String>(); HashMap<String,String> propertiesright = new HashMap<String, String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); + // GetRessourcePropertys property = new GetRessourcePropertys(); try { - propertiesleft=property.getPropertys(s,"LEFT",timeToTimeoutOnServer); - propertiesright=property.getPropertys(s,"RIGHT",timeToTimeoutOnServer); + propertiesleft=ServerUtil.sendServerPropertyRequest(s,"LEFT"); + propertiesright=ServerUtil.sendServerPropertyRequest(s,"RIGHT"); } catch (Exception e){ @@ -709,7 +709,6 @@ HashMap<String,String> properties = new HashMap<String, String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); Boolean goOnAfterProperty = true; @@ -721,7 +720,7 @@ //gets Propertys left or right from the resource! try { - properties=property.getPropertys(queryObject.getHashValue(resource.toLowerCase()),sideOfProperty,timeToTimeoutOnServer); + properties=ServerUtil.sendServerPropertyRequest(queryObject.getHashValue(resource.toLowerCase()),sideOfProperty); if (properties==null){ System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); @@ -844,14 +843,13 @@ } HashMap<String,String> propertiesOne = new HashMap<String, String>(); HashMap<String,String> propertiesTwo = new HashMap<String, String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); Boolean goOnAfterProperty = true; //Get Properties for Resource in condition One and Two from Server try { - propertiesOne=property.getPropertys(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne,timeToTimeoutOnServer); - propertiesTwo=property.getPropertys(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo,timeToTimeoutOnServer); + propertiesOne=ServerUtil.sendServerPropertyRequest(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); + propertiesTwo=ServerUtil.sendServerPropertyRequest(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); if (propertiesOne==null){ System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -13,6 +13,7 @@ private String OrderBy; private String limit; private String question; + private String queryType; private float overallTime; private float time_Templator; @@ -49,7 +50,7 @@ } - public Template(ArrayList<ArrayList<String>>condition_new, String having_new, String filter_new, String SelectTerm_new, String OrderBy_new, String limit_new, String question_new){ + public Template(ArrayList<ArrayList<String>>condition_new, String queryType_new, String having_new, String filter_new, String SelectTerm_new, String OrderBy_new, String limit_new, String question_new){ this.setCondition(condition_new); this.setHaving(having_new); this.setFilter(filter_new); @@ -57,6 +58,7 @@ this.setLimit(limit_new); this.setSelectTerm(SelectTerm_new); this.setQuestion(question_new); + this.setQueryType(queryType_new); } public ArrayList<ArrayList<String>> getCondition() { return condition; @@ -90,6 +92,7 @@ } } System.out.print("\n"); + System.out.println("QueryType "+queryType); System.out.println("selectTerm: "+selectTerm); System.out.println("having: "+having); System.out.println("filter: "+filter); @@ -125,6 +128,12 @@ public void addToList_of_element_uri_pair(ElementList_new newElement) { this.list_of_element_uri_pair.add(newElement); } + public String getQueryType() { + return queryType; + } + public void setQueryType(String queryType) { + this.queryType = queryType; + } } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -160,7 +160,7 @@ * SLOT_title: PROPERTY {title,name,label} mitfuehren */ //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - Template template = new Template(condition, having, filter, selectTerm,OrderBy, limit,question); + Template template = new Template(condition,bqt.getQt().toString(), having, filter, selectTerm,OrderBy, limit,question); boolean add_reverse_template = true; @@ -226,12 +226,12 @@ ArrayList<ArrayList<Hypothesis>> final_list_set_hypothesis = new ArrayList<ArrayList<Hypothesis>>(); //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - /*System.out.println("Alle Hypothesen VOR der Verarbeitung"); + /* System.out.println("Alle Hypothesen VOR der Verarbeitung"); for(Hypothesis x : list_of_hypothesis){ x.printAll(); } - System.out.println("Alle Hypothesen VOR der Verarbeitung - Done \n\n"); - */ + System.out.println("Alle Hypothesen VOR der Verarbeitung - Done \n\n");*/ + for(Hypothesis x : list_of_hypothesis){ /* * TODO: Change if ISA only ask classes, else resource @@ -258,16 +258,19 @@ if(s!=null){ Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), s, h.getType(), 1); new_list.add(new_h); + //new_h.printAll(); } else{ Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), h.getUri(), h.getType(), 1); new_list.add(new_h); + //new_h.printAll(); } } else{ Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), h.getUri(), h.getType(), h.getRank()); new_list.add(new_h); + //new_h.printAll(); } } final_list_set_hypothesis.add(new_list); @@ -275,8 +278,14 @@ } } + /*System.out.println("Alle Hypothesen nach der ERSTEN Verarbeitung"); + for(ArrayList<Hypothesis> lh : final_list_set_hypothesis){ + for(Hypothesis x : lh){ + x.printAll(); + } + } + System.out.println("Alle Hypothesen nach der ERSTEN Verarbeitung - Done \n\n");*/ - //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /* @@ -288,7 +297,7 @@ for(Hypothesis h : x){ - + //h.printAll(); //only if you have a Property or an Unspec, which still has no http:/dbpedia etc //if(h.getType().contains("PROPERTY") || (h.getType().contains("UNSPEC")&& !h.getUri().contains("http"))){ @@ -321,11 +330,23 @@ } } + /*System.out.println("Alle Hypothesen nach der ZWEITEN Verarbeitung"); + for(ArrayList<Hypothesis> lh : final_list_set_hypothesis){ + for(Hypothesis x : lh){ + x.printAll(); + } + } + + System.out.println("Alle Hypothesen nach der ZWEITEN Verarbeitung - Done \n\n");*/ + + + + template.setHypothesen(final_list_set_hypothesis); - Template template_reverse_conditions = new Template(template.getCondition(), template.getHaving(), template.getFilter(), template.getSelectTerm(), template.getOrderBy(), template.getLimit(), template.getQuestion()); + Template template_reverse_conditions = new Template(template.getCondition(),template.getQueryType(), template.getHaving(), template.getFilter(), template.getSelectTerm(), template.getOrderBy(), template.getLimit(), template.getQuestion()); ArrayList<ArrayList<String>> condition_template_reverse_conditions = template_reverse_conditions.getCondition(); ArrayList<ArrayList<String>> condition_reverse_new= new ArrayList<ArrayList<String>>(); @@ -353,9 +374,13 @@ if(add_reverse_template) resultArrayList.add(template_reverse_conditions); } } - /*for(Template temp : resultArrayList){ + + /*System.out.println("Nach allen Verarbeitungsschritten:"); + for(Template temp : resultArrayList){ temp.printAll(); - }*/ + } + + System.out.println("Nach allen Verarbeitungsschritten - DONE");*/ return resultArrayList; } } Deleted: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/GetRessourcePropertys.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/GetRessourcePropertys.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/GetRessourcePropertys.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -1,159 +0,0 @@ -package org.dllearner.algorithm.tbsl.exploration.Utils; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashMap; - - -public class GetRessourcePropertys { - - //String Prefix="http://greententacle.techfak.uni-bielefeld.de:5171/sparql"; - String Prefix="http://dbpedia.org/sparql"; - //String Prefix="http://purpurtentacle.techfak.uni-bielefeld.de:8890/sparql"; - //String Prefix="http://greententacle.techfak.uni-bielefeld.de:5171/sparql"; - - public HashMap<String,String> getPropertys(String element, String side, int timeToTimeoutOnServer) throws IOException{ - - return sendServerPropertyRequest(element,side, timeToTimeoutOnServer); - - - - } - - /** - * Get an uri and saves the properties of this resource - * @param vergleich - * @return - * @throws IOException - */ - private HashMap<String,String> sendServerPropertyRequest(String vergleich, String side, int timeToTimeoutOnServer) throws IOException{ - - String vergleichorig = vergleich; - - String tmp_left=Prefix+"?default-graph-uri=&query="+createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {?y ?p <"+vergleichorig+">. ?p rdfs:label ?s.}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; - - String tmp_right=Prefix+"?default-graph-uri=&query="+createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {<"+vergleichorig+"> ?p ?y. ?p rdfs:label ?s.}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; - - String tmp_both=Prefix+"?default-graph-uri=&query="+createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {{?y ?p <"+vergleichorig+">. ?p rdfs:label ?s.} UNION {<"+vergleichorig+"> ?p ?y. ?p rdfs:label ?s.}}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; - String verarbeitungsurl=null; - - /*Original*/ - if(side.contains("RIGHT")) verarbeitungsurl=tmp_right; - if(side.contains("LEFT")) verarbeitungsurl=tmp_left; - if(side.contains("BOTH")) verarbeitungsurl=tmp_both; - - if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsurl=tmp_left; - - String result=""; - HttpURLConnection connection = null; - BufferedReader rd = null; - StringBuilder sb = null; - String line = null; - - URL serverAddress = null; - - try { - serverAddress = new URL(verarbeitungsurl); - //set up out communications stuff - connection = null; - - //Set up the initial connection - connection = (HttpURLConnection)serverAddress.openConnection(); - connection.setRequestMethod("GET"); - connection.setDoOutput(true); - connection.setReadTimeout(timeToTimeoutOnServer); - - connection.connect(); - rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); - sb = new StringBuilder(); - - while ((line = rd.readLine()) != null) - { - sb.append(line + '\n'); - } - - result=sb.toString(); - - } catch (MalformedURLException e) { - System.out.println("Must enter a valid URL"); - } catch (IOException e) { - System.out.println("Can not connect or timeout"); - } - finally - { - //close the connection, set all objects to null - connection.disconnect(); - rd = null; - sb = null; - connection = null; - } - - HashMap<String,String> hm = new HashMap<String,String>(); - result=result.replace("<th>s</th>",""); - result=result.replace("<th>p</th>",""); - result=result.replace("<table class=\"sparql\" border=\"1\">",""); - result=result.replace("<tr>",""); - result=result.replace("</tr>",""); - result=result.replace("\n", ""); - result=result.replace(" ", ""); - result=result.replaceFirst("<td>", ""); - - - String[] tmp_array=result.split("</td><td>"); - - for(int i =1; i<=tmp_array.length-2;i=i+2) { - hm.put(tmp_array[i-1].toLowerCase(), tmp_array[i]); - } - - return hm; - } - - - - - - private String createServerRequest(String query){ - String anfrage=null; - anfrage=removeSpecialKeys(query); - anfrage=anfrage.replace("<","<"); - anfrage=anfrage.replace("%gt;",">"); - anfrage=anfrage.replace("&","&"); - //anfrage=anfrage.replaceAll("#>","%23%3E%0D%0A%"); - anfrage=anfrage.replace("#","%23"); - anfrage=anfrage.replace(" ","+"); - anfrage=anfrage.replace("/","%2F"); - anfrage=anfrage.replace(":","%3A"); - anfrage=anfrage.replace("?","%3F"); - anfrage=anfrage.replace("$","%24"); - //anfrage=anfrage.replaceAll("F>+","F%3E%0D%0A"); - anfrage=anfrage.replace(">","%3E"); - anfrage=anfrage.replace("<","%3C"); - anfrage=anfrage.replace("\"","%22"); - anfrage=anfrage.replace("\n","%0D%0A%09"); - anfrage=anfrage.replace("%%0D%0A%09","%09"); - anfrage=anfrage.replace("=","%3D"); - anfrage=anfrage.replace("@","%40"); - anfrage=anfrage.replace("&","%26"); - anfrage=anfrage.replace("(","%28"); - anfrage=anfrage.replace(")","%29"); - anfrage=anfrage.replace("%3E%0D%0A%25","%3E"); - //anfrage=anfrage.replaceAll("\n",".%0D%0A%09"); - return anfrage; - } - - private String removeSpecialKeys(String query){ - query=query.replace("\\",""); - //query=query.replaceAll("\a",""); - query=query.replace("\b",""); - query=query.replace("\f",""); - query=query.replace("\r",""); - query=query.replace("\t",""); - // query=query.replaceAll("\v",""); - return query; - } - -} Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -0,0 +1,63 @@ +package org.dllearner.algorithm.tbsl.exploration.Utils; + +import java.util.ArrayList; + +import org.dllearner.algorithm.tbsl.exploration.Sparql.Hypothesis; +import org.dllearner.algorithm.tbsl.exploration.Sparql.Template; + +public class Query { + + /** + * Creates Queries + * @param t + * @return + */ + public static ArrayList<QueryPair> returnSetOfQueries(Template t){ + + ArrayList<QueryPair> queryList = new ArrayList<QueryPair>(); + /* + * TODO: Generate a Query for each Hypothesenset, replacing the variable in the condition with the uri from the hypothesenset in <> + */ + String condition=""; + for(ArrayList<String> conditionList :t.getCondition()){ + for(String s : conditionList) condition+=s+" "; + //to End a triple of variables + condition+="."; + } + /* + * Now replacing varibale with the uri from the Hypot. + */ + for(ArrayList<Hypothesis> hypothesenList : t.getHypothesen()){ + String condition_new = condition; + //System.out.println("New_Condition before replacing "+condition_new); + float global_rank=0; + boolean addQuery=true; + for(Hypothesis h : hypothesenList){ + condition_new=condition_new.replace(h.getVariable(), "<"+h.getUri()+">"); + /* + * Dont create a Query with variables, which dont have a correct uri + */ + if(!h.getUri().contains("http")){ + addQuery=false; + } + condition_new=condition_new.replace("isA", "rdf:type"); + global_rank=global_rank+h.getRank(); + } + //System.out.println("New_Condition after replacing "+condition_new); + String query="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+t.getQueryType()+" "+t.getSelectTerm()+" WHERE {"+ condition_new+" "+ t.getFilter()+"}"+t.getOrderBy()+" "+t.getHaving() +" "+t.getLimit(); + QueryPair qp = new QueryPair(query,global_rank); + if(addQuery)queryList.add(qp); + } + + + return queryList; + } + + /*public static QueryPair getBestQuery(Template t){ + ArrayList<QueryPair> qp = returnSetOfQueries(t); + + + return ; + }*/ + +} Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -0,0 +1,30 @@ +package org.dllearner.algorithm.tbsl.exploration.Utils; + +public class QueryPair { + private String Query; + private float rank; + public String getQuery() { + return Query; + } + public void setQuery(String query) { + Query = query; + } + public float getRank() { + return rank; + } + public void setRank(float rank) { + this.rank = rank; + } + + public void printAll(){ + System.out.println("Query :"+this.getQuery()); + System.out.println("Rank :"+this.getRank()); + } + + public QueryPair(String query_new, float rank_new){ + this.setQuery(query_new); + this.setRank(rank_new); + } + + +} Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -0,0 +1,163 @@ +package org.dllearner.algorithm.tbsl.exploration.Utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; + +public class ServerUtil { + + + //String static server_Prefix="http://greententacle.techfak.uni-bielefeld.de:5171/sparql"; + //private static String server_Prefix="http://dbpedia.org/sparql"; + private static String server_Prefix="http://greententacle.techfak.uni-bielefeld.de:5171/sparql"; + //private static String server_Prefix="http://purpurtentacle.techfak.uni-bielefeld.de:8890/sparql"; + + private static int timeToTimeoutOnServer=30000; + + + public static String createServerRequest(String query){ + String anfrage=null; + anfrage=removeSpecialKeys(query); + anfrage=anfrage.replace("<","<"); + anfrage=anfrage.replace("%gt;",">"); + anfrage=anfrage.replace("&","&"); + //anfrage=anfrage.replaceAll("#>","%23%3E%0D%0A%"); + anfrage=anfrage.replace("#","%23"); + anfrage=anfrage.replace(" ","+"); + anfrage=anfrage.replace("/","%2F"); + anfrage=anfrage.replace(":","%3A"); + anfrage=anfrage.replace("?","%3F"); + anfrage=anfrage.replace("$","%24"); + //anfrage=anfrage.replaceAll("F>+","F%3E%0D%0A"); + anfrage=anfrage.replace(">","%3E"); + anfrage=anfrage.replace("<","%3C"); + anfrage=anfrage.replace("\"","%22"); + anfrage=anfrage.replace("\n","%0D%0A%09"); + anfrage=anfrage.replace("%%0D%0A%09","%09"); + anfrage=anfrage.replace("=","%3D"); + anfrage=anfrage.replace("@","%40"); + anfrage=anfrage.replace("&","%26"); + anfrage=anfrage.replace("(","%28"); + anfrage=anfrage.replace(")","%29"); + anfrage=anfrage.replace("%3E%0D%0A%25","%3E"); + //anfrage=anfrage.replaceAll("\n",".%0D%0A%09"); + return anfrage; + } + + private static String removeSpecialKeys(String query){ + query=query.replace("\\",""); + //query=query.replaceAll("\a",""); + query=query.replace("\b",""); + query=query.replace("\f",""); + query=query.replace("\r",""); + query=query.replace("\t",""); + // query=query.replaceAll("\v",""); + return query; + } + + public static String getServer_Prefix() { + return server_Prefix; + } + + public static int getTimeToTimeoutOnServer() { + return timeToTimeoutOnServer; + } + + public static void setTimeToTimeoutOnServer(int timeToTimeoutOnServer) { + ServerUtil.timeToTimeoutOnServer = timeToTimeoutOnServer; + } + + + /** + * Get an uri and saves the properties of this resource + * @param vergleich + * @return + * @throws IOException + */ + public static HashMap<String,String> sendServerPropertyRequest(String vergleich, String side) throws IOException{ + + String vergleichorig = vergleich; + + String tmp_left=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {?y ?p <"+vergleichorig+">. ?p rdfs:label ?s.}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; + + String tmp_right=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {<"+vergleichorig+"> ?p ?y. ?p rdfs:label ?s.}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; + + String tmp_both=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {{?y ?p <"+vergleichorig+">. ?p rdfs:label ?s.} UNION {<"+vergleichorig+"> ?p ?y. ?p rdfs:label ?s.}}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; + String verarbeitungsurl=null; + + /*Original*/ + if(side.contains("RIGHT")) verarbeitungsurl=tmp_right; + if(side.contains("LEFT")) verarbeitungsurl=tmp_left; + if(side.contains("BOTH")) verarbeitungsurl=tmp_both; + + if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsurl=tmp_left; + + String result=""; + HttpURLConnection connection = null; + BufferedReader rd = null; + StringBuilder sb = null; + String line = null; + + URL serverAddress = null; + + try { + serverAddress = new URL(verarbeitungsurl); + //set up out communications stuff + connection = null; + + //Set up the initial connection + connection = (HttpURLConnection)serverAddress.openConnection(); + connection.setRequestMethod("GET"); + connection.setDoOutput(true); + connection.setReadTimeout(getTimeToTimeoutOnServer()); + + connection.connect(); + rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); + sb = new StringBuilder(); + + while ((line = rd.readLine()) != null) + { + sb.append(line + '\n'); + } + + result=sb.toString(); + + } catch (MalformedURLException e) { + System.out.println("Must enter a valid URL"); + } catch (IOException e) { + System.out.println("Can not connect or timeout"); + } + finally + { + //close the connection, set all objects to null + connection.disconnect(); + rd = null; + sb = null; + connection = null; + } + + HashMap<String,String> hm = new HashMap<String,String>(); + result=result.replace("<th>s</th>",""); + result=result.replace("<th>p</th>",""); + result=result.replace("<table class=\"sparql\" border=\"1\">",""); + result=result.replace("<tr>",""); + result=result.replace("</tr>",""); + result=result.replace("\n", ""); + result=result.replace(" ", ""); + result=result.replaceFirst("<td>", ""); + + + String[] tmp_array=result.split("</td><td>"); + + for(int i =1; i<=tmp_array.length-2;i=i+2) { + hm.put(tmp_array[i-1].toLowerCase(), tmp_array[i]); + } + + return hm; + } + +} Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java 2012-03-15 11:10:37 UTC (rev 3614) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java 2012-03-18 21:44:30 UTC (rev 3615) @@ -10,6 +10,8 @@ import java.io.InputStreamReader; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -17,6 +19,8 @@ import org.dllearner.algorithm.tbsl.exploration.Sparql.Template; import org.dllearner.algorithm.tbsl.exploration.Sparql.TemplateBuilder; import org.dllearner.algorithm.tbsl.exploration.Sparql.queryInformation; +import org.dllearner.algorithm.tbsl.exploration.Utils.Query; +import org.dllearner.algorithm.tbsl.exploration.Utils.QueryPair; public class testClass_new { @@ -37,22 +41,33 @@ String file="dbpedia-train.xml"; long start = System.currentTimeMillis(); + /* + * TODO: WHy is there no Hypothese for the question: "Who is the mayor of Berlin?" + */ + /*String question = "Is the wife of president Obama called Michelle?"; + //String question = "Who is the mayor of Berlin?"; + //temp_list_result=testobject.createTemplates(question); - //String question = "Is the wife of president Obama called Michelle?"; - String question = "Who is the mayor of Berlin?"; - temp_list_result=testobject.createTemplates(question); + Map<QueryPair,String> tm = new HashMap<QueryPair, String>(); + for(Template t : temp_list_result){ - t.printAll(); + //t.printAll(); + ArrayList<QueryPair> qp = Query.returnSetOfQueries(t); + for(QueryPair p : qp){ + tm.put(p, t.getQuestion()); + } } + writeQueriesInFile(tm,filepath,file,start,stop );*/ + ArrayList<queryInformation> list_of_structs = new ArrayList<queryInformation>(); /* * Generate Templates */ - /*list_of_structs=generateStruct(filepath+"XMLDateien/"+file); + list_of_structs=generateStruct(filepath+"XMLDateien/"+file); for(queryInformation s : list_of_structs){ ArrayList<Template> temp_list = new ArrayList<Template>(); temp_list=testobject.createTemplates(s.getQuery().replace("<[CDATA[", "").replace("]]>", "")); @@ -60,9 +75,23 @@ temp_list_result.add(t); } - }*/ + } + /* + * Create Query for each Template + */ + Map<QueryPair,String> tm = new HashMap<QueryPair, String>(); + for(Template t : temp_list_result){ + //t.printAll(); + ArrayList<QueryPair> qp = Query.returnSetOfQueries(t); + for(QueryPair p : qp){ + tm.put(p, t.getQuestion()); + } + } + + + /* * Get Elements for Each Resource and Class */ @@ -74,7 +103,8 @@ /* * Write Results in File */ - writeTemplatesInFile(temp_list_result,filepath,file,start,stop ); + writeQueriesInFile(tm,filepath,file,start,stop ); + //writeTemplatesInFile(temp_list_result,filepath,file,start,stop ); } @@ -105,6 +135,7 @@ } } result+="\n"; + result+="queryType: "+t.getQueryType()+"\n"; result+="selectTerm: "+t.getSelectTerm()+"\n"; result+="having: "+t.getHaving()+"\n"; result+="filter: "+t.getFilter()+"\n"; @@ -130,7 +161,31 @@ bw.flush(); bw.close(); } + + + private static void writeQueriesInFile(Map<QueryPair,String> tm, String filepath,String given_file, float start, float stop ) throws IOException{ + File file = new File(filepath+"Queries"+stop+given_file.replace(".xml", "")+".txt"); + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + String result=""; + /* + * for(String key : map.keySet()) + { + System.out.print("Key: " + key + " - "); + System.out.print("Value: " + map.get(key) + "\n"); + } + */ + for(QueryPair key : tm.keySet()){ + result+=tm.get(key)+": "+key.getQuery()+" "+key.getRank()+"\n"; + } + bw.write(result); + bw.flush(); + bw.close(); + } + + + + private static ArrayList<queryInformation> generateStruct(String filename) { System.out.println("In generate Struct"); String XMLType=null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |