Exemplo n.º 1
0
  /**
   * Create a JCR value from an RDF node with the given JCR type
   *
   * @param valueFactory
   * @param data
   * @param type
   * @return
   * @throws RepositoryException
   */
  public Value createValue(final ValueFactory valueFactory, final RDFNode data, final int type)
      throws RepositoryException {
    assert (valueFactory != null);

    if (data.isURIResource() && (type == REFERENCE || type == WEAKREFERENCE)) {
      // reference to another node (by path)
      final Node nodeFromGraphSubject =
          session.getNode(graphSubjects.getPathFromSubject(data.asResource()));
      return valueFactory.createValue(nodeFromGraphSubject, type == WEAKREFERENCE);
    } else if (data.isURIResource() || type == URI) {
      // some random opaque URI
      return valueFactory.createValue(data.toString(), PropertyType.URI);
    } else if (data.isResource()) {
      // a non-URI resource (e.g. a blank node)
      return valueFactory.createValue(data.toString(), UNDEFINED);
    } else if (data.isLiteral() && type == UNDEFINED) {
      // the JCR schema doesn't know what this should be; so introspect
      // the RDF and try to figure it out
      final Literal literal = data.asLiteral();
      final RDFDatatype dataType = literal.getDatatype();
      final Object rdfValue = literal.getValue();

      if (rdfValue instanceof Boolean) {
        return valueFactory.createValue((Boolean) rdfValue);
      } else if (rdfValue instanceof Byte
          || (dataType != null && dataType.getJavaClass() == Byte.class)) {
        return valueFactory.createValue(literal.getByte());
      } else if (rdfValue instanceof Double) {
        return valueFactory.createValue((Double) rdfValue);
      } else if (rdfValue instanceof Float) {
        return valueFactory.createValue((Float) rdfValue);
      } else if (rdfValue instanceof Long
          || (dataType != null && dataType.getJavaClass() == Long.class)) {
        return valueFactory.createValue(literal.getLong());
      } else if (rdfValue instanceof Short
          || (dataType != null && dataType.getJavaClass() == Short.class)) {
        return valueFactory.createValue(literal.getShort());
      } else if (rdfValue instanceof Integer) {
        return valueFactory.createValue((Integer) rdfValue);
      } else if (rdfValue instanceof XSDDateTime) {
        return valueFactory.createValue(((XSDDateTime) rdfValue).asCalendar());
      } else {
        return valueFactory.createValue(literal.getString(), STRING);
      }

    } else {
      LOGGER.debug("Using default JCR value creation for RDF literal: {}", data);
      return valueFactory.createValue(data.asLiteral().getString(), type);
    }
  }
Exemplo n.º 2
0
 private static void doTypeString(StringBuffer json, RDFNode node) {
   if (node.isURIResource()) {
     json.append("uri");
     return;
   }
   if (node.isAnon()) {
     json.append("bnode");
     return;
   }
   if (node.isLiteral()) {
     json.append("literal");
   }
 }
Exemplo n.º 3
0
 public static boolean isNativeScope(RDFNode node) {
   if (node != null) {
     if (node.isAnon()) {
       if (((Resource) node).hasProperty(RDF.type, SH.NativeScope)) {
         return true;
       }
       if (!((Resource) node).hasProperty(RDF.type)) {
         return SH.NativeScope.equals(SHACLUtil.getDefaultTemplateType((Resource) node));
       }
     } else if (node.isURIResource()) {
       return ((Resource) node).hasProperty(RDF.type, SH.NativeScope);
     }
   }
   return false;
 }
Exemplo n.º 4
0
 private static void doValueString(StringBuffer json, RDFNode node) {
   if (node.isURIResource()) {
     json.append(node.asResource().getURI());
     return;
   }
   if (node.isAnon()) {
     json.append(node.asResource().getId());
     return;
   }
   if (node.isLiteral()) {
     json.append(node.asLiteral().getString());
     doLang(json, node.asLiteral());
     doDatatype(json, node.asLiteral());
   }
 }
 public Collection<URI> getSupportedFacets(URI needUri) throws NoSuchNeedException {
   List<URI> ret = new LinkedList<URI>();
   Need need = DataAccessUtils.loadNeed(needRepository, needUri);
   Model content = rdfStorageService.loadContent(need);
   if (content == null) return ret;
   Resource baseRes = content.getResource(content.getNsPrefixURI(""));
   StmtIterator stmtIterator = baseRes.listProperties(WON.HAS_FACET);
   while (stmtIterator.hasNext()) {
     RDFNode object = stmtIterator.nextStatement().getObject();
     if (object.isURIResource()) {
       ret.add(URI.create(object.toString()));
     }
   }
   return ret;
 }
Exemplo n.º 6
0
 /**
  * Create a ResearchObject from given zip as input stream.
  *
  * @param id Research Object id
  * @param input The content of the zip aggreagated ResearchObject
  * @return a instance of ResearchObject
  */
 @SuppressWarnings("resource")
 public static ResearchObjectSerializable toResearchObject(URI id, InputStream input) {
   File tmpZipFile = null;
   ZipFile zipFile = null;
   try {
     tmpZipFile = File.createTempFile("zipInput", ".zip");
     IOUtils.copy(input, new FileOutputStream(tmpZipFile));
     zipFile = new ZipFile(tmpZipFile);
   } catch (IOException e) {
     LOGGER.error("Can't create a tmpFile for a RO " + id + " given from dArceo", e);
     return null;
   }
   Enumeration<? extends ZipEntry> entries = zipFile.entries();
   // first get Manifest build Jena and parese it
   ResearchObject researchObject = new ResearchObject(id);
   while (entries.hasMoreElements()) {
     ZipEntry entry = entries.nextElement();
     if (entry.getName().equals("content/.ro/manifest.rdf")) {
       OntModel model = ModelFactory.createOntologyModel();
       try {
         model.read(zipFile.getInputStream(entry), id.toString() + ".ro/");
         Individual roIndividual = model.getResource(id.toString()).as(Individual.class);
         for (RDFNode node : roIndividual.listPropertyValues(ORE.aggregates).toList()) {
           if (node.isURIResource()) {
             URI resourceUri = URI.create(node.asResource().getURI());
             URI entryName =
                 URI.create("content/")
                     .resolve(id.relativize(URI.create(node.asResource().getURI())).toString());
             InputStream entryInput = zipFile.getInputStream(new ZipEntry(entryName.toString()));
             researchObject.addSerializable(new ResearchObjectComponent(resourceUri, entryInput));
           }
         }
         // add manifest
         InputStream entryInput = zipFile.getInputStream(new ZipEntry("content/.ro/manifest.rdf"));
         researchObject.addSerializable(
             new ResearchObjectComponent(id.resolve(".ro/manifest.rdf"), entryInput));
         break;
       } catch (IOException e) {
         LOGGER.error("can't load the manifest from zip for RO " + id, e);
         tmpZipFile.delete();
         return researchObject;
       }
     }
   }
   tmpZipFile.delete();
   return researchObject;
 }
  protected List<String> allIndividualsRelatedByObjectPropertyStmts(String uri) {
    List<String> additionalUris = new ArrayList<String>();

    QuerySolutionMap initialBinding = new QuerySolutionMap();
    Resource uriResource = ResourceFactory.createResource(uri);
    initialBinding.add("uri", uriResource);

    Query sparqlQuery = QueryFactory.create(QUERY_FOR_RELATED);
    model.getLock().enterCriticalSection(Lock.READ);
    try {
      QueryExecution qExec = QueryExecutionFactory.create(sparqlQuery, model, initialBinding);
      try {
        ResultSet results = qExec.execSelect();
        while (results.hasNext()) {
          QuerySolution soln = results.nextSolution();
          Iterator<String> iter = soln.varNames();
          while (iter.hasNext()) {
            String name = iter.next();
            RDFNode node = soln.get(name);
            if (node != null) {
              if (node.isURIResource()) {
                additionalUris.add(node.as(Resource.class).getURI());
              } else {
                log.warn(
                    "value from query for var " + name + "  was not a URIResource, it was " + node);
              }
            } else {
              log.warn("value for query for var " + name + " was null");
            }
          }
        }
      } catch (Throwable t) {
        log.error(t, t);
      } finally {
        qExec.close();
      }
    } finally {
      model.getLock().leaveCriticalSection();
    }
    return additionalUris;
  }
Exemplo n.º 8
0
  private void fillTree(
      Resource root,
      RDFResourceTree tree,
      Map<Resource, SortedSet<Statement>> resource2Statements,
      int currentDepth,
      int maxDepth) {
    currentDepth++;
    if (resource2Statements.containsKey(root)) {
      RDFResourceTree subTree;

      for (Statement st : resource2Statements.get(root)) {
        Node predicate = st.getPredicate().asNode();
        RDFNode object = st.getObject();

        if (object.isLiteral()) {
          subTree = new RDFResourceTree(nodeId++, object.asNode());
          tree.addChild(subTree, predicate);
        } else if (object.isURIResource()) {
          subTree = new RDFResourceTree(nodeId++, object.asNode());
          tree.addChild(subTree, predicate);
          //					System.out.println(root + "::" + object + "::" + (currentDepth < maxDepth));
          if (currentDepth < maxDepth) {
            if (currentDepth < maxDepth) {
              fillTree(object.asResource(), subTree, resource2Statements, currentDepth, maxDepth);
            }
          }
        } else if (object.isAnon()) {
          subTree = new RDFResourceTree(nodeId++);
          tree.addChild(subTree, predicate);
          if (currentDepth < maxDepth) {
            fillTree(object.asResource(), subTree, resource2Statements, currentDepth, maxDepth);
          }
        }
      }
    }
    currentDepth--;
  }
Exemplo n.º 9
0
 /**
  * Gets the string.
  *
  * @param rdfNode the rdf node
  * @return the string
  */
 public static String getString(RDFNode rdfNode) {
   if (rdfNode.isURIResource())
     // return ((Resource)rdfNode).getURI();
     return ((Resource) rdfNode).getLocalName();
   else if (rdfNode.isResource()) return "  ";
   // ((Resource) rdfNode).getId().toString(); //
   else if (rdfNode.isLiteral()) {
     String label = ((Literal) rdfNode).toString();
     // if(label.length() > 10){ // TODO come back to this
     // label = "";
     // String[] split = label.split(" ");
     // for(int i=0;i<split.length;i++){
     // label = label + split[i] + " ";
     // if(label.length()>25) continue;
     // if(label.length() % 8 == 0) label = label +"\n";
     // }
     // }
     if (label.length() > 10) {
       label = label.substring(0, 9);
     }
     return label;
   }
   return null;
 }
Exemplo n.º 10
0
  public PagingLoadResult<Example> getSPARQLQueryResultWithProperties(
      String query, List<String> properties, PagingLoadConfig config) throws AutoSPARQLException {
    List<Example> queryResult = new ArrayList<Example>();
    //		properties.remove("label");
    int limit = config.getLimit();
    int offset = config.getOffset();
    int totalLength = 10;

    if (currentQueryResultSize == -1) {
      try {
        ResultSetRewindable rs =
            SparqlQuery.convertJSONtoResultSet(
                selectCache.executeSelectQuery(endpoint, getCountQuery(query)));
        currentQueryResultSize = rs.next().getLiteral(rs.getResultVars().get(0)).getInt();
      } catch (Exception e) {
        e.printStackTrace();
        currentQueryResultSize = 10;
      }
    }
    totalLength = currentQueryResultSize;

    List<String> propertiesToDo = new ArrayList<String>(properties);

    for (Map<String, Object> prop2Value : propertiesCache.values()) {
      propertiesToDo.removeAll(prop2Value.keySet());
    }

    if (propertiesToDo.size() > 0) {
      String queryTriples = query.substring(18, query.length() - 1);
      StringBuilder newQuery = new StringBuilder();
      Map<String, String> var2URIMap = new HashMap<String, String>(propertiesToDo.size());

      if (propertiesToDo.size() == 1 && propertiesToDo.get(0).equals(RDFS.label.getURI())) {
        newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL{");
        newQuery.append(queryTriples);
        newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n");
        newQuery
            .append("OPTIONAL{?x0 <")
            .append("http://dbpedia.org/ontology/thumbnail")
            .append("> ?imageURL.}\n");
        newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))");
        newQuery.append("}");
      } else {
        for (String property : propertiesToDo) {
          var2URIMap.put(
              property2LabelMap.get(property).replace(" ", "_").replace("(", "").replace(")", ""),
              property);
        }

        newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL ");
        for (String var : var2URIMap.keySet()) {
          newQuery.append("?").append(var).append(" ");
          newQuery.append("?").append(var).append("_label ");
        }
        newQuery.append("{");
        newQuery.append(queryTriples);
        newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n");
        newQuery
            .append("OPTIONAL{?x0 <")
            .append("http://dbpedia.org/ontology/thumbnail")
            .append("> ?imageURL.}\n");
        for (Entry<String, String> entry : var2URIMap.entrySet()) {
          newQuery
              .append("OPTIONAL{?x0 <")
              .append(entry.getValue())
              .append("> ?")
              .append(entry.getKey())
              .append(".}\n");
        }
        for (Entry<String, String> entry : var2URIMap.entrySet()) {
          newQuery
              .append("OPTIONAL{?")
              .append(entry.getKey())
              .append(" <")
              .append(RDFS.label)
              .append("> ?")
              .append(entry.getKey())
              .append("_label.\n");
          newQuery.append("FILTER(LANGMATCHES(LANG(?" + entry.getKey() + "_label),'en'))}\n");
        }
        newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))");
        newQuery.append("}");
      }

      logger.debug("Query with properties:\n" + newQuery.toString());
      try {
        ResultSetRewindable rs =
            SparqlQuery.convertJSONtoResultSet(
                selectCache.executeSelectQuery(endpoint, modifyQuery(newQuery + " LIMIT 1000")));

        String uri;
        String label = "";
        String imageURL = "";
        QuerySolution qs;
        RDFNode object;
        while (rs.hasNext()) {
          qs = rs.next();
          uri = qs.getResource("x0").getURI();
          label = qs.getLiteral("label").getLexicalForm();
          imageURL = qs.getResource("imageURL") != null ? qs.getResource("imageURL").getURI() : "";
          Map<String, Object> properties2Value = propertiesCache.get(uri);
          if (properties2Value == null) {
            properties2Value = new HashMap<String, Object>();
            properties2Value.put(RDFS.label.getURI(), label);
            properties2Value.put("http://dbpedia.org/ontology/thumbnail", imageURL);
            propertiesCache.put(uri, properties2Value);
          }

          Object value;
          String property;
          for (Entry<String, String> entry : var2URIMap.entrySet()) {
            value = "";
            property = entry.getValue();

            object = qs.get(entry.getKey() + "_label");
            if (object == null) {
              object = qs.get(entry.getKey());
            }
            if (object != null) {
              if (object.isURIResource()) {
                value = object.asResource().getURI();
              } else if (object.isLiteral()) {
                Literal lit = object.asLiteral();
                //								if(lit.getDatatypeURI().equals(XSD.BOOLEAN)){
                //									property2DatatypeMap.put(property, Boolean.class);
                //									value = lit.getBoolean();
                //								} else if(lit.getDatatypeURI().equals(XSD.INT)){
                //									property2DatatypeMap.put(property, Integer.class);
                //									value = lit.getInt();
                //								} else if(lit.getDatatypeURI().equals(XSD.DOUBLE)){
                //									property2DatatypeMap.put(property, Double.class);
                //									value = lit.getDouble();
                //								} else if(lit.getDatatypeURI().equals(XSD.FLOAT)){
                //									property2DatatypeMap.put(property, Float.class);
                //									value = lit.getFloat();
                //								} else {
                //									property2DatatypeMap.put(property, String.class);
                //									value = object.asLiteral().getLexicalForm();
                //								}
                value = object.asLiteral().getLexicalForm();
              }
            }
            Object oldValue = properties2Value.get(property);
            if (oldValue != null && value != null) {
              value = oldValue + ", " + value;
            }
            properties2Value.put(property, value);
          }
        }
      } catch (Exception e) {
        logger.error("Error while getting result for query \n" + newQuery, e);
      }
    }

    Example example;
    int cnt = 0;
    for (Entry<String, Map<String, Object>> uri2PropertyValues : propertiesCache.entrySet()) {
      //			if(cnt++ == limit+offset){
      //				break;
      //			}
      //			if(cnt > offset){
      example = new Example();
      example.setAllowNestedValues(false);
      example.set("uri", uri2PropertyValues.getKey());
      Object value;
      String property;
      for (Entry<String, Object> property2Value : uri2PropertyValues.getValue().entrySet()) {
        property = property2Value.getKey();
        value = property2Value.getValue();
        //					if(value == null){
        //						Class cls = property2DatatypeMap.get(property);
        //						if(cls == String.class){
        //							value = "";
        //						} else if(cls == Integer.class){
        //							value = Integer.valueOf(-1);
        //						} else if(cls == Double.class){
        //							value = Double.valueOf(-1);
        //						} else if(cls == Float.class){
        //							value = Float.valueOf(-1);
        //						} else if(cls == Boolean.class){
        //							value = Boolean.FALSE;
        //						}
        //					}
        example.set(property, value);
      }
      queryResult.add(example);
      //			}

    }
    if (config.getSortInfo().getSortField() != null) {
      final String sortField = config.getSortInfo().getSortField();
      Collections.sort(
          queryResult,
          config
              .getSortInfo()
              .getSortDir()
              .comparator(
                  new Comparator<Example>() {

                    @Override
                    public int compare(Example o1, Example o2) {
                      return ((String) o1.get(sortField)).compareTo((String) o2.get(sortField));
                    }
                  }));
    }
    int start = config.getOffset();
    int end = queryResult.size();
    if (limit > 0) {
      end = Math.min(start + limit, end);
    }
    //		queryResult = queryResult.subList(start, end);
    ArrayList<Example> tmp = new ArrayList<Example>();
    for (int i = start; i < end; i++) {
      tmp.add(queryResult.get(i));
    }

    PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(tmp);
    result.setOffset(offset);
    result.setTotalLength(totalLength);

    return result;
  }
Exemplo n.º 11
0
 /* (non-Javadoc)
  * @see com.hp.hpl.jena.rdf.model.RDFNode#isURIResource()
  */
 @Override
 public boolean isURIResource() {
   return rdfNode.isURIResource();
 }
  // renames percentage properties and classes
  // activeProperties -> if true, then rename properties
  // activeClasses -> if true, then rename classes
  public OntModel renameResource(
      boolean activeProperties,
      boolean activeClasses,
      float percentage,
      boolean activeRandomString,
      boolean activeTranslateString,
      boolean activeSynonym,
      int activeStringOperation) {
    List<Statement> statements = null; // the list of all statements
    HashMap<String, String> propertiesIdentifiers =
        null; // the HashMap of the properties identifiers
    HashMap<String, String> classesIdentifiers = null; // the HashMap of the classes identifiers

    OntModel newModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); // create new Model
    // get properties and classes identifiers
    if (activeProperties)
      propertiesIdentifiers =
          getPropertiesIdentifiers(
              percentage,
              activeRandomString,
              activeTranslateString,
              activeSynonym,
              activeStringOperation);
    if (activeClasses)
      classesIdentifiers =
          getClassesIdentifiers(
              percentage,
              activeRandomString,
              activeTranslateString,
              activeSynonym,
              activeStringOperation);

    // iterate and modify the identifiers
    for (Statement stm : modifiedModel.listStatements().toList()) {

      Resource subject = stm.getSubject(); // the subject
      Property predicate = stm.getPredicate(); // the predicate
      RDFNode object = stm.getObject(); // the object

      boolean isPred, isSubj, isObj;
      isPred = isSubj = isObj = false;

      String subjuri = subject.getURI();
      String subjectLocalName = getLocalName(subjuri);
      Resource subj = null;
      // if it is the subject of the statement
      if (subjectLocalName != null) {
        String subjectNameSpace = getNameSpace(subjuri);
        if (activeProperties) {
          if (propertiesIdentifiers.containsKey(subjectLocalName)) {
            // if the namespace of the subject is the same as the namespace of the property
            // identifier
            if (subjectNameSpace.equals(modifiedOntologyNS)) { // that we want to remove
              isSubj = true;
              subj =
                  newModel.createResource(
                      subjectNameSpace + propertiesIdentifiers.get(subjectLocalName));
            }
          }
        }

        if (activeClasses) {
          if (classesIdentifiers.containsKey(subjectLocalName)) {
            // if the namespace of the subject is the same as the namespace of the property
            // identifier
            // that we want to remove
            if (subjectNameSpace.equals(modifiedOntologyNS)) {
              isSubj = true;
              subj =
                  newModel.createResource(
                      subjectNameSpace + classesIdentifiers.get(subjectLocalName));
            }
          }
        }
      }

      // if it is the predicate of the statement
      String preduri = predicate.getURI();
      String predicateLocalName = getLocalName(preduri);
      String predicateNameSpace = getNameSpace(preduri);
      Property pred = null;
      if (activeProperties) {
        if (propertiesIdentifiers.containsKey(predicateLocalName)) {
          // if the namespace of the predicate is the same as the namespace of the property
          // identifier
          // that we want to remove
          if (predicateNameSpace.equals(modifiedOntologyNS)) {
            isPred = true;
            pred =
                newModel.createProperty(
                    predicateNameSpace, propertiesIdentifiers.get(predicateLocalName));
          }
        }
      }

      if (activeClasses) {
        if (classesIdentifiers.containsKey(predicateLocalName)) {
          // if the namespace of the predicate is the same as the namespace of the property
          // identifier
          // that we want to remove
          if (predicateNameSpace.equals(modifiedOntologyNS)) {
            isPred = true;
            pred =
                newModel.createProperty(
                    predicateNameSpace, classesIdentifiers.get(predicateLocalName));
          }
        }
      }

      Resource obj = null;
      // if it is the object of the statement
      if (object.canAs(Resource.class))
        if (object.isURIResource()) {
          String uri = object.asResource().getURI();
          String objectLocalName = getLocalName(uri);
          String objectNameSpace = getNameSpace(uri);
          if (activeProperties) {
            if (propertiesIdentifiers.containsKey(objectLocalName)) {
              // if the namespace of the object is the same as the namespace of the property
              // identifier
              // that we want to remove
              if (objectNameSpace.equals(modifiedOntologyNS)) {
                isObj = true;
                obj =
                    newModel.createResource(
                        objectNameSpace + propertiesIdentifiers.get(objectLocalName));
              }
            }
          }

          if (activeClasses) {
            if (classesIdentifiers.containsKey(objectLocalName)) {
              // if the namespace of the object is the same as the namespace of the property
              // identifier that we want to remove
              if (objectNameSpace.equals(modifiedOntologyNS)) {
                isObj = true;
                obj =
                    newModel.createResource(
                        objectNameSpace + classesIdentifiers.get(objectLocalName));
              }
            }
          }
        }

      if (isSubj) {
        if (isPred) {
          if (isObj) newModel.add(subj, pred, obj);
          else newModel.add(subj, pred, object);
        } else {
          if (isObj) newModel.add(subj, predicate, obj);
          else newModel.add(subj, predicate, object);
        }
      } else {
        if (isPred) {
          if (isObj) newModel.add(subject, pred, obj);
          else newModel.add(subject, pred, object);
        } else {
          if (isObj) newModel.add(subject, predicate, obj);
          else newModel.add(subject, predicate, object);
        }
      }
    }
    if (activeClasses) {
      buildClassHierarchy();
      // we update the class hierarchy according to the new modifications
      classHierarchy.updateClassHierarchy(alignment);
      // classHierarchy.printClassHierarchy();
    }
    return newModel;
  }