root/BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/endpointMetadata/MetadataFile.java @ 186

リビジョン 180, 13.8 KB (コミッタ: nori, 10 年 前)

古いクロールファイルを読み込まないよう、時間判定を行うようにした

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