1 | package org.biohackathon.SPARQLBuilder.OWL;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import jp.riken.accc.db.rdf.crawler.dataStructure.SchemaCategory;
|
---|
9 | import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.JenaModelGenerator;
|
---|
10 | import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.RDFsCrawlerImpl;
|
---|
11 |
|
---|
12 | public class StructureCrawler {
|
---|
13 |
|
---|
14 | private File dataDir = null;
|
---|
15 |
|
---|
16 | public static void main(String[] args) throws Exception{
|
---|
17 | StructureCrawler sc = new StructureCrawler(new File("c:\\temp"));
|
---|
18 | sc.crawl("http://dbe-rdf.biosciencedbc.jp/sparql", "biosciencedbc.ttl");
|
---|
19 |
|
---|
20 | System.out.println("done");
|
---|
21 |
|
---|
22 |
|
---|
23 | Map<String,String> acTable = sc.getAcquiredStructureFiles();
|
---|
24 | Set<String> keySet = acTable.keySet();
|
---|
25 | for(String key: keySet){
|
---|
26 | String val = acTable.get(key);
|
---|
27 | System.out.println("File: " + key + " --- " + val);
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | public Map<String, String> getAcquiredStructureFiles(){
|
---|
34 | Map<String, String> table = new HashMap<String, String>();
|
---|
35 | if( dataDir.isDirectory() ){
|
---|
36 | // read files
|
---|
37 | File[] files = dataDir.listFiles();
|
---|
38 | for(File file: files){
|
---|
39 | String uri = null;
|
---|
40 | try{
|
---|
41 | JenaModelGenerator jmGene = new JenaModelGenerator(file.getAbsolutePath());
|
---|
42 | uri = jmGene.getEndpointURI();
|
---|
43 |
|
---|
44 | System.out.println("URI: "+ uri);
|
---|
45 | }catch(Exception ex){
|
---|
46 | //
|
---|
47 | }
|
---|
48 | if( uri != null ){
|
---|
49 | table.put(uri, file.getAbsolutePath());
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }else{
|
---|
53 | if( dataDir.isFile() ){
|
---|
54 | String uri = null;
|
---|
55 | try{
|
---|
56 | JenaModelGenerator jmGene = new JenaModelGenerator(dataDir.getAbsolutePath());
|
---|
57 | uri = jmGene.getEndpointURI();
|
---|
58 |
|
---|
59 | }catch(Exception ex){
|
---|
60 | //
|
---|
61 | }
|
---|
62 | if( uri != null ){
|
---|
63 | table.put(uri, dataDir.getAbsolutePath());
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return table;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | public StructureCrawler(File dataDir) throws Exception {
|
---|
73 | this.dataDir = dataDir;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void crawl(String endPURI, String outFileName) throws Exception {
|
---|
77 | RDFsCrawlerImpl impl = new RDFsCrawlerImpl(endPURI);
|
---|
78 | // Resource[] res = impl.getRDFProperties();
|
---|
79 | // Resource[] res = impl.getInferedRDFsClassesFromInstances();
|
---|
80 | // Resource[] res = impl.getDomainRangeDeclaredRDFProperties();
|
---|
81 | // Resource[] res = impl.getDeclaredRDFsClasses();
|
---|
82 | // for(Resource r: res){
|
---|
83 | // System.out.println(r.getURI().toString());
|
---|
84 | // }
|
---|
85 | // Model model = impl.getProperiesFromDomainRangeDecls();
|
---|
86 | // Model model = impl.getPropertiesFromInstanceDecls();
|
---|
87 | // RDFWriter writer = model.getWriter("N3");
|
---|
88 | // writer.setProperty("showXMLDeclaration","true");
|
---|
89 | // writer.write(model,System.out,"");
|
---|
90 | // model.close();
|
---|
91 |
|
---|
92 | SchemaCategory sc = impl.determineSchemaCategory();
|
---|
93 |
|
---|
94 | // RDF/XML, RDF/XML-ABBREV, N-TRIPLE, N3
|
---|
95 | File outFile = null;
|
---|
96 | if (outFileName == null) {
|
---|
97 | String tFileName = null;
|
---|
98 | if (endPURI.lastIndexOf("/", endPURI.length() - 2) > 0) {
|
---|
99 | tFileName = endPURI.substring(
|
---|
100 | endPURI.lastIndexOf("/", endPURI.length() - 2) + 1,
|
---|
101 | endPURI.length());
|
---|
102 | } else {
|
---|
103 | tFileName = endPURI;
|
---|
104 | }
|
---|
105 | outFile = new File(dataDir, tFileName);
|
---|
106 | if (outFile.exists()) {
|
---|
107 | outFile = File.createTempFile(tFileName, "", dataDir);
|
---|
108 | }
|
---|
109 | } else {
|
---|
110 | outFile = new File(dataDir, outFileName);
|
---|
111 | }
|
---|
112 | sc.write2File(outFile.getAbsolutePath(), "Turtle");
|
---|
113 | // System.out.println("Category:" + sc.getCategory());
|
---|
114 | }
|
---|
115 |
|
---|
116 | }
|
---|