root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/OWL/QueryPathGenerator.java @ 228

リビジョン 228, 4.9 KB (コミッタ: atsuko, 10 年 前)

データ増加に伴い効率性を多少向上

行番号 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7package org.biohackathon.SPARQLBuilder.OWL;
8
9
10/**
11 *
12 * @author atsuko
13 */
14
15public class QueryPathGenerator {
16    private String sparqlEndpoint = null;
17    private RDFSchemaAnalyzerFactory factory = null;
18    private RDFSchemaAnalyzer analyzer = null;
19    private OWLClassGraph graph;
20   
21    private static final String CDIR = "cdata";
22   
23    /*
24    public static void main(String[] args){
25        // For Test
26        //String sp = "http://data.allie.dbcls.jp/sparql";
27        //String sc = "http://purl.org/allie/ontology/201108#ShortForm";
28        //String ec = "http://purl.org/allie/ontology/201108#LongForm";
29
30        String sp = "http://www.ebi.ac.uk/rdf/services/chembl/sparql";
31        String sc = "http://rdf.ebi.ac.uk/terms/chembl#Enzyme";
32        String ec = "http://rdf.ebi.ac.uk/terms/chembl#Activity";
33
34        //String sp = "http://www.ebi.ac.uk/rdf/services/biosamples/sparql";
35        //String sc = "http://rdf.ebi.ac.uk/terms/biosd/Sample";
36        //String ec = "http://purl.obolibrary.org/obo/NCBITaxon_7955";
37        //String sp = "http://www.ebi.ac.uk/rdf/services/biosamples/sparql";
38        //String sc = "http://rdf.ebi.ac.uk/terms/biosd/Sample";
39        //String ec = "http://purl.obolibrary.org/obo/NCBITaxon_7955";
40        //String sp = "http://lsd.dbcls.jp/sparql";
41        //String sc = "http://purl.jp/bio/10/lsd/ontology/201209#EnglishCode";
42        //String ec = "http://purl.jp/bio/10/lsd/ontology/201209#JapaneseCode";
43        //QueryPathGenerator qpg = new QueryPathGenerator(sp, "c:\\cdata");
44        QueryPathGenerator qpg1 = new QueryPathGenerator(sp, "cdata/");
45        //qpg1.testOWLClassGraph();
46        //SClass[] cl = qpg.getClasses(null);
47    }*/
48   
49    public QueryPathGenerator(){
50        factory = new RDFSchemaAnalyzerFactory(CDIR);
51    }
52   
53    public QueryPathGenerator(String sparqlEndpoint){
54        factory = new RDFSchemaAnalyzerFactory(CDIR);
55        setSPARQLendpoint(sparqlEndpoint);
56        setOWLClassGraph();
57    }
58
59    public QueryPathGenerator(String sparqlEndpoint, String crawlFileName){
60        factory = new RDFSchemaAnalyzerFactory(crawlFileName);
61        setSPARQLendpoint(sparqlEndpoint);
62        setOWLClassGraph();
63    }
64   
65    public SClass[] getClasses(String keyword){
66        String[] keywords = null;
67        if ( keyword != null ){
68            if ( keyword.length() != 0 ){
69                keywords = new String[1];
70                keywords[0] = keyword;
71            }
72        }
73        try {
74            return analyzer.getOWLClasses(null, keywords, null, true);
75        }catch(Exception e){
76            System.err.println(e);
77            return null;
78        }
79    }
80   
81    public Path[] getPaths(String startClass, String endClass, boolean countLink){
82        if ( graph == null ){
83            setOWLClassGraph();
84        }
85        graph.setPartClassGraph(analyzer, startClass);
86        return graph.getPaths(startClass, endClass);
87        //return graph.getPaths(startClass, endClass, analyzer);
88        //return graph.getPaths_old(analyzer, true, startClass, endClass);
89    }
90   
91    public void setSPARQLendpoint(String sparqlEndpoint){
92        this.sparqlEndpoint = sparqlEndpoint;
93        setAnalyzer();
94    }
95   
96    public RDFSchemaAnalyzerFactory getFactory(){
97        return factory;
98    }
99
100    private void setAnalyzer(){
101        try {
102            analyzer = factory.create(sparqlEndpoint);
103        } catch (Exception e) {
104            System.err.println(e);
105        }
106    }
107   
108    public static String getClassLabelfromList(String classURI, SClass[] classes){
109        if ( classURI == null ){
110                    return "";
111        }
112        SClass sclass = null;
113        for ( int i = 0 ; i < classes.length; i++ ){
114            if ( classURI.equals(classes[i].getClassURI()) ){
115                return getClassLabelfromClass(classes[i]);
116            }                   
117        }
118        return "";
119    }
120   
121    public static String getClassLabelfromClass(SClass sclass){
122        Label[] labels = sclass.getLabels();
123        for ( int i = 0 ; i < labels.length; i++ ){
124            if ( labels[i].getLanguage() == null ){
125                return labels[i].getLabel();
126            }else if ( labels[i].getLanguage().equals("en") ){
127                return labels[i].getLabel();
128            }
129        }
130        String[] url = sclass.getClassURI().split("/");
131        String tmplabel = url[url.length-1];
132        String[] tmplabel2 = tmplabel.split("#");
133        String label = tmplabel2[tmplabel2.length-1];
134        return label;
135    }
136   
137    private void setOWLClassGraph(){
138        graph = new OWLClassGraph(analyzer);
139    }
140   
141    public OWLClassGraph getOWLClassGraph(){
142        if ( graph == null ){
143            graph = new OWLClassGraph(analyzer);             
144        }
145        return graph;
146    }
147}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。