From: <sk...@us...> - 2007-10-08 12:06:02
|
Revision: 180 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=180&view=rev Author: sknappe Date: 2007-10-08 05:05:59 -0700 (Mon, 08 Oct 2007) Log Message: ----------- Sparql Modul in org.dllearner.kb ?\195?\188bertragen Added Paths: ----------- trunk/src/dl-learner/org/dllearner/kb/Cache.java trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java Added: trunk/src/dl-learner/org/dllearner/kb/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/Cache.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/Cache.java 2007-10-08 12:05:59 UTC (rev 180) @@ -0,0 +1,210 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.net.URLEncoder; +/** + * + * This is a primitive cache. + * The objects of this class can be either the cache itself or just on entry in the cache + * + * the cache remembers: a timestamp, the original sparql-query, the result + * key is the subject http://dbpedia.org/resource/Angela_Merkel which is first urlencoded + * and so serves as the hash for the filename. + * Cache validates if timestamp too old and Sparql-Query the same + * before returning the SPARQL xml-result + * + * @author Sebastian Hellmann + * + */ +public class Cache implements Serializable{ + + + final static long serialVersionUID=104; + transient String basedir=""; + transient String fileending=".cache"; + long timestamp; + String content=""; + long daysoffreshness=15; + long multiplier=24*60*60*1000;//h m s ms + String sparqlquery=""; + + + /** + * Constructor for the cache itself. + * Called once at the beginning + * + * @param path Where the base path to the cache is + */ + public Cache(String path){ + this.basedir=path+File.separator; + if(!new File(path).exists()) + {System.out.println(new File(path).mkdir());;} + + } + +// + /** + * Constructor for single cache object(one entry) + * + * @param content the sparql xml result + * @param sparql the sparql query + */ + public Cache(String content, String sparql){ + this.content=content; + this.sparqlquery=sparql; + this.timestamp=System.currentTimeMillis(); + } + + + + /** + * use only on the cache object describing the cache itself + * + * @param key the individual + * @param sparql the sparql query + * @return the cached sparql result or null + */ + public String get(String key, String sparql){ + //System.out.println("get From "+key); + String ret=null; + try{ + Cache c =readFromFile(makeFilename(key)) ; + if(c==null)return null; + //System.out.println(" file found"); + if(!c.checkFreshness())return null; + //System.out.println("fresh"); + if(!c.validate(sparql))return null; + //System.out.println("valid"); + ret=c.content; + }catch (Exception e) {e.printStackTrace();} + return ret; + } + + /** + * + * constructor for single cache object(one entry) + * + * @param key the individual + * @param content the sparql result + * @param sparql the sparql query + */ + public void put(String key, String content, String sparql){ + //System.out.println("put into "+key); + Cache c=new Cache(content,sparql); + putIntoFile(makeFilename(key), c); + } + + + /** + * to normalize the filenames + * + * @param key + * @return + */ + String makeFilename(String key){ + String ret=""; + try{ + ret=basedir+URLEncoder.encode(key, "UTF-8")+fileending; + }catch (Exception e) {e.printStackTrace();} + return ret; + } + + /** + * how old is the result + * @return + */ + boolean checkFreshness(){ + if((System.currentTimeMillis()-this.timestamp)<=(daysoffreshness*multiplier)) + //fresh + return true; + else return false; + } + + + /** + * some sparql query + * @param sparql + * @return + */ + boolean validate(String sparql){ + if(this.sparqlquery.equals(sparql)) + //valid + return true; + else return false; + } + + /** + * makes a new file if none exists + * @param Filename + */ + public void checkFile(String Filename){ + if(!new File(Filename).exists()){ + try{ + new File(Filename).createNewFile(); + }catch (Exception e) {e.printStackTrace();} + + } + + } + + /** + * internal saving function + * puts a cache object into a file + * + * @param Filename + * @param content + */ + public void putIntoFile(String Filename,Cache content){ + try{ + //FileWriter fw=new FileWriter(new File(Filename),true); + FileOutputStream fos = new FileOutputStream( Filename , false ); + ObjectOutputStream o = new ObjectOutputStream( fos ); + o.writeObject( content ); + fos.flush(); + fos.close(); + }catch (Exception e) {System.out.println("Not in cache creating: "+Filename);} + } + + /** + * internal retrieval function + * + * @param Filename + * @return one entry object + */ + public Cache readFromFile(String Filename){ + Cache content=null; + try{ + FileInputStream fos = new FileInputStream( Filename ); + ObjectInputStream o = new ObjectInputStream( fos ); + content=(Cache)o.readObject(); + //FileReader fr=new FileReader(new File(Filename,"r")); + //BufferedReader br=new BufferedReader(fr); + }catch (Exception e) {} + return content; + + } +} Added: trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java 2007-10-08 12:05:59 UTC (rev 180) @@ -0,0 +1,365 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; + +/** + * This class collects the ontology from dbpedia, + * everything is saved in hashsets, so the doublettes are taken care of + * + * + * @author Sebastian Hellmann + * + */ +public class OntologyCollector { + + boolean print_flag=false; + SimpleHTTPRequest s; + QueryMaker q; + Cache c; + InetAddress ia; + SparqlFilter sf; + String[] subjectList; + int numberOfRecursions; + HashSet<String> properties; + HashSet<String> classes; + HashSet<String> instances; + HashSet<String> triples; + + // some namespaces + String subclass="http://www.w3.org/2000/01/rdf-schema#subClassOf"; + String type="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; + String objectProperty="http://www.w3.org/2002/07/owl#ObjectProperty"; + String classns="http://www.w3.org/2002/07/owl#Class"; + String thing="http://www.w3.org/2002/07/owl#Thing"; + + + String[] defaultClasses={ + "http://dbpedia.org/class/yago", + "http://dbpedia.org/resource/Category:", + "http://dbpedia.org/resource/Template:", + "http://www.w3.org/2004/02/skos/core", + "http://dbpedia.org/class/"}; //TODO FEHLER hier fehlt yago + + + /** + * + * + * @param subjectList + * @param numberOfRecursions + * @param filterMode + * @param FilterPredList + * @param FilterObjList + * @param defClasses + */ + public OntologyCollector(String[] subjectList,int numberOfRecursions, + int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses){ + this.subjectList=subjectList; + this.numberOfRecursions=numberOfRecursions; + + this.s=new SimpleHTTPRequest(); + this.q=new QueryMaker(); + this.c=new Cache("cache"); + if(defClasses!=null && defClasses.length>0 ){ + this.defaultClasses=defClasses; + } + + try{ + this.sf=new SparqlFilter(filterMode,FilterPredList,FilterObjList); + this.ia=InetAddress.getByName("dbpedia.openlinksw.com"); + //this.fw=new FileWriter(new File(System.currentTimeMillis()+".nt"),true); + this.properties=new HashSet<String>(); + this.classes=new HashSet<String>(); + this.instances=new HashSet<String>(); + this.triples=new HashSet<String>(); + //this.all=new HashSet<String>(); + }catch (Exception e) {e.printStackTrace();} + + } + /** + * first collects the ontology + * then types everything so it becomes owl-dl + * + * @return all triples in n-triple format + */ + public String collectOntology(){ + getRecursiveList(subjectList, numberOfRecursions); + finalize(); + String ret=""; + for (Iterator<String> iter = triples.iterator(); iter.hasNext();) { + ret += iter.next(); + + } + return ret; + } + + /** + * calls getRecursive for each subject in list + * @param subjects + * @param NumberofRecursions + */ + public void getRecursiveList(String[] subjects,int NumberofRecursions){ + for (int i = 0; i < subjects.length; i++) { + getRecursive(subjects[i], NumberofRecursions); + + } + + } + + /** + * gets all triples until numberofrecursion-- gets 0 + * + * @param StartingSubject + * @param NumberofRecursions + */ + public void getRecursive(String StartingSubject,int NumberofRecursions){ + System.out.print("SparqlModul: Depth: "+NumberofRecursions+" @ "+StartingSubject+" "); + if(NumberofRecursions<=0) + { return; + } + else {NumberofRecursions--;} + //System.out.println(NumberofRecursions); + try{ + + String sparql=q.makeQueryFilter(StartingSubject,this.sf); + p(sparql); + p("*******************"); + // checks cache + String FromCache=c.get(StartingSubject, sparql); + String xml; + // if not in cache get it from dbpedia + if(FromCache==null){ + xml=s.sendAndReceive(ia, 8890, sparql); + c.put(StartingSubject, xml, sparql); + System.out.print("\n"); + } + else{ + xml=FromCache; + System.out.println("FROM CACHE"); + } + p(xml); + p("***********************"); + // get new Subjects + String[] newSubjects=processResult(StartingSubject,xml); + + for (int i = 0; (i < newSubjects.length)&& NumberofRecursions!=0; i++) { + getRecursive(newSubjects[i], NumberofRecursions); + } + + //System.out.println(xml); + }catch (Exception e) {e.printStackTrace();} + + } + + /** + * process the sparql result xml in a simple manner + * + * + * @param subject + * @param xml + * @return list of new individuals + */ + public String[] processResult(String subject,String xml){ + //TODO if result is empty, catch exceptions + String one="<binding name=\"predicate\"><uri>"; + String two="<binding name=\"object\"><uri>"; + String end="</uri></binding>"; + String predtmp=""; + String objtmp=""; + ArrayList<String> al=new ArrayList<String>(); + + while(xml.indexOf(one)!=-1){ + //get pred + xml=xml.substring(xml.indexOf(one)+one.length()); + predtmp=xml.substring(0,xml.indexOf(end)); + //getobj + xml=xml.substring(xml.indexOf(two)+two.length()); + objtmp=xml.substring(0,xml.indexOf(end)); + + // writes the triples and resources in the hashsets + // also fills the arraylist al + processTriples(subject, predtmp, objtmp,al); + //System.out.println(al.size()); + + } + + // convert al to list + Object[] o=al.toArray(); + String[] ret=new String[o.length]; + for (int i = 0; i < o.length; i++) { + ret[i]=(String)o[i]; + } + return ret; + //return (String[])al.toArray(); + //System.out.println(xml); + } + + + + /** + * + * writes the triples and resources in the hashsets + * also fills the arraylist al with new individals for further processing + * @param s + * @param p + * @param o + * @param al + */ + public void processTriples(String s,String p, String o,ArrayList<String> al){ + // the next two lines bump out some inconsistencies within dbpedia + String t="/Category"; + if(s.equals(t) || o.equals(t))return ; + + if(sf.mode==2) + { + if( o.startsWith("http://dbpedia.org/resource/Category:") + && + !p.startsWith("http://www.w3.org/2004/02/skos/core") + ) + {return;} + if(p.equals("http://www.w3.org/2004/02/skos/core#broader")){ + p=subclass; + } + else if(p.equals("http://www.w3.org/2004/02/skos/core#subject")){ + p=type; + } + else {} + } + + //save for further processing + al.add(o); + + // type classes + if(isClass(o)){ + classes.add(o); + if(isClass(s))p=subclass; + else p=type; + } + else { + instances.add(o); + this.properties.add(p); + } + + + + //maketriples + try{ + this.triples.add(makeTriples(s, p, o)); + //fw.write(makeTriples(subject, predtmp, objtmp)); + }catch (Exception e) {e.printStackTrace();} + + + return; + } +// + /** + * also makes subclass property between classes + * + * @param s + * @param p + * @param o + * @return triple in the n triple notation + */ + public String makeTriples(String s,String p, String o){ + //s=replaceNamespace(s); + //p=replaceNamespace(p); + //o=replaceNamespace(o); + String ret=""; + ret="<"+s+"> <"+p+"> <"+o+">.\n"; + return ret; + } + + /** + * decides if an object is treated as a class + * + * @param obj + * @return true if obj is in the defaultClassesList + */ + public boolean isClass(String obj){ + + boolean retval=false; + for (String defclass : defaultClasses) { + if(obj.contains(defclass))retval=true; + } + return retval; + } + + + /** + * @see java.lang.Object#finalize() + */ + @Override + public void finalize(){ + typeProperties(); + typeClasses(); + typeInstances(); + } + + public void typeProperties(){ + String rdfns="http://www.w3.org/1999/02/22-rdf-syntax-ns"; + String owlns="http://www.w3.org/2002/07/owl"; + Iterator<String> it=properties.iterator(); + String current=""; + while (it.hasNext()){ + try{ + current=it.next(); + if(current.equals(subclass))continue; + if(current.contains(rdfns)||current.contains(owlns)){/*DO NOTHING*/} + else {this.triples.add(makeTriples(current,type,objectProperty));} + }catch (Exception e) {} + + } + } + public void typeClasses(){ + Iterator<String> it=classes.iterator(); + String current=""; + while (it.hasNext()){ + try{ + current=it.next(); + this.triples.add(makeTriples(current,type,classns)); + }catch (Exception e) {} + } + } + public void typeInstances(){ + Iterator<String> it=instances.iterator(); + String current=""; + while (it.hasNext()){ + try{ + current=it.next(); + this.triples.add(makeTriples(current,type,thing)); + }catch (Exception e) {} + } + } + + /** + * debug print turn on print_flag + * @param s + */ + public void p(String s){ + if(print_flag) + System.out.println(s); + } + + +} Added: trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java 2007-10-08 12:05:59 UTC (rev 180) @@ -0,0 +1,123 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +/** + * + * This class produces sparql queries + * + * @author Sebastian Hellmann + * + */ +public class QueryMaker { + //Good + /*public static String owl ="http://www.w3.org/2002/07/owl#"; + public static String xsd="http://www.w3.org/2001/XMLSchema#"; + public static String rdfs="http://www.w3.org/2000/01/rdf-schema#"; + public static String rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + public static String base="http://dbpedia.org/resource/"; + public static String dbpedia2="http://dbpedia.org/property/"; + public static String dbpedia="http://dbpedia.org/"; + + + //BAD + public static String skos="http://www.w3.org/2004/02/skos/core#"; + public static String foaf="http://xmlns.com/foaf/0.1/"; + public static String dc="http://purl.org/dc/elements/1.1/"; + public static String foreign="http://dbpedia.org/property/wikipage-"; + public static String sameAs="http://www.w3.org/2002/07/owl#sameAs"; + public static String reference="http://dbpedia.org/property/reference";*/ + + int tempyago=0; + + /** + * reads all the options and makes the sparql query accordingly + * @param subject + * @param sf special object encapsulating all options + * @return sparql query + */ + public String makeQueryFilter(String subject, SparqlFilter sf){ + + + String Filter=""; + if(!sf.useLiterals)Filter+="!isLiteral(?object))"; + for (String p : sf.getPredFilter()) { + Filter+="\n" + filterPredicate(p); + } + for (String o : sf.getObjFilter()) { + Filter+="\n" + filterObject(o); + } + + + String ret= + "SELECT * WHERE { \n" + + "<"+ + subject+ + + "> ?predicate ?object.\n" + + "FILTER( \n" + + "(" +Filter+").}"; + //System.out.println(ret); + return ret; + } + + + /*public String makeQueryDefault(String subject){ + String ret= + "SELECT * WHERE { \n" + + "<"+ + subject+ + + "> ?predicate ?object.\n" + + "FILTER( \n" + + "(!isLiteral(?object))" + + "\n" + filterPredicate(skos)+ + //"\n" + filterObject(skos)+ + "\n" + filterPredicate(foaf)+ + "\n" + filterObject(foaf)+ + "\n" + filterPredicate(foreign)+ + "\n" + filterPredicate(sameAs)+ + "\n" + filterPredicate(reference)+ + ")." + + " }"; + + //System.out.println(ret); + return ret; +}*/ + + /** + * add a new object filter + * (objects are filtered out of sparql result) + * @param ns namespace + * @return + */ + public String filterObject(String ns){ + return "&&( !regex((?object), '"+ns+"') )"; + } + /** + * add a new object filter + * (objects are filtered out of sparql result) + * @param ns namespace + * * @return + */ + public String filterPredicate(String ns){ + return "&&( !regex(str(?predicate), '"+ns+"') )"; + } +} Added: trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java 2007-10-08 12:05:59 UTC (rev 180) @@ -0,0 +1,126 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.Socket; +import java.net.URLEncoder; + + + +/** + * + * This class makes a simple http request + * the dbpedia url is hardcoded + * + * @author Sebastian Hellmann + * + * + */ +public class SimpleHTTPRequest { + static final char value[]={13,10}; + static final String cut=new String(value); + + + + + public String sendAndReceive(InetAddress ia, int port, String sparql){ + String retval=""; + // + + byte resp[]=null; + + try{ + Socket SparqlServer=new Socket(ia,port); + String request=makeHeader(sparql); + // send request + (SparqlServer.getOutputStream()).write(request.getBytes()); + + //get Response + resp=readBuffer(new BufferedInputStream(SparqlServer.getInputStream())); + retval=new String(resp); + retval=subtractResponseHeader(retval); + //retval="||"+retval; + + SparqlServer.close(); + + + + } + catch(Exception e){e.printStackTrace();} + //System.out.println("got it"); + return retval; + + }//down + + public static byte[] readBuffer(InputStream IS) + throws IOException{ + byte buffer[] = new byte[0xffff]; + int nbytes=0; + byte resp[]=new byte[0]; + while ((nbytes=IS.read(buffer))!=-1) { + byte tmp[]=new byte[resp.length+nbytes]; + int i=0; + for (;i<resp.length;i++){ + tmp[i]=resp[i]; + } + for(int a=0;a<nbytes;a++,i++){ + tmp[i]=buffer[a]; + } + resp=tmp; + } + return resp; + } + + public String subtractResponseHeader(String in){ + //System.out.println(in.indexOf(cut+""+cut)); + return in.substring(in.indexOf(cut+""+cut)+4); + + + } + + public String makeHeader(String query){ + + + String RequestHeader=""; + try{ + + RequestHeader="GET /sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + + //"SELECT%20%2A%20WHERE%20%7B%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FAristotle%3E%20%3Fa%20%3Fb%20%7D%20" + + URLEncoder.encode(query, "UTF-8")+ + //query+// URLencode + "&format=application%2Fsparql-results%2Bxml HTTP/1.1"+cut+ + "Host: dbpedia.openlinksw.com"+cut+ + "Connection: close"+cut+ + //"Accept-Encoding: gzip"+cut+ + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"+cut+ + "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"+cut+ + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+cut+ + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"+cut+ + cut; + }catch (Exception e) {e.printStackTrace();} + return RequestHeader; + + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java 2007-10-08 12:05:59 UTC (rev 180) @@ -0,0 +1,119 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +/** + * + * + * encapsulates all the options + * see the documentation for more help + * + * @author Sebastian Hellmann + * + */ +public class SparqlFilter { + public int mode=0; + // 0 yago, 1 only cat, 2 skos+cat + String[] PredFilter=null; + String[] ObjFilter=null; + boolean useLiterals=false; + + + String[] yagoPredFilterDefault={ + "http://www.w3.org/2004/02/skos/core", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/property/wikipage-", + "http://www.w3.org/2002/07/owl#sameAs", + "http://dbpedia.org/property/reference" }; + String[] yagoObjFilterDefault={ + "http://dbpedia.org/resource/Category:Articles_", + "http://dbpedia.org/resource/Category:Wikipedia_", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/resource/Category", + "http://dbpedia.org/resource/Template", + "http://upload.wikimedia.org/wikipedia/commons"}; + + String[] onlyCatPredFilterDefault={ + "http://www.w3.org/2004/02/skos/core", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/property/wikipage-", + "http://www.w3.org/2002/07/owl#sameAs", + "http://dbpedia.org/property/reference" }; + String[] onlyCatObjFilterDefault={ + "http://dbpedia.org/resource/Category:Articles_", + "http://dbpedia.org/resource/Category:Wikipedia_", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/class/yago", + "http://dbpedia.org/resource/Template", + "http://upload.wikimedia.org/wikipedia/commons"}; + + String[] skosPredFilterDefault={ + "http://www.w3.org/2004/02/skos/core#narrower", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/property/wikipage-", + "http://www.w3.org/2002/07/owl#sameAs", + "http://dbpedia.org/property/reference" }; + String[] skosObjFilterDefault={ + "http://dbpedia.org/resource/Category:Articles_", + "http://dbpedia.org/resource/Category:Wikipedia_", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/class/yago", + "http://dbpedia.org/resource/Template", + "http://upload.wikimedia.org/wikipedia/commons"}; + + public SparqlFilter(int mode, String[] pred, String[] obj) { + if (mode==-1 && (pred==null || pred.length==0 || obj==null||obj.length==0)) + {mode=0;} + + switch (mode){ + case 0: //yago + ObjFilter=yagoObjFilterDefault; + PredFilter=yagoPredFilterDefault; + break; + case 1: // only Categories + ObjFilter=onlyCatObjFilterDefault; + PredFilter=onlyCatPredFilterDefault; + break; + case 2: // there are some other changes to, which are made directly in other functions + ObjFilter=skosObjFilterDefault; + PredFilter=skosPredFilterDefault; + break; + default: + ObjFilter=obj; + PredFilter=pred; + break; + + }} + public SparqlFilter(int mode, String[] pred, String[] obj,boolean uselits) throws Exception{ + this(mode, pred,obj); + this.useLiterals=uselits; + } + + public String[] getObjFilter(){ + return this.ObjFilter; + } + public String[] getPredFilter(){ + return this.PredFilter; + } + + + + } + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sk...@us...> - 2007-10-10 09:01:59
|
Revision: 205 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=205&view=rev Author: sknappe Date: 2007-10-10 02:01:57 -0700 (Wed, 10 Oct 2007) Log Message: ----------- option url is now used Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java Modified: trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java 2007-10-09 15:52:15 UTC (rev 204) +++ trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java 2007-10-10 09:01:57 UTC (rev 205) @@ -20,6 +20,7 @@ package org.dllearner.kb; import java.net.InetAddress; +import java.net.URL; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -75,7 +76,7 @@ * @param defClasses */ public OntologyCollector(String[] subjectList,int numberOfRecursions, - int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses, String format){ + int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses, String format, URL url){ this.subjectList=subjectList; this.numberOfRecursions=numberOfRecursions; this.format=format; @@ -88,7 +89,7 @@ try{ this.sf=new SparqlFilter(filterMode,FilterPredList,FilterObjList); - this.ia=InetAddress.getByName("dbpedia.openlinksw.com"); + this.ia=InetAddress.getByName(url.getHost()); //this.fw=new FileWriter(new File(System.currentTimeMillis()+".nt"),true); this.properties=new HashSet<String>(); this.classes=new HashSet<String>(); Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java 2007-10-09 15:52:15 UTC (rev 204) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java 2007-10-10 09:01:57 UTC (rev 205) @@ -125,7 +125,7 @@ System.out.println("SparqlModul: Collecting Ontology"); String[] a=new String[0]; OntologyCollector oc=new OntologyCollector(instances.toArray(a), numberOfRecursions, - filterMode, Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format); + filterMode, Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url); String ont=oc.collectOntology(); if (format.equals("N-TRIPLES")||dumpToFile){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sk...@us...> - 2007-10-10 12:37:17
|
Revision: 209 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=209&view=rev Author: sknappe Date: 2007-10-10 05:37:11 -0700 (Wed, 10 Oct 2007) Log Message: ----------- Renamed classes to begin with Sparqle and deleted SimpleHttpRequest Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/kb/SparqlCache.java trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java Removed Paths: ------------- trunk/src/dl-learner/org/dllearner/kb/Cache.java trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java Deleted: trunk/src/dl-learner/org/dllearner/kb/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/Cache.java 2007-10-10 12:28:02 UTC (rev 208) +++ trunk/src/dl-learner/org/dllearner/kb/Cache.java 2007-10-10 12:37:11 UTC (rev 209) @@ -1,210 +0,0 @@ -/** - * Copyright (C) 2007, Sebastian Hellmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -package org.dllearner.kb; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.net.URLEncoder; -/** - * - * This is a primitive cache. - * The objects of this class can be either the cache itself or just on entry in the cache - * - * the cache remembers: a timestamp, the original sparql-query, the result - * key is the subject http://dbpedia.org/resource/Angela_Merkel which is first urlencoded - * and so serves as the hash for the filename. - * Cache validates if timestamp too old and Sparql-Query the same - * before returning the SPARQL xml-result - * - * @author Sebastian Hellmann - * - */ -public class Cache implements Serializable{ - - - final static long serialVersionUID=104; - transient String basedir=""; - transient String fileending=".cache"; - long timestamp; - String content=""; - long daysoffreshness=15; - long multiplier=24*60*60*1000;//h m s ms - String sparqlquery=""; - - - /** - * Constructor for the cache itself. - * Called once at the beginning - * - * @param path Where the base path to the cache is - */ - public Cache(String path){ - this.basedir=path+File.separator; - if(!new File(path).exists()) - {System.out.println(new File(path).mkdir());;} - - } - -// - /** - * Constructor for single cache object(one entry) - * - * @param content the sparql xml result - * @param sparql the sparql query - */ - public Cache(String content, String sparql){ - this.content=content; - this.sparqlquery=sparql; - this.timestamp=System.currentTimeMillis(); - } - - - - /** - * use only on the cache object describing the cache itself - * - * @param key the individual - * @param sparql the sparql query - * @return the cached sparql result or null - */ - public String get(String key, String sparql){ - //System.out.println("get From "+key); - String ret=null; - try{ - Cache c =readFromFile(makeFilename(key)) ; - if(c==null)return null; - //System.out.println(" file found"); - if(!c.checkFreshness())return null; - //System.out.println("fresh"); - if(!c.validate(sparql))return null; - //System.out.println("valid"); - ret=c.content; - }catch (Exception e) {e.printStackTrace();} - return ret; - } - - /** - * - * constructor for single cache object(one entry) - * - * @param key the individual - * @param content the sparql result - * @param sparql the sparql query - */ - public void put(String key, String content, String sparql){ - //System.out.println("put into "+key); - Cache c=new Cache(content,sparql); - putIntoFile(makeFilename(key), c); - } - - - /** - * to normalize the filenames - * - * @param key - * @return - */ - String makeFilename(String key){ - String ret=""; - try{ - ret=basedir+URLEncoder.encode(key, "UTF-8")+fileending; - }catch (Exception e) {e.printStackTrace();} - return ret; - } - - /** - * how old is the result - * @return - */ - boolean checkFreshness(){ - if((System.currentTimeMillis()-this.timestamp)<=(daysoffreshness*multiplier)) - //fresh - return true; - else return false; - } - - - /** - * some sparql query - * @param sparql - * @return - */ - boolean validate(String sparql){ - if(this.sparqlquery.equals(sparql)) - //valid - return true; - else return false; - } - - /** - * makes a new file if none exists - * @param Filename - */ - public void checkFile(String Filename){ - if(!new File(Filename).exists()){ - try{ - new File(Filename).createNewFile(); - }catch (Exception e) {e.printStackTrace();} - - } - - } - - /** - * internal saving function - * puts a cache object into a file - * - * @param Filename - * @param content - */ - public void putIntoFile(String Filename,Cache content){ - try{ - //FileWriter fw=new FileWriter(new File(Filename),true); - FileOutputStream fos = new FileOutputStream( Filename , false ); - ObjectOutputStream o = new ObjectOutputStream( fos ); - o.writeObject( content ); - fos.flush(); - fos.close(); - }catch (Exception e) {System.out.println("Not in cache creating: "+Filename);} - } - - /** - * internal retrieval function - * - * @param Filename - * @return one entry object - */ - public Cache readFromFile(String Filename){ - Cache content=null; - try{ - FileInputStream fos = new FileInputStream( Filename ); - ObjectInputStream o = new ObjectInputStream( fos ); - content=(Cache)o.readObject(); - //FileReader fr=new FileReader(new File(Filename,"r")); - //BufferedReader br=new BufferedReader(fr); - }catch (Exception e) {} - return content; - - } -} Deleted: trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java 2007-10-10 12:28:02 UTC (rev 208) +++ trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java 2007-10-10 12:37:11 UTC (rev 209) @@ -1,422 +0,0 @@ -/** - * Copyright (C) 2007, Sebastian Hellmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -package org.dllearner.kb; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; -import java.net.InetAddress; -import java.net.URL; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; - -import org.dllearner.utilities.Files; - -/** - * This class collects the ontology from dbpedia, - * everything is saved in hashsets, so the doublettes are taken care of - * - * - * @author Sebastian Hellmann - * - */ -public class OntologyCollector { - - boolean print_flag=false; - SimpleHTTPRequest s; - QueryMaker q; - Cache c; - URL url; - SparqlFilter sf; - String[] subjectList; - int numberOfRecursions; - HashSet<String> properties; - HashSet<String> classes; - HashSet<String> instances; - HashSet<String> triples; - String format; - static final char value[]={13,10}; - static final String cut=new String(value); - - // some namespaces - String subclass="http://www.w3.org/2000/01/rdf-schema#subClassOf"; - String type="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; - String objectProperty="http://www.w3.org/2002/07/owl#ObjectProperty"; - String classns="http://www.w3.org/2002/07/owl#Class"; - String thing="http://www.w3.org/2002/07/owl#Thing"; - - - String[] defaultClasses={ - "http://dbpedia.org/class/yago", - "http://dbpedia.org/resource/Category:", - "http://dbpedia.org/resource/Template:", - "http://www.w3.org/2004/02/skos/core", - "http://dbpedia.org/class/"}; //TODO FEHLER hier fehlt yago - - - /** - * - * - * @param subjectList - * @param numberOfRecursions - * @param filterMode - * @param FilterPredList - * @param FilterObjList - * @param defClasses - */ - public OntologyCollector(String[] subjectList,int numberOfRecursions, - int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses, String format, URL url){ - this.subjectList=subjectList; - this.numberOfRecursions=numberOfRecursions; - this.format=format; - this.s=new SimpleHTTPRequest(); - this.q=new QueryMaker(); - this.c=new Cache("cache"); - if(defClasses!=null && defClasses.length>0 ){ - this.defaultClasses=defClasses; - } - - try{ - this.sf=new SparqlFilter(filterMode,FilterPredList,FilterObjList); - this.url=url; - this.properties=new HashSet<String>(); - this.classes=new HashSet<String>(); - this.instances=new HashSet<String>(); - this.triples=new HashSet<String>(); - }catch (Exception e) {e.printStackTrace();} - - } - /** - * first collects the ontology - * then types everything so it becomes owl-dl - * - * @return all triples in n-triple format - */ - public String collectOntology(){ - getRecursiveList(subjectList, numberOfRecursions); - finalize(); - String ret=""; - for (Iterator<String> iter = triples.iterator(); iter.hasNext();) { - ret += iter.next(); - - } - return ret; - } - - /** - * calls getRecursive for each subject in list - * @param subjects - * @param NumberofRecursions - */ - public void getRecursiveList(String[] subjects,int NumberofRecursions){ - for (int i = 0; i < subjects.length; i++) { - getRecursive(subjects[i], NumberofRecursions); - - } - - } - - /** - * gets all triples until numberofrecursion-- gets 0 - * - * @param StartingSubject - * @param NumberofRecursions - */ - public void getRecursive(String StartingSubject,int NumberofRecursions){ - System.out.print("SparqlModul: Depth: "+NumberofRecursions+" @ "+StartingSubject+" "); - if(NumberofRecursions<=0) - { return; - } - else {NumberofRecursions--;} - //System.out.println(NumberofRecursions); - try{ - - String sparql=q.makeQueryFilter(StartingSubject,this.sf); - p(sparql); - p("*******************"); - // checks cache - String FromCache=c.get(StartingSubject, sparql); - String xml; - // if not in cache get it from dbpedia - if(FromCache==null){ - xml=sendAndReceive(sparql); - c.put(StartingSubject, xml, sparql); - System.out.print("\n"); - } - else{ - xml=FromCache; - System.out.println("FROM CACHE"); - } - p(xml); - p("***********************"); - // get new Subjects - String[] newSubjects=processResult(StartingSubject,xml); - - for (int i = 0; (i < newSubjects.length)&& NumberofRecursions!=0; i++) { - getRecursive(newSubjects[i], NumberofRecursions); - } - - //System.out.println(xml); - }catch (Exception e) {e.printStackTrace();} - - } - - /** - * process the sparql result xml in a simple manner - * - * - * @param subject - * @param xml - * @return list of new individuals - */ - public String[] processResult(String subject,String xml){ - //TODO if result is empty, catch exceptions - String one="<binding name=\"predicate\"><uri>"; - String two="<binding name=\"object\"><uri>"; - String end="</uri></binding>"; - String predtmp=""; - String objtmp=""; - ArrayList<String> al=new ArrayList<String>(); - - while(xml.indexOf(one)!=-1){ - //get pred - xml=xml.substring(xml.indexOf(one)+one.length()); - predtmp=xml.substring(0,xml.indexOf(end)); - //getobj - xml=xml.substring(xml.indexOf(two)+two.length()); - objtmp=xml.substring(0,xml.indexOf(end)); - - // writes the triples and resources in the hashsets - // also fills the arraylist al - processTriples(subject, predtmp, objtmp,al); - //System.out.println(al.size()); - - } - - // convert al to list - Object[] o=al.toArray(); - String[] ret=new String[o.length]; - for (int i = 0; i < o.length; i++) { - ret[i]=(String)o[i]; - } - return ret; - //return (String[])al.toArray(); - //System.out.println(xml); - } - - - - /** - * - * writes the triples and resources in the hashsets - * also fills the arraylist al with new individals for further processing - * @param s - * @param p - * @param o - * @param al - */ - public void processTriples(String s,String p, String o,ArrayList<String> al){ - // the next two lines bump out some inconsistencies within dbpedia - String t="/Category"; - if(s.equals(t) || o.equals(t))return ; - - if(sf.mode==2) - { - if( o.startsWith("http://dbpedia.org/resource/Category:") - && - !p.startsWith("http://www.w3.org/2004/02/skos/core") - ) - {return;} - if(p.equals("http://www.w3.org/2004/02/skos/core#broader")){ - p=subclass; - } - else if(p.equals("http://www.w3.org/2004/02/skos/core#subject")){ - p=type; - } - else {} - } - - //save for further processing - al.add(o); - - // type classes - if(isClass(o)){ - classes.add(o); - if(isClass(s))p=subclass; - else p=type; - } - else { - instances.add(o); - this.properties.add(p); - } - - - - //maketriples - try{ - this.triples.add(makeTriples(s, p, o)); - //fw.write(makeTriples(subject, predtmp, objtmp)); - }catch (Exception e) {e.printStackTrace();} - - - return; - } -// - /** - * also makes subclass property between classes - * - * @param s - * @param p - * @param o - * @return triple in the n triple notation - */ - public String makeTriples(String s,String p, String o){ - String ret=""; - if (format.equals("N-TRIPLES")) ret="<"+s+"> <"+p+"> <"+o+">.\n"; - else if (format.equals("KB")){ - if (p.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) ret="\""+o+"\"(\""+s+"\").\n"; - else ret="\""+p+"\"(\""+s+"\",\""+o+"\").\n"; - } - return ret; - } - - /** - * decides if an object is treated as a class - * - * @param obj - * @return true if obj is in the defaultClassesList - */ - public boolean isClass(String obj){ - - boolean retval=false; - for (String defclass : defaultClasses) { - if(obj.contains(defclass))retval=true; - } - return retval; - } - - - /** - * @see java.lang.Object#finalize() - */ - @Override - public void finalize(){ - typeProperties(); - typeClasses(); - typeInstances(); - } - - public void typeProperties(){ - String rdfns="http://www.w3.org/1999/02/22-rdf-syntax-ns"; - String owlns="http://www.w3.org/2002/07/owl"; - Iterator<String> it=properties.iterator(); - String current=""; - while (it.hasNext()){ - try{ - current=it.next(); - if(current.equals(subclass))continue; - if(current.contains(rdfns)||current.contains(owlns)){/*DO NOTHING*/} - else {this.triples.add(makeTriples(current,type,objectProperty));} - }catch (Exception e) {} - - } - } - public void typeClasses(){ - Iterator<String> it=classes.iterator(); - String current=""; - while (it.hasNext()){ - try{ - current=it.next(); - this.triples.add(makeTriples(current,type,classns)); - }catch (Exception e) {} - } - } - public void typeInstances(){ - Iterator<String> it=instances.iterator(); - String current=""; - while (it.hasNext()){ - try{ - current=it.next(); - this.triples.add(makeTriples(current,type,thing)); - }catch (Exception e) {} - } - } - - /** - * debug print turn on print_flag - * @param s - */ - public void p(String s){ - if(print_flag) - System.out.println(s); - } - - private String sendAndReceive(String sparql) { - StringBuilder answer = new StringBuilder(); - - // String an Sparql-Endpoint schicken - HttpURLConnection connection; - - try { - connection = (HttpURLConnection) url.openConnection(); - connection.setDoOutput(true); - - connection.addRequestProperty("Host", "dbpedia.openlinksw.com"); - connection.addRequestProperty("Connection","close"); - connection.addRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); - connection.addRequestProperty("Accept-Language","de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"); - connection.addRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"); - connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); - - OutputStream os = connection.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - osw.write("default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + - URLEncoder.encode(sparql, "UTF-8")+ - "&format=application%2Fsparql-results%2Bxml"); - osw.close(); - - // receive answer - InputStream is = connection.getInputStream(); - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - - String line; - do { - line = br.readLine(); - if(line!=null) - answer.append(line); - } while (line != null); - - br.close(); - - } catch (IOException e) { - System.out.println("Communication problem with Sparql Server."); - System.exit(0); - } - - return answer.toString(); - } -} Deleted: trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java 2007-10-10 12:28:02 UTC (rev 208) +++ trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java 2007-10-10 12:37:11 UTC (rev 209) @@ -1,123 +0,0 @@ -/** - * Copyright (C) 2007, Sebastian Hellmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -package org.dllearner.kb; - -/** - * - * This class produces sparql queries - * - * @author Sebastian Hellmann - * - */ -public class QueryMaker { - //Good - /*public static String owl ="http://www.w3.org/2002/07/owl#"; - public static String xsd="http://www.w3.org/2001/XMLSchema#"; - public static String rdfs="http://www.w3.org/2000/01/rdf-schema#"; - public static String rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; - public static String base="http://dbpedia.org/resource/"; - public static String dbpedia2="http://dbpedia.org/property/"; - public static String dbpedia="http://dbpedia.org/"; - - - //BAD - public static String skos="http://www.w3.org/2004/02/skos/core#"; - public static String foaf="http://xmlns.com/foaf/0.1/"; - public static String dc="http://purl.org/dc/elements/1.1/"; - public static String foreign="http://dbpedia.org/property/wikipage-"; - public static String sameAs="http://www.w3.org/2002/07/owl#sameAs"; - public static String reference="http://dbpedia.org/property/reference";*/ - - int tempyago=0; - - /** - * reads all the options and makes the sparql query accordingly - * @param subject - * @param sf special object encapsulating all options - * @return sparql query - */ - public String makeQueryFilter(String subject, SparqlFilter sf){ - - - String Filter=""; - if(!sf.useLiterals)Filter+="!isLiteral(?object))"; - for (String p : sf.getPredFilter()) { - Filter+="\n" + filterPredicate(p); - } - for (String o : sf.getObjFilter()) { - Filter+="\n" + filterObject(o); - } - - - String ret= - "SELECT * WHERE { \n" + - "<"+ - subject+ - - "> ?predicate ?object.\n" + - "FILTER( \n" + - "(" +Filter+").}"; - //System.out.println(ret); - return ret; - } - - - /*public String makeQueryDefault(String subject){ - String ret= - "SELECT * WHERE { \n" + - "<"+ - subject+ - - "> ?predicate ?object.\n" + - "FILTER( \n" + - "(!isLiteral(?object))" + - "\n" + filterPredicate(skos)+ - //"\n" + filterObject(skos)+ - "\n" + filterPredicate(foaf)+ - "\n" + filterObject(foaf)+ - "\n" + filterPredicate(foreign)+ - "\n" + filterPredicate(sameAs)+ - "\n" + filterPredicate(reference)+ - ")." + - " }"; - - //System.out.println(ret); - return ret; -}*/ - - /** - * add a new object filter - * (objects are filtered out of sparql result) - * @param ns namespace - * @return - */ - public String filterObject(String ns){ - return "&&( !regex((?object), '"+ns+"') )"; - } - /** - * add a new object filter - * (objects are filtered out of sparql result) - * @param ns namespace - * * @return - */ - public String filterPredicate(String ns){ - return "&&( !regex(str(?predicate), '"+ns+"') )"; - } -} Deleted: trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java 2007-10-10 12:28:02 UTC (rev 208) +++ trunk/src/dl-learner/org/dllearner/kb/SimpleHTTPRequest.java 2007-10-10 12:37:11 UTC (rev 209) @@ -1,126 +0,0 @@ -/** - * Copyright (C) 2007, Sebastian Hellmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -package org.dllearner.kb; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.InetAddress; -import java.net.Socket; -import java.net.URLEncoder; - - - -/** - * - * This class makes a simple http request - * the dbpedia url is hardcoded - * - * @author Sebastian Hellmann - * - * - */ -public class SimpleHTTPRequest { - static final char value[]={13,10}; - static final String cut=new String(value); - - - - - public String sendAndReceive(InetAddress ia, int port, String sparql){ - String retval=""; - // - - byte resp[]=null; - - try{ - Socket SparqlServer=new Socket(ia,port); - String request=makeHeader(sparql); - // send request - (SparqlServer.getOutputStream()).write(request.getBytes()); - - //get Response - resp=readBuffer(new BufferedInputStream(SparqlServer.getInputStream())); - retval=new String(resp); - retval=subtractResponseHeader(retval); - //retval="||"+retval; - - SparqlServer.close(); - - - - } - catch(Exception e){e.printStackTrace();} - //System.out.println("got it"); - return retval; - - }//down - - public static byte[] readBuffer(InputStream IS) - throws IOException{ - byte buffer[] = new byte[0xffff]; - int nbytes=0; - byte resp[]=new byte[0]; - while ((nbytes=IS.read(buffer))!=-1) { - byte tmp[]=new byte[resp.length+nbytes]; - int i=0; - for (;i<resp.length;i++){ - tmp[i]=resp[i]; - } - for(int a=0;a<nbytes;a++,i++){ - tmp[i]=buffer[a]; - } - resp=tmp; - } - return resp; - } - - public String subtractResponseHeader(String in){ - //System.out.println(in.indexOf(cut+""+cut)); - return in.substring(in.indexOf(cut+""+cut)+4); - - - } - - public String makeHeader(String query){ - - - String RequestHeader=""; - try{ - - RequestHeader="GET /sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + - //"SELECT%20%2A%20WHERE%20%7B%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FAristotle%3E%20%3Fa%20%3Fb%20%7D%20" + - URLEncoder.encode(query, "UTF-8")+ - //query+// URLencode - "&format=application%2Fsparql-results%2Bxml HTTP/1.1"+cut+ - "Host: dbpedia.openlinksw.com"+cut+ - "Connection: close"+cut+ - //"Accept-Encoding: gzip"+cut+ - "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"+cut+ - "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"+cut+ - "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+cut+ - "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"+cut+ - cut; - }catch (Exception e) {e.printStackTrace();} - return RequestHeader; - - } - -} Copied: trunk/src/dl-learner/org/dllearner/kb/SparqlCache.java (from rev 205, trunk/src/dl-learner/org/dllearner/kb/Cache.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlCache.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlCache.java 2007-10-10 12:37:11 UTC (rev 209) @@ -0,0 +1,210 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.net.URLEncoder; +/** + * + * This is a primitive cache. + * The objects of this class can be either the cache itself or just on entry in the cache + * + * the cache remembers: a timestamp, the original sparql-query, the result + * key is the subject http://dbpedia.org/resource/Angela_Merkel which is first urlencoded + * and so serves as the hash for the filename. + * Cache validates if timestamp too old and Sparql-Query the same + * before returning the SPARQL xml-result + * + * @author Sebastian Hellmann + * + */ +public class SparqlCache implements Serializable{ + + + final static long serialVersionUID=104; + transient String basedir=""; + transient String fileending=".cache"; + long timestamp; + String content=""; + long daysoffreshness=15; + long multiplier=24*60*60*1000;//h m s ms + String sparqlquery=""; + + + /** + * Constructor for the cache itself. + * Called once at the beginning + * + * @param path Where the base path to the cache is + */ + public SparqlCache(String path){ + this.basedir=path+File.separator; + if(!new File(path).exists()) + {System.out.println(new File(path).mkdir());;} + + } + +// + /** + * Constructor for single cache object(one entry) + * + * @param content the sparql xml result + * @param sparql the sparql query + */ + public SparqlCache(String content, String sparql){ + this.content=content; + this.sparqlquery=sparql; + this.timestamp=System.currentTimeMillis(); + } + + + + /** + * use only on the cache object describing the cache itself + * + * @param key the individual + * @param sparql the sparql query + * @return the cached sparql result or null + */ + public String get(String key, String sparql){ + //System.out.println("get From "+key); + String ret=null; + try{ + SparqlCache c =readFromFile(makeFilename(key)) ; + if(c==null)return null; + //System.out.println(" file found"); + if(!c.checkFreshness())return null; + //System.out.println("fresh"); + if(!c.validate(sparql))return null; + //System.out.println("valid"); + ret=c.content; + }catch (Exception e) {e.printStackTrace();} + return ret; + } + + /** + * + * constructor for single cache object(one entry) + * + * @param key the individual + * @param content the sparql result + * @param sparql the sparql query + */ + public void put(String key, String content, String sparql){ + //System.out.println("put into "+key); + SparqlCache c=new SparqlCache(content,sparql); + putIntoFile(makeFilename(key), c); + } + + + /** + * to normalize the filenames + * + * @param key + * @return + */ + String makeFilename(String key){ + String ret=""; + try{ + ret=basedir+URLEncoder.encode(key, "UTF-8")+fileending; + }catch (Exception e) {e.printStackTrace();} + return ret; + } + + /** + * how old is the result + * @return + */ + boolean checkFreshness(){ + if((System.currentTimeMillis()-this.timestamp)<=(daysoffreshness*multiplier)) + //fresh + return true; + else return false; + } + + + /** + * some sparql query + * @param sparql + * @return + */ + boolean validate(String sparql){ + if(this.sparqlquery.equals(sparql)) + //valid + return true; + else return false; + } + + /** + * makes a new file if none exists + * @param Filename + */ + public void checkFile(String Filename){ + if(!new File(Filename).exists()){ + try{ + new File(Filename).createNewFile(); + }catch (Exception e) {e.printStackTrace();} + + } + + } + + /** + * internal saving function + * puts a cache object into a file + * + * @param Filename + * @param content + */ + public void putIntoFile(String Filename,SparqlCache content){ + try{ + //FileWriter fw=new FileWriter(new File(Filename),true); + FileOutputStream fos = new FileOutputStream( Filename , false ); + ObjectOutputStream o = new ObjectOutputStream( fos ); + o.writeObject( content ); + fos.flush(); + fos.close(); + }catch (Exception e) {System.out.println("Not in cache creating: "+Filename);} + } + + /** + * internal retrieval function + * + * @param Filename + * @return one entry object + */ + public SparqlCache readFromFile(String Filename){ + SparqlCache content=null; + try{ + FileInputStream fos = new FileInputStream( Filename ); + ObjectInputStream o = new ObjectInputStream( fos ); + content=(SparqlCache)o.readObject(); + //FileReader fr=new FileReader(new File(Filename,"r")); + //BufferedReader br=new BufferedReader(fr); + }catch (Exception e) {} + return content; + + } +} Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java 2007-10-10 12:28:02 UTC (rev 208) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java 2007-10-10 12:37:11 UTC (rev 209) @@ -124,7 +124,7 @@ public void init() { System.out.println("SparqlModul: Collecting Ontology"); String[] a=new String[0]; - OntologyCollector oc=new OntologyCollector(instances.toArray(a), numberOfRecursions, + SparqlOntologyCollector oc=new SparqlOntologyCollector(instances.toArray(a), numberOfRecursions, filterMode, Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url); String ont=oc.collectOntology(); Copied: trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java (from rev 208, trunk/src/dl-learner/org/dllearner/kb/OntologyCollector.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java 2007-10-10 12:37:11 UTC (rev 209) @@ -0,0 +1,420 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; + +import org.dllearner.utilities.Files; + +/** + * This class collects the ontology from dbpedia, + * everything is saved in hashsets, so the doublettes are taken care of + * + * + * @author Sebastian Hellmann + * + */ +public class SparqlOntologyCollector { + + boolean print_flag=false; + SparqlQueryMaker q; + SparqlCache c; + URL url; + SparqlFilter sf; + String[] subjectList; + int numberOfRecursions; + HashSet<String> properties; + HashSet<String> classes; + HashSet<String> instances; + HashSet<String> triples; + String format; + static final char value[]={13,10}; + static final String cut=new String(value); + + // some namespaces + String subclass="http://www.w3.org/2000/01/rdf-schema#subClassOf"; + String type="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; + String objectProperty="http://www.w3.org/2002/07/owl#ObjectProperty"; + String classns="http://www.w3.org/2002/07/owl#Class"; + String thing="http://www.w3.org/2002/07/owl#Thing"; + + + String[] defaultClasses={ + "http://dbpedia.org/class/yago", + "http://dbpedia.org/resource/Category:", + "http://dbpedia.org/resource/Template:", + "http://www.w3.org/2004/02/skos/core", + "http://dbpedia.org/class/"}; //TODO FEHLER hier fehlt yago + + + /** + * + * + * @param subjectList + * @param numberOfRecursions + * @param filterMode + * @param FilterPredList + * @param FilterObjList + * @param defClasses + */ + public SparqlOntologyCollector(String[] subjectList,int numberOfRecursions, + int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses, String format, URL url){ + this.subjectList=subjectList; + this.numberOfRecursions=numberOfRecursions; + this.format=format; + this.q=new SparqlQueryMaker(); + this.c=new SparqlCache("cache"); + if(defClasses!=null && defClasses.length>0 ){ + this.defaultClasses=defClasses; + } + + try{ + this.sf=new SparqlFilter(filterMode,FilterPredList,FilterObjList); + this.url=url; + this.properties=new HashSet<String>(); + this.classes=new HashSet<String>(); + this.instances=new HashSet<String>(); + this.triples=new HashSet<String>(); + }catch (Exception e) {e.printStackTrace();} + + } + /** + * first collects the ontology + * then types everything so it becomes owl-dl + * + * @return all triples in n-triple format + */ + public String collectOntology(){ + getRecursiveList(subjectList, numberOfRecursions); + finalize(); + String ret=""; + for (Iterator<String> iter = triples.iterator(); iter.hasNext();) { + ret += iter.next(); + + } + return ret; + } + + /** + * calls getRecursive for each subject in list + * @param subjects + * @param NumberofRecursions + */ + public void getRecursiveList(String[] subjects,int NumberofRecursions){ + for (int i = 0; i < subjects.length; i++) { + getRecursive(subjects[i], NumberofRecursions); + + } + + } + + /** + * gets all triples until numberofrecursion-- gets 0 + * + * @param StartingSubject + * @param NumberofRecursions + */ + public void getRecursive(String StartingSubject,int NumberofRecursions){ + System.out.print("SparqlModul: Depth: "+NumberofRecursions+" @ "+StartingSubject+" "); + if(NumberofRecursions<=0) + { return; + } + else {NumberofRecursions--;} + //System.out.println(NumberofRecursions); + try{ + + String sparql=q.makeQueryFilter(StartingSubject,this.sf); + p(sparql); + p("*******************"); + // checks cache + String FromCache=c.get(StartingSubject, sparql); + String xml; + // if not in cache get it from dbpedia + if(FromCache==null){ + xml=sendAndReceive(sparql); + c.put(StartingSubject, xml, sparql); + System.out.print("\n"); + } + else{ + xml=FromCache; + System.out.println("FROM CACHE"); + } + p(xml); + p("***********************"); + // get new Subjects + String[] newSubjects=processResult(StartingSubject,xml); + + for (int i = 0; (i < newSubjects.length)&& NumberofRecursions!=0; i++) { + getRecursive(newSubjects[i], NumberofRecursions); + } + + //System.out.println(xml); + }catch (Exception e) {e.printStackTrace();} + + } + + /** + * process the sparql result xml in a simple manner + * + * + * @param subject + * @param xml + * @return list of new individuals + */ + public String[] processResult(String subject,String xml){ + //TODO if result is empty, catch exceptions + String one="<binding name=\"predicate\"><uri>"; + String two="<binding name=\"object\"><uri>"; + String end="</uri></binding>"; + String predtmp=""; + String objtmp=""; + ArrayList<String> al=new ArrayList<String>(); + + while(xml.indexOf(one)!=-1){ + //get pred + xml=xml.substring(xml.indexOf(one)+one.length()); + predtmp=xml.substring(0,xml.indexOf(end)); + //getobj + xml=xml.substring(xml.indexOf(two)+two.length()); + objtmp=xml.substring(0,xml.indexOf(end)); + + // writes the triples and resources in the hashsets + // also fills the arraylist al + processTriples(subject, predtmp, objtmp,al); + //System.out.println(al.size()); + + } + + // convert al to list + Object[] o=al.toArray(); + String[] ret=new String[o.length]; + for (int i = 0; i < o.length; i++) { + ret[i]=(String)o[i]; + } + return ret; + //return (String[])al.toArray(); + //System.out.println(xml); + } + + + + /** + * + * writes the triples and resources in the hashsets + * also fills the arraylist al with new individals for further processing + * @param s + * @param p + * @param o + * @param al + */ + public void processTriples(String s,String p, String o,ArrayList<String> al){ + // the next two lines bump out some inconsistencies within dbpedia + String t="/Category"; + if(s.equals(t) || o.equals(t))return ; + + if(sf.mode==2) + { + if( o.startsWith("http://dbpedia.org/resource/Category:") + && + !p.startsWith("http://www.w3.org/2004/02/skos/core") + ) + {return;} + if(p.equals("http://www.w3.org/2004/02/skos/core#broader")){ + p=subclass; + } + else if(p.equals("http://www.w3.org/2004/02/skos/core#subject")){ + p=type; + } + else {} + } + + //save for further processing + al.add(o); + + // type classes + if(isClass(o)){ + classes.add(o); + if(isClass(s))p=subclass; + else p=type; + } + else { + instances.add(o); + this.properties.add(p); + } + + + + //maketriples + try{ + this.triples.add(makeTriples(s, p, o)); + //fw.write(makeTriples(subject, predtmp, objtmp)); + }catch (Exception e) {e.printStackTrace();} + + + return; + } +// + /** + * also makes subclass property between classes + * + * @param s + * @param p + * @param o + * @return triple in the n triple notation + */ + public String makeTriples(String s,String p, String o){ + String ret=""; + if (format.equals("N-TRIPLES")) ret="<"+s+"> <"+p+"> <"+o+">.\n"; + else if (format.equals("KB")){ + if (p.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) ret="\""+o+"\"(\""+s+"\").\n"; + else ret="\""+p+"\"(\""+s+"\",\""+o+"\").\n"; + } + return ret; + } + + /** + * decides if an object is treated as a class + * + * @param obj + * @return true if obj is in the defaultClassesList + */ + public boolean isClass(String obj){ + + boolean retval=false; + for (String defclass : defaultClasses) { + if(obj.contains(defclass))retval=true; + } + return retval; + } + + + /** + * @see java.lang.Object#finalize() + */ + @Override + public void finalize(){ + typeProperties(); + typeClasses(); + typeInstances(); + } + + public void typeProperties(){ + String rdfns="http://www.w3.org/1999/02/22-rdf-syntax-ns"; + String owlns="http://www.w3.org/2002/07/owl"; + Iterator<String> it=properties.iterator(); + String current=""; + while (it.hasNext()){ + try{ + current=it.next(); + if(current.equals(subclass))continue; + if(current.contains(rdfns)||current.contains(owlns)){/*DO NOTHING*/} + else {this.triples.add(makeTriples(current,type,objectProperty));} + }catch (Exception e) {} + + } + } + public void typeClasses(){ + Iterator<String> it=classes.iterator(); + String current=""; + while (it.hasNext()){ + try{ + current=it.next(); + this.triples.add(makeTriples(current,type,classns)); + }catch (Exception e) {} + } + } + public void typeInstances(){ + Iterator<String> it=instances.iterator(); + String current=""; + while (it.hasNext()){ + try{ + current=it.next(); + this.triples.add(makeTriples(current,type,thing)); + }catch (Exception e) {} + } + } + + /** + * debug print turn on print_flag + * @param s + */ + public void p(String s){ + if(print_flag) + System.out.println(s); + } + + private String sendAndReceive(String sparql) { + StringBuilder answer = new StringBuilder(); + + // String an Sparql-Endpoint schicken + HttpURLConnection connection; + + try { + connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + + connection.addRequestProperty("Host", "dbpedia.openlinksw.com"); + connection.addRequestProperty("Connection","close"); + connection.addRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); + connection.addRequestProperty("Accept-Language","de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"); + connection.addRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"); + connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); + + OutputStream os = connection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + osw.write("default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + + URLEncoder.encode(sparql, "UTF-8")+ + "&format=application%2Fsparql-results%2Bxml"); + osw.close(); + + // receive answer + InputStream is = connection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String line; + do { + line = br.readLine(); + if(line!=null) + answer.append(line); + } while (line != null); + + br.close(); + + } catch (IOException e) { + System.out.println("Communication problem with Sparql Server."); + System.exit(0); + } + + return answer.toString(); + } +} Copied: trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java (from rev 205, trunk/src/dl-learner/org/dllearner/kb/QueryMaker.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java 2007-10-10 12:37:11 UTC (rev 209) @@ -0,0 +1,123 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +/** + * + * This class produces sparql queries + * + * @author Sebastian Hellmann + * + */ +public class SparqlQueryMaker { + //Good + /*public static String owl ="http://www.w3.org/2002/07/owl#"; + public static String xsd="http://www.w3.org/2001/XMLSchema#"; + public static String rdfs="http://www.w3.org/2000/01/rdf-schema#"; + public static String rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + public static String base="http://dbpedia.org/resource/"; + public static String dbpedia2="http://dbpedia.org/property/"; + public static String dbpedia="http://dbpedia.org/"; + + + //BAD + public static String skos="http://www.w3.org/2004/02/skos/core#"; + public static String foaf="http://xmlns.com/foaf/0.1/"; + public static String dc="http://purl.org/dc/elements/1.1/"; + public static String foreign="http://dbpedia.org/property/wikipage-"; + public static String sameAs="http://www.w3.org/2002/07/owl#sameAs"; + public static String reference="http://dbpedia.org/property/reference";*/ + + int tempyago=0; + + /** + * reads all the options and makes the sparql query accordingly + * @param subject + * @param sf special object encapsulating all options + * @return sparql query + */ + public String makeQueryFilter(String subject, SparqlFilter sf){ + + + String Filter=""; + if(!sf.useLiterals)Filter+="!isLiteral(?object))"; + for (String p : sf.getPredFilter()) { + Filter+="\n" + filterPredicate(p); + } + for (String o : sf.getObjFilter()) { + Filter+="\n" + filterObject(o); + } + + + String ret= + "SELECT * WHERE { \n" + + "<"+ + subject+ + + "> ?predicate ?object.\n" + + "FILTER( \n" + + "(" +Filter+").}"; + //System.out.println(ret); + return ret; + } + + + /*public String makeQueryDefault(String subject){ + String ret= + "SELECT * WHERE { \n" + + "<"+ + subject+ + + "> ?predicate ?object.\n" + + "FILTER( \n" + + "(!isLiteral(?object))" + + "\n" + filterPredicate(skos)+ + //"\n" + filterObject(skos)+ + "\n" + filterPredicate(foaf)+ + "\n" + filterObject(foaf)+ + "\n" + filterPredicate(foreign)+ + "\n" + filterPredicate(sameAs)+ + "\n" + filterPredicate(reference)+ + ")." + + " }"; + + //System.out.println(ret); + return ret; +}*/ + + /** + * add a new object filter + * (objects are filtered out of sparql result) + * @param ns namespace + * @return + */ + public String filterObject(String ns){ + return "&&( !regex((?object), '"+ns+"') )"; + } + /** + * add a new object filter + * (objects are filtered out of sparql result) + * @param ns namespace + * * @return + */ + public String filterPredicate(String ns){ + return "&&( !regex(str(?predicate), '"+ns+"') )"; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sk...@us...> - 2007-10-18 15:55:01
|
Revision: 234 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=234&view=rev Author: sknappe Date: 2007-10-18 08:54:56 -0700 (Thu, 18 Oct 2007) Log Message: ----------- Added functions for DBPedia-Navigator Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java 2007-10-18 15:54:15 UTC (rev 233) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpoint.java 2007-10-18 15:54:56 UTC (rev 234) @@ -28,6 +28,8 @@ import java.util.Collection; import java.util.LinkedList; import java.util.Set; +import java.util.StringTokenizer; +import java.util.Vector; import org.dllearner.core.BooleanConfigOption; import org.dllearner.core.ConfigEntry; @@ -56,7 +58,7 @@ private URL url; private Set<String> instances; - private URL ntFile; + private URL dumpFile; private int numberOfRecursions; private int filterMode; private Set<String> predList; @@ -65,6 +67,8 @@ private KB kb; private String format; private boolean dumpToFile; + private boolean useLits=false; + private String[] ontArray; public static String getName() { return "SPARQL Endpoint"; @@ -81,6 +85,7 @@ options.add(new StringSetConfigOption("classList","a class list")); options.add(new StringConfigOption("format", "N-TRIPLES or KB format")); options.add(new BooleanConfigOption("dumpToFile", "wether Ontology from DBPedia is written to a file or not")); + options.add(new BooleanConfigOption("useLits","use Literals in SPARQL query")); return options; } @@ -114,6 +119,8 @@ format=(String)entry.getValue(); } else if(option.equals("dumpToFile")){ dumpToFile=(Boolean)entry.getValue(); + } else if(option.equals("useLits")){ + useLits=(Boolean)entry.getValue(); } } @@ -125,10 +132,16 @@ System.out.println("SparqlModul: Collecting Ontology"); String[] a=new String[0]; SparqlOntologyCollector oc=new SparqlOntologyCollector(instances.toArray(a), numberOfRecursions, - filterMode, Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url); - String ont=oc.collectOntology(); + filterMode, Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url,useLits); + String ont=""; + if (format.equals("Array")){ + ontArray=oc.collectOntologyAsArray(); + } + else{ + ont=oc.collectOntology(); + } - if (format.equals("N-TRIPLES")||dumpToFile){ + if (dumpToFile){ String filename=System.currentTimeMillis()+".nt"; String basedir="cache"+File.separator; try{ @@ -140,7 +153,7 @@ fw.flush(); fw.close(); - ntFile=(new File(basedir+filename)).toURI().toURL(); + dumpFile=(new File(basedir+filename)).toURI().toURL(); }catch (Exception e) {e.printStackTrace();} } if (format.equals("KB")) { @@ -159,11 +172,24 @@ */ @Override public String toDIG(URI kbURI) { - if (format.equals("N-TRIPLES")) return JenaOWLDIGConverter.getTellsString(ntFile, OntologyFileFormat.N_TRIPLES, kbURI); + if (format.equals("N-TRIPLES")) return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFileFormat.N_TRIPLES, kbURI); else return DIGConverter.getDIGString(kb, kbURI).toString(); } public URL getURL() { return url; } + + public String[] getOntArray() { + return ontArray; + } + + public String[] getSubjects(String label,int limit) + { + System.out.println("SparqlModul: Collecting Subjects"); + SparqlOntologyCollector oc=new SparqlOntologyCollector(null, 1,0,null,null,null,null,url,false); + String[] ret=oc.getSubjectsFromLabel(label,limit); + System.out.println("SparqlModul: ****Finished"); + return ret; + } } Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java 2007-10-18 15:54:15 UTC (rev 233) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlFilter.java 2007-10-18 15:54:56 UTC (rev 234) @@ -81,6 +81,7 @@ public SparqlFilter(int mode, String[] pred, String[] obj) { if (mode==-1 && (pred==null || pred.length==0 || obj==null||obj.length==0)) {mode=0;} + else this.mode=mode; switch (mode){ case 0: //yago Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java 2007-10-18 15:54:15 UTC (rev 233) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlOntologyCollector.java 2007-10-18 15:54:56 UTC (rev 234) @@ -31,6 +31,8 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; +import java.util.StringTokenizer; +import java.util.Vector; /** @@ -85,7 +87,7 @@ * @param defClasses */ public SparqlOntologyCollector(String[] subjectList,int numberOfRecursions, - int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses, String format, URL url){ + int filterMode, String[] FilterPredList,String[] FilterObjList,String[] defClasses, String format, URL url, boolean useLits){ this.subjectList=subjectList; this.numberOfRecursions=numberOfRecursions; this.format=format; @@ -93,10 +95,9 @@ this.c=new SparqlCache("cache"); if(defClasses!=null && defClasses.length>0 ){ this.defaultClasses=defClasses; - } - + } try{ - this.sf=new SparqlFilter(filterMode,FilterPredList,FilterObjList); + this.sf=new SparqlFilter(filterMode,FilterPredList,FilterObjList,useLits); this.url=url; this.properties=new HashSet<String>(); this.classes=new HashSet<String>(); @@ -122,6 +123,32 @@ return ret; } + public String[] collectOntologyAsArray(){ + getRecursiveList(subjectList, numberOfRecursions); + if (sf.mode!=3) finalize(); + String[] a=new String[0]; + return triples.toArray(a); + } + + public String[] getSubjectsFromLabel(String label, int limit){ + System.out.println("Searching for Label: "+label); + String sparql=q.makeLabelQuery(label,limit); + String FromCache=c.get(label, sparql); + String xml; + // if not in cache get it from dbpedia + if(FromCache==null){ + xml=sendAndReceive(sparql); + c.put(label, xml, sparql); + System.out.print("\n"); + } + else{ + xml=FromCache; + System.out.println("FROM CACHE"); + } + + return processSubjects(xml); + } + /** * calls getRecursive for each subject in list * @param subjects @@ -191,20 +218,25 @@ public String[] processResult(String subject,String xml){ //TODO if result is empty, catch exceptions String one="<binding name=\"predicate\"><uri>"; - String two="<binding name=\"object\"><uri>"; + String two="<binding name=\"object\">"; String end="</uri></binding>"; String predtmp=""; String objtmp=""; ArrayList<String> al=new ArrayList<String>(); - while(xml.indexOf(one)!=-1){ //get pred xml=xml.substring(xml.indexOf(one)+one.length()); predtmp=xml.substring(0,xml.indexOf(end)); //getobj xml=xml.substring(xml.indexOf(two)+two.length()); - objtmp=xml.substring(0,xml.indexOf(end)); + if (xml.startsWith("<literal xml:lang=\"en\">")){ + xml=xml.substring(xml.indexOf(">")+1); + objtmp=xml.substring(0,xml.indexOf("</literal>")); + } + else if (xml.startsWith("<uri>")) objtmp=xml.substring(5,xml.indexOf(end)); + else continue; + System.out.println("Pred: "+predtmp+" Obj: "+objtmp); // writes the triples and resources in the hashsets // also fills the arraylist al processTriples(subject, predtmp, objtmp,al); @@ -280,6 +312,23 @@ return; } + + private String[] processSubjects(String xml){ + Vector<String> vec=new Vector<String>(); + String one="<binding name=\"subject\"><uri>"; + String end="</uri></binding>"; + String subject=""; + while(xml.indexOf(one)!=-1){ + //get subject + xml=xml.substring(xml.indexOf(one)+one.length()); + subject=xml.substring(0,xml.indexOf(end)); + + System.out.println("Subject: "+subject); + vec.addElement(subject); + } + String[] a=new String[vec.size()]; + return vec.toArray(a); + } // /** * also makes subclass property between classes @@ -296,6 +345,10 @@ if (p.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) ret="\""+o+"\"(\""+s+"\").\n"; else ret="\""+p+"\"(\""+s+"\",\""+o+"\").\n"; } + else if (format.equals("Array")) + { + ret=s+"<"+p+"<"+o+"\n"; + } return ret; } @@ -375,7 +428,7 @@ // String an Sparql-Endpoint schicken HttpURLConnection connection; - + try { connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java 2007-10-18 15:54:15 UTC (rev 233) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlQueryMaker.java 2007-10-18 15:54:56 UTC (rev 234) @@ -27,26 +27,7 @@ * */ public class SparqlQueryMaker { - //Good - /*public static String owl ="http://www.w3.org/2002/07/owl#"; - public static String xsd="http://www.w3.org/2001/XMLSchema#"; - public static String rdfs="http://www.w3.org/2000/01/rdf-schema#"; - public static String rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; - public static String base="http://dbpedia.org/resource/"; - public static String dbpedia2="http://dbpedia.org/property/"; - public static String dbpedia="http://dbpedia.org/"; - - //BAD - public static String skos="http://www.w3.org/2004/02/skos/core#"; - public static String foaf="http://xmlns.com/foaf/0.1/"; - public static String dc="http://purl.org/dc/elements/1.1/"; - public static String foreign="http://dbpedia.org/property/wikipage-"; - public static String sameAs="http://www.w3.org/2002/07/owl#sameAs"; - public static String reference="http://dbpedia.org/property/reference";*/ - - int tempyago=0; - /** * reads all the options and makes the sparql query accordingly * @param subject @@ -57,7 +38,7 @@ String Filter=""; - if(!sf.useLiterals)Filter+="!isLiteral(?object))"; + if(!sf.useLiterals)Filter+="!isLiteral(?object)"; for (String p : sf.getPredFilter()) { Filter+="\n" + filterPredicate(p); } @@ -65,43 +46,19 @@ Filter+="\n" + filterObject(o); } - String ret= "SELECT * WHERE { \n" + "<"+ subject+ - - "> ?predicate ?object.\n" + - "FILTER( \n" + - "(" +Filter+").}"; + "> ?predicate ?object.\n"; + if (!(Filter.length()==0)) + ret+="FILTER( \n" + + "(" +Filter+"))."; + ret+="}"; //System.out.println(ret); return ret; } - - /*public String makeQueryDefault(String subject){ - String ret= - "SELECT * WHERE { \n" + - "<"+ - subject+ - - "> ?predicate ?object.\n" + - "FILTER( \n" + - "(!isLiteral(?object))" + - "\n" + filterPredicate(skos)+ - //"\n" + filterObject(skos)+ - "\n" + filterPredicate(foaf)+ - "\n" + filterObject(foaf)+ - "\n" + filterPredicate(foreign)+ - "\n" + filterPredicate(sameAs)+ - "\n" + filterPredicate(reference)+ - ")." + - " }"; - - //System.out.println(ret); - return ret; -}*/ - /** * add a new object filter * (objects are filtered out of sparql result) @@ -120,4 +77,11 @@ public String filterPredicate(String ns){ return "&&( !regex(str(?predicate), '"+ns+"') )"; } + + public String makeLabelQuery(String label,int limit){ + return "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>\n"+ + "SELECT DISTINCT ?subject\n"+ + "WHERE { ?subject rdfs:label ?object.FILTER regex(?object,\""+label+"\"@en)}\n"+ + "LIMIT "+limit; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2007-12-01 20:50:21
|
Revision: 293 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=293&view=rev Author: kurzum Date: 2007-12-01 12:50:16 -0800 (Sat, 01 Dec 2007) Log Message: ----------- all files Added Paths: ----------- trunk/src/dl-learner/org/dllearner/kb/extraction/ trunk/src/dl-learner/org/dllearner/kb/extraction/Configuration.java trunk/src/dl-learner/org/dllearner/kb/extraction/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/extraction/Manager.java trunk/src/dl-learner/org/dllearner/kb/extraction/Manipulator.java trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlEndpoint.java trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlQueryType.java trunk/src/dl-learner/org/dllearner/kb/extraction/Test.java trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/ trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/ClassNode.java trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/InstanceNode.java trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Node.java trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/PropertyNode.java trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Tupel.java trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/ trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/Cache.java trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SimpleHTTPRequest.java trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlHTTPRequest.java trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlQueryMaker.java trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/TypedSparqlQuery.java Added: trunk/src/dl-learner/org/dllearner/kb/extraction/Configuration.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/Configuration.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/Configuration.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,202 @@ +package org.dllearner.kb.extraction; + +import java.io.File; +import java.net.URI; +import java.net.URL; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.semanticweb.owl.apibinding.OWLManager; +import org.semanticweb.owl.model.OWLConstant; +import org.semanticweb.owl.model.OWLDataPropertyExpression; +import org.semanticweb.owl.model.OWLIndividual; +import org.semanticweb.owl.model.OWLObjectPropertyExpression; +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.model.OWLOntologyManager; + +public class Configuration { + private SparqlEndpoint SparqlEndpoint; + private SparqlQueryType SparqlQueryType; + private Manipulator Manipulator; + + + private Configuration(){ + } + public Configuration(SparqlEndpoint SparqlEndpoint,SparqlQueryType SparqlQueryType){ + this.SparqlEndpoint=SparqlEndpoint; + this.SparqlQueryType=SparqlQueryType; + } + + + public static Configuration getConfiguration(URI uri){ + //public static String getTellsString(URL file, URI kbURI){//throws OWLOntologyCreationException{ + Configuration ret=new Configuration(); + try{ + String file="config/config.owl"; + + + File f= new File(file); + String fileURL="file:///"+f.getAbsolutePath(); + URL u=new URL(fileURL); + /* Load an ontology from a physical URI */ + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + OWLOntology ontology = manager.loadOntologyFromPhysicalURI(u.toURI()); + //System.out.println( ontology.containsIndividualReference(uri)); + //OWLIndividualImpl ind=new OWLIndividualImpl(); + //System.out.println(ontology.getReferencedIndividuals()); + Set<OWLIndividual> s=ontology.getReferencedIndividuals(); + //System.out.println(ontology.getReferencedClasses()); + //Set<OWLIndividualAxiom> s= ontology.getIndividualAxioms(); + Iterator<OWLIndividual> it=s.iterator(); + while (it.hasNext()){ + OWLIndividual tmp=(OWLIndividual)it.next(); + //tmp.getURI() + if(tmp.getURI().equals(uri)){ + OWLIndividual[] arr=getIndividualsForProperty("hasSparqlEndpoint",tmp.getObjectPropertyValues(ontology)); + OWLIndividual sEndpoint=arr[0]; + ret.SparqlEndpoint=makeEndpoint(sEndpoint, ontology); + arr=getIndividualsForProperty("hasTypedQuery",tmp.getObjectPropertyValues(ontology)); + OWLIndividual typedQuery=arr[0]; + ret.SparqlQueryType=makeSparqlQueryType(typedQuery, ontology); + + } + //{hasSparqlEndpoint=[dbpediaEndpoint]} + } + + ret.Manipulator=makeManipulator(); + }catch (Exception e) {e.printStackTrace();} + + return ret; + } + + + + + + public static OWLIndividual[] getIndividualsForProperty(String propertyname,Map<OWLObjectPropertyExpression, Set<OWLIndividual>> m){ + Set<OWLObjectPropertyExpression> s=m.keySet(); + + Iterator<OWLObjectPropertyExpression> it=s.iterator(); + while (it.hasNext()){ + OWLObjectPropertyExpression tmp=(OWLObjectPropertyExpression)it.next(); + //System.out.println(tmp); + //System.out.println(propertyname); + if(tmp.toString().equals(propertyname)) + { + Object[] arr=((Set<OWLIndividual>)m.get(tmp)).toArray() ; + OWLIndividual[] o=new OWLIndividual[arr.length]; + for (int i = 0; i < o.length; i++) { + o[i]=(OWLIndividual)arr[i]; + } + + return o;} + } + return null; + + } + + public static String getFirstValueForDataProperty(String propertyname,Map<OWLDataPropertyExpression, Set<OWLConstant>> m){ + return getValuesForDataProperty(propertyname, m)[0]; + } + + public static String[] getValuesForDataProperty(String propertyname,Map<OWLDataPropertyExpression, Set<OWLConstant>> m){ + Set<OWLDataPropertyExpression> s=m.keySet(); + + Iterator<OWLDataPropertyExpression> it=s.iterator(); + while (it.hasNext()){ + OWLDataPropertyExpression tmp=(OWLDataPropertyExpression)it.next(); + if(tmp.toString().equals(propertyname)) + { + Object[] arr=((Set<OWLConstant>)m.get(tmp)).toArray() ; + String[] str=new String[arr.length]; + for (int i = 0; i < str.length; i++) { + str[i]=((OWLConstant)arr[i]).getLiteral(); + } + return str;} + } + return null; + + } + + public static SparqlEndpoint makeEndpoint(OWLIndividual sEndpoint,OWLOntology o){ + String host=getFirstValueForDataProperty("hasHost",sEndpoint.getDataPropertyValues(o)); + String port=getFirstValueForDataProperty("hasPort",sEndpoint.getDataPropertyValues(o)); + String hasAfterGET=getFirstValueForDataProperty("hasAfterGET",sEndpoint.getDataPropertyValues(o)); + String hasQueryParameter=getFirstValueForDataProperty("hasQueryParameter",sEndpoint.getDataPropertyValues(o)); + OWLIndividual[] para=getIndividualsForProperty("hasGETParameter",sEndpoint.getObjectPropertyValues(o)); + //System.out.println("test"); + HashMap<String,String> parameters=new HashMap<String,String>(); + if(para==null)return new SparqlEndpoint( host, port, hasAfterGET, hasQueryParameter, parameters); + for (OWLIndividual p : para) { + //System.out.println("test2"); + String a1=getFirstValueForDataProperty("hasParameterName",p.getDataPropertyValues(o)); + String a2=getFirstValueForDataProperty("hasParameterContent",p.getDataPropertyValues(o)); + parameters.put(a1, a2); + } + //System.out.println("test2"); + //System.out.println(host+port+ hasAfterGET+ hasQueryParameter+ parameters); + return new SparqlEndpoint( host, port, hasAfterGET, hasQueryParameter, parameters); + + + + } + + public static SparqlQueryType makeSparqlQueryType(OWLIndividual typedQuery,OWLOntology o){ + String useLiterals=getFirstValueForDataProperty("usesLiterals",typedQuery.getDataPropertyValues(o)); + String hasMode=getFirstValueForDataProperty("hasMode",typedQuery.getDataPropertyValues(o)); + //String hasAfterGET=getValuesForDataProperty("hasAfterGET",sEndpoint.getDataPropertyValues(o)); + //String hasQueryParameter=getValuesForDataProperty("hasQueryParameter",sEndpoint.getDataPropertyValues(o)); + OWLIndividual[] objFilter=getIndividualsForProperty("hasObjectFilterSet",typedQuery.getObjectPropertyValues(o)); + OWLIndividual[] predFilter=getIndividualsForProperty("hasPredicateFilterSet",typedQuery.getObjectPropertyValues(o)); + + Set<String> objectFilter=new HashSet<String>(); + Set<String> predicateFilter=new HashSet<String>(); + + for (OWLIndividual of : objFilter) { + String[] tmp=getValuesForDataProperty("filtersURI",of.getDataPropertyValues(o)); + for (String s : tmp){ + objectFilter.add(s); + + } + } + + for (OWLIndividual pf : predFilter) { + String[] tmp=getValuesForDataProperty("filtersURI",pf.getDataPropertyValues(o)); + for (String s : tmp){ + predicateFilter.add(s); + + } + } + //System.out.println(predicateFilter); + //System.out.println(hasMode+objectFilter+predicateFilter+useLiterals); + return new SparqlQueryType(hasMode,objectFilter,predicateFilter,useLiterals); + + + + } + + public static Manipulator makeManipulator() { + return new Manipulator(); + } + + + public Manipulator getManipulator() { + return this.Manipulator; + } + + public SparqlEndpoint getSparqlEndpoint() { + return SparqlEndpoint; + } + + + + public SparqlQueryType getSparqlQueryType() { + return SparqlQueryType; + } + + + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/ExtractionAlgorithm.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/ExtractionAlgorithm.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,74 @@ +package org.dllearner.kb.extraction; + +import java.net.URI; +import java.util.Vector; + +import org.dllearner.kb.extraction.datastructures.InstanceNode; +import org.dllearner.kb.extraction.datastructures.Node; +import org.dllearner.kb.extraction.sparql.TypedSparqlQuery; + +public class ExtractionAlgorithm { + + private Configuration Configuration; + private Manipulator Manipulator; + private int recursiondepth=2; + private boolean getAllBackground=true; + + + + public ExtractionAlgorithm (Configuration Configuration){ + this.Configuration=Configuration; + this.Manipulator=Configuration.getManipulator(); + + } + + public Node getFirstNode(URI u){ + return new InstanceNode(u); + } + public Vector<Node> expandAll(URI[] u,TypedSparqlQuery tsp){ + Vector<Node> v=new Vector<Node>(); + for(URI one:u){ + v.add(expandNode(one, tsp)); + } + return v; + } + + + public Node expandNode(URI u, TypedSparqlQuery tsp){ + Node n=getFirstNode(u); + Vector<Node> v=new Vector<Node>(); + v.add(n); + System.out.println("StartVector: "+v); + // n.expand(tsp, this.Manipulator); + //Vector<Node> second= + for(int x=1;x<=this.recursiondepth;x++){ + + Vector<Node>tmp=new Vector<Node>(); + while (v.size()>0) { + Node tmpNode=v.remove(0); + System.out.println("Expanding "+tmpNode); + Vector<Node> tmpVec=tmpNode.expand(tsp, this.Manipulator); + + tmp.addAll(tmpVec); + } + v=tmp; + System.out.println("Rec: "+x+" with "+v); + } + if(this.getAllBackground){ + Vector<Node> classes=new Vector<Node>(); + for(Node one:v){ + if(one.isClass()) {classes.add(one);} + } + while(classes.size()>0){ + System.out.println(classes.size()); + classes.addAll(classes.remove(0).expand(tsp, this.Manipulator)); + } + + } + return n; + + } + + + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/Manager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/Manager.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/Manager.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,67 @@ +package org.dllearner.kb.extraction; + +import java.net.URI; +import java.util.HashSet; +import java.util.Set; + +import org.dllearner.kb.extraction.datastructures.Node; +import org.dllearner.kb.extraction.sparql.TypedSparqlQuery; + +public class Manager { + + private Configuration Configuration; + private TypedSparqlQuery TypedSparqlQuery; + private ExtractionAlgorithm ExtractionAlgorithm; + + + public void usePredefinedConfiguration(URI uri){ + + this.Configuration=org.dllearner.kb.extraction.Configuration.getConfiguration(uri); + this.TypedSparqlQuery=new TypedSparqlQuery(Configuration); + this.ExtractionAlgorithm=new ExtractionAlgorithm(Configuration); + } + + public void useConfiguration(SparqlQueryType SparqlQueryType, SparqlEndpoint SparqlEndpoint){ + + this.Configuration=new Configuration(SparqlEndpoint,SparqlQueryType); + this.TypedSparqlQuery=new TypedSparqlQuery(Configuration); + this.ExtractionAlgorithm=new ExtractionAlgorithm(Configuration); + } + + public String extract(URI uri){ + //this.TypedSparqlQuery.query(uri); + //System.out.println(ExtractionAlgorithm.getFirstNode(uri)); + System.out.println("Start extracting"); + Node n=this.ExtractionAlgorithm.expandNode(uri, this.TypedSparqlQuery); + Set<String> s=n.toNTriple(); + String nt=""; + for(String str:s){ + nt+=str+"\n"; + } + return nt; + } + + public String extract(Set<String> instances){ + //this.TypedSparqlQuery.query(uri); + //System.out.println(ExtractionAlgorithm.getFirstNode(uri)); + System.out.println("Start extracting"); + Set<String> ret=new HashSet<String>(); + + + for(String one:instances){ + try{ + Node n=this.ExtractionAlgorithm.expandNode(new URI(one),this.TypedSparqlQuery); + ret.addAll(n.toNTriple()); + }catch (Exception e) {e.printStackTrace();} + } + + + String nt=""; + for(String str:ret){ + nt+=str+"\n"; + } + return nt; + } + + +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/kb/extraction/Manipulator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/Manipulator.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/Manipulator.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,55 @@ +package org.dllearner.kb.extraction; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.dllearner.kb.extraction.datastructures.Node; +import org.dllearner.kb.extraction.datastructures.Tupel; + +public class Manipulator { + public String subclass="http://www.w3.org/2000/01/rdf-schema#subClassOf"; + public String type="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; + + String objectProperty="http://www.w3.org/2002/07/owl#ObjectProperty"; + String classns="http://www.w3.org/2002/07/owl#Class"; + String thing="http://www.w3.org/2002/07/owl#Thing"; + + Set<String> classproperties; + + + String[] defaultClasses={ + "http://dbpedia.org/class/yago", + "http://dbpedia.org/resource/Category:", + "http://dbpedia.org/resource/Template:", + "http://www.w3.org/2004/02/skos/core", + "http://dbpedia.org/class/"}; //TODO FEHLER hier fehlt yago + + public Manipulator(){ + Set<String> classproperties=new HashSet<String>(); + classproperties.add(subclass); + + } + + public Set<Tupel> check(Set<Tupel> s,Node node){ + Set<Tupel> toRemove=new HashSet<Tupel>(); + Iterator<Tupel> it=s.iterator(); + while(it.hasNext()){ + Tupel t=(Tupel)it.next(); + //all classes with owl:type class + if(t.a.equals(this.type) && t.b.equals(this.classns)&& node.isClass() ) + {toRemove.add(t);}; + // all with type class + if( t.b.equals(this.classns) && node.isClass() ) + {toRemove.add(t);}; + // all instances with owl:type thing + if(t.a.equals(this.type) && t.b.equals(this.thing)&& node.isInstance() ) + {toRemove.add(t);}; + + } + s.removeAll(toRemove); + + return s; + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlEndpoint.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlEndpoint.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,69 @@ +package org.dllearner.kb.extraction; + +import java.util.HashMap; + +public class SparqlEndpoint { + + String host; + int port; + String hasAfterGET; + String hasQueryParameter; + String hasURL; + public HashMap<String,String> parameters=new HashMap<String,String>(); + public SparqlEndpoint(String host, String port, String hasAfterGET, String hasQueryParameter, HashMap<String, String> parameters) { + super(); + this.host = host; + this.port = Integer.parseInt(port); + this.hasAfterGET = hasAfterGET; + this.hasQueryParameter = hasQueryParameter; + this.parameters = parameters; + } + + public SparqlEndpoint(String host, int port, String hasURL, HashMap<String, String> parameters) { + super(); + this.port=port; + this.host=host; + this.hasURL = hasURL; + this.hasQueryParameter = "query"; + this.parameters = parameters; + } + public String getHasAfterGET() { + return hasAfterGET; + } + public void setHasAfterGET(String hasAfterGET) { + this.hasAfterGET = hasAfterGET; + } + public String getHasQueryParameter() { + return hasQueryParameter; + } + public void setHasQueryParameter(String hasQueryParameter) { + this.hasQueryParameter = hasQueryParameter; + } + public String getHost() { + return host; + } + public void setHost(String host) { + this.host = host; + } + public HashMap<String, String> getParameters() { + return parameters; + } + public void setParameters(HashMap<String, String> parameters) { + this.parameters = parameters; + } + public int getPort() { + return port; + } + public void setPort(int port) { + this.port = port; + } + + + + + /*sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + + //"SELECT%20%2A%20WHERE%20%7B%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FAristotle%3E%20%3Fa%20%3Fb%20%7D%20" + + URLEncoder.encode(query, "UTF-8")+ + //query+// URLencode + "&format=application%2Fsparql-results%2Bxml*/ +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlQueryType.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlQueryType.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/SparqlQueryType.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,68 @@ +package org.dllearner.kb.extraction; + +import java.util.Set; + +public class SparqlQueryType { + + private String mode="forbid"; + private String[] objectfilterlist={ + "http://dbpedia.org/resource/Category:Articles_", + "http://dbpedia.org/resource/Category:Wikipedia_", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/resource/Category", + "http://dbpedia.org/resource/Template", + "http://upload.wikimedia.org/wikipedia/commons"}; + private String[] predicatefilterlist={ + "http://www.w3.org/2004/02/skos/core", + "http://xmlns.com/foaf/0.1/", + "http://dbpedia.org/property/wikipage-", + "http://www.w3.org/2002/07/owl#sameAs", + "http://dbpedia.org/property/reference" }; + private boolean literals=false; + + public SparqlQueryType(String mode, String[] obectfilterlist, String[] predicatefilterlist, boolean literals) { + super(); + this.mode = mode; + this.objectfilterlist = obectfilterlist; + this.predicatefilterlist = predicatefilterlist; + this.literals = literals; + } + + public SparqlQueryType(String mode, Set<String> objectfilterlist, Set<String> predicatefilterlist, String literals) { + super(); + this.mode = mode; + this.literals = (literals.equals("true"))?true:false; + + Object[] arr=objectfilterlist.toArray(); + Object[] arr2=predicatefilterlist.toArray(); + this.objectfilterlist = new String[arr.length]; + this.predicatefilterlist = new String[arr2.length]; + for (int i = 0; i < arr.length; i++) { + this.objectfilterlist[i]=(String)arr[i]; + } + for (int i = 0; i < arr2.length; i++) { + this.predicatefilterlist[i]=(String)arr2[i]; + } + + + } + + public boolean isLiterals() { + return literals; + } + + public String getMode() { + return mode; + } + + public String[] getObjectfilterlist() { + return objectfilterlist; + } + + public String[] getPredicatefilterlist() { + return predicatefilterlist; + } + + + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/Test.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/Test.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/Test.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,31 @@ +package org.dllearner.kb.extraction; + +import java.io.File; +import java.io.FileWriter; +import java.net.URI; + +public class Test { + + + public static void main(String[] args) { + System.out.println("Start"); + String test2="http://www.extraction.org/config#dbpediatest"; + String test="http://www.extraction.org/config#localjoseki"; + try{ + URI u=new URI(test); + Manager m=new Manager(); + m.usePredefinedConfiguration(u); + + + URI u2=new URI("http://dbpedia.org/resource/Angela_Merkel"); + + String filename=System.currentTimeMillis()+".nt"; + FileWriter fw=new FileWriter(new File(filename),true); + fw.write(m.extract(u2)); + fw.flush(); + fw.close(); + + }catch (Exception e) {e.printStackTrace();} + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/ClassNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/ClassNode.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/ClassNode.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,60 @@ +package org.dllearner.kb.extraction.datastructures; + +import java.net.URI; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.Vector; + +import org.dllearner.kb.extraction.Manipulator; +import org.dllearner.kb.extraction.sparql.TypedSparqlQuery; + +public class ClassNode extends Node{ + Set<PropertyNode> properties=new HashSet<PropertyNode>(); + + public ClassNode(URI u) { + super(u); + this.type="class"; + } + + @Override + public Vector<Node> expand(TypedSparqlQuery tsq,Manipulator m){ + Set<Tupel> s =tsq.query(this.URI); + s=m.check(s, this); + Vector<Node> Nodes=new Vector<Node>(); + // Manipulation + + Iterator<Tupel> it=s.iterator(); + while(it.hasNext()){ + Tupel t=(Tupel)it.next(); + try{ + if(t.a.equals(m.type) || t.a.equals(m.subclass)){ + ClassNode tmp=new ClassNode(new URI(t.b)); + properties.add(new PropertyNode(new URI(m.subclass),this,tmp)); + Nodes.add(tmp); + } + }catch (Exception e) {System.out.println(t);e.printStackTrace();} + + } + return Nodes; + } + + @Override + public boolean isClass(){ + return true; + } + + @Override + public Set<String> toNTriple(){ + Set<String> s= new HashSet<String>(); + s.add("<"+this.URI+"><"+"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"+"><"+"http://www.w3.org/2002/07/owl#Class"+">."); + + for (PropertyNode one:properties){ + s.add("<"+this.URI+"><"+one.getURI()+"><"+one.getB().getURI()+">."); + s.addAll(one.getB().toNTriple()); + } + + return s; + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/InstanceNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/InstanceNode.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/InstanceNode.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,77 @@ +package org.dllearner.kb.extraction.datastructures; + +import java.net.URI; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.Vector; + +import org.dllearner.kb.extraction.Manipulator; +import org.dllearner.kb.extraction.sparql.TypedSparqlQuery; + +public class InstanceNode extends Node{ + + Set<ClassNode> classes=new HashSet<ClassNode>(); + Set<Tupel> datatypes=new HashSet<Tupel>(); + Set<PropertyNode> properties=new HashSet<PropertyNode>(); + + public InstanceNode(URI u) { + super(u); + this.type="instance"; + + } + + @Override + public Vector<Node> expand(TypedSparqlQuery tsq,Manipulator m){ + + Set<Tupel> s =tsq.query(this.URI); + // Manipulation + m.check(s,this); + Vector<Node> Nodes=new Vector<Node>(); + + Iterator<Tupel> it=s.iterator(); + while(it.hasNext()){ + Tupel t=(Tupel)it.next(); + + try{ + if(t.a.equals(m.type)){ + ClassNode tmp=new ClassNode(new URI(t.b)); + classes.add(tmp); + Nodes.add(tmp); + }else { + InstanceNode tmp=new InstanceNode(new URI(t.b)); + properties.add(new PropertyNode(new URI(t.a),this,tmp)); + Nodes.add(tmp); + + } + }catch (Exception e) { + System.out.println("Problem with: "+t); + e.printStackTrace();} + + } + this.expanded=true; + return Nodes; + } + @Override + public boolean isInstance(){ + return true; + } + + @Override + public Set<String> toNTriple(){ + Set<String> s= new HashSet<String>(); + s.add("<"+this.URI+"><"+"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"+"><"+"http://www.w3.org/2002/07/owl#Thing"+">."); + for (ClassNode one:classes){ + s.add("<"+this.URI+"><"+"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"+"><"+one.getURI()+">."); + s.addAll(one.toNTriple()); + } + for (PropertyNode one:properties){ + s.add("<"+this.URI+"><"+one.getURI()+"><"+one.getB().getURI()+">."); + s.addAll(one.toNTriple()); + s.addAll(one.getB().toNTriple()); + } + + return s; + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Node.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Node.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Node.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,54 @@ +package org.dllearner.kb.extraction.datastructures; + +import java.net.URI; +import java.util.Set; +import java.util.Vector; + +import org.dllearner.kb.extraction.Manipulator; +import org.dllearner.kb.extraction.sparql.TypedSparqlQuery; + +public abstract class Node { + URI URI; + protected String type; + protected boolean expanded=false; + + //Hashtable<String,Node> classes=new Hashtable<String,Node>(); + //Hashtable<String,Node> instances=new Hashtable<String,Node>();; + //Hashtable<String,Node> datatype=new Hashtable<String,Node>();; + + public Node (URI u){ + this.URI=u; + + } + + /*public void checkConsistency(){ + if (type.equals("class") && ( instances.size()>0 || datatype.size()>0)){ + System.out.println("Warning, inconsistent:"+this.toString()); + } + + }*/ + + public abstract Vector<Node> expand(TypedSparqlQuery tsq,Manipulator m); + public abstract Set<String> toNTriple(); + + @Override + public String toString(){ + return "Node: "+URI+":"+type; + + } + + + public boolean isClass(){ + return false; + } + public boolean isInstance(){ + return false; + } + public boolean isProperty(){ + return false; + } + public URI getURI() { + return URI; + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/PropertyNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/PropertyNode.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/PropertyNode.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,74 @@ +package org.dllearner.kb.extraction.datastructures; + +import java.net.URI; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.Vector; + +import org.dllearner.kb.extraction.Manipulator; +import org.dllearner.kb.extraction.sparql.TypedSparqlQuery; + +public class PropertyNode extends Node{ + + private Node a; + private Node b; + private Set<String> SpecialTypes; + + + public PropertyNode(URI u) { + super(u); + this.type="property"; + + } + + public PropertyNode(URI u,Node a, Node b) { + super(u); + this.type="property"; + this.a=a; + this.b=b; + this.SpecialTypes=new HashSet<String>(); + } + + @Override + public Vector<Node> expand(TypedSparqlQuery tsq,Manipulator m){ + Set<Tupel> s =tsq.query(this.URI); + Vector<Node> Nodes=new Vector<Node>(); + // Manipulation + + Iterator<Tupel> it=s.iterator(); + while(it.hasNext()){ + Tupel t=(Tupel)it.next(); + try{ + if(t.a.equals(m.type) ){ + SpecialTypes.add(t.b); + } + }catch (Exception e) {System.out.println(t);e.printStackTrace();} + + } + return Nodes; + } + @Override + public boolean isProperty(){ + return true; + } + public Node getB(){ + return this.b; + } + + @Override + public Set<String> toNTriple(){ + Set<String> s= new HashSet<String>(); + s.add("<"+this.URI+"><"+"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"+"><"+"http://www.w3.org/2002/07/owl#ObjectProperty"+">."); + for (String one:SpecialTypes){ + s.add("<"+this.URI+"><"+"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"+"><"+one+">."); + + } + + return s; + } + + + + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Tupel.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Tupel.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/datastructures/Tupel.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,25 @@ +package org.dllearner.kb.extraction.datastructures; + +public class Tupel { + + public String a; + public String b; + + public Tupel(String a, String b) { + this.a = a; + this.b = b; + } + + @Override + public String toString(){ + return "<"+a+"|"+b+">"; + } + + public boolean equals(Tupel t){ + if(this.a.equals(t.a)&& this.b.equals(t.b))return true; + else return false; + } + + + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/Cache.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/Cache.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,115 @@ +package org.dllearner.kb.extraction.sparql; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.net.URLEncoder; + +public class Cache implements Serializable{ + // Object can be the cache itself + // or a cache object(one entry) + + final static long serialVersionUID=104; + transient String basedir=""; + transient String fileending=".cache"; + long timestamp; + String content=""; + long daysoffreshness=15; + long multiplier=24*60*60*1000;//h m s ms + String sparqlquery=""; + + //constructor for the cache itself + public Cache(String path){ + this.basedir=path+File.separator; + if(!new File(path).exists()) + {System.out.println(new File(path).mkdir());;} + + } + +// constructor for single cache object(one entry) + public Cache(String c, String sparql){ + this.content=c; + this.sparqlquery=sparql; + this.timestamp=System.currentTimeMillis(); + } + + + public String get(String key, String sparql){ + //System.out.println("get From "+key); + String ret=null; + try{ + Cache c =readFromFile(makeFilename(key)) ; + if(c==null)return null; + //System.out.println(" file found"); + if(!c.checkFreshness())return null; + //System.out.println("fresh"); + if(!c.validate(sparql))return null; + //System.out.println("valid"); + ret=c.content; + }catch (Exception e) {e.printStackTrace();} + return ret; + }; + public void put(String key, String content, String sparql){ + //System.out.println("put into "+key); + Cache c=new Cache(content,sparql); + putIntoFile(makeFilename(key), c); + } + + + String makeFilename(String key){ + String ret=""; + try{ + ret=basedir+URLEncoder.encode(key, "UTF-8")+fileending; + }catch (Exception e) {e.printStackTrace();} + return ret; + } + boolean checkFreshness(){ + if((System.currentTimeMillis()-this.timestamp)<=(daysoffreshness*multiplier)) + //fresh + return true; + else return false; + } + boolean validate(String sparql){ + if(this.sparqlquery.equals(sparql)) + //valid + return true; + else return false; + } + + public void checkFile(String Filename){ + if(!new File(Filename).exists()){ + try{ + new File(Filename).createNewFile(); + }catch (Exception e) {e.printStackTrace();} + + } + + } + + public void putIntoFile(String Filename,Cache content){ + try{ + //FileWriter fw=new FileWriter(new File(Filename),true); + FileOutputStream fos = new FileOutputStream( Filename , false ); + ObjectOutputStream o = new ObjectOutputStream( fos ); + o.writeObject( content ); + fos.flush(); + fos.close(); + }catch (Exception e) {System.out.println("Not in cache creating: "+Filename);} + } + + public Cache readFromFile(String Filename){ + Cache content=null; + try{ + FileInputStream fos = new FileInputStream( Filename ); + ObjectInputStream o = new ObjectInputStream( fos ); + content=(Cache)o.readObject(); + //FileReader fr=new FileReader(new File(Filename,"r")); + //BufferedReader br=new BufferedReader(fr); + }catch (Exception e) {} + return content; + + } +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SimpleHTTPRequest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SimpleHTTPRequest.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SimpleHTTPRequest.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,129 @@ +package org.dllearner.kb.extraction.sparql; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.Socket; +import java.net.URL; +import java.net.URLEncoder; + + + +public class SimpleHTTPRequest { + static final char value[]={13,10}; + static final String cut=new String(value); + private InetAddress ia; + private int port; + + + + public SimpleHTTPRequest(InetAddress ia, int port) { + super(); + this.ia = ia; + this.port = port; + + } + + public String sendAndReceive( String content){ + String retval=""; + // + + byte resp[]=null; + + try{ + Socket SparqlServer=new Socket(this.ia,this.port); + //String request=makeHeader(content); + // send request + (SparqlServer.getOutputStream()).write(content.getBytes()); + + //get Response + resp=readBuffer(new BufferedInputStream(SparqlServer.getInputStream())); + retval=new String(resp); + retval=subtractResponseHeader(retval); + //retval="||"+retval; + + SparqlServer.close(); + + + + } + catch(Exception e){e.printStackTrace();} + //System.out.println("got it"); + return retval; + + }//down + + public static byte[] readBuffer(InputStream IS) + throws IOException{ + byte buffer[] = new byte[0xffff]; + int nbytes=0; + byte resp[]=new byte[0]; + while ((nbytes=IS.read(buffer))!=-1) { + byte tmp[]=new byte[resp.length+nbytes]; + int i=0; + for (;i<resp.length;i++){ + tmp[i]=resp[i]; + } + for(int a=0;a<nbytes;a++,i++){ + tmp[i]=buffer[a]; + } + resp=tmp; + } + return resp; + } + + public String subtractResponseHeader(String in){ + //System.out.println(in.indexOf(cut+""+cut)); + return in.substring(in.indexOf(cut+""+cut)+4); + + + } + + + private String sendAndReceive2(String sparql,URL url) throws IOException{ + StringBuilder answer = new StringBuilder(); + + // String an Sparql-Endpoint schicken + HttpURLConnection connection; + + connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + + connection.addRequestProperty("Host", "dbpedia.openlinksw.com"); + connection.addRequestProperty("Connection","close"); + connection.addRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); + connection.addRequestProperty("Accept-Language","de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"); + connection.addRequestProperty("Accept-Charset","utf-8;q=1.0"); + connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); + + OutputStream os = connection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + osw.write("default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + + URLEncoder.encode(sparql, "UTF-8")+ + "&format=application%2Fsparql-results%2Bxml"); + osw.close(); + + // receive answer + InputStream is = connection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is,"UTF-8"); + BufferedReader br = new BufferedReader(isr); + + String line; + do { + line = br.readLine(); + if(line!=null) + answer.append(line); + } while (line != null); + + br.close(); + + return answer.toString(); + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlHTTPRequest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlHTTPRequest.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlHTTPRequest.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,82 @@ +package org.dllearner.kb.extraction.sparql; + +import java.net.InetAddress; +import java.net.URLEncoder; +import java.util.Iterator; +import java.util.Set; + +import org.dllearner.kb.extraction.SparqlEndpoint; + + + +public class SparqlHTTPRequest { + static final char value[]={13,10}; + static final String cut=new String(value); + + private SparqlEndpoint SparqlEndpoint; + private SimpleHTTPRequest SimpleHTTPRequest; + + + public SparqlHTTPRequest(SparqlEndpoint SparqlEndpoint){ + this.SparqlEndpoint=SparqlEndpoint; + InetAddress ia=null; + try{ + ia=InetAddress.getByName(SparqlEndpoint.getHost()); + }catch (Exception e) {e.printStackTrace();} + this.SimpleHTTPRequest=new SimpleHTTPRequest(ia,SparqlEndpoint.getPort()); + + } + + + public String sendAndReceiveSPARQL( String sparql){ + + //System.out.println(sparql); + String content=makeContent(sparql ); + //System.out.println(content); + String ret= this.SimpleHTTPRequest.sendAndReceive(content); + //System.out.println(ret); + + //this.sendAndReceiveSPARQL("SELECT * WHERE {?a ?b ?c} LIMIT 10"); + + return ret; + + + }//down + + + + + public String makeContent(String query){ + + + String RequestHeader=""; + try{ + + RequestHeader="GET "; + RequestHeader+=SparqlEndpoint.getHasAfterGET()+"?"; + // parameters + Set<String> s =SparqlEndpoint.getParameters().keySet(); + Iterator<String> it=s.iterator(); + while (it.hasNext()) { + String element = (String) it.next(); + RequestHeader+=""+URLEncoder.encode(element, "UTF-8")+"="+ + URLEncoder.encode(SparqlEndpoint.getParameters().get(element), "UTF-8")+"&"; + } + RequestHeader+=""+SparqlEndpoint.getHasQueryParameter()+"="+URLEncoder.encode(query, "UTF-8"); + RequestHeader+=" HTTP/1.1"+cut; + RequestHeader+="Host: "+SparqlEndpoint.getHost()+cut; + + RequestHeader+= + "Connection: close"+cut+ + //"Accept-Encoding: gzip"+cut+ + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"+cut+ + "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"+cut+ + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+cut+ + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"+cut+ + cut; + }catch (Exception e) {e.printStackTrace();} + return RequestHeader; + + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlQueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlQueryMaker.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/SparqlQueryMaker.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,46 @@ +package org.dllearner.kb.extraction.sparql; + +import org.dllearner.kb.extraction.SparqlQueryType; + + + +public class SparqlQueryMaker { + + private SparqlQueryType SparqlQueryType; + + public SparqlQueryMaker(SparqlQueryType SparqlQueryType){ + this.SparqlQueryType=SparqlQueryType; + } + + public String makeQueryUsingFilters(String subject){ + String lineend="\n"; + + String Filter=""; + if(!this.SparqlQueryType.isLiterals())Filter+="!isLiteral(?object))"; + for (String p : this.SparqlQueryType.getPredicatefilterlist()) { + Filter+=lineend + filterPredicate(p); + } + for (String o : this.SparqlQueryType.getObjectfilterlist()) { + Filter+=lineend + filterObject(o); + } + + + String ret= + "SELECT * WHERE { "+lineend + + "<"+ + subject+ + "> ?predicate ?object. "+ lineend+ + "FILTER( "+lineend + + "(" +Filter+").}"; + //System.out.println(ret); + return ret; + } + + + public String filterObject(String ns){ + return "&&( !regex(str(?object), '"+ns+"') )"; + } + public String filterPredicate(String ns){ + return "&&( !regex(str(?predicate), '"+ns+"') )"; + } +} Added: trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/TypedSparqlQuery.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/TypedSparqlQuery.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/extraction/sparql/TypedSparqlQuery.java 2007-12-01 20:50:16 UTC (rev 293) @@ -0,0 +1,83 @@ +package org.dllearner.kb.extraction.sparql; + +import java.net.URI; +import java.util.HashSet; +import java.util.Set; + +import org.dllearner.kb.extraction.Configuration; +import org.dllearner.kb.extraction.datastructures.Tupel; + + +public class TypedSparqlQuery { + private Configuration Configuration; + private SparqlHTTPRequest SparqlHTTPRequest; + private SparqlQueryMaker SparqlQueryMaker; + Cache Cache; + + public TypedSparqlQuery(Configuration Configuration){ + this.Configuration=Configuration; + this.SparqlHTTPRequest=new SparqlHTTPRequest(Configuration.getSparqlEndpoint()); + this.SparqlQueryMaker=new SparqlQueryMaker(Configuration.getSparqlQueryType()); + this.Cache=new Cache("cache"); + } + + public Set<Tupel> query(URI u){ + + //getQuery + String sparql=SparqlQueryMaker.makeQueryUsingFilters(u.toString()); + + // check cache + String FromCache=this.Cache.get(u.toString(), sparql); + FromCache=null; + String xml; + // if not in cache get it from EndPoint + if(FromCache==null){ + xml=this.SparqlHTTPRequest.sendAndReceiveSPARQL(sparql); + //this.Cache.put(u.toString(), xml, sparql); + System.out.print("\n"); + } + else{ + xml=FromCache; + System.out.println("FROM CACHE"); + } + + //System.out.println(xml); + //process XML + Set<Tupel> s=this.processResult(xml); + try { + System.out.println("retrieved "+s.size()+" tupels"); + } catch (Exception e) { } + return s; + } + + public Set<Tupel> processResult(String xml){ + + Set<Tupel> ret=new HashSet<Tupel>(); + //TODO if result is empty, catch exceptions + String one="<binding name=\"predicate\">"; + String two="<binding name=\"object\">"; + String uridel="<uri>"; + String end="</uri>"; + String predtmp=""; + String objtmp=""; + + while(xml.indexOf(one)!=-1){ + //get pred + xml=xml.substring(xml.indexOf(one)+one.length()); + xml=xml.substring(xml.indexOf(uridel)+uridel.length()); + predtmp=xml.substring(0,xml.indexOf(end)); + + //getobj + xml=xml.substring(xml.indexOf(two)+two.length()); + xml=xml.substring(xml.indexOf(uridel)+uridel.length()); + objtmp=xml.substring(0,xml.indexOf(end)); + ret.add(new Tupel(predtmp,objtmp)); + //System.out.println(new Tupel(predtmp,objtmp)); + } + + + return ret; + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2007-12-02 13:30:02
|
Revision: 298 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=298&view=rev Author: kurzum Date: 2007-12-02 05:29:58 -0800 (Sun, 02 Dec 2007) Log Message: ----------- working, filtermode gives a predefined set wchich is stored in kb.sparql.Predefined. Manual configuration might not work yet. Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlHTTPRequest.java trunk/src/dl-learner/org/dllearner/kb/sparql/Test.java trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedConfigurations.java trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java Removed Paths: ------------- trunk/src/dl-learner/org/dllearner/kb/sparql/SimpleHTTPRequest.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-02 13:29:58 UTC (rev 298) @@ -27,6 +27,7 @@ import java.net.URI; import java.net.URL; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import java.util.Set; @@ -42,12 +43,11 @@ import org.dllearner.core.config.StringSetConfigOption; import org.dllearner.core.dl.KB; import org.dllearner.kb.sparql.Manager; -import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.kb.sparql.SparqlQueryType; +import org.dllearner.kb.sparql.SpecificSparqlEndpoint; import org.dllearner.parser.KBParser; import org.dllearner.reasoning.DIGConverter; import org.dllearner.reasoning.JenaOWLDIGConverter; -import org.dllearner.utilities.Datastructures; /** * Represents a SPARQL Endpoint. @@ -59,7 +59,8 @@ public class SparqlEndpointRestructured extends KnowledgeSource { //ConfigOptions - private URL url; + private URL url; + String host; private Set<String> instances; private URL dumpFile; private int numberOfRecursions; @@ -70,6 +71,7 @@ private String format; private boolean dumpToFile; private boolean useLits=false; + private boolean getAllBackground=false; /** * Holds the results of the calculateSubjects method @@ -120,8 +122,9 @@ * @return */ public static Collection<ConfigOption<?>> createConfigOptions() { - Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); + Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); options.add(new StringConfigOption("url", "URL of SPARQL Endpoint")); + options.add(new StringConfigOption("host", "host of SPARQL Endpoint")); options.add(new StringSetConfigOption("instances","relevant instances e.g. positive and negative examples in a learning problem")); options.add(new IntegerConfigOption("numberOfRecursions","number of Recursions, the Sparql-Endpoint is asked")); options.add(new IntegerConfigOption("filterMode","the mode of the SPARQL Filter")); @@ -131,6 +134,7 @@ options.add(new StringConfigOption("format", "N-TRIPLES or KB format")); options.add(new BooleanConfigOption("dumpToFile", "wether Ontology from DBPedia is written to a file or not")); options.add(new BooleanConfigOption("useLits","use Literals in SPARQL query")); + options.add(new BooleanConfigOption("getAllBackground","get")); return options; } @@ -148,6 +152,8 @@ } catch (MalformedURLException e) { throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(),"malformed URL " + s); } + } else if(option.equals("host")) { + host = (String) entry.getValue(); } else if(option.equals("instances")) { instances = (Set<String>) entry.getValue(); } else if(option.equals("numberOfRecursions")){ @@ -166,6 +172,8 @@ dumpToFile=(Boolean)entry.getValue(); } else if(option.equals("useLits")){ useLits=(Boolean)entry.getValue(); + } else if(option.equals("getAllBackground")){ + getAllBackground=(Boolean)entry.getValue(); } } @@ -182,15 +190,16 @@ //Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url,useLits); Manager m=new Manager(); if(filterMode==0){ - try{ - m.usePredefinedConfiguration(new URI("http://www.extraction.org/config#dbpediatest")); - }catch (Exception e) {e.printStackTrace();} + + m.usePredefinedConfiguration(filterMode); + } else{ - //SparqlQueryType sqt=new SparqlQueryType("forbid", objList,predList,useLits+""); - //SparqlEndpoint se=new SparqlEndpoint(); - //m.useConfiguration(SparqlQueryType, SparqlEndpoint) + SparqlQueryType sqt=new SparqlQueryType("forbid", objList,predList,useLits+""); + SpecificSparqlEndpoint se=new SpecificSparqlEndpoint(url, host, new HashMap<String, String>()); + m.useConfiguration(sqt, se,numberOfRecursions,getAllBackground); } + try { String ont=m.extract(instances); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java 2007-12-02 13:29:58 UTC (rev 298) @@ -18,16 +18,26 @@ import org.semanticweb.owl.model.OWLOntologyManager; public class Configuration { - private SparqlEndpoint SparqlEndpoint; + private SpecificSparqlEndpoint SparqlEndpoint; private SparqlQueryType SparqlQueryType; private Manipulator Manipulator; - + private int recursiondepth = 2; + private boolean getAllBackground = true; + private Configuration() { } - public Configuration(SparqlEndpoint SparqlEndpoint, SparqlQueryType SparqlQueryType) { + + public Configuration(SpecificSparqlEndpoint SparqlEndpoint, + SparqlQueryType SparqlQueryType, int recursiondepth, + boolean getAllBackground) { this.SparqlEndpoint = SparqlEndpoint; this.SparqlQueryType = SparqlQueryType; + this.Manipulator=new Manipulator(); + this.recursiondepth = recursiondepth; + this.getAllBackground = getAllBackground; + + } public static Configuration getConfiguration(URI uri) { @@ -123,7 +133,7 @@ } - public static SparqlEndpoint makeEndpoint(OWLIndividual sEndpoint, OWLOntology o) { + public static SpecificSparqlEndpoint makeEndpoint(OWLIndividual sEndpoint, OWLOntology o) { String host = getFirstValueForDataProperty("hasHost", sEndpoint.getDataPropertyValues(o)); String port = getFirstValueForDataProperty("hasPort", sEndpoint.getDataPropertyValues(o)); String hasAfterGET = getFirstValueForDataProperty("hasAfterGET", sEndpoint @@ -135,7 +145,7 @@ // System.out.println("test"); HashMap<String, String> parameters = new HashMap<String, String>(); if (para == null) - return new SparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); + return null;//new SpecificSparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); for (OWLIndividual p : para) { // System.out.println("test2"); String a1 = getFirstValueForDataProperty("hasParameterName", p.getDataPropertyValues(o)); @@ -146,7 +156,7 @@ // System.out.println("test2"); // System.out.println(host+port+ hasAfterGET+ hasQueryParameter+ // parameters); - return new SparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); + return null;//new SpecificSparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); } @@ -196,7 +206,7 @@ return this.Manipulator; } - public SparqlEndpoint getSparqlEndpoint() { + public SpecificSparqlEndpoint getSparqlEndpoint() { return SparqlEndpoint; } @@ -204,4 +214,14 @@ return SparqlQueryType; } + + public boolean isGetAllBackground() { + return getAllBackground; + } + + + public int getRecursiondepth() { + return recursiondepth; + } + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-02 13:29:58 UTC (rev 298) @@ -13,6 +13,8 @@ public ExtractionAlgorithm(Configuration Configuration) { this.Configuration = Configuration; this.Manipulator = Configuration.getManipulator(); + this.recursiondepth=Configuration.getRecursiondepth(); + this.getAllBackground=Configuration.isGetAllBackground(); } @@ -41,6 +43,7 @@ while (v.size() > 0) { Node tmpNode = v.remove(0); System.out.println("Expanding " + tmpNode); + //System.out.println(this.Manipulator); Vector<Node> tmpVec = tmpNode.expand(tsp, this.Manipulator); tmp.addAll(tmpVec); @@ -57,7 +60,9 @@ } while (classes.size() > 0) { System.out.println(classes.size()); - classes.addAll(classes.remove(0).expand(tsp, this.Manipulator)); + Node next=classes.remove(0); + System.out.println(next); + classes.addAll(next.expand(tsp, this.Manipulator)); } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-02 13:29:58 UTC (rev 298) @@ -24,6 +24,7 @@ Set<Tupel> s = tsq.query(this.URI); // Manipulation m.check(s, this); + //System.out.println("fffffff"+m); Vector<Node> Nodes = new Vector<Node>(); Iterator<Tupel> it = s.iterator(); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-02 13:29:58 UTC (rev 298) @@ -10,16 +10,23 @@ private TypedSparqlQuery TypedSparqlQuery; private ExtractionAlgorithm ExtractionAlgorithm; - public void usePredefinedConfiguration(URI uri) { + /*public void usePredefinedConfiguration(URI uri) { this.Configuration = org.dllearner.kb.sparql.Configuration.getConfiguration(uri); this.TypedSparqlQuery = new TypedSparqlQuery(Configuration); this.ExtractionAlgorithm = new ExtractionAlgorithm(Configuration); + }*/ + + public void usePredefinedConfiguration(int i) { + + this.Configuration = PredefinedConfigurations.get(i); + this.TypedSparqlQuery = new TypedSparqlQuery(Configuration); + this.ExtractionAlgorithm = new ExtractionAlgorithm(Configuration); } - public void useConfiguration(SparqlQueryType SparqlQueryType, SparqlEndpoint SparqlEndpoint) { + public void useConfiguration(SparqlQueryType SparqlQueryType, SpecificSparqlEndpoint SparqlEndpoint, int recursiondepth,boolean getAllBackground) { - this.Configuration = new Configuration(SparqlEndpoint, SparqlQueryType); + this.Configuration = new Configuration(SparqlEndpoint, SparqlQueryType,recursiondepth,getAllBackground); this.TypedSparqlQuery = new TypedSparqlQuery(Configuration); this.ExtractionAlgorithm = new ExtractionAlgorithm(Configuration); } Added: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedConfigurations.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedConfigurations.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedConfigurations.java 2007-12-02 13:29:58 UTC (rev 298) @@ -0,0 +1,59 @@ +package org.dllearner.kb.sparql; + +import java.net.URL; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +public class PredefinedConfigurations { + + public static Configuration get(int i){ + + switch (i){ + case 0: return dbpediaYago(); + + } + return null; + } + + public static Configuration dbpediaYago(){ + URL u=null; + HashMap<String, String>m=new HashMap<String, String>(); + m.put("default-graph-uri","http://dbpedia.org"); + m.put("format","application/sparql-results.xml"); + try{ + u=new URL("http://dbpedia.openlinksw.com:8890/sparql"); + }catch (Exception e) {e.printStackTrace();} + SpecificSparqlEndpoint sse=new SpecificSparqlEndpoint( + u,"dbpedia.openlinksw.com",m); + //System.out.println(u); + Set<String>pred=new HashSet<String>(); + pred.add("http://www.w3.org/2004/02/skos/core"); + pred.add("http://www.w3.org/2002/07/owl#sameAs"); + pred.add("http://xmlns.com/foaf/0.1/"); + pred.add("http://dbpedia.org/property/reference"); + pred.add("http://dbpedia.org/property/website"); + pred.add("http://dbpedia.org/property/wikipage"); + + + Set<String>obj=new HashSet<String>(); + obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); + obj.add("http://dbpedia.org/resource/Category:Articles_"); + obj.add("http://xmlns.com/foaf/0.1/"); + obj.add("http://upload.wikimedia.org/wikipedia/commons"); + obj.add("http://upload.wikimedia.org/wikipedia"); + obj.add("http://www.geonames.org"); + obj.add("http://www.w3.org/2006/03/wn/wn20/instances/synset"); + obj.add("http://www4.wiwiss.fu-berlin.de/flickrwrappr"); + obj.add("http://www.w3.org/2004/02/skos/core"); + + SparqlQueryType sqt=new SparqlQueryType("forbid",obj,pred,"false"); + + + + return new Configuration(sse,sqt,2,true); + + } + + +} Deleted: trunk/src/dl-learner/org/dllearner/kb/sparql/SimpleHTTPRequest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SimpleHTTPRequest.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SimpleHTTPRequest.java 2007-12-02 13:29:58 UTC (rev 298) @@ -1,128 +0,0 @@ -package org.dllearner.kb.sparql; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; -import java.net.InetAddress; -import java.net.Socket; -import java.net.URL; -import java.net.URLEncoder; - -public class SimpleHTTPRequest { - static final char value[] = { 13, 10 }; - static final String cut = new String(value); - private InetAddress ia; - private int port; - - public SimpleHTTPRequest(InetAddress ia, int port) { - super(); - this.ia = ia; - this.port = port; - - } - - public String sendAndReceive(String content) { - String retval = ""; - // - - byte resp[] = null; - - try { - Socket SparqlServer = new Socket(this.ia, this.port); - // String request=makeHeader(content); - // send request - (SparqlServer.getOutputStream()).write(content.getBytes()); - - // get Response - resp = readBuffer(new BufferedInputStream(SparqlServer.getInputStream())); - retval = new String(resp); - retval = subtractResponseHeader(retval); - // retval="||"+retval; - - SparqlServer.close(); - - } catch (Exception e) { - e.printStackTrace(); - } - // System.out.println("got it"); - return retval; - - }// down - - public static byte[] readBuffer(InputStream IS) throws IOException { - byte buffer[] = new byte[0xffff]; - int nbytes = 0; - byte resp[] = new byte[0]; - while ((nbytes = IS.read(buffer)) != -1) { - byte tmp[] = new byte[resp.length + nbytes]; - int i = 0; - for (; i < resp.length; i++) { - tmp[i] = resp[i]; - } - for (int a = 0; a < nbytes; a++, i++) { - tmp[i] = buffer[a]; - } - resp = tmp; - } - return resp; - } - - public String subtractResponseHeader(String in) { - // System.out.println(in.indexOf(cut+""+cut)); - return in.substring(in.indexOf(cut + "" + cut) + 4); - - } - - private String sendAndReceive2(String sparql, URL url) throws IOException { - StringBuilder answer = new StringBuilder(); - - // String an Sparql-Endpoint schicken - HttpURLConnection connection; - - connection = (HttpURLConnection) url.openConnection(); - connection.setDoOutput(true); - - connection.addRequestProperty("Host", "dbpedia.openlinksw.com"); - connection.addRequestProperty("Connection", "close"); - connection - .addRequestProperty( - "Accept", - "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); - connection.addRequestProperty("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"); - connection.addRequestProperty("Accept-Charset", "utf-8;q=1.0"); - connection - .addRequestProperty( - "User-Agent", - "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); - - OutputStream os = connection.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - osw - .write("default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" - + URLEncoder.encode(sparql, "UTF-8") - + "&format=application%2Fsparql-results%2Bxml"); - osw.close(); - - // receive answer - InputStream is = connection.getInputStream(); - InputStreamReader isr = new InputStreamReader(is, "UTF-8"); - BufferedReader br = new BufferedReader(isr); - - String line; - do { - line = br.readLine(); - if (line != null) - answer.append(line); - } while (line != null); - - br.close(); - - return answer.toString(); - } - -} Deleted: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java 2007-12-02 13:29:58 UTC (rev 298) @@ -1,79 +0,0 @@ -package org.dllearner.kb.sparql; - -import java.util.HashMap; - -public class SparqlEndpoint { - - String host; - int port; - String hasAfterGET; - String hasQueryParameter; - String hasURL; - public HashMap<String, String> parameters = new HashMap<String, String>(); - - public SparqlEndpoint(String host, String port, String hasAfterGET, String hasQueryParameter, - HashMap<String, String> parameters) { - super(); - this.host = host; - this.port = Integer.parseInt(port); - this.hasAfterGET = hasAfterGET; - this.hasQueryParameter = hasQueryParameter; - this.parameters = parameters; - } - - public SparqlEndpoint(String host, int port, String hasURL, HashMap<String, String> parameters) { - super(); - this.port = port; - this.host = host; - this.hasURL = hasURL; - this.hasQueryParameter = "query"; - this.parameters = parameters; - } - - public String getHasAfterGET() { - return hasAfterGET; - } - - public void setHasAfterGET(String hasAfterGET) { - this.hasAfterGET = hasAfterGET; - } - - public String getHasQueryParameter() { - return hasQueryParameter; - } - - public void setHasQueryParameter(String hasQueryParameter) { - this.hasQueryParameter = hasQueryParameter; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public HashMap<String, String> getParameters() { - return parameters; - } - - public void setParameters(HashMap<String, String> parameters) { - this.parameters = parameters; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - /* - * sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + - * //"SELECT%20%2A%20WHERE%20%7B%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FAristotle%3E%20%3Fa%20%3Fb%20%7D%20" + - * URLEncoder.encode(query, "UTF-8")+ //query+// URLencode - * "&format=application%2Fsparql-results%2Bxml - */ -} Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlHTTPRequest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlHTTPRequest.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlHTTPRequest.java 2007-12-02 13:29:58 UTC (rev 298) @@ -1,81 +1,105 @@ package org.dllearner.kb.sparql; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.URLEncoder; import java.util.Iterator; import java.util.Set; -public class SparqlHTTPRequest { - static final char value[] = { 13, 10 }; - static final String cut = new String(value); - private SparqlEndpoint SparqlEndpoint; - private SimpleHTTPRequest SimpleHTTPRequest; - public SparqlHTTPRequest(SparqlEndpoint SparqlEndpoint) { - this.SparqlEndpoint = SparqlEndpoint; - InetAddress ia = null; - try { - ia = InetAddress.getByName(SparqlEndpoint.getHost()); - } catch (Exception e) { - e.printStackTrace(); - } - this.SimpleHTTPRequest = new SimpleHTTPRequest(ia, SparqlEndpoint.getPort()); - +public class SparqlHTTPRequest { + + + private SpecificSparqlEndpoint SparqlEndpoint; + + + + public SparqlHTTPRequest(SpecificSparqlEndpoint SparqlEndpoint){ + this.SparqlEndpoint=SparqlEndpoint; + + } - - public String sendAndReceiveSPARQL(String sparql) { - - // System.out.println(sparql); - String content = makeContent(sparql); - // System.out.println(content); - String ret = this.SimpleHTTPRequest.sendAndReceive(content); - // System.out.println(ret); - - // this.sendAndReceiveSPARQL("SELECT * WHERE {?a ?b ?c} LIMIT 10"); - + + + public String sendAndReceiveSPARQL( String sparql){ + String ret= ""; + try{ + //System.out.println(sparql); + + //System.out.println(content); + + ret=this.sendAndReceive(sparql); + //System.out.println(ret); + + //this.sendAndReceiveSPARQL("SELECT * WHERE {?a ?b ?c} LIMIT 10"); + }catch (Exception e) {e.printStackTrace();} return ret; - - }// down - - public String makeContent(String query) { - - String RequestHeader = ""; - try { - - RequestHeader = "GET "; - RequestHeader += SparqlEndpoint.getHasAfterGET() + "?"; - // parameters - Set<String> s = SparqlEndpoint.getParameters().keySet(); - Iterator<String> it = s.iterator(); - while (it.hasNext()) { - String element = (String) it.next(); - RequestHeader += "" + URLEncoder.encode(element, "UTF-8") + "=" - + URLEncoder.encode(SparqlEndpoint.getParameters().get(element), "UTF-8") - + "&"; - } - RequestHeader += "" + SparqlEndpoint.getHasQueryParameter() + "=" - + URLEncoder.encode(query, "UTF-8"); - RequestHeader += " HTTP/1.1" + cut; - RequestHeader += "Host: " + SparqlEndpoint.getHost() + cut; - - RequestHeader += "Connection: close" - + cut - + - // "Accept-Encoding: gzip"+cut+ - "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" - + cut - + "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3" - + cut - + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" - + cut - + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24" - + cut + cut; - } catch (Exception e) { - e.printStackTrace(); + + + }//down + + + + + + + private String sendAndReceive(String sparql) throws IOException{ + StringBuilder answer = new StringBuilder(); + + // String an Sparql-Endpoint schicken + HttpURLConnection connection; + + connection = (HttpURLConnection) this.SparqlEndpoint.getURL().openConnection(); + connection.setDoOutput(true); + + connection.addRequestProperty("Host", this.SparqlEndpoint.getHost()); + connection.addRequestProperty("Connection","close"); + connection.addRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); + connection.addRequestProperty("Accept-Language","de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"); + connection.addRequestProperty("Accept-Charset","utf-8;q=1.0"); + connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); + + OutputStream os = connection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + + Set<String> s =SparqlEndpoint.getParameters().keySet(); + Iterator<String> it=s.iterator(); + String FullURI=""; + while (it.hasNext()) { + String element = (String) it.next(); + FullURI+=""+URLEncoder.encode(element, "UTF-8")+"="+ + URLEncoder.encode(SparqlEndpoint.getParameters().get(element), "UTF-8")+"&"; } - return RequestHeader; - + //System.out.println(FullURI); + FullURI+=""+SparqlEndpoint.getHasQueryParameter()+"="+URLEncoder.encode(sparql, "UTF-8"); + + + osw.write(FullURI); + osw.close(); + + // receive answer + InputStream is = connection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is,"UTF-8"); + BufferedReader br = new BufferedReader(isr); + + String line; + do { + line = br.readLine(); + if(line!=null) + answer.append(line); + } while (line != null); + + br.close(); + + return answer.toString(); } + +} -} Copied: trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java (from rev 296, trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java 2007-12-02 13:29:58 UTC (rev 298) @@ -0,0 +1,53 @@ +package org.dllearner.kb.sparql; + +import java.net.URL; +import java.util.HashMap; + +public class SpecificSparqlEndpoint { + + + + String host; + String hasQueryParameter; + URL URL; + public HashMap<String, String> parameters = new HashMap<String, String>(); + + + public SpecificSparqlEndpoint(URL url,String host, HashMap<String, String> parameters) { + super(); + this.host=host; + this.URL = url; + this.hasQueryParameter = "query"; + this.parameters = parameters; + } + + + public String getHasQueryParameter() { + return hasQueryParameter; + } + + public void setHasQueryParameter(String hasQueryParameter) { + this.hasQueryParameter = hasQueryParameter; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public HashMap<String, String> getParameters() { + return parameters; + } + + public void setParameters(HashMap<String, String> parameters) { + this.parameters = parameters; + } + + public URL getURL() { + return this.URL; + } + +} Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Test.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Test.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Test.java 2007-12-02 13:29:58 UTC (rev 298) @@ -13,7 +13,7 @@ try { URI u = new URI(test); Manager m = new Manager(); - m.usePredefinedConfiguration(u); + //m.usePredefinedConfiguration(u); URI u2 = new URI("http://dbpedia.org/resource/Angela_Merkel"); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java 2007-12-02 12:03:41 UTC (rev 297) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java 2007-12-02 13:29:58 UTC (rev 298) @@ -24,12 +24,14 @@ // check cache String FromCache = this.Cache.get(u.toString(), sparql); - FromCache = null; + String xml; // if not in cache get it from EndPoint if (FromCache == null) { xml = this.SparqlHTTPRequest.sendAndReceiveSPARQL(sparql); - // this.Cache.put(u.toString(), xml, sparql); + //System.out.println(sparql); + //System.out.println(xml); + this.Cache.put(u.toString(), xml, sparql); System.out.print("\n"); } else { xml = FromCache; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2007-12-03 14:45:16
|
Revision: 310 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=310&view=rev Author: kurzum Date: 2007-12-03 06:45:11 -0800 (Mon, 03 Dec 2007) Log Message: ----------- changed and Formatted everything Delete old Cache files Examples will follow Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java trunk/src/dl-learner/org/dllearner/kb/sparql/Test.java trunk/src/dl-learner/org/dllearner/kb/sparql/Tupel.java trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-03 14:45:11 UTC (rev 310) @@ -43,6 +43,7 @@ import org.dllearner.core.config.StringSetConfigOption; import org.dllearner.core.dl.KB; import org.dllearner.kb.sparql.Manager; +import org.dllearner.kb.sparql.Manipulator; import org.dllearner.kb.sparql.PredefinedEndpoint; import org.dllearner.kb.sparql.PredefinedFilter; import org.dllearner.kb.sparql.SparqlQueryType; @@ -52,94 +53,108 @@ import org.dllearner.reasoning.JenaOWLDIGConverter; /** - * Represents a SPARQL Endpoint. - * TODO: Is it necessary to create a class DBpediaSparqlEndpoint? + * Represents a SPARQL Endpoint. TODO: Is it necessary to create a class + * DBpediaSparqlEndpoint? * * @author Jens Lehmann * @author Sebastian Knappe */ public class SparqlEndpointRestructured extends KnowledgeSource { - - //ConfigOptions - private URL url; + + // ConfigOptions + private URL url; String host; private Set<String> instances; private URL dumpFile; private int numberOfRecursions; - private int predefinedFilter=0; - private int predefinedEndpoint=0; + private int predefinedFilter = 0; + private int predefinedEndpoint = 0; private Set<String> predList; private Set<String> objList; private Set<String> classList; private String format; private boolean dumpToFile; - private boolean useLits=false; - private boolean getAllBackground=false; - + private boolean useLits = false; + private boolean getAllBackground = false; + + private boolean learnDomain = false; + private String role; + private String blankNodeIdentifier = "bnode"; + /** - * Holds the results of the calculateSubjects method + * Holds the results of the calculateSubjects method */ private String[] subjects; - + /** - * Holds the results of the calculateTriples method + * Holds the results of the calculateTriples method */ private String[] triples; - + /** - * Holds the results of the calculateConceptSubjects method + * Holds the results of the calculateConceptSubjects method */ private String[] conceptSubjects; - + /** * if a method is running this becomes true */ - private boolean subjectThreadRunning=false; - - private boolean triplesThreadRunning=false; - - private boolean conceptThreadRunning=false; - + private boolean subjectThreadRunning = false; + + private boolean triplesThreadRunning = false; + + private boolean conceptThreadRunning = false; + /** * the Thread that is running a method */ private Thread subjectThread; - + private Thread triplesThread; - + private Thread conceptThread; - - //received ontology as array, used if format=Array(an element of the - //array consists of the subject, predicate and object separated by '<' + + // received ontology as array, used if format=Array(an element of the + // array consists of the subject, predicate and object separated by '<' private String[] ontArray; - - //received ontology as KB, the internal format + + // received ontology as KB, the internal format private KB kb; - + public static String getName() { return "SPARQL Endpoint Restructured"; } - + /** * sets the ConfigOptions for this KnowledgeSource + * * @return */ public static Collection<ConfigOption<?>> createConfigOptions() { - Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); + Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); options.add(new StringConfigOption("url", "URL of SPARQL Endpoint")); options.add(new StringConfigOption("host", "host of SPARQL Endpoint")); - options.add(new StringSetConfigOption("instances","relevant instances e.g. positive and negative examples in a learning problem")); - options.add(new IntegerConfigOption("numberOfRecursions","number of Recursions, the Sparql-Endpoint is asked")); - options.add(new IntegerConfigOption("predefinedFilter","the mode of the SPARQL Filter")); - options.add(new IntegerConfigOption("predefinedEndpoint","the mode of the SPARQL Filter")); - - options.add(new StringSetConfigOption("predList","a predicate list")); - options.add(new StringSetConfigOption("objList","an object list")); - options.add(new StringSetConfigOption("classList","a class list")); + options.add(new StringSetConfigOption("instances", + "relevant instances e.g. positive and negative examples in a learning problem")); + options.add(new IntegerConfigOption("numberOfRecursions", + "number of Recursions, the Sparql-Endpoint is asked")); + options.add(new IntegerConfigOption("predefinedFilter", "the mode of the SPARQL Filter")); + options.add(new IntegerConfigOption("predefinedEndpoint", "the mode of the SPARQL Filter")); + + options.add(new StringSetConfigOption("predList", "a predicate list")); + options.add(new StringSetConfigOption("objList", "an object list")); + options.add(new StringSetConfigOption("classList", "a class list")); options.add(new StringConfigOption("format", "N-TRIPLES or KB format")); - options.add(new BooleanConfigOption("dumpToFile", "wether Ontology from DBPedia is written to a file or not")); - options.add(new BooleanConfigOption("useLits","use Literals in SPARQL query")); - options.add(new BooleanConfigOption("getAllBackground","get")); + options.add(new BooleanConfigOption("dumpToFile", + "wether Ontology from DBPedia is written to a file or not")); + options.add(new BooleanConfigOption("useLits", "use Literals in SPARQL query")); + options.add(new BooleanConfigOption("getAllBackground", "get")); + + options.add(new BooleanConfigOption("learnDomain", "learns the Domain for a Role")); + options.add(new StringConfigOption("role", "role to learn Domain from")); + options.add(new StringConfigOption("blankNodeIdentifier", + "used to identify blanknodes in Tripels")); + return options; } @@ -147,7 +162,7 @@ * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) */ @Override - @SuppressWarnings({"unchecked"}) + @SuppressWarnings( { "unchecked" }) public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { String option = entry.getOptionName(); if (option.equals("url")) { @@ -155,103 +170,124 @@ try { url = new URL(s); } catch (MalformedURLException e) { - throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(),"malformed URL " + s); + throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(), + "malformed URL " + s); } - } else if(option.equals("host")) { + } else if (option.equals("host")) { host = (String) entry.getValue(); - } else if(option.equals("instances")) { + } else if (option.equals("instances")) { instances = (Set<String>) entry.getValue(); - } else if(option.equals("numberOfRecursions")){ - numberOfRecursions=(Integer)entry.getValue(); - } else if(option.equals("predList")) { + } else if (option.equals("numberOfRecursions")) { + numberOfRecursions = (Integer) entry.getValue(); + } else if (option.equals("predList")) { predList = (Set<String>) entry.getValue(); - } else if(option.equals("objList")) { + } else if (option.equals("objList")) { objList = (Set<String>) entry.getValue(); - } else if(option.equals("classList")) { + } else if (option.equals("classList")) { classList = (Set<String>) entry.getValue(); - } else if(option.equals("predefinedEndpoint")){ - predefinedEndpoint=(Integer)entry.getValue(); - } else if(option.equals("predefinedFilter")){ - predefinedFilter=(Integer)entry.getValue(); - } else if(option.equals("format")){ - format=(String)entry.getValue(); - } else if(option.equals("dumpToFile")){ - dumpToFile=(Boolean)entry.getValue(); - } else if(option.equals("useLits")){ - useLits=(Boolean)entry.getValue(); - } else if(option.equals("getAllBackground")){ - getAllBackground=(Boolean)entry.getValue(); + } else if (option.equals("predefinedEndpoint")) { + predefinedEndpoint = (Integer) entry.getValue(); + } else if (option.equals("predefinedFilter")) { + predefinedFilter = (Integer) entry.getValue(); + } else if (option.equals("format")) { + format = (String) entry.getValue(); + } else if (option.equals("dumpToFile")) { + dumpToFile = (Boolean) entry.getValue(); + } else if (option.equals("useLits")) { + useLits = (Boolean) entry.getValue(); + } else if (option.equals("getAllBackground")) { + getAllBackground = (Boolean) entry.getValue(); + } else if (option.equals("learnDomain")) { + learnDomain = (Boolean) entry.getValue(); + } else if (option.equals("role")) { + role = (String) entry.getValue(); + } else if (option.equals("blankNodeIdentifier")) { + blankNodeIdentifier = (String) entry.getValue(); } + } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.dllearner.core.Component#init() */ @Override public void init() { System.out.println("SparqlModul: Collecting Ontology"); - //SparqlOntologyCollector oc= - - - //new SparqlOntologyCollector(Datastructures.setToArray(instances), numberOfRecursions, filterMode, - //Datastructures.setToArray(predList),Datastructures.setToArray( objList),Datastructures.setToArray(classList),format,url,useLits); - Manager m=new Manager(); - SpecificSparqlEndpoint sse=null; - SparqlQueryType sqt=null; + // SparqlOntologyCollector oc= + // new SparqlOntologyCollector(Datastructures.setToArray(instances), + // numberOfRecursions, filterMode, + // Datastructures.setToArray(predList),Datastructures.setToArray( + // objList),Datastructures.setToArray(classList),format,url,useLits); + Manager m = new Manager(); + SpecificSparqlEndpoint sse = null; + SparqlQueryType sqt = null; + // get Options for Manipulator + Manipulator man = new Manipulator(blankNodeIdentifier); HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("default-graph-uri", "http://dbpedia.org"); parameters.put("format", "application/sparql-results.xml"); - - if(predefinedEndpoint>=1){ - sse=PredefinedEndpoint.getEndpoint(predefinedEndpoint); - + + // get Options for endpoints + if (predefinedEndpoint >= 1) { + sse = PredefinedEndpoint.getEndpoint(predefinedEndpoint); + } else { + sse = new SpecificSparqlEndpoint(url, host, parameters); } - else{ - SpecificSparqlEndpoint se=new SpecificSparqlEndpoint(url, host, parameters); - + + // get Options for Filters + if (predefinedFilter >= 1) { + sqt = PredefinedFilter.getFilter(predefinedFilter); + + } else { + sqt = new SparqlQueryType("forbid", objList, predList, useLits + ""); } - - if(predefinedFilter>=1){ - sqt=PredefinedFilter.getFilter(predefinedFilter); - + // give everything to the manager + m.useConfiguration(sqt, sse, man, numberOfRecursions, getAllBackground); + try { + String ont = ""; + // used to learn a domain of a role + if (learnDomain) { + instances = m.getDomainInstancesForRole(role); + // ad the role to the filter(a solution is always EXISTS + // role.TOP) + m.addPredicateFilter(role); + System.out.println(instances); } - else{ - sqt=new SparqlQueryType("forbid", objList,predList,useLits+""); - - - - } - m.useConfiguration(sqt, sse,numberOfRecursions,getAllBackground); - try - { - String ont=m.extract(instances); - - if (dumpToFile){ - String filename=System.currentTimeMillis()+".nt"; - String basedir="cache"+File.separator; - try{ - if(!new File(basedir).exists()) + // the actual extraction is started here + ont = m.extract(instances); + + if (dumpToFile) { + String filename = System.currentTimeMillis() + ".nt"; + String basedir = "cache" + File.separator; + try { + if (!new File(basedir).exists()) new File(basedir).mkdir(); - - FileWriter fw=new FileWriter(new File(basedir+filename),true); + + FileWriter fw = new FileWriter(new File(basedir + filename), true); fw.write(ont); fw.flush(); fw.close(); - - dumpFile=(new File(basedir+filename)).toURI().toURL(); - }catch (Exception e) {e.printStackTrace();} + + dumpFile = (new File(basedir + filename)).toURI().toURL(); + } catch (Exception e) { + e.printStackTrace(); + } } if (format.equals("KB")) { - try{ - kb=KBParser.parseKBFile(new StringReader(ont)); - } catch(Exception e) {e.printStackTrace();} + try { + kb = KBParser.parseKBFile(new StringReader(ont)); + } catch (Exception e) { + e.printStackTrace(); + } } - }catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } System.out.println("SparqlModul: ****Finished"); } - + /* * (non-Javadoc) * @@ -259,97 +295,94 @@ */ @Override public String toDIG(URI kbURI) { - if (format.equals("N-TRIPLES")) return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFormat.N_TRIPLES, kbURI); - else return DIGConverter.getDIGString(kb, kbURI).toString(); + if (format.equals("N-TRIPLES")) + return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFormat.N_TRIPLES, kbURI); + else + return DIGConverter.getDIGString(kb, kbURI).toString(); } - /* (non-Javadoc) - * @see org.dllearner.core.KnowledgeSource#export(java.io.File, org.dllearner.core.OntologyFormat) + /* + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#export(java.io.File, + * org.dllearner.core.OntologyFormat) */ @Override public void export(File file, OntologyFormat format) throws OntologyFormatUnsupportedException { - // currently no export functions implemented, so we just throw an exception + // currently no export functions implemented, so we just throw an + // exception throw new OntologyFormatUnsupportedException("export", format); } - + public URL getURL() { return url; } - + public String[] getOntArray() { return ontArray; } - - public void calculateSubjects(String label,int limit) - { + + public void calculateSubjects(String label, int limit) { System.out.println("SparqlModul: Collecting Subjects"); - SparqlOntologyCollector oc=new SparqlOntologyCollector(url); - try{ - subjects=oc.getSubjectsFromLabel(label,limit); - }catch (IOException e){ - subjects=new String[1]; - subjects[0]="[Error]Sparql Endpoint could not be reached."; + SparqlOntologyCollector oc = new SparqlOntologyCollector(url); + try { + subjects = oc.getSubjectsFromLabel(label, limit); + } catch (IOException e) { + subjects = new String[1]; + subjects[0] = "[Error]Sparql Endpoint could not be reached."; } System.out.println("SparqlModul: ****Finished"); } - + public void calculateTriples(String subject) { System.out.println("SparqlModul: Collecting Triples"); - SparqlOntologyCollector oc=new SparqlOntologyCollector(url); - try{ - triples=oc.collectTriples(subject); - }catch (IOException e){ - triples=new String[1]; - triples[0]="[Error]Sparql Endpoint could not be reached."; + SparqlOntologyCollector oc = new SparqlOntologyCollector(url); + try { + triples = oc.collectTriples(subject); + } catch (IOException e) { + triples = new String[1]; + triples[0] = "[Error]Sparql Endpoint could not be reached."; } System.out.println("SparqlModul: ****Finished"); } - - public void calculateConceptSubjects(String concept) - { + + public void calculateConceptSubjects(String concept) { System.out.println("SparqlModul: Collecting Subjects"); - SparqlOntologyCollector oc=new SparqlOntologyCollector(url); - try{ - conceptSubjects=oc.getSubjectsFromConcept(concept); - }catch (IOException e){ - conceptSubjects=new String[1]; - conceptSubjects[0]="[Error]Sparql Endpoint could not be reached."; + SparqlOntologyCollector oc = new SparqlOntologyCollector(url); + try { + conceptSubjects = oc.getSubjectsFromConcept(concept); + } catch (IOException e) { + conceptSubjects = new String[1]; + conceptSubjects[0] = "[Error]Sparql Endpoint could not be reached."; } System.out.println("SparqlModul: ****Finished"); } - - public boolean subjectThreadIsRunning() - { + + public boolean subjectThreadIsRunning() { return subjectThreadRunning; } - - public void setSubjectThreadRunning(boolean bool) - { - subjectThreadRunning=bool; + + public void setSubjectThreadRunning(boolean bool) { + subjectThreadRunning = bool; } - - public boolean triplesThreadIsRunning() - { + + public boolean triplesThreadIsRunning() { return triplesThreadRunning; } - - public void setTriplesThreadRunning(boolean bool) - { - triplesThreadRunning=bool; + + public void setTriplesThreadRunning(boolean bool) { + triplesThreadRunning = bool; } - - public boolean conceptThreadIsRunning() - { + + public boolean conceptThreadIsRunning() { return conceptThreadRunning; } - - public void setConceptThreadRunning(boolean bool) - { - conceptThreadRunning=bool; + + public void setConceptThreadRunning(boolean bool) { + conceptThreadRunning = bool; } - - public String[] getSubjects() - { + + public String[] getSubjects() { return subjects; } @@ -377,13 +410,11 @@ this.conceptThread = conceptThread; } - public String[] getTriples() - { + public String[] getTriples() { return triples; } - - public String[] getConceptSubjects() - { + + public String[] getConceptSubjects() { return conceptSubjects; } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.io.File; @@ -7,19 +26,20 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.URLEncoder; +import java.util.HashMap; public class Cache implements Serializable { // Object can be the cache itself // or a cache object(one entry) + // it now uses a hashmap and can contain different queries at once final static long serialVersionUID = 104; transient String basedir = ""; transient String fileending = ".cache"; long timestamp; - String content = ""; long daysoffreshness = 15; long multiplier = 24 * 60 * 60 * 1000;// h m s ms - String sparqlquery = ""; + private HashMap<String, String> hm; // constructor for the cache itself public Cache(String path) { @@ -28,14 +48,15 @@ System.out.println(new File(path).mkdir()); ; } - } // constructor for single cache object(one entry) - public Cache(String c, String sparql) { - this.content = c; - this.sparqlquery = sparql; + public Cache(String sparql, String content) { + // this.content = c; + // this.sparqlquery = sparql; this.timestamp = System.currentTimeMillis(); + this.hm = new HashMap<String, String>(); + hm.put(sparql, content); } public String get(String key, String sparql) { @@ -49,20 +70,32 @@ if (!c.checkFreshness()) return null; // System.out.println("fresh"); - if (!c.validate(sparql)) + String xml = ""; + try { + xml = c.hm.get(sparql); + } catch (Exception e) { return null; + } + return xml; // System.out.println("valid"); - ret = c.content; + // ret = c.content; } catch (Exception e) { e.printStackTrace(); } return ret; }; - public void put(String key, String content, String sparql) { + public void put(String key, String sparql, String content) { // System.out.println("put into "+key); - Cache c = new Cache(content, sparql); - putIntoFile(makeFilename(key), c); + Cache c = readFromFile(makeFilename(key)); + if (c == null) { + c = new Cache(sparql, content); + putIntoFile(makeFilename(key), c); + } else { + c.hm.put(sparql, content); + putIntoFile(makeFilename(key), c); + } + } String makeFilename(String key) { @@ -83,14 +116,6 @@ return false; } - boolean validate(String sparql) { - if (this.sparqlquery.equals(sparql)) - // valid - return true; - else - return false; - } - public void checkFile(String Filename) { if (!new File(Filename).exists()) { try { Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -6,6 +25,7 @@ import java.util.Set; import java.util.Vector; +// is a node in the graph that is a class public class ClassNode extends Node { Set<PropertyNode> properties = new HashSet<PropertyNode>(); @@ -29,6 +49,17 @@ ClassNode tmp = new ClassNode(new URI(t.b)); properties.add(new PropertyNode(new URI(m.subclass), this, tmp)); Nodes.add(tmp); + } else { + ClassNode tmp = new ClassNode(new URI(t.b)); + properties.add(new PropertyNode(new URI(t.a), this, tmp)); + // System.out.println(m.blankNodeIdentifier); + // System.out.println("XXXXX"+t.b); + if (t.b.startsWith(m.blankNodeIdentifier)) { + tmp.expand(tsq, m); + System.out.println(m.blankNodeIdentifier); + System.out.println("XXXXX" + t.b); + } + // Nodes.add(tmp); } } catch (Exception e) { System.out.println(t); @@ -40,6 +71,12 @@ } @Override + public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + // TODO return type doesn't make sense + return new Vector<Node>(); + } + + @Override public Set<String> toNTriple() { Set<String> s = new HashSet<String>(); s.add("<" + this.uri + "><" + "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" + "><" Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Configuration.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,227 +1,71 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; -import java.io.File; -import java.net.URI; -import java.net.URL; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import org.semanticweb.owl.apibinding.OWLManager; -import org.semanticweb.owl.model.OWLConstant; -import org.semanticweb.owl.model.OWLDataPropertyExpression; -import org.semanticweb.owl.model.OWLIndividual; -import org.semanticweb.owl.model.OWLObjectPropertyExpression; -import org.semanticweb.owl.model.OWLOntology; -import org.semanticweb.owl.model.OWLOntologyManager; - +//stores all configuration settings public class Configuration { - private SpecificSparqlEndpoint SparqlEndpoint; - private SparqlQueryType SparqlQueryType; - private Manipulator Manipulator; + private SpecificSparqlEndpoint specificSparqlEndpoint; + private SparqlQueryType sparqlQueryType; + private Manipulator manipulator; private int recursiondepth = 2; private boolean getAllBackground = true; - + private Configuration() { } - - public Configuration(SpecificSparqlEndpoint SparqlEndpoint, - SparqlQueryType SparqlQueryType, int recursiondepth, + public Configuration(SpecificSparqlEndpoint specificSparqlEndpoint, + SparqlQueryType sparqlQueryType, Manipulator manipulator, int recursiondepth, boolean getAllBackground) { - this.SparqlEndpoint = SparqlEndpoint; - this.SparqlQueryType = SparqlQueryType; - this.Manipulator=new Manipulator(); + this.specificSparqlEndpoint = specificSparqlEndpoint; + this.sparqlQueryType = sparqlQueryType; + this.manipulator = manipulator; this.recursiondepth = recursiondepth; this.getAllBackground = getAllBackground; - } - public static Configuration getConfiguration(URI uri) { - // public static String getTellsString(URL file, URI kbURI){//throws - // OWLOntologyCreationException{ - Configuration ret = new Configuration(); - try { - String file = "config/config.owl"; + public Configuration changeQueryType(SparqlQueryType sqt) { + // TODO must clone here + return new Configuration(this.specificSparqlEndpoint, sqt, this.manipulator, + this.recursiondepth, this.getAllBackground); - File f = new File(file); - String fileURL = "file:///" + f.getAbsolutePath(); - URL u = new URL(fileURL); - /* Load an ontology from a physical URI */ - OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - OWLOntology ontology = manager.loadOntologyFromPhysicalURI(u.toURI()); - // System.out.println( ontology.containsIndividualReference(uri)); - // OWLIndividualImpl ind=new OWLIndividualImpl(); - // System.out.println(ontology.getReferencedIndividuals()); - Set<OWLIndividual> s = ontology.getReferencedIndividuals(); - // System.out.println(ontology.getReferencedClasses()); - // Set<OWLIndividualAxiom> s= ontology.getIndividualAxioms(); - Iterator<OWLIndividual> it = s.iterator(); - while (it.hasNext()) { - OWLIndividual tmp = (OWLIndividual) it.next(); - // tmp.getURI() - if (tmp.getURI().equals(uri)) { - OWLIndividual[] arr = getIndividualsForProperty("hasSparqlEndpoint", tmp - .getObjectPropertyValues(ontology)); - OWLIndividual sEndpoint = arr[0]; - ret.SparqlEndpoint = makeEndpoint(sEndpoint, ontology); - arr = getIndividualsForProperty("hasTypedQuery", tmp - .getObjectPropertyValues(ontology)); - OWLIndividual typedQuery = arr[0]; - ret.SparqlQueryType = makeSparqlQueryType(typedQuery, ontology); - - } - // {hasSparqlEndpoint=[dbpediaEndpoint]} - } - - ret.Manipulator = makeManipulator(); - } catch (Exception e) { - e.printStackTrace(); - } - - return ret; } - public static OWLIndividual[] getIndividualsForProperty(String propertyname, - Map<OWLObjectPropertyExpression, Set<OWLIndividual>> m) { - Set<OWLObjectPropertyExpression> s = m.keySet(); - - Iterator<OWLObjectPropertyExpression> it = s.iterator(); - while (it.hasNext()) { - OWLObjectPropertyExpression tmp = (OWLObjectPropertyExpression) it.next(); - // System.out.println(tmp); - // System.out.println(propertyname); - if (tmp.toString().equals(propertyname)) { - Object[] arr = ((Set<OWLIndividual>) m.get(tmp)).toArray(); - OWLIndividual[] o = new OWLIndividual[arr.length]; - for (int i = 0; i < o.length; i++) { - o[i] = (OWLIndividual) arr[i]; - } - - return o; - } - } - return null; - - } - - public static String getFirstValueForDataProperty(String propertyname, - Map<OWLDataPropertyExpression, Set<OWLConstant>> m) { - return getValuesForDataProperty(propertyname, m)[0]; - } - - public static String[] getValuesForDataProperty(String propertyname, - Map<OWLDataPropertyExpression, Set<OWLConstant>> m) { - Set<OWLDataPropertyExpression> s = m.keySet(); - - Iterator<OWLDataPropertyExpression> it = s.iterator(); - while (it.hasNext()) { - OWLDataPropertyExpression tmp = (OWLDataPropertyExpression) it.next(); - if (tmp.toString().equals(propertyname)) { - Object[] arr = ((Set<OWLConstant>) m.get(tmp)).toArray(); - String[] str = new String[arr.length]; - for (int i = 0; i < str.length; i++) { - str[i] = ((OWLConstant) arr[i]).getLiteral(); - } - return str; - } - } - return null; - - } - - public static SpecificSparqlEndpoint makeEndpoint(OWLIndividual sEndpoint, OWLOntology o) { - String host = getFirstValueForDataProperty("hasHost", sEndpoint.getDataPropertyValues(o)); - String port = getFirstValueForDataProperty("hasPort", sEndpoint.getDataPropertyValues(o)); - String hasAfterGET = getFirstValueForDataProperty("hasAfterGET", sEndpoint - .getDataPropertyValues(o)); - String hasQueryParameter = getFirstValueForDataProperty("hasQueryParameter", sEndpoint - .getDataPropertyValues(o)); - OWLIndividual[] para = getIndividualsForProperty("hasGETParameter", sEndpoint - .getObjectPropertyValues(o)); - // System.out.println("test"); - HashMap<String, String> parameters = new HashMap<String, String>(); - if (para == null) - return null;//new SpecificSparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); - for (OWLIndividual p : para) { - // System.out.println("test2"); - String a1 = getFirstValueForDataProperty("hasParameterName", p.getDataPropertyValues(o)); - String a2 = getFirstValueForDataProperty("hasParameterContent", p - .getDataPropertyValues(o)); - parameters.put(a1, a2); - } - // System.out.println("test2"); - // System.out.println(host+port+ hasAfterGET+ hasQueryParameter+ - // parameters); - return null;//new SpecificSparqlEndpoint(host, port, hasAfterGET, hasQueryParameter, parameters); - - } - - public static SparqlQueryType makeSparqlQueryType(OWLIndividual typedQuery, OWLOntology o) { - String useLiterals = getFirstValueForDataProperty("usesLiterals", typedQuery - .getDataPropertyValues(o)); - String hasMode = getFirstValueForDataProperty("hasMode", typedQuery - .getDataPropertyValues(o)); - // String - // hasAfterGET=getValuesForDataProperty("hasAfterGET",sEndpoint.getDataPropertyValues(o)); - // String - // hasQueryParameter=getValuesForDataProperty("hasQueryParameter",sEndpoint.getDataPropertyValues(o)); - OWLIndividual[] objFilter = getIndividualsForProperty("hasObjectFilterSet", typedQuery - .getObjectPropertyValues(o)); - OWLIndividual[] predFilter = getIndividualsForProperty("hasPredicateFilterSet", typedQuery - .getObjectPropertyValues(o)); - - Set<String> objectFilter = new HashSet<String>(); - Set<String> predicateFilter = new HashSet<String>(); - - for (OWLIndividual of : objFilter) { - String[] tmp = getValuesForDataProperty("filtersURI", of.getDataPropertyValues(o)); - for (String s : tmp) { - objectFilter.add(s); - - } - } - - for (OWLIndividual pf : predFilter) { - String[] tmp = getValuesForDataProperty("filtersURI", pf.getDataPropertyValues(o)); - for (String s : tmp) { - predicateFilter.add(s); - - } - } - // System.out.println(predicateFilter); - // System.out.println(hasMode+objectFilter+predicateFilter+useLiterals); - return new SparqlQueryType(hasMode, objectFilter, predicateFilter, useLiterals); - - } - - public static Manipulator makeManipulator() { - return new Manipulator(); - } - public Manipulator getManipulator() { - return this.Manipulator; + return this.manipulator; } public SpecificSparqlEndpoint getSparqlEndpoint() { - return SparqlEndpoint; + return specificSparqlEndpoint; } public SparqlQueryType getSparqlQueryType() { - return SparqlQueryType; + return sparqlQueryType; } - public boolean isGetAllBackground() { - return getAllBackground; + return getAllBackground; } - public int getRecursiondepth() { - return recursiondepth; + return recursiondepth; } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -3,4 +22,5 @@ import java.util.Vector; +// this class is used to extract the information recursively public class ExtractionAlgorithm { @@ -13,8 +33,8 @@ public ExtractionAlgorithm(Configuration Configuration) { this.configuration = Configuration; this.manipulator = Configuration.getManipulator(); - this.recursionDepth=Configuration.getRecursiondepth(); - this.getAllBackground=Configuration.isGetAllBackground(); + this.recursionDepth = Configuration.getRecursiondepth(); + this.getAllBackground = Configuration.isGetAllBackground(); } @@ -43,13 +63,14 @@ while (v.size() > 0) { Node tmpNode = v.remove(0); System.out.println("Expanding " + tmpNode); - //System.out.println(this.Manipulator); + // System.out.println(this.Manipulator); Vector<Node> tmpVec = tmpNode.expand(tsp, manipulator); tmp.addAll(tmpVec); } v = tmp; - System.out.println("Recursion counter: " + x + " with " + v.size()+" Nodes remaining"); + System.out + .println("Recursion counter: " + x + " with " + v.size() + " Nodes remaining"); } if (this.getAllBackground) { Vector<Node> classes = new Vector<Node>(); @@ -59,9 +80,9 @@ } } while (classes.size() > 0) { - System.out.println("Remaining classes: "+classes.size()); - Node next=classes.remove(0); - System.out.println("Expanding: "+next); + System.out.println("Remaining classes: " + classes.size()); + Node next = classes.remove(0); + System.out.println("Expanding: " + next); classes.addAll(next.expand(tsp, manipulator)); } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -6,6 +25,7 @@ import java.util.Set; import java.util.Vector; +// a node in the graph that is an instance public class InstanceNode extends Node { Set<ClassNode> classes = new HashSet<ClassNode>(); @@ -24,7 +44,7 @@ Set<Tupel> s = tsq.query(uri); // Manipulation m.check(s, this); - //System.out.println("fffffff"+m); + // System.out.println("fffffff"+m); Vector<Node> Nodes = new Vector<Node>(); Iterator<Tupel> it = s.iterator(); @@ -53,6 +73,14 @@ } @Override + public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + for (PropertyNode one : properties) { + one.expandProperties(tsq, m); + } + return new Vector<Node>(); + } + + @Override public Set<String> toNTriple() { Set<String> s = new HashSet<String>(); s.add("<" + uri + "><" + "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" + "><" Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -4,21 +23,53 @@ import java.util.HashSet; import java.util.Set; +// an object of this class encapsulates everything public class Manager { private Configuration configuration; private TypedSparqlQuery typedSparqlQuery; private ExtractionAlgorithm extractionAlgorithm; - + public void useConfiguration(SparqlQueryType SparqlQueryType, + SpecificSparqlEndpoint SparqlEndpoint, Manipulator manipulator, int recursiondepth, + boolean getAllBackground) { - public void useConfiguration(SparqlQueryType SparqlQueryType, SpecificSparqlEndpoint SparqlEndpoint, int recursiondepth,boolean getAllBackground) { - - this.configuration = new Configuration(SparqlEndpoint, SparqlQueryType,recursiondepth,getAllBackground); + this.configuration = new Configuration(SparqlEndpoint, SparqlQueryType, manipulator, + recursiondepth, getAllBackground); this.typedSparqlQuery = new TypedSparqlQuery(configuration); this.extractionAlgorithm = new ExtractionAlgorithm(configuration); } + public Set<String> getDomainInstancesForRole(String role) { + URI u = null; + try { + u = new URI(role); + } catch (Exception e) { + e.printStackTrace(); + } + Set<Tupel> t = this.typedSparqlQuery.getTupelsForRole(u); + Set<String> ret = new HashSet<String>(); + for (Tupel one : t) { + ret.add(one.a); + } + return ret; + } + + public Set<String> getRangeInstancesForRole(String role) { + URI u = null; + try { + u = new URI(role); + } catch (Exception e) { + e.printStackTrace(); + } + Set<Tupel> t = this.typedSparqlQuery.getTupelsForRole(u); + Set<String> ret = new HashSet<String>(); + for (Tupel one : t) { + ret.add(one.b); + } + return ret; + } + public String extract(URI uri) { // this.TypedSparqlQuery.query(uri); // System.out.println(ExtractionAlgorithm.getFirstNode(uri)); @@ -54,4 +105,9 @@ return nt; } + public void addPredicateFilter(String str) { + this.configuration.getSparqlQueryType().addPredicateFilter(str); + + } + } \ No newline at end of file Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.util.HashSet; @@ -4,9 +23,11 @@ import java.util.Iterator; import java.util.Set; +// used to manipulate retrieved tupels, identify blanknodes, etc. public class Manipulator { public String subclass = "http://www.w3.org/2000/01/rdf-schema#subClassOf"; public String type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; + public String blankNodeIdentifier = "bnode"; String objectProperty = "http://www.w3.org/2002/07/owl#ObjectProperty"; String classns = "http://www.w3.org/2002/07/owl#Class"; @@ -16,13 +37,10 @@ String[] defaultClasses = { "http://dbpedia.org/class/yago", "http://dbpedia.org/resource/Category:", "http://dbpedia.org/resource/Template:", - "http://www.w3.org/2004/02/skos/core", "http://dbpedia.org/class/" }; // TODO - // FEHLER - // hier - // fehlt - // yago + "http://www.w3.org/2004/02/skos/core", "http://dbpedia.org/class/" }; - public Manipulator() { + public Manipulator(String blankNodeIdentifier) { + this.blankNodeIdentifier = blankNodeIdentifier; Set<String> classproperties = new HashSet<String>(); classproperties.add(subclass); @@ -33,21 +51,22 @@ Iterator<Tupel> it = s.iterator(); while (it.hasNext()) { Tupel t = (Tupel) it.next(); + // System.out.println(t); // all classes with owl:type class if (t.a.equals(type) && t.b.equals(classns) && node instanceof ClassNode) { toRemove.add(t); } - + // all with type class if (t.b.equals(classns) && node instanceof ClassNode) { toRemove.add(t); } - + // all instances with owl:type thing if (t.a.equals(type) && t.b.equals(thing) && node instanceof InstanceNode) { toRemove.add(t); } - + } s.removeAll(toRemove); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -4,29 +23,20 @@ import java.util.Set; import java.util.Vector; +// abstract class public abstract class Node { URI uri; protected String type; protected boolean expanded = false; - // Hashtable<String,Node> classes=new Hashtable<String,Node>(); - // Hashtable<String,Node> instances=new Hashtable<String,Node>();; - // Hashtable<String,Node> datatype=new Hashtable<String,Node>();; - public Node(URI u) { this.uri = u; - } - /* - * public void checkConsistency(){ if (type.equals("class") && ( - * instances.size()>0 || datatype.size()>0)){ System.out.println("Warning, - * inconsistent:"+this.toString()); } - * } - */ - public abstract Vector<Node> expand(TypedSparqlQuery tsq, Manipulator m); + public abstract Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m); + public abstract Set<String> toNTriple(); @Override Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URL; @@ -3,26 +22,41 @@ import java.util.HashMap; +// holds some predefined endpoints public class PredefinedEndpoint { - public static SpecificSparqlEndpoint getEndpoint(int i) { + public static SpecificSparqlEndpoint getEndpoint(int i) { - switch (i) { - case 1: - return dbpediaEndpoint(); + switch (i) { + case 1: + return dbpediaEndpoint(); + case 2: + return localJoseki(); + } + return null; + } + public static SpecificSparqlEndpoint dbpediaEndpoint() { + URL u = null; + HashMap<String, String> m = new HashMap<String, String>(); + m.put("default-graph-uri", "http://dbpedia.org"); + m.put("format", "application/sparql-results.xml"); + try { + u = new URL("http://dbpedia.openlinksw.com:8890/sparql"); + } catch (Exception e) { + e.printStackTrace(); + } + return new SpecificSparqlEndpoint(u, "dbpedia.openlinksw.com", m); } - return null; - } - - public static SpecificSparqlEndpoint dbpediaEndpoint(){ - URL u = null; - HashMap<String, String> m = new HashMap<String, String>(); - m.put("default-graph-uri", "http://dbpedia.org"); - m.put("format", "application/sparql-results.xml"); - try { - u = new URL("http://dbpedia.openlinksw.com:8890/sparql"); - } catch (Exception e) { - e.printStackTrace(); + + public static SpecificSparqlEndpoint localJoseki() { + URL u = null; + HashMap<String, String> m = new HashMap<String, String>(); + // m.put("default-graph-uri", "http://dbpedia.org"); + // m.put("format", "application/sparql-results.xml"); + try { + u = new URL("http://localhost:2020/books"); + } catch (Exception e) { + e.printStackTrace(); + } + return new SpecificSparqlEndpoint(u, "localost", m); } - return new SpecificSparqlEndpoint(u, "dbpedia.openlinksw.com", m); } -} Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.util.HashSet; @@ -4,14 +23,15 @@ import java.util.Set; public class PredefinedFilter { - + public static SparqlQueryType getFilter(int i) { switch (i) { case 1: return YagoFilter(); - + case 2: + return CategoriesOnly(); } return null; } @@ -22,6 +42,7 @@ pred.add("http://www.w3.org/2004/02/skos/core"); pred.add("http://www.w3.org/2002/07/owl#sameAs"); pred.add("http://xmlns.com/foaf/0.1/"); + pred.add("http://dbpedia.org/property/reference"); pred.add("http://dbpedia.org/property/website"); pred.add("http://dbpedia.org/property/wikipage"); @@ -39,4 +60,36 @@ return new SparqlQueryType("forbid", obj, pred, "false"); } + + public static SparqlQueryType CategoriesOnly(){ + Set<String> pred = new HashSet<String>(); + pred.add("http://www.w3.org/2004/02/skos/core"); + pred.add("http://www.w3.org/2002/07/owl#sameAs"); + pred.add("http://xmlns.com/foaf/0.1/"); + + pred.add("http://dbpedia.org/property/reference"); + pred.add("http://dbpedia.org/property/website"); + pred.add("http://dbpedia.org/property/wikipage"); + + Set<String> obj = new HashSet<String>(); + obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); + obj.add("http://dbpedia.org/resource/Category:Articles_"); + obj.add("http://xmlns.com/foaf/0.1/"); + obj.add("http://upload.wikimedia.org/wikipedia/commons"); + obj.add("http://upload.wikimedia.org/wikipedia"); + + obj.add("http://www.geonames.org"); + obj.add("http://www.w3.org/2006/03/wn/wn20/instances/synset"); + obj.add("http://www4.wiwiss.fu-berlin.de/flickrwrappr"); + obj.add("http://www.w3.org/2004/02/skos/core"); + + obj.add("http://dbpedia.org/class/yago"); + obj.add("http://dbpedia.org/resource/Template"); + + + return new SparqlQueryType("forbid", obj, pred, "false"); + } + + + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.net.URI; @@ -47,6 +66,12 @@ } return Nodes; } + @Override + public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + b.expandProperties(tsq, m); + return this.expand(tsq, m); + } + public Node getB() { return b; Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,5 +1,25 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; +// can assemble sparql queries public class SparqlQueryMaker { private SparqlQueryType sparqlQueryType; @@ -8,9 +28,27 @@ this.sparqlQueryType = SparqlQueryType; } - public String makeQueryUsingFilters(String subject) { + public String makeSubjectQueryUsingFilters(String subject) { String lineend = "\n"; + String Filter = internalFilterAssemblySubject(); + String ret = "SELECT * WHERE { " + lineend + "<" + subject + "> ?predicate ?object. " + + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; + // System.out.println(ret); + return ret; + } + public String makeRoleQueryUsingFilters(String role) { + String lineend = "\n"; + String Filter = internalFilterAssemblyRole(); + String ret = "SELECT * WHERE { " + lineend + "?subject <" + role + "> ?object. " + lineend + + "FILTER( " + lineend + "(" + Filter + ").}"; + // System.out.println(ret); + + return ret; + } + + private String internalFilterAssemblySubject() { + String lineend = "\n"; String Filter = ""; if (!this.sparqlQueryType.isLiterals()) Filter += "!isLiteral(?object))"; @@ -20,18 +58,32 @@ for (String o : sparqlQueryType.getObjectfilterlist()) { Filter += lineend + filterObject(o); } + return Filter; + } - String ret = "SELECT * WHERE { " + lineend + "<" + subject + "> ?predicate ?object. " - + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; - // System.out.println(ret); - return ret; + private String internalFilterAssemblyRole() { + String lineend = "\n"; + String Filter = ""; + if (!this.sparqlQueryType.isLiterals()) + Filter += "!isLiteral(?object))"; + for (String s : sparqlQueryType.getObjectfilterlist()) { + Filter += lineend + filterSubject(s); + } + for (String o : sparqlQueryType.getObjectfilterlist()) { + Filter += lineend + filterObject(o); + } + return Filter; } - public String filterObject(String ns) { - return "&&( !regex(str(?object), '" + ns + "') )"; + public String filterSubject(String ns) { + return "&&( !regex(str(?subject), '" + ns + "') )"; } public String filterPredicate(String ns) { return "&&( !regex(str(?predicate), '" + ns + "') )"; } + + public String filterObject(String ns) { + return "&&( !regex(str(?object), '" + ns + "') )"; + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java 2007-12-02 20:52:38 UTC (rev 309) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java 2007-12-03 14:45:11 UTC (rev 310) @@ -1,3 +1,22 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ package org.dllearner.kb.sparql; import java.util.Set; @@ -2,4 +21,5 @@ +// is used to set the filter: configuration public class SparqlQueryType { - + // TODO make sets out of them private String mode = "forbid"; @@ -58,4 +78,15 @@ return predicatefilterlist; } + public void addPredicateFilter(String filter) { + String[] tmp = new String[predicatefilterlist.length + 1]; + int i = 0; + for (; i < predicatefilterlist.length; i++) { + tmp[i] = predicatefilterlist[i]; + + } + tmp[i] = filter; + + } + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SpecificSparqlEndpoint.java... [truncated message content] |
From: <ku...@us...> - 2007-12-05 12:44:59
|
Revision: 325 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=325&view=rev Author: kurzum Date: 2007-12-05 04:44:53 -0800 (Wed, 05 Dec 2007) Log Message: ----------- added option breakSuperClassRetrievalAfter needed for SKOS Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-05 12:24:13 UTC (rev 324) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-05 12:44:53 UTC (rev 325) @@ -80,6 +80,7 @@ private boolean dumpToFile = true; private boolean useLits = false; private boolean getAllSuperClasses = true; + private int breakSuperClassRetrievalAfter = 500; private boolean learnDomain = false; private String role; @@ -166,8 +167,9 @@ options.add(new StringTupleListConfigOption("example", "example")); options.add(new StringTupleListConfigOption("replacePredicate", "rule for replacing predicates")); options.add(new StringTupleListConfigOption("replaceObject", "rule for replacing predicates")); + options.add(new IntegerConfigOption("breakSuperClassRetrievalAfter", "stops a cyclic hierarchy after specified number of classes")); + - return options; } @@ -222,9 +224,10 @@ replacePredicate = (LinkedList)entry.getValue(); }else if (option.equals("replaceObject")) { replaceObject = (LinkedList)entry.getValue(); - + }else if (option.equals("breakSuperClassRetrievalAfter")) { + breakSuperClassRetrievalAfter = (Integer) entry.getValue(); } - + } /* @@ -244,7 +247,7 @@ SpecificSparqlEndpoint sse = null; SparqlQueryType sqt = null; // get Options for Manipulator - Manipulator man = new Manipulator(blankNodeIdentifier,replacePredicate,replaceObject); + Manipulator man = new Manipulator(blankNodeIdentifier,breakSuperClassRetrievalAfter,replacePredicate,replaceObject); HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("default-graph-uri", "http://dbpedia.org"); parameters.put("format", "application/sparql-results.xml"); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-05 12:24:13 UTC (rev 324) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-05 12:44:53 UTC (rev 325) @@ -94,7 +94,7 @@ Node next = classes.remove(0); System.out.println("Expanding: " + next); classes.addAll(next.expand(tsp, manipulator)); - if (classes.size()>=500){break;} + if (classes.size()>=manipulator.breakSuperClassRetrievalAfter){break;} } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-05 12:24:13 UTC (rev 324) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-05 12:44:53 UTC (rev 325) @@ -31,6 +31,7 @@ public String subclass = "http://www.w3.org/2000/01/rdf-schema#subClassOf"; public String type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; public String blankNodeIdentifier = "bnode"; + public int breakSuperClassRetrievalAfter=200; public LinkedList<StringTuple> replacePredicate; public LinkedList<StringTuple> replaceObject; @@ -44,7 +45,7 @@ "http://dbpedia.org/resource/Category:", "http://dbpedia.org/resource/Template:", "http://www.w3.org/2004/02/skos/core", "http://dbpedia.org/class/" }; - public Manipulator(String blankNodeIdentifier,LinkedList<StringTuple> replacePredicate,LinkedList<StringTuple> replaceObject) { + public Manipulator(String blankNodeIdentifier,int breakSuperClassRetrievalAfter,LinkedList<StringTuple> replacePredicate,LinkedList<StringTuple> replaceObject) { this.blankNodeIdentifier = blankNodeIdentifier; this.replaceObject=replaceObject; this.replacePredicate=replacePredicate; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2007-12-08 18:46:58
|
Revision: 331 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=331&view=rev Author: kurzum Date: 2007-12-08 10:46:56 -0800 (Sat, 08 Dec 2007) Log Message: ----------- some code changes mainly needed for role learning Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-05 16:18:15 UTC (rev 330) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-08 18:46:56 UTC (rev 331) @@ -84,7 +84,8 @@ private boolean learnDomain = false; private boolean learnRange = false; - private String role; + private int numberOfInstancesUsedForRoleLearning=40; + private String role=""; private String blankNodeIdentifier = "bnode"; LinkedList<StringTuple> URIParameters = new LinkedList<StringTuple>(); @@ -170,8 +171,10 @@ options.add(new StringTupleListConfigOption("replacePredicate", "rule for replacing predicates")); options.add(new StringTupleListConfigOption("replaceObject", "rule for replacing predicates")); options.add(new IntegerConfigOption("breakSuperClassRetrievalAfter", "stops a cyclic hierarchy after specified number of classes")); + options.add(new IntegerConfigOption("numberOfInstancesUsedForRoleLearning", "")); + return options; } @@ -230,6 +233,8 @@ replaceObject = (LinkedList)entry.getValue(); }else if (option.equals("breakSuperClassRetrievalAfter")) { breakSuperClassRetrievalAfter = (Integer) entry.getValue(); + }else if (option.equals("numberOfInstancesUsedForRoleLearning")) { + numberOfInstancesUsedForRoleLearning = (Integer) entry.getValue(); } } @@ -279,12 +284,12 @@ // used to learn a domain of a role if (learnDomain || learnRange) { Set<String> pos=new HashSet<String>(); - Set<String> neg=new HashSet<String>(); + //Set<String> neg=new HashSet<String>(); if(learnDomain){ pos = m.getDomainInstancesForRole(role); - neg = m.getRangeInstancesForRole(role); + //neg = m.getRangeInstancesForRole(role); }else if(learnRange){ - neg = m.getDomainInstancesForRole(role); + //neg = m.getDomainInstancesForRole(role); pos = m.getRangeInstancesForRole(role); } //choose 30 @@ -293,29 +298,29 @@ Set<String> tmp=new HashSet<String>(); for(String one:pos){ tmp.add(one); - if(tmp.size()>=5)break; + if(tmp.size()>=numberOfInstancesUsedForRoleLearning)break; } pos=tmp; - System.out.println(pos.size()); + System.out.println("Instances used: "+pos.size()); - tmp=new HashSet<String>(); + /*tmp=new HashSet<String>(); for(String one:neg){ tmp.add(one); if(tmp.size()>=5)break; } - neg=tmp; + neg=tmp;*/ instances=new HashSet<String>(); - instances.addAll( pos); + instances.addAll(pos); - instances.addAll(neg); + //instances.addAll(neg); for(String one:pos){ System.out.println("+\""+one+"\""); } - for(String one:neg){ + /*for(String one:neg){ System.out.println("-\""+one+"\""); - } + }*/ /*Random r= new Random(); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-05 16:18:15 UTC (rev 330) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-08 18:46:56 UTC (rev 331) @@ -52,7 +52,7 @@ } catch (Exception e) { e.printStackTrace(); } - Set<StringTuple> t = this.typedSparqlQuery.getTupelsForRole(u); + Set<StringTuple> t = this.typedSparqlQuery.getTupelsForRole(u, true); Set<String> ret = new HashSet<String>(); for (StringTuple one : t) { @@ -68,7 +68,7 @@ } catch (Exception e) { e.printStackTrace(); } - Set<StringTuple> t = this.typedSparqlQuery.getTupelsForRole(u); + Set<StringTuple> t = this.typedSparqlQuery.getTupelsForRole(u,false); Set<String> ret = new HashSet<String>(); for (StringTuple one : t) { Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java 2007-12-05 16:18:15 UTC (rev 330) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedEndpoint.java 2007-12-08 18:46:56 UTC (rev 331) @@ -32,13 +32,13 @@ case 2: return localJoseki(); case 3: - return worldFactBook(); - case 4: return govTrack(); + case 4: + return revyu(); case 5: - return revyu(); - case 6: return myopenlink(); + case 6: + return worldFactBook(); } return null; } @@ -66,7 +66,7 @@ } catch (Exception e) { e.printStackTrace(); } - return new SpecificSparqlEndpoint(u, "localost", m); + return new SpecificSparqlEndpoint(u, "localhost", m); } public static SpecificSparqlEndpoint worldFactBook() { URL u = null; Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-05 16:18:15 UTC (rev 330) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-08 18:46:56 UTC (rev 331) @@ -21,7 +21,8 @@ // can assemble sparql queries public class SparqlQueryMaker { - + String lineend="\n"; + boolean print_flag=false; /* can make queries for subject, predicate, object * according to the filter settings * object not yet implemented @@ -35,26 +36,47 @@ } public String makeSubjectQueryUsingFilters(String subject) { - String lineend = "\n"; + String Filter = internalFilterAssemblySubject(); String ret = "SELECT * WHERE { " + lineend + "<" + subject + "> ?predicate ?object. " + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; // System.out.println(ret); + //System.out.println(sparqlQueryType.getPredicatefilterlist().length); return ret; } public String makeRoleQueryUsingFilters(String role) { - String lineend = "\n"; + String Filter = internalFilterAssemblyRole(); - String ret = "SELECT * WHERE { " + lineend + "?subject <" + role + "> ?object. " + lineend + String ret = "SELECT * WHERE { " + lineend + " ?subject <" + role + "> ?object. " + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; // System.out.println(ret); return ret; } + public String makeRoleQueryUsingFilters(String role,boolean domain) { + + String Filter = internalFilterAssemblyRole(); + String ret=""; + if(domain){ + ret = "SELECT * WHERE { " + lineend + + "?subject <" + role + "> ?object; a []. " + lineend + + "FILTER( " + lineend + "(" + Filter + ").}"; + // System.out.println(ret); + }else{ + ret = "SELECT * WHERE { " + lineend + + "?object a [] . " + + "?subject <" + role + "> ?object . " + lineend + + "FILTER( " + lineend + "(" + Filter + ").}"; + + } + //System.out.println(ret); + return ret; + } + private String internalFilterAssemblySubject() { - String lineend = "\n"; + String Filter = ""; if (!this.sparqlQueryType.isLiterals()) Filter += "!isLiteral(?object))"; @@ -68,7 +90,7 @@ } private String internalFilterAssemblyRole() { - String lineend = "\n"; + String Filter = ""; if (!this.sparqlQueryType.isLiterals()) Filter += "!isLiteral(?object))"; @@ -92,4 +114,10 @@ public String filterObject(String ns) { return "&&( !regex(str(?object), '" + ns + "') )"; } + + public void p(String str){ + if(print_flag){ + System.out.println(str); + } + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java 2007-12-05 16:18:15 UTC (rev 330) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryType.java 2007-12-08 18:46:56 UTC (rev 331) @@ -85,9 +85,11 @@ int i = 0; for (; i < predicatefilterlist.length; i++) { tmp[i] = predicatefilterlist[i]; - + //System.out.println(tmp[i]); } tmp[i] = filter; + predicatefilterlist=tmp; + //System.out.println("added filter: "+filter); } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java 2007-12-05 16:18:15 UTC (rev 330) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java 2007-12-08 18:46:56 UTC (rev 331) @@ -36,6 +36,8 @@ // can execute different queries public class TypedSparqlQuery { + boolean print_flag=false; + boolean debug_no_cache=false; private Configuration configuration; // private SparqlHTTPRequest SparqlHTTPRequest; private SparqlQueryMaker sparqlQueryMaker; @@ -68,13 +70,25 @@ return s; } + public Set<StringTuple> getTupelsForRole(URI u,boolean domain) { + // getQuery + String sparql = sparqlQueryMaker.makeRoleQueryUsingFilters(u.toString(),domain); + + Set<StringTuple> s = cachedSparql(u, sparql, "subject", "object"); + // System.out.println(s); + return s; + + } + // uses a cache private Set<StringTuple> cachedSparql(URI u, String sparql, String a, String b) { // check cache String FromCache = cache.get(u.toString(), sparql); - + if(debug_no_cache) { + FromCache=null; + } String xml = null; // if not in cache get it from EndPoint if (FromCache == null) { @@ -84,9 +98,11 @@ // TODO Auto-generated catch block e.printStackTrace(); } - // System.out.println(sparql); + p(sparql); // System.out.println(xml); - cache.put(u.toString(), sparql, xml); + if(!debug_no_cache) { + cache.put(u.toString(), sparql, xml); + } //System.out.print("\n"); } else { xml = FromCache; @@ -134,16 +150,20 @@ } private String sendAndReceiveSPARQL(String sparql) throws IOException { + p("sendAndReceiveSPARQL"); StringBuilder answer = new StringBuilder(); + //sparql="SELECT * WHERE {?a ?b ?c}LIMIT 10"; // String an Sparql-Endpoint schicken HttpURLConnection connection; SpecificSparqlEndpoint se = configuration.getSparqlEndpoint(); - + p("URL: "+se.getURL()); + p("Host: "+se.getHost()); + connection = (HttpURLConnection) se.getURL().openConnection(); connection.setDoOutput(true); - connection.addRequestProperty("Host", se.getHost()); + //connection.addRequestProperty("Host", se.getHost()); connection.addRequestProperty("Connection", "close"); connection .addRequestProperty( @@ -155,7 +175,7 @@ .addRequestProperty( "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); - + OutputStream os = connection.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); @@ -167,9 +187,9 @@ FullURI += "" + URLEncoder.encode(element, "UTF-8") + "=" + URLEncoder.encode(se.getParameters().get(element), "UTF-8") + "&"; } - // System.out.println(FullURI); + FullURI += "" + se.getHasQueryParameter() + "=" + URLEncoder.encode(sparql, "UTF-8"); - + p(FullURI); osw.write(FullURI); osw.close(); @@ -186,8 +206,13 @@ } while (line != null); br.close(); - + p(answer.toString()); return answer.toString(); } + public void p(String str){ + if(print_flag){ + System.out.println(str); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2007-12-14 20:16:54
|
Revision: 335 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=335&view=rev Author: kurzum Date: 2007-12-14 12:16:47 -0800 (Fri, 14 Dec 2007) Log Message: ----------- a lot of changes Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryClasses.java trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryInterface.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-14 20:16:47 UTC (rev 335) @@ -80,7 +80,7 @@ private boolean dumpToFile = true; private boolean useLits = false; private boolean getAllSuperClasses = true; - private int breakSuperClassRetrievalAfter = 500; + private int breakSuperClassRetrievalAfter = 200; private boolean learnDomain = false; private boolean learnRange = false; @@ -281,15 +281,16 @@ m.useConfiguration(sqt, sse, man, recursionDepth, getAllSuperClasses); try { String ont = ""; + System.out.println(learnDomain); // used to learn a domain of a role if (learnDomain || learnRange) { Set<String> pos=new HashSet<String>(); - //Set<String> neg=new HashSet<String>(); + Set<String> neg=new HashSet<String>(); if(learnDomain){ pos = m.getDomainInstancesForRole(role); - //neg = m.getRangeInstancesForRole(role); + neg = m.getRangeInstancesForRole(role); }else if(learnRange){ - //neg = m.getDomainInstancesForRole(role); + neg = m.getDomainInstancesForRole(role); pos = m.getRangeInstancesForRole(role); } //choose 30 @@ -303,24 +304,24 @@ pos=tmp; System.out.println("Instances used: "+pos.size()); - /*tmp=new HashSet<String>(); + tmp=new HashSet<String>(); for(String one:neg){ tmp.add(one); - if(tmp.size()>=5)break; + if(tmp.size()>=numberOfInstancesUsedForRoleLearning)break; } - neg=tmp;*/ + neg=tmp; instances=new HashSet<String>(); instances.addAll(pos); - //instances.addAll(neg); + instances.addAll(neg); for(String one:pos){ System.out.println("+\""+one+"\""); } - /*for(String one:neg){ + for(String one:neg){ System.out.println("-\""+one+"\""); - }*/ + } /*Random r= new Random(); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2007-12-14 20:16:47 UTC (rev 335) @@ -32,6 +32,7 @@ // Object can be the cache itself // or a cache object(one entry) // it now uses a hashmap and can contain different queries at once + private HashMap<String, String> hm; final static long serialVersionUID = 104; transient String basedir = ""; @@ -39,7 +40,7 @@ long timestamp; long daysoffreshness = 15; long multiplier = 24 * 60 * 60 * 1000;// h m s ms - private HashMap<String, String> hm; + private HashMap<String, String> inmem_cache; // constructor for the cache itself public Cache(String path) { @@ -154,4 +155,5 @@ return content; } + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-14 20:16:47 UTC (rev 335) @@ -40,7 +40,7 @@ //expands all directly connected nodes @Override - public Vector<Node> expand(TypedSparqlQuery tsq, Manipulator m) { + public Vector<Node> expand(TypedSparqlQueryInterface tsq, Manipulator m) { Set<StringTuple> s = tsq.query(this.uri); // see manipulator s = m.check(s, this); @@ -83,7 +83,7 @@ // gets the types for properties recursively @Override - public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + public Vector<Node> expandProperties(TypedSparqlQueryInterface tsq, Manipulator m) { // TODO return type doesn't make sense return new Vector<Node>(); } @@ -101,5 +101,16 @@ return s; } + + @Override + public int compareTo(Object n){ + return super.compareTo((Node)n); + // + } + @Override + public int compareTo(Node n){ + return super.compareTo(n); + // + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-14 20:16:47 UTC (rev 335) @@ -20,6 +20,10 @@ package org.dllearner.kb.sparql; import java.net.URI; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.TreeSet; import java.util.Vector; // this class is used to extract the information recursively @@ -29,6 +33,8 @@ private Manipulator manipulator; private int recursionDepth = 2; private boolean getAllBackground = true; + private boolean closeAfterRecursion = true; + private boolean print_flag=false; public ExtractionAlgorithm(Configuration Configuration) { this.configuration = Configuration; @@ -42,7 +48,7 @@ return new InstanceNode(u); } - public Vector<Node> expandAll(URI[] u, TypedSparqlQuery tsp) { + public Vector<Node> expandAll(URI[] u, TypedSparqlQueryInterface tsp) { Vector<Node> v = new Vector<Node>(); for (URI one : u) { v.add(expandNode(one, tsp)); @@ -55,11 +61,12 @@ cave: the recursion is not a recursion anymore, it was transformed to an iteration */ - public Node expandNode(URI u, TypedSparqlQuery tsp) { + public Node expandNode(URI u, TypedSparqlQueryInterface tsp) { + long time=System.currentTimeMillis(); Node n = getFirstNode(u); Vector<Node> v = new Vector<Node>(); v.add(n); - System.out.println("StartVector: " + v); + p("StartVector: " + v); // n.expand(tsp, this.Manipulator); // Vector<Node> second= for (int x = 1; x <= recursionDepth; x++) { @@ -67,7 +74,7 @@ Vector<Node> tmp = new Vector<Node>(); while (v.size() > 0) { Node tmpNode = v.remove(0); - System.out.println("Expanding " + tmpNode); + p("Expanding " + tmpNode); // System.out.println(this.Manipulator); // these are the new not expanded nodes @@ -77,29 +84,74 @@ tmp.addAll(tmpVec); } v = tmp; - System.out - .println("Recursion counter: " + x + " with " + v.size() + " Nodes remaining"); + System.out.println("Recursion counter: " + x + + " with " + v.size() + " Nodes remaining, needed: " + +(System.currentTimeMillis()-time)); + time=System.currentTimeMillis(); } + HashSet<String> hadAlready=new HashSet<String>(); // gets All Class Nodes and expands them further if (this.getAllBackground) { + //Set<Node> classes = new TreeSet<Node>(); Vector<Node> classes = new Vector<Node>(); + + Vector<Node> instances = new Vector<Node>(); for (Node one : v) { if (one instanceof ClassNode) { classes.add(one); } + if (one instanceof InstanceNode) { + instances.add(one); + } + } + System.out.println(instances.size()); + TypedSparqlQueryClasses tsqc=new TypedSparqlQueryClasses(configuration); + if(closeAfterRecursion){ + while (instances.size() > 0) { + p("Getting classes for remaining instances: " + instances.size()); + Node next = instances.remove(0); + p("Getting classes for: " + next); + classes.addAll(next.expand(tsqc, manipulator)); + if (classes.size()>=manipulator.breakSuperClassRetrievalAfter){break;} + } + } + Vector<Node>tmp=new Vector<Node>(); + int i=0; while (classes.size() > 0) { - System.out.println("Remaining classes: " + classes.size()); + p("Remaining classes: " + classes.size()); + //Iterator<Node> it=classes.iterator(); + //Node next =(Node) it.next(); + //classes.remove(next); Node next = classes.remove(0); - System.out.println("Expanding: " + next); - classes.addAll(next.expand(tsp, manipulator)); - if (classes.size()>=manipulator.breakSuperClassRetrievalAfter){break;} + + if(!hadAlready.contains(next.uri.toString())){ + p("Expanding: " + next); + //System.out.println(hadAlready.size()); + hadAlready.add(next.uri.toString()); + tmp=next.expand(tsp, manipulator); + classes.addAll(tmp); + tmp=new Vector<Node>(); + if(i % 50==0)System.out.println("got "+i+" extra classes, max: "+manipulator.breakSuperClassRetrievalAfter); + i++; + if (i>=manipulator.breakSuperClassRetrievalAfter){break;} + } + //System.out.println("Skipping"); + + + //if (classes.size()>=manipulator.breakSuperClassRetrievalAfter){break;} + } + //System.out.println((System.currentTimeMillis()-time)+""); } return n; } + + void p(String s){ + if(print_flag)System.out.println(s); + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-14 20:16:47 UTC (rev 335) @@ -44,7 +44,7 @@ //expands all directly connected nodes @Override - public Vector<Node> expand(TypedSparqlQuery tsq, Manipulator m) { + public Vector<Node> expand(TypedSparqlQueryInterface tsq, Manipulator m) { Set<StringTuple> s = tsq.query(uri); // see Manipulator @@ -80,7 +80,7 @@ // gets the types for properties recursively @Override - public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + public Vector<Node> expandProperties(TypedSparqlQueryInterface tsq, Manipulator m) { for (PropertyNode one : properties) { one.expandProperties(tsq, m); } @@ -105,5 +105,16 @@ return s; } + + @Override + public int compareTo(Object n){ + return 0; + // + } + @Override + public int compareTo(Node n){ + return super.compareTo(n); + // + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manager.java 2007-12-14 20:16:47 UTC (rev 335) @@ -52,7 +52,7 @@ } catch (Exception e) { e.printStackTrace(); } - Set<StringTuple> t = this.typedSparqlQuery.getTupelsForRole(u, true); + Set<StringTuple> t = ((TypedSparqlQuery)this.typedSparqlQuery).getTupelsForRole(u, true); Set<String> ret = new HashSet<String>(); for (StringTuple one : t) { @@ -68,7 +68,7 @@ } catch (Exception e) { e.printStackTrace(); } - Set<StringTuple> t = this.typedSparqlQuery.getTupelsForRole(u,false); + Set<StringTuple> t = ((TypedSparqlQuery)this.typedSparqlQuery).getTupelsForRole(u,false); Set<String> ret = new HashSet<String>(); for (StringTuple one : t) { Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Manipulator.java 2007-12-14 20:16:47 UTC (rev 335) @@ -49,7 +49,7 @@ this.blankNodeIdentifier = blankNodeIdentifier; this.replaceObject=replaceObject; this.replacePredicate=replacePredicate; - + this.breakSuperClassRetrievalAfter=breakSuperClassRetrievalAfter; Set<String> classproperties = new HashSet<String>(); classproperties.add(subclass); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-14 20:16:47 UTC (rev 335) @@ -24,7 +24,7 @@ import java.util.Vector; // abstract class -public abstract class Node { +public abstract class Node implements Comparable{ URI uri; protected String type; protected boolean expanded = false; @@ -33,9 +33,9 @@ this.uri = u; } - public abstract Vector<Node> expand(TypedSparqlQuery tsq, Manipulator m); + public abstract Vector<Node> expand(TypedSparqlQueryInterface tsq, Manipulator m); - public abstract Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m); + public abstract Vector<Node> expandProperties(TypedSparqlQueryInterface tsq, Manipulator m); public abstract Set<String> toNTriple(); @@ -48,5 +48,12 @@ public URI getURI() { return uri; } + public boolean equals(Node n){ + if(this.uri.equals(n.uri))return true; + else return false; + } + public int compareTo(Node n){ + return this.uri.toString().compareTo(n.uri.toString()); + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PredefinedFilter.java 2007-12-14 20:16:47 UTC (rev 335) @@ -32,6 +32,10 @@ return YagoFilter(); case 2: return SKOS(); + case 3: + return YAGOSKOS(); + case 4: + return YagoSpecialHierarchy(); } return null; } @@ -47,11 +51,12 @@ pred.add("http://dbpedia.org/property/website"); pred.add("http://dbpedia.org/property/wikipage"); pred.add("http://dbpedia.org/property/wikiPageUsesTemplate"); + pred.add("http://dbpedia.org/property/relatedInstance"); - Set<String> obj = new HashSet<String>(); - obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); - obj.add("http://dbpedia.org/resource/Category:Articles_"); + //obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); + //obj.add("http://dbpedia.org/resource/Category:Articles_"); + obj.add("http://dbpedia.org/resource/Category:"); obj.add("http://dbpedia.org/resource/Template"); obj.add("http://xmlns.com/foaf/0.1/"); obj.add("http://upload.wikimedia.org/wikipedia/commons"); @@ -63,7 +68,36 @@ return new SparqlQueryType("forbid", obj, pred, "false"); } + public static SparqlQueryType YagoSpecialHierarchy(){ + Set<String> pred = new HashSet<String>(); + pred.add("http://www.w3.org/2004/02/skos/core"); + pred.add("http://www.w3.org/2002/07/owl#sameAs"); + pred.add("http://xmlns.com/foaf/0.1/"); + + pred.add("http://dbpedia.org/property/reference"); + pred.add("http://dbpedia.org/property/website"); + pred.add("http://dbpedia.org/property/wikipage"); + pred.add("http://dbpedia.org/property/wikiPageUsesTemplate"); + pred.add("http://dbpedia.org/property/relatedInstance"); + pred.add("http://dbpedia.org/property/monarch"); + + + Set<String> obj = new HashSet<String>(); + obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); + obj.add("http://dbpedia.org/resource/Category:Articles_"); + obj.add("http://dbpedia.org/resource/Template"); + obj.add("http://xmlns.com/foaf/0.1/"); + obj.add("http://upload.wikimedia.org/wikipedia/commons"); + obj.add("http://upload.wikimedia.org/wikipedia"); + obj.add("http://www.geonames.org"); + obj.add("http://www.w3.org/2006/03/wn/wn20/instances/synset"); + obj.add("http://www4.wiwiss.fu-berlin.de/flickrwrappr"); + obj.add("http://www.w3.org/2004/02/skos/core"); + + return new SparqlQueryType("forbid", obj, pred, "false"); + } + public static SparqlQueryType SKOS(){ Set<String> pred = new HashSet<String>(); //pred.add("http://www.w3.org/2004/02/skos/core"); @@ -94,6 +128,36 @@ return new SparqlQueryType("forbid", obj, pred, "false"); } + public static SparqlQueryType YAGOSKOS(){ + Set<String> pred = new HashSet<String>(); + //pred.add("http://www.w3.org/2004/02/skos/core"); + pred.add("http://www.w3.org/2002/07/owl#sameAs"); + pred.add("http://xmlns.com/foaf/0.1/"); + + pred.add("http://dbpedia.org/property/reference"); + pred.add("http://dbpedia.org/property/website"); + pred.add("http://dbpedia.org/property/wikipage"); + //pred.add("http://www.w3.org/2004/02/skos/core#narrower"); + pred.add("http://dbpedia.org/property/wikiPageUsesTemplate"); + + Set<String> obj = new HashSet<String>(); + //obj.add("http://dbpedia.org/resource/Category:Wikipedia_"); + //obj.add("http://dbpedia.org/resource/Category:Articles_"); + obj.add("http://xmlns.com/foaf/0.1/"); + obj.add("http://upload.wikimedia.org/wikipedia/commons"); + obj.add("http://upload.wikimedia.org/wikipedia"); + + obj.add("http://www.geonames.org"); + obj.add("http://www.w3.org/2006/03/wn/wn20/instances/synset"); + obj.add("http://www4.wiwiss.fu-berlin.de/flickrwrappr"); + + + //obj.add("http://dbpedia.org/class/yago"); + obj.add("http://dbpedia.org/resource/Template"); + + + return new SparqlQueryType("forbid", obj, pred, "false"); + } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-14 20:16:47 UTC (rev 335) @@ -23,13 +23,11 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; import java.util.Vector; import org.dllearner.utilities.StringTuple; -public class PropertyNode extends Node { +public class PropertyNode extends Node implements Comparable{ // the a and b part of a property private Node a; @@ -52,7 +50,7 @@ } @Override - public Vector<Node> expand(TypedSparqlQuery tsq, Manipulator m) { + public Vector<Node> expand(TypedSparqlQueryInterface tsq, Manipulator m) { Set<StringTuple> s = tsq.query(uri); Vector<Node> Nodes = new Vector<Node>(); // Manipulation @@ -75,7 +73,7 @@ // gets the types for properties recursively @Override - public Vector<Node> expandProperties(TypedSparqlQuery tsq, Manipulator m) { + public Vector<Node> expandProperties(TypedSparqlQueryInterface tsq, Manipulator m) { b.expandProperties(tsq, m); return this.expand(tsq, m); } @@ -98,5 +96,21 @@ return s; } + + @Override + public boolean equals(Node n){ + if(this.uri.equals(n.uri))return true; + else return false; + } + @Override + public int compareTo(Object n){ + return 0; + // + } + @Override + public int compareTo(Node n){ + return super.compareTo(n); + // + } } Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQueryMaker.java 2007-12-14 20:16:47 UTC (rev 335) @@ -61,13 +61,15 @@ if(domain){ ret = "SELECT * WHERE { " + lineend + "?subject <" + role + "> ?object; a []. " + lineend - + "FILTER( " + lineend + "(" + Filter + ").}"; + + "FILTER( " + lineend + "(" + Filter + ").}" ; + //"ORDER BY ?subject"; // System.out.println(ret); }else{ ret = "SELECT * WHERE { " + lineend + "?object a [] . " + "?subject <" + role + "> ?object . " + lineend + "FILTER( " + lineend + "(" + Filter + ").}"; + //"ORDER BY ?object"; } //System.out.println(ret); Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java 2007-12-08 19:37:08 UTC (rev 334) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQuery.java 2007-12-14 20:16:47 UTC (rev 335) @@ -35,9 +35,9 @@ import org.dllearner.utilities.StringTuple; // can execute different queries -public class TypedSparqlQuery { +public class TypedSparqlQuery implements TypedSparqlQueryInterface{ boolean print_flag=false; - boolean debug_no_cache=false; + boolean debug_no_cache=false;// true means no cahce is used private Configuration configuration; // private SparqlHTTPRequest SparqlHTTPRequest; private SparqlQueryMaker sparqlQueryMaker; @@ -106,7 +106,7 @@ //System.out.print("\n"); } else { xml = FromCache; - System.out.println("FROM CACHE"); + //System.out.println("FROM CACHE"); } // System.out.println(sparql); @@ -114,7 +114,7 @@ // process XML Set<StringTuple> s = processResult(xml, a, b); try { - System.out.println("retrieved " + s.size() + " tupels\n"); + //System.out.println("retrieved " + s.size() + " tupels\n"); } catch (Exception e) { } return s; @@ -124,30 +124,73 @@ Set<StringTuple> ret = new HashSet<StringTuple>(); // TODO if result is empty, catch exceptions - String one = "<binding name=\"" + a + "\">"; - String two = "<binding name=\"" + b + "\">"; + String resEnd="</result>"; + String one = "binding name=\"" + a + "\""; + String two = "binding name=\"" + b + "\""; + String endbinding= "binding"; + String uri="uri"; String uridel = "<uri>"; - String end = "</uri>"; + String bnode = "<bnode>"; + String uriend = "</uri>"; String predtmp = ""; String objtmp = ""; - - while (xml.indexOf(one) != -1) { + //System.out.println(getNextResult(xml)); + String nextResult=""; + while ((nextResult=getNextResult( xml))!=null){ + //System.out.println(xml.indexOf(resEnd)); + //System.out.println(xml); + if(nextResult.indexOf(bnode)!=-1) + {xml=xml.substring(xml.indexOf(resEnd)+resEnd.length());continue;} // get pred - xml = xml.substring(xml.indexOf(one) + one.length()); - xml = xml.substring(xml.indexOf(uridel) + uridel.length()); - predtmp = xml.substring(0, xml.indexOf(end)); - + //predtmp = nextResult.substring(nextResult.indexOf(one) + one.length()); + predtmp=getinTag(nextResult, one,endbinding); + predtmp=getinTag(predtmp, uri,uri); + //System.out.println(predtmp); + // getobj - xml = xml.substring(xml.indexOf(two) + two.length()); - xml = xml.substring(xml.indexOf(uridel) + uridel.length()); - objtmp = xml.substring(0, xml.indexOf(end)); - ret.add(new StringTuple(predtmp, objtmp)); - // System.out.println(new Tupel(predtmp,objtmp)); + objtmp=getinTag(nextResult, two,endbinding); + objtmp=getinTag(objtmp, uri,uri); + //System.out.println(objtmp); + + StringTuple st=new StringTuple(predtmp, objtmp); + //System.out.println(st); + ret.add(st); + xml=xml.substring(xml.indexOf(resEnd)+resEnd.length()); + } + /*while (xml.indexOf(one) != -1) { + + + + // System.out.println(new Tupel(predtmp,objtmp)); + }*/ + return ret; } + + private String getNextResult(String xml){ + String res1="<result>"; + String res2="</result>"; + if(xml.indexOf(res1)==-1)return null; + xml = xml.substring(xml.indexOf(res1) + res1.length()); + xml = xml.substring(0,xml.indexOf(res2) ); + //System.out.println(xml); + return xml; + } + private String getinTag(String xml, String starttag, String endtag){ + String res1="<"+starttag+">"; + //System.out.println(res1); + String res2="</"+endtag+">"; + if(xml.indexOf(res1)==-1)return null; + xml = xml.substring(xml.indexOf(res1) + res1.length()); + //System.out.println(xml); + xml = xml.substring(0,xml.indexOf(res2) ); + //System.out.println(xml); + + return xml; + } private String sendAndReceiveSPARQL(String sparql) throws IOException { p("sendAndReceiveSPARQL"); @@ -214,5 +257,7 @@ System.out.println(str); } } + + } Added: trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryClasses.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryClasses.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryClasses.java 2007-12-14 20:16:47 UTC (rev 335) @@ -0,0 +1,245 @@ +/** + * Copyright (C) 2007, Sebastian Hellmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb.sparql; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URLEncoder; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.dllearner.utilities.StringTuple; + +// can execute different queries +public class TypedSparqlQueryClasses implements TypedSparqlQueryInterface{ + boolean print_flag=false; + boolean debug_no_cache=false; + private Configuration configuration; + // private SparqlHTTPRequest SparqlHTTPRequest; + //private SparqlQueryMaker sparqlQueryMaker; + Cache cache; + + + public TypedSparqlQueryClasses(Configuration configuration) { + this.configuration = configuration; + this.cache = new Cache("cache"); + } + + // standard query get a tupels (p,o) for subject s + public Set<StringTuple> query(URI u) { + + // getQuery + String sparql = "SELECT ?predicate ?object " + + "WHERE {" + + "<"+u.toString()+"> ?predicate ?object;" + + "a ?object . " + + " FILTER (!regex(str(?object),'http://xmlns.com/foaf/0.1/'))"+ + "}"; + + return cachedSparql(u, sparql, "predicate", "object"); + + } + + + + // uses a cache + private Set<StringTuple> cachedSparql(URI u, String sparql, String a, String b) { + // check cache + String FromCache = cache.get(u.toString(), sparql); + if(debug_no_cache) { + FromCache=null; + } + String xml = null; + // if not in cache get it from EndPoint + if (FromCache == null) { + try { + xml = sendAndReceiveSPARQL(sparql); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + p(sparql); + // System.out.println(xml); + if(!debug_no_cache) { + cache.put(u.toString(), sparql, xml); + } + //System.out.print("\n"); + } else { + xml = FromCache; + //System.out.println("FROM CACHE"); + } + + // System.out.println(sparql); + // System.out.println(xml); + // process XML + Set<StringTuple> s = processResult(xml, a, b); + try { + //System.out.println("retrieved " + s.size() + " tupels\n"); + } catch (Exception e) { + } + return s; + } + + public Set<StringTuple> processResult(String xml, String a, String b) { + + Set<StringTuple> ret = new HashSet<StringTuple>(); + // TODO if result is empty, catch exceptions + String resEnd="</result>"; + String one = "binding name=\"" + a + "\""; + String two = "binding name=\"" + b + "\""; + String endbinding= "binding"; + String uri="uri"; + String uridel = "<uri>"; + String bnode = "<bnode>"; + String uriend = "</uri>"; + String predtmp = ""; + String objtmp = ""; + //System.out.println(getNextResult(xml)); + String nextResult=""; + while ((nextResult=getNextResult( xml))!=null){ + //System.out.println(xml.indexOf(resEnd)); + //System.out.println(xml); + if(nextResult.indexOf(bnode)!=-1) + {xml=xml.substring(xml.indexOf(resEnd)+resEnd.length());continue;} + // get pred + //predtmp = nextResult.substring(nextResult.indexOf(one) + one.length()); + predtmp=getinTag(nextResult, one,endbinding); + predtmp=getinTag(predtmp, uri,uri); + //System.out.println(predtmp); + + // getobj + objtmp=getinTag(nextResult, two,endbinding); + objtmp=getinTag(objtmp, uri,uri); + //System.out.println(objtmp); + + StringTuple st=new StringTuple(predtmp, objtmp); + //System.out.println(st); + ret.add(st); + xml=xml.substring(xml.indexOf(resEnd)+resEnd.length()); + + } + /*while (xml.indexOf(one) != -1) { + + + + + // System.out.println(new Tupel(predtmp,objtmp)); + }*/ + + return ret; + + } + + private String getNextResult(String xml){ + String res1="<result>"; + String res2="</result>"; + if(xml.indexOf(res1)==-1)return null; + xml = xml.substring(xml.indexOf(res1) + res1.length()); + xml = xml.substring(0,xml.indexOf(res2) ); + //System.out.println(xml); + return xml; + } + private String getinTag(String xml, String starttag, String endtag){ + String res1="<"+starttag+">"; + //System.out.println(res1); + String res2="</"+endtag+">"; + if(xml.indexOf(res1)==-1)return null; + xml = xml.substring(xml.indexOf(res1) + res1.length()); + //System.out.println(xml); + xml = xml.substring(0,xml.indexOf(res2) ); + //System.out.println(xml); + + return xml; + } + + private String sendAndReceiveSPARQL(String sparql) throws IOException { + p("sendAndReceiveSPARQL"); + StringBuilder answer = new StringBuilder(); + //sparql="SELECT * WHERE {?a ?b ?c}LIMIT 10"; + + // String an Sparql-Endpoint schicken + HttpURLConnection connection; + SpecificSparqlEndpoint se = configuration.getSparqlEndpoint(); + p("URL: "+se.getURL()); + p("Host: "+se.getHost()); + + connection = (HttpURLConnection) se.getURL().openConnection(); + connection.setDoOutput(true); + + //connection.addRequestProperty("Host", se.getHost()); + connection.addRequestProperty("Connection", "close"); + connection + .addRequestProperty( + "Accept", + "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); + connection.addRequestProperty("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"); + connection.addRequestProperty("Accept-Charset", "utf-8;q=1.0"); + connection + .addRequestProperty( + "User-Agent", + "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 Web-Sniffer/1.0.24"); + + OutputStream os = connection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + + Set<String> s = se.getParameters().keySet(); + Iterator<String> it = s.iterator(); + String FullURI = ""; + while (it.hasNext()) { + String element = it.next(); + FullURI += "" + URLEncoder.encode(element, "UTF-8") + "=" + + URLEncoder.encode(se.getParameters().get(element), "UTF-8") + "&"; + } + + FullURI += "" + se.getHasQueryParameter() + "=" + URLEncoder.encode(sparql, "UTF-8"); + p(FullURI); + osw.write(FullURI); + osw.close(); + + // receive answer + InputStream is = connection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is, "UTF-8"); + BufferedReader br = new BufferedReader(isr); + + String line; + do { + line = br.readLine(); + if (line != null) + answer.append(line); + } while (line != null); + + br.close(); + p(answer.toString()); + return answer.toString(); + } + public void p(String str){ + if(print_flag){ + System.out.println(str); + } + } + +} Added: trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryInterface.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryInterface.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/TypedSparqlQueryInterface.java 2007-12-14 20:16:47 UTC (rev 335) @@ -0,0 +1,11 @@ +package org.dllearner.kb.sparql; + +import java.net.URI; +import java.util.Set; + +import org.dllearner.utilities.StringTuple; + +public interface TypedSparqlQueryInterface { + + public Set<StringTuple> query(URI u); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jen...@us...> - 2008-01-02 12:59:34
|
Revision: 336 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=336&view=rev Author: jenslehmann Date: 2008-01-02 04:59:17 -0800 (Wed, 02 Jan 2008) Log Message: ----------- fixed a couple of errors and warnings Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java Modified: trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2007-12-14 20:16:47 UTC (rev 335) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlEndpointRestructured.java 2008-01-02 12:59:17 UTC (rev 336) @@ -30,7 +30,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; -import java.util.Random; import java.util.Set; import org.dllearner.core.KnowledgeSource; Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2007-12-14 20:16:47 UTC (rev 335) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ClassNode.java 2008-01-02 12:59:17 UTC (rev 336) @@ -23,8 +23,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; import java.util.Vector; import org.dllearner.utilities.StringTuple; @@ -103,11 +101,6 @@ } @Override - public int compareTo(Object n){ - return super.compareTo((Node)n); - // - } - @Override public int compareTo(Node n){ return super.compareTo(n); // Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2007-12-14 20:16:47 UTC (rev 335) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/ExtractionAlgorithm.java 2008-01-02 12:59:17 UTC (rev 336) @@ -21,9 +21,6 @@ import java.net.URI; import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import java.util.TreeSet; import java.util.Vector; // this class is used to extract the information recursively Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2007-12-14 20:16:47 UTC (rev 335) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/InstanceNode.java 2008-01-02 12:59:17 UTC (rev 336) @@ -23,8 +23,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; import java.util.Vector; import org.dllearner.utilities.StringTuple; @@ -107,11 +105,6 @@ } @Override - public int compareTo(Object n){ - return 0; - // - } - @Override public int compareTo(Node n){ return super.compareTo(n); // Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2007-12-14 20:16:47 UTC (rev 335) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Node.java 2008-01-02 12:59:17 UTC (rev 336) @@ -24,7 +24,7 @@ import java.util.Vector; // abstract class -public abstract class Node implements Comparable{ +public abstract class Node implements Comparable<Node> { URI uri; protected String type; protected boolean expanded = false; Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2007-12-14 20:16:47 UTC (rev 335) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/PropertyNode.java 2008-01-02 12:59:17 UTC (rev 336) @@ -27,7 +27,7 @@ import org.dllearner.utilities.StringTuple; -public class PropertyNode extends Node implements Comparable{ +public class PropertyNode extends Node { // the a and b part of a property private Node a; @@ -102,15 +102,10 @@ if(this.uri.equals(n.uri))return true; else return false; } + @Override - public int compareTo(Object n){ - return 0; - // - } - @Override public int compareTo(Node n){ return super.compareTo(n); - // } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2008-01-18 10:47:36
|
Revision: 388 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=388&view=rev Author: kurzum Date: 2008-01-18 02:47:32 -0800 (Fri, 18 Jan 2008) Log Message: ----------- renamed knowledgeSource Added Paths: ----------- trunk/src/dl-learner/org/dllearner/kb/SparqlKnowledgeSource.java Removed Paths: ------------- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlKnowledgeSource.java Copied: trunk/src/dl-learner/org/dllearner/kb/SparqlKnowledgeSource.java (from rev 386, trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlKnowledgeSource.java) =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/SparqlKnowledgeSource.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/kb/SparqlKnowledgeSource.java 2008-01-18 10:47:32 UTC (rev 388) @@ -0,0 +1,524 @@ +/** + * Copyright (C) 2007, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.kb; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Set; + +import org.dllearner.core.KnowledgeSource; +import org.dllearner.core.OntologyFormat; +import org.dllearner.core.OntologyFormatUnsupportedException; +import org.dllearner.core.config.BooleanConfigOption; +import org.dllearner.core.config.ConfigEntry; +import org.dllearner.core.config.ConfigOption; +import org.dllearner.core.config.IntegerConfigOption; +import org.dllearner.core.config.InvalidConfigOptionValueException; +import org.dllearner.core.config.StringConfigOption; +import org.dllearner.core.config.StringSetConfigOption; +import org.dllearner.core.config.StringTupleListConfigOption; +import org.dllearner.core.dl.KB; +import org.dllearner.kb.sparql.Manager; +import org.dllearner.kb.sparql.Manipulator; +import org.dllearner.kb.sparql.configuration.PredefinedEndpoint; +import org.dllearner.kb.sparql.configuration.PredefinedFilter; +import org.dllearner.kb.sparql.configuration.SparqlQueryType; +import org.dllearner.kb.sparql.configuration.SpecificSparqlEndpoint; +import org.dllearner.kb.sparql.old.*; +import org.dllearner.parser.KBParser; +import org.dllearner.reasoning.DIGConverter; +import org.dllearner.reasoning.JenaOWLDIGConverter; +import org.dllearner.utilities.StringTuple; + +/** + * Represents a SPARQL Endpoint. + * + * @author Jens Lehmann + * @author Sebastian Knappe + * @author Sebastian Hellmann + */ +public class SparqlKnowledgeSource extends KnowledgeSource { + + // ConfigOptions + private URL url; + String host; + private Set<String> instances=new HashSet<String>();; + private URL dumpFile; + private int recursionDepth = 1; + private int predefinedFilter = 0; + private int predefinedEndpoint = 0; + private Set<String> predList=new HashSet<String>(); + private Set<String> objList=new HashSet<String>(); + // private Set<String> classList; + private String format = "N-TRIPLES"; + private boolean dumpToFile = true; + private boolean useLits = false; + private boolean getAllSuperClasses = true; + private boolean closeAfterRecursion = true; + private int breakSuperClassRetrievalAfter = 200; + + private boolean learnDomain = false; + private boolean learnRange = false; + private int numberOfInstancesUsedForRoleLearning=40; + private String role=""; + private String blankNodeIdentifier = "bnode"; + + LinkedList<StringTuple> URIParameters = new LinkedList<StringTuple>(); + LinkedList<StringTuple> replacePredicate = new LinkedList<StringTuple>(); + LinkedList<StringTuple> replaceObject = new LinkedList<StringTuple>(); + + /** + * Holds the results of the calculateSubjects method + */ + private String[] subjects; + + /** + * Holds the results of the calculateTriples method + */ + private String[] triples; + + /** + * Holds the results of the calculateConceptSubjects method + */ + private String[] conceptSubjects; + + /** + * if a method is running this becomes true + */ + private boolean subjectThreadRunning = false; + + private boolean triplesThreadRunning = false; + + private boolean conceptThreadRunning = false; + + /** + * the Thread that is running a method + */ + private Thread subjectThread; + + private Thread triplesThread; + + private Thread conceptThread; + + // received ontology as array, used if format=Array(an element of the + // array consists of the subject, predicate and object separated by '<' + private String[] ontArray; + + // received ontology as KB, the internal format + private KB kb; + + public static String getName() { + return "SPARQL Endpoint Restructured"; + } + + /** + * sets the ConfigOptions for this KnowledgeSource + * + * @return + */ + public static Collection<ConfigOption<?>> createConfigOptions() { + Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); + options.add(new StringConfigOption("url", "URL of SPARQL Endpoint")); + options.add(new StringConfigOption("host", "host of SPARQL Endpoint")); + options.add(new StringSetConfigOption("instances", + "relevant instances e.g. positive and negative examples in a learning problem")); + options.add(new IntegerConfigOption("recursionDepth", + "recursion depth of KB fragment selection", 2)); + options.add(new IntegerConfigOption("predefinedFilter", "the mode of the SPARQL Filter")); + options.add(new IntegerConfigOption("predefinedEndpoint", "the mode of the SPARQL Filter")); + + options.add(new StringSetConfigOption("predList", "list of all ignored roles")); + options.add(new StringSetConfigOption("objList", "list of all ignored objects")); + options.add(new StringSetConfigOption("classList", "list of all ignored classes")); + options.add(new StringConfigOption("format", "N-TRIPLES or KB format", "N-TRIPLES")); + options.add(new BooleanConfigOption("dumpToFile", + "Specifies whether the extracted ontology is written to a file or not.", true)); + options.add(new BooleanConfigOption("useLits", "use Literals in SPARQL query")); + options.add(new BooleanConfigOption("getAllSuperClasses", "If true then all superclasses are retrieved until the most general class (owl:Thing) is reached.", true)); + + options.add(new BooleanConfigOption("learnDomain", "learns the Domain for a Role")); + options.add(new BooleanConfigOption("learnRange", "learns the Range for a Role")); + options.add(new StringConfigOption("role", "role to learn Domain/Range from")); + options.add(new StringConfigOption("blankNodeIdentifier", + "used to identify blanknodes in Tripels")); + + options.add(new StringTupleListConfigOption("example", "example")); + options.add(new StringTupleListConfigOption("replacePredicate", "rule for replacing predicates")); + options.add(new StringTupleListConfigOption("replaceObject", "rule for replacing predicates")); + options.add(new IntegerConfigOption("breakSuperClassRetrievalAfter", "stops a cyclic hierarchy after specified number of classes")); + options.add(new IntegerConfigOption("numberOfInstancesUsedForRoleLearning", "")); + options.add(new BooleanConfigOption("closeAfterRecursion", "gets all classes for all instances")); + + + + return options; + } + + /* + * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) + */ + @Override + @SuppressWarnings( { "unchecked" }) + public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { + String option = entry.getOptionName(); + if (option.equals("url")) { + String s = (String) entry.getValue(); + try { + url = new URL(s); + } catch (MalformedURLException e) { + throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(), + "malformed URL " + s); + } + } else if (option.equals("host")) { + host = (String) entry.getValue(); + } else if (option.equals("instances")) { + instances = (Set<String>) entry.getValue(); + } else if (option.equals("recursionDepth")) { + recursionDepth = (Integer) entry.getValue(); + } else if (option.equals("predList")) { + predList = (Set<String>) entry.getValue(); + } else if (option.equals("objList")) { + objList = (Set<String>) entry.getValue(); + //} else if (option.equals("classList")) { + // classList = (Set<String>) entry.getValue(); + } else if (option.equals("predefinedEndpoint")) { + predefinedEndpoint = (Integer) entry.getValue(); + } else if (option.equals("predefinedFilter")) { + predefinedFilter = (Integer) entry.getValue(); + } else if (option.equals("format")) { + format = (String) entry.getValue(); + } else if (option.equals("dumpToFile")) { + dumpToFile = (Boolean) entry.getValue(); + } else if (option.equals("useLits")) { + useLits = (Boolean) entry.getValue(); + } else if (option.equals("getAllSuperClasses")) { + getAllSuperClasses = (Boolean) entry.getValue(); + } else if (option.equals("learnDomain")) { + learnDomain = (Boolean) entry.getValue(); + }else if (option.equals("learnRange")) { + learnRange = (Boolean) entry.getValue(); + } else if (option.equals("role")) { + role = (String) entry.getValue(); + } else if (option.equals("blankNodeIdentifier")) { + blankNodeIdentifier = (String) entry.getValue(); + } else if (option.equals("example")) { + //System.out.println(entry.getValue()); + }else if (option.equals("replacePredicate")) { + replacePredicate = (LinkedList)entry.getValue(); + }else if (option.equals("replaceObject")) { + replaceObject = (LinkedList)entry.getValue(); + }else if (option.equals("breakSuperClassRetrievalAfter")) { + breakSuperClassRetrievalAfter = (Integer) entry.getValue(); + }else if (option.equals("numberOfInstancesUsedForRoleLearning")) { + numberOfInstancesUsedForRoleLearning = (Integer) entry.getValue(); + }else if (option.equals("closeAfterRecursion")) { + closeAfterRecursion = (Boolean) entry.getValue(); + } + + } + + /* + * (non-Javadoc) + * + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() { + System.out.println("SparqlModul: Collecting Ontology"); + // SparqlOntologyCollector oc= + // new SparqlOntologyCollector(Datastructures.setToArray(instances), + // numberOfRecursions, filterMode, + // Datastructures.setToArray(predList),Datastructures.setToArray( + // objList),Datastructures.setToArray(classList),format,url,useLits); + Manager m = new Manager(); + SpecificSparqlEndpoint sse = null; + SparqlQueryType sqt = null; + // get Options for Manipulator + Manipulator man = new Manipulator(blankNodeIdentifier,breakSuperClassRetrievalAfter,replacePredicate,replaceObject); + HashMap<String, String> parameters = new HashMap<String, String>(); + parameters.put("default-graph-uri", "http://dbpedia.org"); + parameters.put("format", "application/sparql-results.xml"); + + // get Options for endpoints + if (predefinedEndpoint >= 1) { + sse = PredefinedEndpoint.getEndpoint(predefinedEndpoint); + } else { + sse = new SpecificSparqlEndpoint(url, host, parameters); + } + + // get Options for Filters + + if (predefinedFilter >= 1) { + sqt = PredefinedFilter.getFilter(predefinedFilter); + + } else { + sqt = new SparqlQueryType("forbid", objList, predList, useLits + ""); + + } + // give everything to the manager + m.useConfiguration(sqt, sse, man, recursionDepth, getAllSuperClasses,closeAfterRecursion); + try { + String ont = ""; + //System.out.println(learnDomain); + // used to learn a domain of a role + if (learnDomain || learnRange) { + Set<String> pos=new HashSet<String>(); + Set<String> neg=new HashSet<String>(); + if(learnDomain){ + pos = m.getDomainInstancesForRole(role); + neg = m.getRangeInstancesForRole(role); + }else if(learnRange){ + neg = m.getDomainInstancesForRole(role); + pos = m.getRangeInstancesForRole(role); + } + //choose 30 + + + Set<String> tmp=new HashSet<String>(); + for(String one:pos){ + tmp.add(one); + if(tmp.size()>=numberOfInstancesUsedForRoleLearning)break; + } + pos=tmp; + System.out.println("Instances used: "+pos.size()); + + tmp=new HashSet<String>(); + for(String one:neg){ + tmp.add(one); + if(tmp.size()>=numberOfInstancesUsedForRoleLearning)break; + } + neg=tmp; + + instances=new HashSet<String>(); + instances.addAll(pos); + + instances.addAll(neg); + + for(String one:pos){ + System.out.println("+\""+one+"\""); + } + for(String one:neg){ + System.out.println("-\""+one+"\""); + } + + /*Random r= new Random(); + + + Object[] arr=instances.toArray(); + while(instances.size()>=30){ + + }*/ + // add the role to the filter(a solution is always EXISTS + // role.TOP) + m.addPredicateFilter(role); + //System.out.println(instances); + // THIS is a workaround + + } + // the actual extraction is started here + ont = m.extract(instances); + System.out.println("Number of cached SPARQL queries: "+m.getConfiguration().numberOfCachedSparqlQueries); + System.out.println("Number of uncached SPARQL queries: "+m.getConfiguration().numberOfUncachedSparqlQueries); + + System.out.println("Finished collecting Fragment"); + + if (dumpToFile) { + String filename = System.currentTimeMillis() + ".nt"; + String basedir = "cache" + File.separator; + try { + if (!new File(basedir).exists()) + new File(basedir).mkdir(); + + FileWriter fw = new FileWriter(new File(basedir + filename), true); + fw.write(ont); + fw.flush(); + fw.close(); + + dumpFile = (new File(basedir + filename)).toURI().toURL(); + } catch (Exception e) { + e.printStackTrace(); + } + } + if (format.equals("KB")) { + try { + //kb = KBParser.parseKBFile(new StringReader(ont)); + kb=KBParser.parseKBFile(dumpFile); + } catch (Exception e) { + e.printStackTrace(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("SparqlModul: ****Finished"); + } + + /* + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#toDIG() + */ + @Override + public String toDIG(URI kbURI) { + if (format.equals("N-TRIPLES")) + return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFormat.N_TRIPLES, kbURI); + else + return DIGConverter.getDIGString(kb, kbURI).toString(); + } + + /* + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#export(java.io.File, + * org.dllearner.core.OntologyFormat) + */ + @Override + public void export(File file, OntologyFormat format) throws OntologyFormatUnsupportedException { + // currently no export functions implemented, so we just throw an + // exception + throw new OntologyFormatUnsupportedException("export", format); + } + + public URL getURL() { + return url; + } + + public String[] getOntArray() { + return ontArray; + } + + + /** + * TODO SparqlOntologyCollector needs to be removed + * @param label + * @param limit + */ + public void calculateSubjects(String label, int limit) { + System.out.println("SparqlModul: Collecting Subjects"); + oldSparqlOntologyCollector oc = new oldSparqlOntologyCollector(url); + try { + subjects = oc.getSubjectsFromLabel(label, limit); + } catch (IOException e) { + subjects = new String[1]; + subjects[0] = "[Error]Sparql Endpoint could not be reached."; + } + System.out.println("SparqlModul: ****Finished"); + } + + /** + * TODO SparqlOntologyCollector needs to be removed + * @param subject + */ + public void calculateTriples(String subject) { + System.out.println("SparqlModul: Collecting Triples"); + oldSparqlOntologyCollector oc = new oldSparqlOntologyCollector(url); + try { + triples = oc.collectTriples(subject); + } catch (IOException e) { + triples = new String[1]; + triples[0] = "[Error]Sparql Endpoint could not be reached."; + } + System.out.println("SparqlModul: ****Finished"); + } + + /** + * TODO SparqlOntologyCollector needs to be removed + * @param concept + */ + public void calculateConceptSubjects(String concept) { + System.out.println("SparqlModul: Collecting Subjects"); + oldSparqlOntologyCollector oc = new oldSparqlOntologyCollector(url); + try { + conceptSubjects = oc.getSubjectsFromConcept(concept); + } catch (IOException e) { + conceptSubjects = new String[1]; + conceptSubjects[0] = "[Error]Sparql Endpoint could not be reached."; + } + System.out.println("SparqlModul: ****Finished"); + } + + public boolean subjectThreadIsRunning() { + return subjectThreadRunning; + } + + public void setSubjectThreadRunning(boolean bool) { + subjectThreadRunning = bool; + } + + public boolean triplesThreadIsRunning() { + return triplesThreadRunning; + } + + public void setTriplesThreadRunning(boolean bool) { + triplesThreadRunning = bool; + } + + public boolean conceptThreadIsRunning() { + return conceptThreadRunning; + } + + public void setConceptThreadRunning(boolean bool) { + conceptThreadRunning = bool; + } + + public String[] getSubjects() { + return subjects; + } + + public Thread getSubjectThread() { + return subjectThread; + } + + public void setSubjectThread(Thread subjectThread) { + this.subjectThread = subjectThread; + } + + public Thread getTriplesThread() { + return triplesThread; + } + + public void setTriplesThread(Thread triplesThread) { + this.triplesThread = triplesThread; + } + + public Thread getConceptThread() { + return conceptThread; + } + + public void setConceptThread(Thread conceptThread) { + this.conceptThread = conceptThread; + } + + public String[] getTriples() { + return triples; + } + + public String[] getConceptSubjects() { + return conceptSubjects; + } +} Deleted: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlKnowledgeSource.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlKnowledgeSource.java 2008-01-18 10:29:26 UTC (rev 387) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlKnowledgeSource.java 2008-01-18 10:47:32 UTC (rev 388) @@ -1,522 +0,0 @@ -/** - * Copyright (C) 2007, Jens Lehmann - * - * This file is part of DL-Learner. - * - * DL-Learner is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DL-Learner is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -package org.dllearner.kb.sparql; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Set; - -import org.dllearner.core.KnowledgeSource; -import org.dllearner.core.OntologyFormat; -import org.dllearner.core.OntologyFormatUnsupportedException; -import org.dllearner.core.config.BooleanConfigOption; -import org.dllearner.core.config.ConfigEntry; -import org.dllearner.core.config.ConfigOption; -import org.dllearner.core.config.IntegerConfigOption; -import org.dllearner.core.config.InvalidConfigOptionValueException; -import org.dllearner.core.config.StringConfigOption; -import org.dllearner.core.config.StringSetConfigOption; -import org.dllearner.core.config.StringTupleListConfigOption; -import org.dllearner.core.dl.KB; -import org.dllearner.kb.sparql.configuration.PredefinedEndpoint; -import org.dllearner.kb.sparql.configuration.PredefinedFilter; -import org.dllearner.kb.sparql.configuration.SparqlQueryType; -import org.dllearner.kb.sparql.configuration.SpecificSparqlEndpoint; -import org.dllearner.kb.sparql.old.*; -import org.dllearner.parser.KBParser; -import org.dllearner.reasoning.DIGConverter; -import org.dllearner.reasoning.JenaOWLDIGConverter; -import org.dllearner.utilities.StringTuple; - -/** - * Represents a SPARQL Endpoint. - * - * @author Jens Lehmann - * @author Sebastian Knappe - * @author Sebastian Hellmann - */ -public class SparqlKnowledgeSource extends KnowledgeSource { - - // ConfigOptions - private URL url; - String host; - private Set<String> instances=new HashSet<String>();; - private URL dumpFile; - private int recursionDepth = 1; - private int predefinedFilter = 0; - private int predefinedEndpoint = 0; - private Set<String> predList=new HashSet<String>(); - private Set<String> objList=new HashSet<String>(); - // private Set<String> classList; - private String format = "N-TRIPLES"; - private boolean dumpToFile = true; - private boolean useLits = false; - private boolean getAllSuperClasses = true; - private boolean closeAfterRecursion = true; - private int breakSuperClassRetrievalAfter = 200; - - private boolean learnDomain = false; - private boolean learnRange = false; - private int numberOfInstancesUsedForRoleLearning=40; - private String role=""; - private String blankNodeIdentifier = "bnode"; - - LinkedList<StringTuple> URIParameters = new LinkedList<StringTuple>(); - LinkedList<StringTuple> replacePredicate = new LinkedList<StringTuple>(); - LinkedList<StringTuple> replaceObject = new LinkedList<StringTuple>(); - - /** - * Holds the results of the calculateSubjects method - */ - private String[] subjects; - - /** - * Holds the results of the calculateTriples method - */ - private String[] triples; - - /** - * Holds the results of the calculateConceptSubjects method - */ - private String[] conceptSubjects; - - /** - * if a method is running this becomes true - */ - private boolean subjectThreadRunning = false; - - private boolean triplesThreadRunning = false; - - private boolean conceptThreadRunning = false; - - /** - * the Thread that is running a method - */ - private Thread subjectThread; - - private Thread triplesThread; - - private Thread conceptThread; - - // received ontology as array, used if format=Array(an element of the - // array consists of the subject, predicate and object separated by '<' - private String[] ontArray; - - // received ontology as KB, the internal format - private KB kb; - - public static String getName() { - return "SPARQL Endpoint Restructured"; - } - - /** - * sets the ConfigOptions for this KnowledgeSource - * - * @return - */ - public static Collection<ConfigOption<?>> createConfigOptions() { - Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); - options.add(new StringConfigOption("url", "URL of SPARQL Endpoint")); - options.add(new StringConfigOption("host", "host of SPARQL Endpoint")); - options.add(new StringSetConfigOption("instances", - "relevant instances e.g. positive and negative examples in a learning problem")); - options.add(new IntegerConfigOption("recursionDepth", - "recursion depth of KB fragment selection", 2)); - options.add(new IntegerConfigOption("predefinedFilter", "the mode of the SPARQL Filter")); - options.add(new IntegerConfigOption("predefinedEndpoint", "the mode of the SPARQL Filter")); - - options.add(new StringSetConfigOption("predList", "list of all ignored roles")); - options.add(new StringSetConfigOption("objList", "list of all ignored objects")); - options.add(new StringSetConfigOption("classList", "list of all ignored classes")); - options.add(new StringConfigOption("format", "N-TRIPLES or KB format", "N-TRIPLES")); - options.add(new BooleanConfigOption("dumpToFile", - "Specifies whether the extracted ontology is written to a file or not.", true)); - options.add(new BooleanConfigOption("useLits", "use Literals in SPARQL query")); - options.add(new BooleanConfigOption("getAllSuperClasses", "If true then all superclasses are retrieved until the most general class (owl:Thing) is reached.", true)); - - options.add(new BooleanConfigOption("learnDomain", "learns the Domain for a Role")); - options.add(new BooleanConfigOption("learnRange", "learns the Range for a Role")); - options.add(new StringConfigOption("role", "role to learn Domain/Range from")); - options.add(new StringConfigOption("blankNodeIdentifier", - "used to identify blanknodes in Tripels")); - - options.add(new StringTupleListConfigOption("example", "example")); - options.add(new StringTupleListConfigOption("replacePredicate", "rule for replacing predicates")); - options.add(new StringTupleListConfigOption("replaceObject", "rule for replacing predicates")); - options.add(new IntegerConfigOption("breakSuperClassRetrievalAfter", "stops a cyclic hierarchy after specified number of classes")); - options.add(new IntegerConfigOption("numberOfInstancesUsedForRoleLearning", "")); - options.add(new BooleanConfigOption("closeAfterRecursion", "gets all classes for all instances")); - - - - return options; - } - - /* - * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) - */ - @Override - @SuppressWarnings( { "unchecked" }) - public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { - String option = entry.getOptionName(); - if (option.equals("url")) { - String s = (String) entry.getValue(); - try { - url = new URL(s); - } catch (MalformedURLException e) { - throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(), - "malformed URL " + s); - } - } else if (option.equals("host")) { - host = (String) entry.getValue(); - } else if (option.equals("instances")) { - instances = (Set<String>) entry.getValue(); - } else if (option.equals("recursionDepth")) { - recursionDepth = (Integer) entry.getValue(); - } else if (option.equals("predList")) { - predList = (Set<String>) entry.getValue(); - } else if (option.equals("objList")) { - objList = (Set<String>) entry.getValue(); - //} else if (option.equals("classList")) { - // classList = (Set<String>) entry.getValue(); - } else if (option.equals("predefinedEndpoint")) { - predefinedEndpoint = (Integer) entry.getValue(); - } else if (option.equals("predefinedFilter")) { - predefinedFilter = (Integer) entry.getValue(); - } else if (option.equals("format")) { - format = (String) entry.getValue(); - } else if (option.equals("dumpToFile")) { - dumpToFile = (Boolean) entry.getValue(); - } else if (option.equals("useLits")) { - useLits = (Boolean) entry.getValue(); - } else if (option.equals("getAllSuperClasses")) { - getAllSuperClasses = (Boolean) entry.getValue(); - } else if (option.equals("learnDomain")) { - learnDomain = (Boolean) entry.getValue(); - }else if (option.equals("learnRange")) { - learnRange = (Boolean) entry.getValue(); - } else if (option.equals("role")) { - role = (String) entry.getValue(); - } else if (option.equals("blankNodeIdentifier")) { - blankNodeIdentifier = (String) entry.getValue(); - } else if (option.equals("example")) { - //System.out.println(entry.getValue()); - }else if (option.equals("replacePredicate")) { - replacePredicate = (LinkedList)entry.getValue(); - }else if (option.equals("replaceObject")) { - replaceObject = (LinkedList)entry.getValue(); - }else if (option.equals("breakSuperClassRetrievalAfter")) { - breakSuperClassRetrievalAfter = (Integer) entry.getValue(); - }else if (option.equals("numberOfInstancesUsedForRoleLearning")) { - numberOfInstancesUsedForRoleLearning = (Integer) entry.getValue(); - }else if (option.equals("closeAfterRecursion")) { - closeAfterRecursion = (Boolean) entry.getValue(); - } - - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.Component#init() - */ - @Override - public void init() { - System.out.println("SparqlModul: Collecting Ontology"); - // SparqlOntologyCollector oc= - // new SparqlOntologyCollector(Datastructures.setToArray(instances), - // numberOfRecursions, filterMode, - // Datastructures.setToArray(predList),Datastructures.setToArray( - // objList),Datastructures.setToArray(classList),format,url,useLits); - Manager m = new Manager(); - SpecificSparqlEndpoint sse = null; - SparqlQueryType sqt = null; - // get Options for Manipulator - Manipulator man = new Manipulator(blankNodeIdentifier,breakSuperClassRetrievalAfter,replacePredicate,replaceObject); - HashMap<String, String> parameters = new HashMap<String, String>(); - parameters.put("default-graph-uri", "http://dbpedia.org"); - parameters.put("format", "application/sparql-results.xml"); - - // get Options for endpoints - if (predefinedEndpoint >= 1) { - sse = PredefinedEndpoint.getEndpoint(predefinedEndpoint); - } else { - sse = new SpecificSparqlEndpoint(url, host, parameters); - } - - // get Options for Filters - - if (predefinedFilter >= 1) { - sqt = PredefinedFilter.getFilter(predefinedFilter); - - } else { - sqt = new SparqlQueryType("forbid", objList, predList, useLits + ""); - - } - // give everything to the manager - m.useConfiguration(sqt, sse, man, recursionDepth, getAllSuperClasses,closeAfterRecursion); - try { - String ont = ""; - //System.out.println(learnDomain); - // used to learn a domain of a role - if (learnDomain || learnRange) { - Set<String> pos=new HashSet<String>(); - Set<String> neg=new HashSet<String>(); - if(learnDomain){ - pos = m.getDomainInstancesForRole(role); - neg = m.getRangeInstancesForRole(role); - }else if(learnRange){ - neg = m.getDomainInstancesForRole(role); - pos = m.getRangeInstancesForRole(role); - } - //choose 30 - - - Set<String> tmp=new HashSet<String>(); - for(String one:pos){ - tmp.add(one); - if(tmp.size()>=numberOfInstancesUsedForRoleLearning)break; - } - pos=tmp; - System.out.println("Instances used: "+pos.size()); - - tmp=new HashSet<String>(); - for(String one:neg){ - tmp.add(one); - if(tmp.size()>=numberOfInstancesUsedForRoleLearning)break; - } - neg=tmp; - - instances=new HashSet<String>(); - instances.addAll(pos); - - instances.addAll(neg); - - for(String one:pos){ - System.out.println("+\""+one+"\""); - } - for(String one:neg){ - System.out.println("-\""+one+"\""); - } - - /*Random r= new Random(); - - - Object[] arr=instances.toArray(); - while(instances.size()>=30){ - - }*/ - // add the role to the filter(a solution is always EXISTS - // role.TOP) - m.addPredicateFilter(role); - //System.out.println(instances); - // THIS is a workaround - - } - // the actual extraction is started here - ont = m.extract(instances); - System.out.println("Number of cached SPARQL queries: "+m.getConfiguration().numberOfCachedSparqlQueries); - System.out.println("Number of uncached SPARQL queries: "+m.getConfiguration().numberOfUncachedSparqlQueries); - - System.out.println("Finished collecting Fragment"); - - if (dumpToFile) { - String filename = System.currentTimeMillis() + ".nt"; - String basedir = "cache" + File.separator; - try { - if (!new File(basedir).exists()) - new File(basedir).mkdir(); - - FileWriter fw = new FileWriter(new File(basedir + filename), true); - fw.write(ont); - fw.flush(); - fw.close(); - - dumpFile = (new File(basedir + filename)).toURI().toURL(); - } catch (Exception e) { - e.printStackTrace(); - } - } - if (format.equals("KB")) { - try { - //kb = KBParser.parseKBFile(new StringReader(ont)); - kb=KBParser.parseKBFile(dumpFile); - } catch (Exception e) { - e.printStackTrace(); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println("SparqlModul: ****Finished"); - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.KnowledgeSource#toDIG() - */ - @Override - public String toDIG(URI kbURI) { - if (format.equals("N-TRIPLES")) - return JenaOWLDIGConverter.getTellsString(dumpFile, OntologyFormat.N_TRIPLES, kbURI); - else - return DIGConverter.getDIGString(kb, kbURI).toString(); - } - - /* - * (non-Javadoc) - * - * @see org.dllearner.core.KnowledgeSource#export(java.io.File, - * org.dllearner.core.OntologyFormat) - */ - @Override - public void export(File file, OntologyFormat format) throws OntologyFormatUnsupportedException { - // currently no export functions implemented, so we just throw an - // exception - throw new OntologyFormatUnsupportedException("export", format); - } - - public URL getURL() { - return url; - } - - public String[] getOntArray() { - return ontArray; - } - - - /** - * TODO SparqlOntologyCollector needs to be removed - * @param label - * @param limit - */ - public void calculateSubjects(String label, int limit) { - System.out.println("SparqlModul: Collecting Subjects"); - oldSparqlOntologyCollector oc = new oldSparqlOntologyCollector(url); - try { - subjects = oc.getSubjectsFromLabel(label, limit); - } catch (IOException e) { - subjects = new String[1]; - subjects[0] = "[Error]Sparql Endpoint could not be reached."; - } - System.out.println("SparqlModul: ****Finished"); - } - - /** - * TODO SparqlOntologyCollector needs to be removed - * @param subject - */ - public void calculateTriples(String subject) { - System.out.println("SparqlModul: Collecting Triples"); - oldSparqlOntologyCollector oc = new oldSparqlOntologyCollector(url); - try { - triples = oc.collectTriples(subject); - } catch (IOException e) { - triples = new String[1]; - triples[0] = "[Error]Sparql Endpoint could not be reached."; - } - System.out.println("SparqlModul: ****Finished"); - } - - /** - * TODO SparqlOntologyCollector needs to be removed - * @param concept - */ - public void calculateConceptSubjects(String concept) { - System.out.println("SparqlModul: Collecting Subjects"); - oldSparqlOntologyCollector oc = new oldSparqlOntologyCollector(url); - try { - conceptSubjects = oc.getSubjectsFromConcept(concept); - } catch (IOException e) { - conceptSubjects = new String[1]; - conceptSubjects[0] = "[Error]Sparql Endpoint could not be reached."; - } - System.out.println("SparqlModul: ****Finished"); - } - - public boolean subjectThreadIsRunning() { - return subjectThreadRunning; - } - - public void setSubjectThreadRunning(boolean bool) { - subjectThreadRunning = bool; - } - - public boolean triplesThreadIsRunning() { - return triplesThreadRunning; - } - - public void setTriplesThreadRunning(boolean bool) { - triplesThreadRunning = bool; - } - - public boolean conceptThreadIsRunning() { - return conceptThreadRunning; - } - - public void setConceptThreadRunning(boolean bool) { - conceptThreadRunning = bool; - } - - public String[] getSubjects() { - return subjects; - } - - public Thread getSubjectThread() { - return subjectThread; - } - - public void setSubjectThread(Thread subjectThread) { - this.subjectThread = subjectThread; - } - - public Thread getTriplesThread() { - return triplesThread; - } - - public void setTriplesThread(Thread triplesThread) { - this.triplesThread = triplesThread; - } - - public Thread getConceptThread() { - return conceptThread; - } - - public void setConceptThread(Thread conceptThread) { - this.conceptThread = conceptThread; - } - - public String[] getTriples() { - return triples; - } - - public String[] getConceptSubjects() { - return conceptSubjects; - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |