[158] | 1 | package org.biohackathon.SPARQLBuilder.endpointMetadata;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
[176] | 4 | import java.util.ArrayList;
|
---|
[160] | 5 | import java.util.Calendar;
|
---|
[158] | 6 |
|
---|
[160] | 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;
|
---|
[176] | 16 | import com.hp.hpl.jena.rdf.model.ResIterator;
|
---|
[160] | 17 | import com.hp.hpl.jena.rdf.model.Resource;
|
---|
| 18 |
|
---|
[158] | 19 | public class MetadataFile {
|
---|
| 20 |
|
---|
[160] | 21 | Model model = null;
|
---|
[190] | 22 | File path = null;
|
---|
[160] | 23 | String endpointURI = null;
|
---|
| 24 | Calendar startDateTime = null;
|
---|
| 25 | Calendar endDateTime = null;
|
---|
[163] | 26 | long numTriples = 0;
|
---|
| 27 | long numClasses = 0;
|
---|
[158] | 28 |
|
---|
[163] | 29 |
|
---|
| 30 | public long getNumTriples(){
|
---|
| 31 | return numTriples;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public long getNumClasses(){
|
---|
| 35 | return numClasses;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[158] | 38 | public MetadataFile(File file) throws Exception{
|
---|
| 39 | readFile(file);
|
---|
[190] | 40 | this.path = file;
|
---|
[158] | 41 | }
|
---|
| 42 |
|
---|
[191] | 43 | public String getFilePath(){
|
---|
[190] | 44 | return path.getAbsolutePath();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[160] | 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 |
|
---|
[176] | 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{
|
---|
[180] | 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]);
|
---|
[176] | 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 |
|
---|
| 327 |
|
---|
| 328 |
|
---|
[158] | 329 | private void readFile(File file) throws Exception{
|
---|
[187] | 330 | System.out.println("readfile: " + file.getAbsolutePath());
|
---|
[160] | 331 | model = RDFDataMgr.loadModel(file.getAbsolutePath());
|
---|
| 332 | Property sd_endpoint = model.getProperty(URICollection.PROPERTY_SD_ENDPOINT);
|
---|
| 333 | NodeIterator nit = model.listObjectsOfProperty(sd_endpoint);
|
---|
| 334 | Resource endPointRes = null;
|
---|
| 335 | endpointURI = null;
|
---|
| 336 | if( nit.hasNext() ){
|
---|
| 337 | RDFNode endPointNode = nit.next();
|
---|
| 338 | endPointRes = endPointNode.asResource();
|
---|
| 339 | endpointURI = endPointRes.getURI();
|
---|
| 340 | }
|
---|
[158] | 341 |
|
---|
[160] | 342 | Property sd_default_dataset = model.getProperty(URICollection.PROPERTY_SD_DEFAULT_DATA_SET);
|
---|
| 343 | nit = model.listObjectsOfProperty(sd_default_dataset);
|
---|
| 344 | Resource defaultDataSet = null;
|
---|
| 345 | if( nit.hasNext() ){
|
---|
| 346 | RDFNode node = nit.next();
|
---|
| 347 | defaultDataSet = node.asResource();
|
---|
| 348 | }
|
---|
| 349 | // log
|
---|
| 350 | Property sbm_crawlLog = model.getProperty(URICollection.PROPERTY_SB_CRAWL_LOG);
|
---|
| 351 | nit = model.listObjectsOfProperty(sbm_crawlLog);
|
---|
| 352 | Resource crawlLogBlankNode = null;
|
---|
| 353 | if( nit.hasNext() ){
|
---|
| 354 | RDFNode node = nit.next();
|
---|
| 355 | crawlLogBlankNode = node.asResource();
|
---|
| 356 | }
|
---|
| 357 | // start
|
---|
| 358 | Property sbm_startTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_START_TIME);
|
---|
| 359 | nit = model.listObjectsOfProperty(sbm_startTime);
|
---|
| 360 | startDateTime = null;
|
---|
| 361 | if( nit.hasNext() ){
|
---|
| 362 | Literal startTimeLit = null;
|
---|
| 363 | RDFNode node = nit.next();
|
---|
| 364 | startTimeLit = node.asLiteral();
|
---|
| 365 | startDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(startTimeLit.getValue())).asCalendar();
|
---|
| 366 | }
|
---|
| 367 | // end
|
---|
| 368 | Property sbm_endTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_END_TIME);
|
---|
| 369 | nit = model.listObjectsOfProperty(sbm_endTime);
|
---|
| 370 | endDateTime = null;
|
---|
| 371 | if( nit.hasNext() ){
|
---|
| 372 | Literal endTimeLit = null;
|
---|
| 373 | RDFNode node = nit.next();
|
---|
| 374 | endTimeLit = node.asLiteral();
|
---|
| 375 | endDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(endTimeLit.getValue())).asCalendar();
|
---|
| 376 | }
|
---|
[163] | 377 | // numTriples
|
---|
| 378 | Property void_triples = model.getProperty(URICollection.PROPERTY_VOID_TRIPLES);
|
---|
| 379 | nit = model.listObjectsOfProperty(defaultDataSet, void_triples);
|
---|
| 380 | if( nit.hasNext()){
|
---|
| 381 | Literal numTriplesLit = nit.next().asLiteral();
|
---|
| 382 | numTriples = numTriplesLit.getLong();
|
---|
| 383 | }
|
---|
| 384 | // numClasses
|
---|
| 385 | Property void_classes = model.getProperty(URICollection.PROPERTY_VOID_CLASSES);
|
---|
| 386 | nit = model.listObjectsOfProperty(defaultDataSet, void_classes);
|
---|
| 387 | if( nit.hasNext()){
|
---|
| 388 | Literal numClassesLit = nit.next().asLiteral();
|
---|
| 389 | numClasses = numClassesLit.getLong();
|
---|
| 390 | }
|
---|
[158] | 391 | }
|
---|
| 392 |
|
---|
| 393 | }
|
---|