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://data.allie.dbcls.jp/sparql", "allie.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 | }catch(Exception ex){
|
---|
44 | //
|
---|
45 | }
|
---|
46 | if( uri != null ){
|
---|
47 | table.put(uri, file.getAbsolutePath());
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }else{
|
---|
51 | if( dataDir.isFile() ){
|
---|
52 | String uri = null;
|
---|
53 | try{
|
---|
54 | JenaModelGenerator jmGene = new JenaModelGenerator(dataDir.getAbsolutePath());
|
---|
55 | uri = jmGene.getEndpointURI();
|
---|
56 | }catch(Exception ex){
|
---|
57 | //
|
---|
58 | }
|
---|
59 | if( uri != null ){
|
---|
60 | table.put(uri, dataDir.getAbsolutePath());
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | return table;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | public StructureCrawler(File dataDir) throws Exception {
|
---|
70 | this.dataDir = dataDir;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void crawl(String endPURI, String outFileName) throws Exception {
|
---|
74 | RDFsCrawlerImpl impl = new RDFsCrawlerImpl(endPURI);
|
---|
75 | // Resource[] res = impl.getRDFProperties();
|
---|
76 | // Resource[] res = impl.getInferedRDFsClassesFromInstances();
|
---|
77 | // Resource[] res = impl.getDomainRangeDeclaredRDFProperties();
|
---|
78 | // Resource[] res = impl.getDeclaredRDFsClasses();
|
---|
79 | // for(Resource r: res){
|
---|
80 | // System.out.println(r.getURI().toString());
|
---|
81 | // }
|
---|
82 | // Model model = impl.getProperiesFromDomainRangeDecls();
|
---|
83 | // Model model = impl.getPropertiesFromInstanceDecls();
|
---|
84 | // RDFWriter writer = model.getWriter("N3");
|
---|
85 | // writer.setProperty("showXMLDeclaration","true");
|
---|
86 | // writer.write(model,System.out,"");
|
---|
87 | // model.close();
|
---|
88 |
|
---|
89 | SchemaCategory sc = impl.determineSchemaCategory();
|
---|
90 |
|
---|
91 | // RDF/XML, RDF/XML-ABBREV, N-TRIPLE, N3
|
---|
92 | File outFile = null;
|
---|
93 | if (outFileName == null) {
|
---|
94 | String tFileName = null;
|
---|
95 | if (endPURI.lastIndexOf("/", endPURI.length() - 2) > 0) {
|
---|
96 | tFileName = endPURI.substring(
|
---|
97 | endPURI.lastIndexOf("/", endPURI.length() - 2) + 1,
|
---|
98 | endPURI.length());
|
---|
99 | } else {
|
---|
100 | tFileName = endPURI;
|
---|
101 | }
|
---|
102 | outFile = new File(dataDir, tFileName);
|
---|
103 | if (outFile.exists()) {
|
---|
104 | outFile = File.createTempFile(tFileName, "", dataDir);
|
---|
105 | }
|
---|
106 | } else {
|
---|
107 | outFile = new File(dataDir, outFileName);
|
---|
108 | }
|
---|
109 | sc.write2File(outFile.getAbsolutePath(), "Turtle");
|
---|
110 | // System.out.println("Category:" + sc.getCategory());
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|