[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;
|
---|
| 22 | String endpointURI = null;
|
---|
| 23 | Calendar startDateTime = null;
|
---|
| 24 | Calendar endDateTime = null;
|
---|
[163] | 25 | long numTriples = 0;
|
---|
| 26 | long numClasses = 0;
|
---|
[158] | 27 |
|
---|
[163] | 28 |
|
---|
| 29 | public long getNumTriples(){
|
---|
| 30 | return numTriples;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public long getNumClasses(){
|
---|
| 34 | return numClasses;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[158] | 37 | public MetadataFile(File file) throws Exception{
|
---|
| 38 | readFile(file);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[160] | 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 |
|
---|
[176] | 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 | //TODO
|
---|
| 255 | return null;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | public static String getEndpointURI(Model model) throws Exception{
|
---|
| 259 | Property sd_endpoint = model.getProperty(URICollection.PROPERTY_SD_ENDPOINT);
|
---|
| 260 | NodeIterator nit = model.listObjectsOfProperty(sd_endpoint);
|
---|
| 261 | Resource endPointRes = null;
|
---|
| 262 | String endpointURI = null;
|
---|
| 263 | if( nit.hasNext() ){
|
---|
| 264 | RDFNode endPointNode = nit.next();
|
---|
| 265 | endPointRes = endPointNode.asResource();
|
---|
| 266 | endpointURI = endPointRes.getURI();
|
---|
| 267 | }
|
---|
| 268 | return endpointURI;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 | private Resource getCrawlLogBlankNode(Resource defaultDataset) throws Exception {
|
---|
| 273 | Model model = defaultDataset.getModel();
|
---|
| 274 | // log
|
---|
| 275 | Property sbm_crawlLog = model.getProperty(URICollection.PROPERTY_SB_CRAWL_LOG);
|
---|
| 276 | NodeIterator nit = model.listObjectsOfProperty(sbm_crawlLog);
|
---|
| 277 | Resource crawlLogBlankNode = null;
|
---|
| 278 | if( nit.hasNext() ){
|
---|
| 279 | RDFNode node = nit.next();
|
---|
| 280 | crawlLogBlankNode = node.asResource();
|
---|
| 281 | }
|
---|
| 282 | return crawlLogBlankNode;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | private Calendar getStartTime(Resource crawlLogBlankNode) throws Exception{
|
---|
| 286 | Model model = crawlLogBlankNode.getModel();
|
---|
| 287 | Property sbm_startTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_START_TIME);
|
---|
| 288 | NodeIterator nit = model.listObjectsOfProperty(sbm_startTime);
|
---|
| 289 | Calendar startCal = null;
|
---|
| 290 | if( nit.hasNext() ){
|
---|
| 291 | Literal startTimeLit = null;
|
---|
| 292 | RDFNode node = nit.next();
|
---|
| 293 | startTimeLit = node.asLiteral();
|
---|
| 294 | startCal = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(startTimeLit.getValue())).asCalendar();
|
---|
| 295 | }
|
---|
| 296 | return startCal;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | private Calendar getEndTime(Resource crawlLogBlankNode) throws Exception{
|
---|
| 300 | Model model = crawlLogBlankNode.getModel();
|
---|
| 301 | Property sbm_endTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_END_TIME);
|
---|
| 302 | NodeIterator nit = model.listObjectsOfProperty(sbm_endTime);
|
---|
| 303 | Calendar endCal = null;
|
---|
| 304 | if( nit.hasNext() ){
|
---|
| 305 | Literal endTimeLit = null;
|
---|
| 306 | RDFNode node = nit.next();
|
---|
| 307 | endTimeLit = node.asLiteral();
|
---|
| 308 | endCal = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(endTimeLit.getValue())).asCalendar();
|
---|
| 309 | }
|
---|
| 310 | return endCal;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 |
|
---|
| 314 |
|
---|
| 315 |
|
---|
[158] | 316 | private void readFile(File file) throws Exception{
|
---|
[160] | 317 | model = RDFDataMgr.loadModel(file.getAbsolutePath());
|
---|
| 318 | Property sd_endpoint = model.getProperty(URICollection.PROPERTY_SD_ENDPOINT);
|
---|
| 319 | NodeIterator nit = model.listObjectsOfProperty(sd_endpoint);
|
---|
| 320 | Resource endPointRes = null;
|
---|
| 321 | endpointURI = null;
|
---|
| 322 | if( nit.hasNext() ){
|
---|
| 323 | RDFNode endPointNode = nit.next();
|
---|
| 324 | endPointRes = endPointNode.asResource();
|
---|
| 325 | endpointURI = endPointRes.getURI();
|
---|
| 326 | }
|
---|
[158] | 327 |
|
---|
[160] | 328 | Property sd_default_dataset = model.getProperty(URICollection.PROPERTY_SD_DEFAULT_DATA_SET);
|
---|
| 329 | nit = model.listObjectsOfProperty(sd_default_dataset);
|
---|
| 330 | Resource defaultDataSet = null;
|
---|
| 331 | if( nit.hasNext() ){
|
---|
| 332 | RDFNode node = nit.next();
|
---|
| 333 | defaultDataSet = node.asResource();
|
---|
| 334 | }
|
---|
| 335 | // log
|
---|
| 336 | Property sbm_crawlLog = model.getProperty(URICollection.PROPERTY_SB_CRAWL_LOG);
|
---|
| 337 | nit = model.listObjectsOfProperty(sbm_crawlLog);
|
---|
| 338 | Resource crawlLogBlankNode = null;
|
---|
| 339 | if( nit.hasNext() ){
|
---|
| 340 | RDFNode node = nit.next();
|
---|
| 341 | crawlLogBlankNode = node.asResource();
|
---|
| 342 | }
|
---|
| 343 | // start
|
---|
| 344 | Property sbm_startTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_START_TIME);
|
---|
| 345 | nit = model.listObjectsOfProperty(sbm_startTime);
|
---|
| 346 | startDateTime = null;
|
---|
| 347 | if( nit.hasNext() ){
|
---|
| 348 | Literal startTimeLit = null;
|
---|
| 349 | RDFNode node = nit.next();
|
---|
| 350 | startTimeLit = node.asLiteral();
|
---|
| 351 | startDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(startTimeLit.getValue())).asCalendar();
|
---|
| 352 | }
|
---|
| 353 | // end
|
---|
| 354 | Property sbm_endTime = model.getProperty(URICollection.PROPERTY_SB_CRAWL_END_TIME);
|
---|
| 355 | nit = model.listObjectsOfProperty(sbm_endTime);
|
---|
| 356 | endDateTime = null;
|
---|
| 357 | if( nit.hasNext() ){
|
---|
| 358 | Literal endTimeLit = null;
|
---|
| 359 | RDFNode node = nit.next();
|
---|
| 360 | endTimeLit = node.asLiteral();
|
---|
| 361 | endDateTime = ((com.hp.hpl.jena.datatypes.xsd.XSDDateTime)(endTimeLit.getValue())).asCalendar();
|
---|
| 362 | }
|
---|
[163] | 363 | // numTriples
|
---|
| 364 | Property void_triples = model.getProperty(URICollection.PROPERTY_VOID_TRIPLES);
|
---|
| 365 | nit = model.listObjectsOfProperty(defaultDataSet, void_triples);
|
---|
| 366 | if( nit.hasNext()){
|
---|
| 367 | Literal numTriplesLit = nit.next().asLiteral();
|
---|
| 368 | numTriples = numTriplesLit.getLong();
|
---|
| 369 | }
|
---|
| 370 | // numClasses
|
---|
| 371 | Property void_classes = model.getProperty(URICollection.PROPERTY_VOID_CLASSES);
|
---|
| 372 | nit = model.listObjectsOfProperty(defaultDataSet, void_classes);
|
---|
| 373 | if( nit.hasNext()){
|
---|
| 374 | Literal numClassesLit = nit.next().asLiteral();
|
---|
| 375 | numClasses = numClassesLit.getLong();
|
---|
| 376 | }
|
---|
[158] | 377 | }
|
---|
| 378 |
|
---|
| 379 | }
|
---|