Mercurial > hg > NamedIdentityManager
comparison src/de/mpiwg/itgroup/nimanager/owl/TripleStoreHandler.java @ 2:e3ecb88314a5
minor bugs
ontologies added
author | dwinter |
---|---|
date | Fri, 02 Dec 2011 08:37:03 +0100 |
parents | 1384a0d382fa |
children |
comparison
equal
deleted
inserted
replaced
1:b8333fab0d95 | 2:e3ecb88314a5 |
---|---|
12 import java.sql.Connection; | 12 import java.sql.Connection; |
13 import java.sql.DriverManager; | 13 import java.sql.DriverManager; |
14 import java.util.Collection; | 14 import java.util.Collection; |
15 import java.util.HashMap; | 15 import java.util.HashMap; |
16 import java.util.Iterator; | 16 import java.util.Iterator; |
17 import java.util.List; | |
17 import java.util.Map; | 18 import java.util.Map; |
18 import java.util.Set; | 19 import java.util.Set; |
19 | 20 |
20 import org.apache.log4j.Logger; | 21 import org.apache.log4j.Logger; |
21 import org.openrdf.model.Resource; | 22 import org.openrdf.model.Resource; |
43 import virtuoso.sesame2.driver.VirtuosoRepository; | 44 import virtuoso.sesame2.driver.VirtuosoRepository; |
44 | 45 |
45 import com.hp.hpl.jena.vocabulary.OWL; | 46 import com.hp.hpl.jena.vocabulary.OWL; |
46 | 47 |
47 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException; | 48 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException; |
49 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler.Quadruple; | |
48 | 50 |
49 | 51 |
50 import edu.stanford.smi.protege.widget.StringListWidget; | 52 import edu.stanford.smi.protege.widget.StringListWidget; |
51 import edu.stanford.smi.protegex.owl.model.OWLIndividual; | 53 import edu.stanford.smi.protegex.owl.model.OWLIndividual; |
52 import edu.stanford.smi.protegex.owl.model.OWLModel; | 54 import edu.stanford.smi.protegex.owl.model.OWLModel; |
59 | 61 |
60 | 62 |
61 | 63 |
62 public class TripleStoreHandler { | 64 public class TripleStoreHandler { |
63 | 65 |
66 | |
67 public static class Quadruple { | |
68 String subj; | |
69 String pred; | |
70 Object obj; | |
71 String ctx; | |
72 | |
73 public String getSubj() { | |
74 return subj; | |
75 } | |
76 | |
77 public String getPred() { | |
78 return pred; | |
79 } | |
80 | |
81 public Object getObj() { | |
82 return obj; | |
83 } | |
84 | |
85 public String getCtx() { | |
86 return ctx; | |
87 } | |
88 | |
89 | |
90 | |
91 public Quadruple(String s, String p, String o,String c){ | |
92 subj=s; | |
93 pred=p; | |
94 obj=o; | |
95 ctx=c; | |
96 } | |
97 | |
98 | |
99 public Quadruple(String s, String p, Value o,String c){ | |
100 subj=s; | |
101 pred=p; | |
102 obj=o; | |
103 ctx=c; | |
104 } | |
105 | |
106 public String toString(){ | |
107 return String.format("%s %s %s %s.", subj,pred,obj,ctx); | |
108 } | |
109 | |
110 | |
111 } | |
112 | |
113 public static class LiteralQuadruple extends Quadruple{ | |
114 String subj; | |
115 String pred; | |
116 Object obj; | |
117 String ctx; | |
118 | |
119 public LiteralQuadruple(String s, String p, String o,String c){ | |
120 super(s,p,o,c); | |
121 | |
122 | |
123 } | |
124 | |
125 } | |
64 public static String ONTOLOGY_NS="http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/"; | 126 public static String ONTOLOGY_NS="http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/"; |
65 private boolean save=true; // wenn false dann schreibt write nicht ins repositorium. | 127 private boolean save=true; // wenn false dann schreibt write nicht ins repositorium. |
66 | 128 |
67 private Logger logger = Logger.getRootLogger(); | 129 private Logger logger = Logger.getRootLogger(); |
68 | 130 |
87 | 149 |
88 | 150 |
89 repository = new VirtuosoRepository(url, username, password); | 151 repository = new VirtuosoRepository(url, username, password); |
90 try { | 152 try { |
91 Class.forName("virtuoso.jdbc4.Driver"); | 153 Class.forName("virtuoso.jdbc4.Driver"); |
92 String connectString = "jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111/charset=UTF-8"; | 154 String connectString = url+"/charset=UTF-8"; |
93 this.sqlCon = DriverManager.getConnection(connectString, username, | 155 this.sqlCon = DriverManager.getConnection(connectString, username, |
94 password); | 156 password); |
95 } catch (Exception e) { | 157 } catch (Exception e) { |
96 throw new TripleStoreHandlerException(); | 158 throw new TripleStoreHandlerException(); |
97 } | 159 } |
231 | 293 |
232 public URI createUri(String subj){ | 294 public URI createUri(String subj){ |
233 return createUri(repository, subj); | 295 return createUri(repository, subj); |
234 } | 296 } |
235 | 297 |
236 public Value createLiteral(String subj){ | 298 public Value createLiteral(String subj){ |
237 return repository.getValueFactory().createLiteral(subj); | 299 return repository.getValueFactory().createLiteral(subj); |
238 } | 300 } |
239 | 301 |
240 public Value createLiteral(String subj, String lang){ | 302 public Value createLiteral(String subj, String lang){ |
241 return repository.getValueFactory().createLiteral(subj,lang); | 303 return repository.getValueFactory().createLiteral(subj,lang); |
356 con.add(statement); | 418 con.add(statement); |
357 con.commit(); | 419 con.commit(); |
358 con.close(); | 420 con.close(); |
359 } | 421 } |
360 } | 422 } |
361 | 423 |
424 public void write(Quadruple q) throws RepositoryException{ | |
425 if (LiteralQuadruple.class.isInstance(q)){ | |
426 write(q.getSubj(),q.getPred(),createLiteral((String)q.getObj()),q.getCtx()); | |
427 } else { | |
428 if (String.class.isInstance(q.getObj())){ | |
429 write(q.getSubj(),q.getPred(),(String)q.getObj(),q.getCtx());} | |
430 else { | |
431 write(q.getSubj(),q.getPred(),(Value)q.getObj(),q.getCtx());} | |
432 | |
433 } | |
434 } | |
435 | |
436 public void write(List<Quadruple> annots) throws RepositoryException { | |
437 for(Quadruple q:annots){ | |
438 write(q); | |
439 } | |
440 | |
441 } | |
362 public void exportCtx(String ctx, File file) throws RepositoryException, RDFHandlerException, IOException{ | 442 public void exportCtx(String ctx, File file) throws RepositoryException, RDFHandlerException, IOException{ |
363 | 443 |
364 RepositoryConnection conn = repository.getConnection(); | 444 RepositoryConnection conn = repository.getConnection(); |
365 OutputStream output = (file != null) ? new FileOutputStream(file) : System.out; | 445 OutputStream output = (file != null) ? new FileOutputStream(file) : System.out; |
366 | 446 |
369 conn.exportStatements(null, null, null, true, rdfxmlfWriter, repository.getValueFactory().createURI(ctx)); | 449 conn.exportStatements(null, null, null, true, rdfxmlfWriter, repository.getValueFactory().createURI(ctx)); |
370 output.write('\n'); | 450 output.write('\n'); |
371 conn.close(); | 451 conn.close(); |
372 } | 452 } |
373 | 453 |
454 | |
374 public static void main(String args[]) throws RepositoryException, RDFHandlerException, IOException, TripleStoreHandlerException{ | 455 public static void main(String args[]) throws RepositoryException, RDFHandlerException, IOException, TripleStoreHandlerException{ |
375 TripleStoreHandler th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", "dba", "dba"); | 456 TripleStoreHandler th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", "dba", "dba"); |
376 //th.exportCtx("http://import", new File("/tmp/out.rdf")); | 457 //th.exportCtx("http://import", new File("/tmp/out.rdf")); |
377 // | 458 // |
378 th.exportCtx("file://out.owl", new File("/tmp/out2.rdf")); | 459 th.exportCtx("file://out.owl", new File("/tmp/out2.rdf")); |