1 | package org.biohackathon.SPARQLBuilder.endpointMetadata;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Calendar;
|
---|
6 |
|
---|
7 | import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.URICollection;
|
---|
8 |
|
---|
9 | import org.apache.jena.riot.RDFDataMgr;
|
---|
10 |
|
---|
11 | import com.hp.hpl.jena.rdf.model.Literal;
|
---|
12 | import com.hp.hpl.jena.rdf.model.Model;
|
---|
13 | import com.hp.hpl.jena.rdf.model.NodeIterator;
|
---|
14 | import com.hp.hpl.jena.rdf.model.Property;
|
---|
15 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
---|
16 | import com.hp.hpl.jena.rdf.model.ResIterator;
|
---|
17 | import com.hp.hpl.jena.rdf.model.Resource;
|
---|
18 |
|
---|
19 | public 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 | System.out.println("readfile: " + file.getAbsolutePath());
|
---|
325 | model = RDFDataMgr.loadModel(file.getAbsolutePath());
|
---|
326 | Property sd_endpoint = model.getProperty(URICollection.PROPERTY_SD_ENDPOINT);
|
---|
327 | NodeIterator nit = model.listObjectsOfProperty(sd_endpoint);
|
---|
328 | Resource endPointRes = null;
|
---|
329 | endpointURI = null;
|
---|
330 | if( nit.hasNext() ){
|
---|
331 | RDFNode endPointNode = nit.next();
|
---|
332 | endPointRes = endPointNode.asResource();
|
---|
333 | endpointURI = endPointRes.getURI();
|
---|
334 | }
|
---|
335 |
|
---|
336 | Property sd_default_dataset = model.getProperty(URICollection.PROPERTY_SD_DEFAULT_DATA_SET);
|
---|
337 | nit = model.listObjectsOfProperty(sd_default_dataset);
|
---|
338 | Resource defaultDataSet = null;
|
---|
339 | if( nit.hasNext() ){
|
---|
340 | RDFNode node = nit.next();
|
---|
341 | defaultDataSet = node.asResource();
|
---|
342 | }
|
---|
343 | // log
|
---|
344 | Property sbm_crawlLog = model.getProperty(URICollection.PROPERTY_SB_CRAWL_LOG);
|
---|
345 | nit = model.listObjectsOfProperty(sbm_crawlLog);
|
---|
346 | Resource crawlLogBlankNode = null;
|
---|
347 | if( nit.hasNext() ){
|
---|
348 | RDFNode node = nit.next();
|
---|
349 | crawlLogBlankNode = node.asResource();
|
---|
350 | }
|
---|
351 | // start
|
---|
352 | Property sbm_startTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_START_TIME);
|
---|
353 | nit = model.listObjectsOfProperty(sbm_startTime);
|
---|
354 | startDateTime = null;
|
---|
355 | if( nit.hasNext() ){
|
---|
356 | Literal startTimeLit = null;
|
---|
357 | RDFNode node = nit.next();
|
---|
358 | startTimeLit = node.asLiteral();
|
---|
359 | startDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(startTimeLit.getValue())).asCalendar();
|
---|
360 | }
|
---|
361 | // end
|
---|
362 | Property sbm_endTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_END_TIME);
|
---|
363 | nit = model.listObjectsOfProperty(sbm_endTime);
|
---|
364 | endDateTime = null;
|
---|
365 | if( nit.hasNext() ){
|
---|
366 | Literal endTimeLit = null;
|
---|
367 | RDFNode node = nit.next();
|
---|
368 | endTimeLit = node.asLiteral();
|
---|
369 | endDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(endTimeLit.getValue())).asCalendar();
|
---|
370 | }
|
---|
371 | // numTriples
|
---|
372 | Property void_triples = model.getProperty(URICollection.PROPERTY_VOID_TRIPLES);
|
---|
373 | nit = model.listObjectsOfProperty(defaultDataSet, void_triples);
|
---|
374 | if( nit.hasNext()){
|
---|
375 | Literal numTriplesLit = nit.next().asLiteral();
|
---|
376 | numTriples = numTriplesLit.getLong();
|
---|
377 | }
|
---|
378 | // numClasses
|
---|
379 | Property void_classes = model.getProperty(URICollection.PROPERTY_VOID_CLASSES);
|
---|
380 | nit = model.listObjectsOfProperty(defaultDataSet, void_classes);
|
---|
381 | if( nit.hasNext()){
|
---|
382 | Literal numClassesLit = nit.next().asLiteral();
|
---|
383 | numClasses = numClassesLit.getLong();
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | }
|
---|