| 1 | package org.biohackathon.SPARQLBuilder.OWL;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.*;
|
|---|
| 4 |
|
|---|
| 5 | import org.biohackathon.SPARQLBuilder.endpointMetadata.MetadataManager;
|
|---|
| 6 |
|
|---|
| 7 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.JenaModelGenerator;
|
|---|
| 8 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.URICollection;
|
|---|
| 9 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.ClassPartition;
|
|---|
| 10 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.ClassRelation;
|
|---|
| 11 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.CrawledMetadata;
|
|---|
| 12 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.Dataset;
|
|---|
| 13 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.Label;
|
|---|
| 14 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.PropertyPartition;
|
|---|
| 15 |
|
|---|
| 16 | import com.hp.hpl.jena.query.Query;
|
|---|
| 17 | import com.hp.hpl.jena.query.QueryExecution;
|
|---|
| 18 | import com.hp.hpl.jena.query.QueryExecutionFactory;
|
|---|
| 19 | import com.hp.hpl.jena.query.QueryFactory;
|
|---|
| 20 | import com.hp.hpl.jena.query.QuerySolution;
|
|---|
| 21 | import com.hp.hpl.jena.query.ResultSet;
|
|---|
| 22 | import com.hp.hpl.jena.rdf.model.Literal;
|
|---|
| 23 | import com.hp.hpl.jena.rdf.model.Model;
|
|---|
| 24 | import com.hp.hpl.jena.rdf.model.Property;
|
|---|
| 25 | import com.hp.hpl.jena.rdf.model.Resource;
|
|---|
| 26 |
|
|---|
| 27 | //public class OWLQueryBuilderForCrawlerImpl implements OWLQueryBuilder {
|
|---|
| 28 | public class AcquiredStructureAnalyzer implements RDFSchemaAnalyzer {
|
|---|
| 29 |
|
|---|
| 30 | // key: endpointURI, value: crawled metadata
|
|---|
| 31 | private HashMap<String, CrawledMetadata> crawledMetadataTable= null;
|
|---|
| 32 |
|
|---|
| 33 | public String[] getEndpointURIs(){
|
|---|
| 34 | if( crawledMetadataTable == null ){
|
|---|
| 35 | return new String[0];
|
|---|
| 36 | }else{
|
|---|
| 37 | return crawledMetadataTable.keySet().toArray(new String[0]);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public String[] getGraphURIs(String endpointURI){
|
|---|
| 42 | if( crawledMetadataTable == null ){
|
|---|
| 43 | return new String[0];
|
|---|
| 44 | }else{
|
|---|
| 45 | CrawledMetadata crawledMetadata = crawledMetadataTable.get(endpointURI);
|
|---|
| 46 | if( crawledMetadata == null ){
|
|---|
| 47 | return new String[0];
|
|---|
| 48 | }else{
|
|---|
| 49 | return crawledMetadata.getGraphURIs();
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | public AcquiredStructureAnalyzer(MetadataManager metadataManager){
|
|---|
| 56 | CrawledMetadata[] cmList = metadataManager.getCrawlerMetadataList();
|
|---|
| 57 | crawledMetadataTable = new HashMap<String, CrawledMetadata>();
|
|---|
| 58 |
|
|---|
| 59 | if( cmList != null ){
|
|---|
| 60 | for(CrawledMetadata cm: cmList){
|
|---|
| 61 | String endpointURI = cm.getEndpointURI();
|
|---|
| 62 | crawledMetadataTable.put(endpointURI, cm);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | public SClass[] listClasses() throws Exception{
|
|---|
| 70 | return getOWLClasses(null, null);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | public SClass[] getOWLClasses(String[] keywords, String language) throws Exception{
|
|---|
| 75 | return getOWLClassList(keywords, language).toArray(new SClass[0]);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | public List<SClass> getOWLClassList(String[] keywords, String language) throws Exception{
|
|---|
| 80 | ArrayList<SClass> classList = new ArrayList<SClass>();
|
|---|
| 81 |
|
|---|
| 82 | Set<String> endpointURISet = crawledMetadataTable.keySet();
|
|---|
| 83 | for(String endpointURI: endpointURISet){
|
|---|
| 84 | CrawledMetadata cm = crawledMetadataTable.get(endpointURI);
|
|---|
| 85 | // default
|
|---|
| 86 | Dataset dataset = cm.getDefaultDataset();
|
|---|
| 87 | List<SClass> tempClassList = getOWLClassList(dataset, keywords, language);
|
|---|
| 88 | for(SClass sClass: tempClassList){
|
|---|
| 89 | classList.add(sClass);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // graphs
|
|---|
| 93 | String[] graphURIs = cm.getGraphURIs();
|
|---|
| 94 | if( graphURIs != null ){
|
|---|
| 95 | for(String graphURI: graphURIs){
|
|---|
| 96 | dataset = cm.getDataset(graphURI);
|
|---|
| 97 | tempClassList = getOWLClassList(dataset, keywords, language);
|
|---|
| 98 | for(SClass sClass: tempClassList){
|
|---|
| 99 | classList.add(sClass);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | return classList;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | private List<SClass> getOWLClassList(Dataset dataset, String[] keywords, String language) throws Exception{
|
|---|
| 108 | ArrayList<SClass> results = new ArrayList<SClass>();
|
|---|
| 109 |
|
|---|
| 110 | ClassPartition[] classPartitionList = dataset.getClassPartitions();
|
|---|
| 111 | if( classPartitionList == null || classPartitionList.length == 0 ){
|
|---|
| 112 | return new ArrayList<SClass>();
|
|---|
| 113 | }
|
|---|
| 114 | for( ClassPartition cp: classPartitionList){
|
|---|
| 115 | String classURI = cp.classDef.classURI;
|
|---|
| 116 | Label[] rLabels = cp.classDef.labels;
|
|---|
| 117 | org.biohackathon.SPARQLBuilder.OWL.Label[] labels = null;
|
|---|
| 118 | if( rLabels == null ){
|
|---|
| 119 | labels = new org.biohackathon.SPARQLBuilder.OWL.Label[0];
|
|---|
| 120 | }else{
|
|---|
| 121 | labels = new org.biohackathon.SPARQLBuilder.OWL.Label[rLabels.length];
|
|---|
| 122 | for(int i = 0; i < rLabels.length; i++ ) {
|
|---|
| 123 | labels[i] = new org.biohackathon.SPARQLBuilder.OWL.Label(rLabels[i].value, rLabels[i].language);
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | int entities = cp.entities;
|
|---|
| 127 |
|
|---|
| 128 | if( keywords == null || keywords.length == 0 ){
|
|---|
| 129 | SClass sClass = new SClass(classURI, labels, entities);
|
|---|
| 130 | results.add(sClass);
|
|---|
| 131 | }else{
|
|---|
| 132 | boolean hit = false;
|
|---|
| 133 | for(org.biohackathon.SPARQLBuilder.OWL.Label label: labels){
|
|---|
| 134 | if( language == null || label.getLanguage().equals(language)){
|
|---|
| 135 | String value = label.getLabel();
|
|---|
| 136 | if( value != null ){
|
|---|
| 137 | value = value.toLowerCase().trim();
|
|---|
| 138 | for(String keyword: keywords){
|
|---|
| 139 | if( value.contains(keyword.toLowerCase().trim())){
|
|---|
| 140 | hit = true;
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | if( hit ){
|
|---|
| 147 | SClass sClass = new SClass(classURI, labels, entities);
|
|---|
| 148 | results.add(sClass);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | return results;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | public ClassLink[] getNextClass(String originClass, int limit) throws Exception{
|
|---|
| 161 |
|
|---|
| 162 | ArrayList<ClassLink> classLinkList = new ArrayList<ClassLink>();
|
|---|
| 163 |
|
|---|
| 164 | Set<String> endpointURISet = crawledMetadataTable.keySet();
|
|---|
| 165 | for(String endpointURI: endpointURISet){
|
|---|
| 166 | CrawledMetadata cm = crawledMetadataTable.get(endpointURI);
|
|---|
| 167 | // default
|
|---|
| 168 | Dataset dataset = cm.getDefaultDataset();
|
|---|
| 169 | List<ClassLink> tempClassLinkList = getNextClass(endpointURI, null, dataset, originClass, limit);
|
|---|
| 170 | for(ClassLink classLink: tempClassLinkList){
|
|---|
| 171 | classLinkList.add(classLink);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | // graphs
|
|---|
| 175 | String[] graphURIs = cm.getGraphURIs();
|
|---|
| 176 | if( graphURIs != null ){
|
|---|
| 177 | for(String graphURI: graphURIs){
|
|---|
| 178 | dataset = cm.getDataset(graphURI);
|
|---|
| 179 | tempClassLinkList = getNextClass(endpointURI, graphURI, dataset, originClass, limit);
|
|---|
| 180 | for(ClassLink classLink: tempClassLinkList){
|
|---|
| 181 | classLinkList.add(classLink);
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | return classLinkList.toArray(new ClassLink[0]);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 | private List<ClassLink> getNextClass(String endpointURI, String graphURI, Dataset dataset, String originClass, int limit) throws Exception{
|
|---|
| 192 | ArrayList<ClassLink> classLinkList = new ArrayList<ClassLink>();
|
|---|
| 193 |
|
|---|
| 194 | PropertyPartition[] pps = dataset.getPropertyPartitions();
|
|---|
| 195 | if( pps == null ){
|
|---|
| 196 | return classLinkList;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | for(PropertyPartition pp: pps){
|
|---|
| 200 | ClassRelation[] classRelations = pp.classRelations;
|
|---|
| 201 | if( classRelations != null ){
|
|---|
| 202 | for(ClassRelation classRelation: classRelations){
|
|---|
| 203 | String subjClassURI = classRelation.subjectClassURI;
|
|---|
| 204 | String objClassURI = classRelation.objectClassURI;
|
|---|
| 205 | boolean forward = false;
|
|---|
| 206 | boolean reverse = false;
|
|---|
| 207 | if( objClassURI != null && objClassURI.equals(originClass) ){
|
|---|
| 208 | if( subjClassURI != null ){
|
|---|
| 209 | reverse = true;
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | if(subjClassURI != null && subjClassURI.equals(originClass)){
|
|---|
| 213 | if( objClassURI != null || classRelation.objectDatatypeURI != null ){
|
|---|
| 214 | forward = true;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | ClassLink classLink = null;
|
|---|
| 218 | if( forward && !reverse ){
|
|---|
| 219 | classLink = new ClassLink();
|
|---|
| 220 | classLink.setDirection(Direction.forward);
|
|---|
| 221 | classLink.setNumOfOriginClassInstances(classRelation.distinctSubjects);
|
|---|
| 222 | if( objClassURI != null ){
|
|---|
| 223 | classLink.setLinkedClassURI(objClassURI);
|
|---|
| 224 | classLink.setNumOfLinkedClassInstances(classRelation.distinctObjects);
|
|---|
| 225 | }else{
|
|---|
| 226 | classLink.setLinkedLiteralDatatypeURI(classRelation.objectDatatypeURI);
|
|---|
| 227 | classLink.setNumOfLinkedInstances(classRelation.triples);
|
|---|
| 228 | }
|
|---|
| 229 | classLink.setNumOfOriginInstances(pp.distinctSubjects);
|
|---|
| 230 | classLink.setNumOfLinkedInstances(pp.distinctObjects);
|
|---|
| 231 |
|
|---|
| 232 | }
|
|---|
| 233 | if( !forward && reverse ){
|
|---|
| 234 | classLink = new ClassLink();
|
|---|
| 235 | classLink.setDirection(Direction.reverse);
|
|---|
| 236 | classLink.setLinkedClassURI(objClassURI);
|
|---|
| 237 | classLink.setNumOfOriginClassInstances(classRelation.distinctObjects);
|
|---|
| 238 | classLink.setNumOfOriginInstances(pp.distinctObjects);
|
|---|
| 239 | classLink.setNumOfLinkedInstances(pp.distinctSubjects);
|
|---|
| 240 | classLink.setNumOfLinkedClassInstances(classRelation.distinctSubjects);
|
|---|
| 241 | }
|
|---|
| 242 | if( forward && reverse){
|
|---|
| 243 | classLink = new ClassLink();
|
|---|
| 244 | classLink.setDirection(Direction.both);
|
|---|
| 245 | classLink.setLinkedClassURI(objClassURI);
|
|---|
| 246 | classLink.setNumOfOriginClassInstances(classRelation.distinctSubjects);
|
|---|
| 247 | classLink.setNumOfOriginInstances(pp.distinctSubjects);
|
|---|
| 248 | classLink.setNumOfLinkedInstances(pp.distinctObjects);
|
|---|
| 249 | classLink.setNumOfLinkedClassInstances(classRelation.distinctObjects);
|
|---|
| 250 | }
|
|---|
| 251 | // hit
|
|---|
| 252 | if( classLink != null ){
|
|---|
| 253 | classLink.setEndpointURI(endpointURI);
|
|---|
| 254 | classLink.setGraphURI(graphURI);
|
|---|
| 255 | classLink.setPropertyURI(pp.propertyDef.propertyURI);
|
|---|
| 256 | classLink.setNumOfLinks(classRelation.triples);
|
|---|
| 257 | classLinkList.add(classLink);
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 | return classLinkList;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 | public LabelMap[] getLabels(String[] resourceURIs, String language) throws Exception {
|
|---|
| 272 | if( resourceURIs == null || resourceURIs.length == 0 ){
|
|---|
| 273 | return new LabelMap[0];
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | HashSet<String> resourceURIset = new HashSet<String>();
|
|---|
| 277 | for(String resourceURI: resourceURIs){
|
|---|
| 278 | resourceURIset.add(resourceURI);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | HashMap<String, LabelMap> labelMapTable = new HashMap<String, LabelMap>();
|
|---|
| 282 |
|
|---|
| 283 | Set<String> endpointURISet = crawledMetadataTable.keySet();
|
|---|
| 284 | for(String endpointURI: endpointURISet){
|
|---|
| 285 | CrawledMetadata cm = crawledMetadataTable.get(endpointURI);
|
|---|
| 286 | // default
|
|---|
| 287 | Dataset dataset = cm.getDefaultDataset();
|
|---|
| 288 | labelMapTable = getLabels(dataset, resourceURIset, language, labelMapTable);
|
|---|
| 289 |
|
|---|
| 290 | // graphs
|
|---|
| 291 | String[] graphURIs = cm.getGraphURIs();
|
|---|
| 292 | if( graphURIs != null ){
|
|---|
| 293 | for(String graphURI: graphURIs){
|
|---|
| 294 | dataset = cm.getDataset(graphURI);
|
|---|
| 295 | labelMapTable = getLabels(dataset, resourceURIset, language, labelMapTable);
|
|---|
| 296 | }
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 | return labelMapTable.values().toArray(new LabelMap[0]);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | private HashMap<String, LabelMap> getLabels(Dataset dataset, HashSet<String> resourceURISet , String language, HashMap<String, LabelMap> labelMapTable) throws Exception {
|
|---|
| 304 | ClassPartition[] cps = dataset.getClassPartitions();
|
|---|
| 305 | if( cps != null ){
|
|---|
| 306 | for(ClassPartition cp: cps){
|
|---|
| 307 | String uri = cp.classDef.classURI;
|
|---|
| 308 | if( resourceURISet.contains(uri)){
|
|---|
| 309 | Label[] rLabels = cp.classDef.labels;
|
|---|
| 310 | if( rLabels != null ){
|
|---|
| 311 | for(Label rLabel: rLabels){
|
|---|
| 312 | if( language == null || ( rLabel.language == null || rLabel.language.equals(language))){
|
|---|
| 313 | LabelMap labelMap = null;
|
|---|
| 314 | if( labelMapTable.containsKey(uri)){
|
|---|
| 315 | labelMap = labelMapTable.get(uri);
|
|---|
| 316 | }else{
|
|---|
| 317 | labelMap = new LabelMap();
|
|---|
| 318 | labelMapTable.put(uri, labelMap);
|
|---|
| 319 | labelMap.setResourceURI(uri);
|
|---|
| 320 | }
|
|---|
| 321 | labelMap.addLabel(new org.biohackathon.SPARQLBuilder.OWL.Label(rLabel.value, rLabel.language));
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | PropertyPartition[] pps = dataset.getPropertyPartitions();
|
|---|
| 330 | if( pps != null ){
|
|---|
| 331 | for(PropertyPartition pp: pps){
|
|---|
| 332 | String uri = pp.propertyDef.propertyURI;
|
|---|
| 333 | if( resourceURISet.contains(uri)){
|
|---|
| 334 | Label[] rLabels = pp.propertyDef.labels;
|
|---|
| 335 | if( rLabels != null ){
|
|---|
| 336 | for(Label rLabel: rLabels){
|
|---|
| 337 | if( language == null || ( rLabel.language == null || rLabel.language.equals(language))){
|
|---|
| 338 | LabelMap labelMap = null;
|
|---|
| 339 | if( labelMapTable.containsKey(uri)){
|
|---|
| 340 | labelMap = labelMapTable.get(uri);
|
|---|
| 341 | }else{
|
|---|
| 342 | labelMap = new LabelMap();
|
|---|
| 343 | labelMapTable.put(uri, labelMap);
|
|---|
| 344 | labelMap.setResourceURI(uri);
|
|---|
| 345 | }
|
|---|
| 346 | labelMap.addLabel(new org.biohackathon.SPARQLBuilder.OWL.Label(rLabel.value, rLabel.language));
|
|---|
| 347 | }
|
|---|
| 348 | }
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 | }
|
|---|
| 353 | return labelMapTable;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | } |
|---|