root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/endpointMetadata/MetadataFile.java @ 205

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

CLServlet がラベル+インスタンス数を出力するように変更

行番号 
1package org.biohackathon.SPARQLBuilder.endpointMetadata;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Calendar;
6
7import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.URICollection;
8
9import org.apache.jena.riot.RDFDataMgr;
10
11import com.hp.hpl.jena.rdf.model.Literal;
12import com.hp.hpl.jena.rdf.model.Model;
13import com.hp.hpl.jena.rdf.model.NodeIterator;
14import com.hp.hpl.jena.rdf.model.Property;
15import com.hp.hpl.jena.rdf.model.RDFNode;
16import com.hp.hpl.jena.rdf.model.ResIterator;
17import com.hp.hpl.jena.rdf.model.Resource;
18
19public class MetadataFile {
20
21        Model model = null;
22        File path = null;
23        String endpointURI = null;
24        Calendar startDateTime = null;
25        Calendar endDateTime = null;
26        long numTriples = 0;
27        long numClasses = 0;
28       
29       
30        public long getNumTriples(){
31                return numTriples;
32        }
33       
34        public long getNumClasses(){
35                return numClasses;
36        }
37       
38        public MetadataFile(File file) throws Exception{
39                readFile(file);
40                this.path = file;
41        }
42
43        public String getPath(){
44                return path.getAbsolutePath();
45        }
46       
47        public String getEndpointURI(){
48                return endpointURI;
49        }
50
51        public Calendar getStartDateTime(){
52                return startDateTime;
53        }
54
55        public Calendar getEndDateTime(){
56                return endDateTime;
57        }
58       
59       
60        //TODO
61        public static boolean validate(File file) throws Exception{
62                Model tmpModel = RDFDataMgr.loadModel(file.getAbsolutePath());
63                String endpointURI = getEndpointURI(tmpModel);
64               
65                //TODO
66                return true;
67        }
68
69
70        public static Resource getDefaultDataset(Model model) throws Exception{
71                Property sd_default_dataset = model.getProperty(URICollection.PROPERTY_SD_DEFAULT_DATA_SET);
72                NodeIterator nit = model.listObjectsOfProperty(sd_default_dataset);
73                Resource defaultDataSet = null;
74                if( nit.hasNext() ){
75                        RDFNode node = nit.next();
76                        defaultDataSet = node.asResource();
77                }
78                return defaultDataSet;
79        }
80
81
82        public static long getTriples(Resource defaultDataset) throws Exception{
83                // numTriples
84                Model model = defaultDataset.getModel();
85                Property void_triples = model.getProperty(URICollection.PROPERTY_VOID_TRIPLES);
86                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, void_triples);
87                long numTriples = 0;
88                if( nit.hasNext()){
89                        Literal numTriplesLit = nit.next().asLiteral();
90                        numTriples = numTriplesLit.getLong();
91                }
92                return numTriples;
93        }
94       
95       
96        public static long getProperties(Resource defaultDataset) throws Exception{
97                Model model = defaultDataset.getModel();
98                Property void_properties = model.getProperty(URICollection.PROPERTY_VOID_PROPERTIES);
99                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, void_properties);
100                long numProperties = 0;
101                if( nit.hasNext()){
102                        Literal numPropertiesLit = nit.next().asLiteral();
103                        numProperties = numPropertiesLit.getLong();
104                }
105                return numProperties;
106        }
107       
108
109        public static long getClasses(Resource defaultDataset) throws Exception{
110                Model model = defaultDataset.getModel();
111                Property void_classes = model.getProperty(URICollection.PROPERTY_VOID_CLASSES);
112                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, void_classes);
113                long numClasses = 0;
114                if( nit.hasNext()){
115                        Literal numClassesLit = nit.next().asLiteral();
116                        numClasses = numClassesLit.getLong();
117                }
118                return numClasses;
119        }
120
121       
122        public static long getDatatypes(Resource defaultDataset) throws Exception{
123                Model model = defaultDataset.getModel();
124                Property sbm_datatypes = model.getProperty(URICollection.PROPERTY_SB_DATATYPES);
125                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, sbm_datatypes);
126                long numDatatypes = 0;
127                if( nit.hasNext()){
128                        Literal numDatatypesLit = nit.next().asLiteral();
129                        numDatatypes = numDatatypesLit.getLong();
130                }
131                return numDatatypes;
132        }
133
134       
135        public static long getEndpointCategory(Resource defaultDataset) throws Exception{
136                Model model = defaultDataset.getModel();
137                Property sbm_endpointCategory = model.getProperty(URICollection.PROPERTY_SB_ENDPOINT_CATEGORY);
138                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, sbm_endpointCategory);
139                long endpointCategory = 0;
140                if( nit.hasNext()){
141                        Literal endpointCategoryLit = nit.next().asLiteral();
142                        endpointCategory = endpointCategoryLit.getLong();
143                }
144                return endpointCategory;
145        }
146
147        public static long getPropertyategory(Resource defaultDataset) throws Exception{
148                Model model = defaultDataset.getModel();
149                Property sbm_propertyCategory = model.getProperty(URICollection.PROPERTY_SB_PROPERTY_CATEGORY);
150                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, sbm_propertyCategory);
151                long propertyCategory = 0;
152                if( nit.hasNext()){
153                        Literal propertyCategoryLit = nit.next().asLiteral();
154                        propertyCategory = propertyCategoryLit.getLong();
155                }
156                return propertyCategory;
157        }
158
159
160        public static long getClassCategory(Resource defaultDataset) throws Exception{
161                Model model = defaultDataset.getModel();
162                Property sbm_classCategory = model.getProperty(URICollection.PROPERTY_SB_CLASS_CATEGORY);
163                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, sbm_classCategory);
164                long classCategory = 0;
165                if( nit.hasNext()){
166                        Literal classCategoryLit = nit.next().asLiteral();
167                        classCategory = classCategoryLit.getLong();
168                }
169                return classCategory;
170        }
171
172       
173        public static Resource[] getPropertyPartitions(Resource defaultDataset) throws Exception{
174                Model model = defaultDataset.getModel();
175                Property void_propertyPartition = model.getProperty(URICollection.PROPERTY_VOID_PROPERTY_PARTITION);
176                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, void_propertyPartition);
177                ArrayList<Resource> propertyPartitions = new ArrayList<Resource>();
178                if( nit.hasNext()){
179                        Resource propertyPartition = nit.next().asResource();
180                        propertyPartitions.add(propertyPartition);
181                }
182                return propertyPartitions.toArray(new Resource[0]);
183        }
184
185        public static Resource[] getClassPartitions(Resource defaultDataset) throws Exception{
186                Model model = defaultDataset.getModel();
187                Property void_classPartition = model.getProperty(URICollection.PROPERTY_VOID_CLASS_PARTITION);
188                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, void_classPartition);
189                ArrayList<Resource> classPartitions = new ArrayList<Resource>();
190                if( nit.hasNext()){
191                        Resource classPartition = nit.next().asResource();
192                        classPartitions.add(classPartition);
193                }
194                return classPartitions.toArray(new Resource[0]);
195        }
196       
197        public static Resource[] getNamedGraphs(Resource defaultDataset) throws Exception{
198                Model model = defaultDataset.getModel();
199                Property sd_namedGraph = model.getProperty(URICollection.PROPERTY_SD_NAMED_GRAPH);
200                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, sd_namedGraph);
201                ArrayList<Resource> namedGraphs = new ArrayList<Resource>();
202                if( nit.hasNext()){
203                        Resource namedGraph = nit.next().asResource();
204                        namedGraphs.add(namedGraph);
205                }
206                return namedGraphs.toArray(new Resource[0]);
207        }
208       
209        public static String getName(Resource namedGraph) throws Exception{
210                Model model = namedGraph.getModel();
211                Property sd_name = model.getProperty(URICollection.PROPERTY_SD_NAME);
212                NodeIterator nit = model.listObjectsOfProperty(namedGraph, sd_name);
213                String name = null;
214                if( nit.hasNext()){
215                        Literal nameLit = nit.next().asLiteral();
216                        name = nameLit.getString();
217                }
218                return name;
219        }
220
221        public static Resource[] getPropertyCategorySubsets(Resource defaultDataset) throws Exception{
222                Model model = defaultDataset.getModel();
223                Property void_propertyCategorySubset = model.getProperty(URICollection.PROPERTY_SB_PROPERTY_CATEGORY_SUBSET);
224                NodeIterator nit = model.listObjectsOfProperty(defaultDataset, void_propertyCategorySubset);
225                ArrayList<Resource> propertyCategorySubsets = new ArrayList<Resource>();
226                if( nit.hasNext()){
227                        Resource propertyCategorySubset = nit.next().asResource();
228                        propertyCategorySubsets.add(propertyCategorySubset);
229                }
230                return propertyCategorySubsets.toArray(new Resource[0]);
231        }
232
233        public static Property[] getProperties(Model model) throws Exception{
234                Resource rdf_property = model.getResource(URICollection.CLASS_RDF_PROPERTY);
235                Property rdf_type = model.getProperty(URICollection.PROPERTY_RDF_TYPE);
236                ResIterator rit = model.listSubjectsWithProperty(rdf_type, rdf_property);
237                ArrayList<Resource> properties = new ArrayList<Resource>();
238                if( rit.hasNext()){
239                        String propertyURI  = rit.next().getURI();
240                        Property property = model.getProperty(propertyURI);
241                        properties.add(property);
242                }
243                return properties.toArray(new Property[0]);
244        }
245
246       
247        public static Resource[] getClasses(Model model) throws Exception{
248                Resource rdfs_class = model.getResource(URICollection.RESOURCE_RDFS_CLASS);
249                Property rdf_type = model.getProperty(URICollection.PROPERTY_RDF_TYPE);
250                ResIterator rit = model.listSubjectsWithProperty(rdf_type, rdfs_class);
251                ArrayList<Resource> classes = new ArrayList<Resource>();
252                if( rit.hasNext()){
253                        Resource classRes  = rit.next();
254                        classes.add(classRes);
255                }
256                return classes.toArray(new Resource[0]);
257        }
258
259        public static Resource[] getClassRelations(Resource propertyPartition) throws Exception{
260                Model model = propertyPartition.getModel();
261                Property sbm_classRelation = model.getProperty(URICollection.PROPERTY_SB_CLASS_RELATION);
262                NodeIterator nit = model.listObjectsOfProperty(propertyPartition, sbm_classRelation);
263                ArrayList<Resource> classRelations = new ArrayList<Resource>();
264                if( nit.hasNext()){
265                        Resource classRelation = nit.next().asResource();
266                        classRelations.add(classRelation);
267                }
268                return classRelations.toArray(new Resource[0]);
269        }
270       
271        public static String getEndpointURI(Model model) throws Exception{
272                Property sd_endpoint = model.getProperty(URICollection.PROPERTY_SD_ENDPOINT);
273                NodeIterator nit = model.listObjectsOfProperty(sd_endpoint);
274                Resource endPointRes = null;
275                String endpointURI = null;
276                if( nit.hasNext() ){
277                                RDFNode endPointNode = nit.next();
278                                endPointRes = endPointNode.asResource();
279                                endpointURI = endPointRes.getURI();
280                }
281                return endpointURI;
282        }
283
284       
285        private Resource getCrawlLogBlankNode(Resource defaultDataset) throws Exception {
286                Model model = defaultDataset.getModel();
287                // log
288                Property sbm_crawlLog = model.getProperty(URICollection.PROPERTY_SB_CRAWL_LOG);
289                NodeIterator nit = model.listObjectsOfProperty(sbm_crawlLog);
290                Resource crawlLogBlankNode = null;
291                if( nit.hasNext() ){
292                                RDFNode node = nit.next();
293                                crawlLogBlankNode = node.asResource();
294                }
295                return crawlLogBlankNode;
296        }
297
298        private Calendar getStartTime(Resource crawlLogBlankNode) throws Exception{
299                Model model = crawlLogBlankNode.getModel();
300                Property sbm_startTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_START_TIME);
301                NodeIterator nit = model.listObjectsOfProperty(sbm_startTime);
302                Calendar startCal = null;
303                if( nit.hasNext() ){
304                        Literal startTimeLit = null;
305                        RDFNode node = nit.next();
306                        startTimeLit = node.asLiteral();
307                        startCal = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(startTimeLit.getValue())).asCalendar();
308                }
309                return startCal;
310        }
311
312        private Calendar getEndTime(Resource crawlLogBlankNode) throws Exception{
313                Model model = crawlLogBlankNode.getModel();
314                Property sbm_endTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_END_TIME);
315                NodeIterator nit = model.listObjectsOfProperty(sbm_endTime);
316                Calendar endCal = null;
317                if( nit.hasNext() ){
318                        Literal endTimeLit = null;
319                        RDFNode node = nit.next();
320                        endTimeLit = node.asLiteral();
321                        endCal = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(endTimeLit.getValue())).asCalendar();
322                }
323                return endCal;
324        }
325       
326        private void readFile(File file) throws Exception{
327                System.out.println("readfile: " + file.getAbsolutePath());
328                model = RDFDataMgr.loadModel(file.getAbsolutePath());
329                Property sd_endpoint = model.getProperty(URICollection.PROPERTY_SD_ENDPOINT);
330                NodeIterator nit = model.listObjectsOfProperty(sd_endpoint);
331                Resource endPointRes = null;
332                endpointURI = null;
333                if( nit.hasNext() ){
334                                RDFNode endPointNode = nit.next();
335                                endPointRes = endPointNode.asResource();
336                                endpointURI = endPointRes.getURI();
337                }
338               
339                Property sd_default_dataset = model.getProperty(URICollection.PROPERTY_SD_DEFAULT_DATA_SET);
340                nit = model.listObjectsOfProperty(sd_default_dataset);
341                Resource defaultDataSet = null;
342                if( nit.hasNext() ){
343                        RDFNode node = nit.next();
344                        defaultDataSet = node.asResource();
345                }
346                // log
347                Property sbm_crawlLog = model.getProperty(URICollection.PROPERTY_SB_CRAWL_LOG);
348                nit = model.listObjectsOfProperty(sbm_crawlLog);
349                Resource crawlLogBlankNode = null;
350                if( nit.hasNext() ){
351                                RDFNode node = nit.next();
352                                crawlLogBlankNode = node.asResource();
353                }
354                // start
355                Property sbm_startTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_START_TIME);
356                nit = model.listObjectsOfProperty(sbm_startTime);
357                startDateTime = null;
358                if( nit.hasNext() ){
359                        Literal startTimeLit = null;
360                        RDFNode node = nit.next();
361                        startTimeLit = node.asLiteral();
362                        startDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(startTimeLit.getValue())).asCalendar();
363                }
364                // end
365                Property sbm_endTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_END_TIME);
366                nit = model.listObjectsOfProperty(sbm_endTime);
367                endDateTime = null;
368                if( nit.hasNext() ){
369                        Literal endTimeLit = null;
370                        RDFNode node = nit.next();
371                        endTimeLit = node.asLiteral();
372                        endDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(endTimeLit.getValue())).asCalendar();
373                }
374                // numTriples
375                Property void_triples = model.getProperty(URICollection.PROPERTY_VOID_TRIPLES);
376                nit = model.listObjectsOfProperty(defaultDataSet, void_triples);
377                if( nit.hasNext()){
378                        Literal numTriplesLit = nit.next().asLiteral();
379                        numTriples = numTriplesLit.getLong();
380                }
381                // numClasses
382                Property void_classes = model.getProperty(URICollection.PROPERTY_VOID_CLASSES);
383                nit = model.listObjectsOfProperty(defaultDataSet, void_classes);
384                if( nit.hasNext()){
385                        Literal numClassesLit = nit.next().asLiteral();
386                        numClasses = numClassesLit.getLong();
387                }
388        }
389
390}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。