Exemplo n.º 1
0
  private static boolean hasPermission(
      String userPrefix, String username, String uri, Property[] permissions) {
    Set<Property> permissionSet = new HashSet<Property>();
    Resource resource = configModel.createResource(uri);
    Resource user;

    if (username == null) user = PERM.Public;
    else user = configModel.createResource(userPrefix + username);

    for (Property permission : permissions) {
      if (configModel.contains(user, permission, resource)) return true;
      permissionSet.add(permission);
    }

    StmtIterator stmts = user.listProperties();
    while (stmts.hasNext()) {
      Statement stmt = stmts.next();
      if (!permissionSet.contains(stmt.getPredicate())) continue;
      RDFNode resourceMatch = stmt.getObject();
      if (!(resourceMatch.isResource()
          && configModel.contains((Resource) resourceMatch, RDF.type, PERM.ResourceMatch)))
        continue;

      RDFNode matchRegex = ((Resource) resourceMatch).getProperty(PERM.matchExpression).getObject();
      if (matchRegex == null || !matchRegex.isLiteral()) continue;

      try {
        if (uri.matches(((Literal) matchRegex).getString())) return true;
      } catch (PatternSyntaxException e) {
      }
    }

    if (username != null) return hasPermission(userPrefix, null, uri, permissions);
    else return false;
  }
  /**
   * Query SPARQL endpoint with a SELECT query
   *
   * @param qExec QueryExecution encapsulating the query
   * @return model retrieved by querying the endpoint
   */
  private Model getSelectModel(QueryExecution qExec) {
    Model model = ModelFactory.createDefaultModel();
    Graph graph = model.getGraph();
    ResultSet results = qExec.execSelect();

    while (results.hasNext()) {
      QuerySolution sol = results.next();
      String subject;
      String predicate;
      RDFNode object;

      try {
        subject = sol.getResource("s").toString();
        predicate = sol.getResource("p").toString();
        object = sol.get("o");
      } catch (NoSuchElementException e) {
        logger.error("SELECT query does not return a (?s ?p ?o) Triple");
        continue;
      }

      Node objNode;
      if (object.isLiteral()) {
        Literal obj = object.asLiteral();
        objNode = NodeFactory.createLiteral(obj.getString(), obj.getDatatype());
      } else {
        objNode = NodeFactory.createLiteral(object.toString());
      }

      graph.add(
          new Triple(NodeFactory.createURI(subject), NodeFactory.createURI(predicate), objNode));
    }

    return model;
  }
Exemplo n.º 3
0
  private int getMaxRank(String objectUri, String subjectUri, VitroRequest vreq) {

    int maxRank = 0; // default value
    if (objectUri == null) { // adding new webpage
      String queryStr = QueryUtils.subUriForQueryVar(MAX_RANK_QUERY, "subject", subjectUri);
      log.debug("Query string is: " + queryStr);
      try {
        ResultSet results = QueryUtils.getQueryResults(queryStr, vreq);
        if (results != null && results.hasNext()) { // there is at most one result
          QuerySolution soln = results.next();
          RDFNode node = soln.get("rank");
          if (node != null && node.isLiteral()) {
            // node.asLiteral().getInt() won't return an xsd:string that
            // can be parsed as an int.
            int rank = Integer.parseInt(node.asLiteral().getLexicalForm());
            if (rank > maxRank) {
              log.debug("setting maxRank to " + rank);
              maxRank = rank;
            }
          }
        }
      } catch (NumberFormatException e) {
        log.error("Invalid rank returned from query: not an integer value.");
      } catch (Exception e) {
        log.error(e, e);
      }
    }
    return maxRank;
  }
  /**
   * Builds a String result for Elastic Search from an RDFNode
   *
   * @param node An RDFNode representing the value of a property for a given resource
   * @return If the RDFNode has a Literal value, among Boolean, Byte, Double, Float, Integer Long,
   *     Short, this value is returned, converted to String
   *     <p>If the RDFNode has a String Literal value, this value will be returned, surrounded by
   *     double quotes
   *     <p>If the RDFNode has a Resource value (URI) and toDescribeURIs is set to true, the value
   *     of @getLabelForUri for the resource is returned, surrounded by double quotes. Otherwise,
   *     the URI will be returned
   */
  private String getStringForResult(RDFNode node, boolean getNodeLabel) {
    String result = "";
    boolean quote = false;

    if (node.isLiteral()) {
      Object literalValue = node.asLiteral().getValue();
      try {
        Class<?> literalJavaClass = node.asLiteral().getDatatype().getJavaClass();

        if (literalJavaClass.equals(Boolean.class)
            || Number.class.isAssignableFrom(literalJavaClass)) {

          result += literalValue;
        } else {
          result = EEASettings.parseForJson(node.asLiteral().getLexicalForm());
          quote = true;
        }
      } catch (java.lang.NullPointerException npe) {
        result = EEASettings.parseForJson(node.asLiteral().getLexicalForm());
        quote = true;
      }

    } else if (node.isResource()) {
      result = node.asResource().getURI();
      if (getNodeLabel) {
        result = getLabelForUri(result);
      }
      quote = true;
    }
    if (quote) {
      result = "\"" + result + "\"";
    }
    return result;
  }
  @Override
  public List<Literal> getDataPropertyValuesForIndividualByProperty(
      String subjectUri, String propertyUri) {
    log.debug("Data property value query string:\n" + DATA_PROPERTY_VALUE_QUERY_STRING);
    log.debug("Data property value:\n" + dataPropertyValueQuery);

    QuerySolutionMap initialBindings = new QuerySolutionMap();
    initialBindings.add("subject", ResourceFactory.createResource(subjectUri));
    initialBindings.add("property", ResourceFactory.createResource(propertyUri));

    // Run the SPARQL query to get the properties
    List<Literal> values = new ArrayList<Literal>();
    DatasetWrapper w = dwf.getDatasetWrapper();
    Dataset dataset = w.getDataset();
    dataset.getLock().enterCriticalSection(Lock.READ);
    try {
      QueryExecution qexec =
          QueryExecutionFactory.create(dataPropertyValueQuery, dataset, initialBindings);
      ResultSet results = qexec.execSelect();

      while (results.hasNext()) {
        QuerySolution soln = results.next();
        RDFNode node = soln.get("value");
        if (!node.isLiteral()) {
          continue;
        }
        Literal value = soln.getLiteral("value");
        values.add(value);
      }
    } finally {
      dataset.getLock().leaveCriticalSection();
      w.close();
    }
    return values;
  }
Exemplo n.º 6
0
 /**
  * Answer the string described by the value of the unique optional <code>classProperty</code>
  * property of <code>root</code>, or null if there's no such property. The value may be a URI, in
  * which case it must be a <b>java:</b> URI with content the class name; or it may be a literal,
  * in which case its lexical form is its class name; otherwise, BOOM.
  */
 public static String getOptionalClassName(Resource root, Property classProperty) {
   RDFNode classNode = getUnique(root, classProperty);
   return classNode == null
       ? null
       : classNode.isLiteral()
           ? classNode.asNode().getLiteralLexicalForm()
           : classNode.isResource() ? mustBeJava(classNode.asNode().getURI()) : null;
 }
Exemplo n.º 7
0
  /**
   * Equals rdf.
   *
   * @param object the object
   * @return true, if successful
   */
  public boolean equalsRDF(RDFNode object) {
    // URI resources & properties
    if (object.isResource() && ((Resource) object).isURIResource()) {
      if (rdfNode.isResource() && ((Resource) rdfNode).isURIResource()) {
        if (((Resource) rdfNode).getURI().equals(((Resource) object).getURI())) return true;
        else return false;
      }
    }
    // bnode
    if (object.isAnon() && rdfNode.isAnon()) {
      if (((Resource) rdfNode).getId().equals(((Resource) object).getId())) return true;
      else return false;
    }
    // literal
    if (object.isLiteral() && rdfNode.isLiteral()) {
      if (((Literal) object).equals((rdfNode))
          || (((Literal) object).sameValueAs((Literal) rdfNode))) return true;
    }

    return false;
  }
Exemplo n.º 8
0
 public void add(Statement stmt, DataSet datastore) {
   RDFNode object = stmt.getObject();
   if (object.isLiteral()) {
     if (object.asLiteral().getDatatype() != null) {
       if (object.asLiteral().getDatatype() instanceof XSDBaseNumericType) {
         integers.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDDouble) {
         doubles.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDFloat) {
         floats.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDYearType) {
         gYears.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDDateType) {
         dates.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDYearMonthType) {
         gYearMonths.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDMonthDayType) {
         gMonthDays.add(stmt, datastore);
       } else if (object.asLiteral().getDatatype() instanceof XSDTimeType) {
         times.add(stmt, datastore);
       }
       //                    else if (object.asLiteral().getDatatype() instanceof XSDboolean)
       //                    {
       //                        System.out.println(" -> XSDboolean");
       //                        booleans.add(stmt);
       //                        System.out.println("datatype:" +
       // object.asLiteral().getDatatype());
       //                    }
       //                else if (object.asLiteral().getDatatypeURI().contains("usDollar"))
       //                {
       //                    doubles.add(stmt);
       //                }
       //                else if (object.asLiteral().getDatatypeURI().contains("euro"))
       //                {
       //                    doubles.add(stmt);
       //                }
       else if (object.asLiteral().getDatatypeURI().contains("dbpedia.org/datatype")) {
         doubles.add(stmt, datastore);
       } else {
         unknowns.add(stmt, datastore);
         Evaluator.printStatement(stmt, "LiteralSwitch - unable to add an unknown data type:");
         System.out.println("datatype:" + object.asLiteral().getDatatype());
         System.out.println("datatype uri:" + object.asLiteral().getDatatypeURI());
       }
     } else if (object.asLiteral().getLanguage() != null) {
       strings.add(stmt, datastore);
     } else {
       Evaluator.printStatement(stmt, "LiteralSwitch - unable to add an unknown literal type:");
       System.out.println("data type:" + object.asLiteral().getDatatype());
       System.out.println("data type uri:" + object.asLiteral().getDatatypeURI());
     }
   }
 }
Exemplo n.º 9
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.º 10
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.º 11
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());
   }
 }
Exemplo n.º 12
0
  @Override
  protected Object readObject(final int columnOrdinal) throws SQLException {
    checkPosition();
    checkColumn(columnOrdinal);
    final QuerySolution soln = (QuerySolution) getRowObject();
    final String colName = query.getProjectVars().get(columnOrdinal - 1).getName();
    final RDFNode node = soln.get(colName);

    if (node == null) {
      return null;
    }
    if (node.isLiteral()) {
      return TypeConverter.getJavaValue(node.asLiteral());
    }
    return node.toString();
  }
Exemplo n.º 13
0
  public static void main(String[] args) {
    // Create the data.
    // This wil be the background (unnamed) graph in the dataset.
    Model model = createModel();

    // First part or the query string
    String prolog = "PREFIX dc: <" + DC.getURI() + ">";

    // Query string.
    String queryString = prolog + NL + "SELECT ?title WHERE {?x dc:title ?title}";

    Query query = QueryFactory.create(queryString);
    // Print with line numbers
    query.serialize(new IndentedWriter(System.out, true));
    System.out.println();

    // Create a single execution of this query, apply to a model
    // which is wrapped up as a Dataset

    QueryExecution qexec = QueryExecutionFactory.create(query, model);
    // Or QueryExecutionFactory.create(queryString, model) ;

    System.out.println("Titles: ");

    try {
      // Assumption: it's a SELECT query.
      ResultSet rs = qexec.execSelect();

      // The order of results is undefined.
      for (; rs.hasNext(); ) {
        QuerySolution rb = rs.nextSolution();

        // Get title - variable names do not include the '?' (or '$')
        RDFNode x = rb.get("title");

        // Check the type of the result value
        if (x.isLiteral()) {
          Literal titleStr = (Literal) x;
          System.out.println("    " + titleStr);
        } else System.out.println("Strange - not a literal: " + x);
      }
    } finally {
      // QueryExecution objects should be closed to free any system resources
      qexec.close();
    }
  }
Exemplo n.º 14
0
 private String renderStatement(Statement stmt) {
   String subjStr =
       (stmt.getSubject().getURI() != null)
           ? stmt.getSubject().getURI()
           : stmt.getSubject().getId().toString();
   String predStr = stmt.getPredicate().getURI();
   String objStr = "";
   RDFNode obj = stmt.getObject();
   if (obj.isLiteral()) {
     objStr = "\"" + (((Literal) obj).getLexicalForm());
   } else {
     objStr =
         (((Resource) stmt.getObject()).getURI() != null)
             ? ((Resource) stmt.getObject()).getURI()
             : ((Resource) stmt.getObject()).getId().toString();
   }
   return (subjStr + " : " + predStr + " : " + objStr);
 }
  @SuppressWarnings("unchecked")
  public Set<S> getNegativeExamples(EvaluatedAxiom<T> axiom) {

    ResultSet rs = executeSelectQuery(negExamplesQueryTemplate.toString());

    Set<OWLObject> negExamples = new TreeSet<OWLObject>();

    while (rs.hasNext()) {
      RDFNode node = rs.next().get("s");
      if (node.isResource()) {
        negExamples.add(df.getOWLNamedIndividual(IRI.create(node.asResource().getURI())));
      } else if (node.isLiteral()) {
        negExamples.add(convertLiteral(node.asLiteral()));
      }
    }
    return (Set<S>) negExamples;
    //		throw new UnsupportedOperationException("Getting negative examples is not possible.");
  }
Exemplo n.º 16
0
 @Override
 public void nextTuple() {
   try {
     if (numOfReports < maxReports) {
       currTime = System.nanoTime();
       LOG.info(
           "startTime: "
               + startTime
               + " currTime: "
               + currTime
               + " interval: "
               + ((currTime - startTime) * 1e-6));
       if (((currTime - startTime) * 1e-6) >= interval) {
         if (query == null) query = QueryFactory.create(queryString);
         Store store = StoreFactory.getJenaHBaseStore(configFile, iri, isReified);
         int solnCount = 0;
         ResultSet rs = store.executeSelectQuery(query);
         List<Var> listVars = query.getProject().getVars();
         while (rs.hasNext()) {
           solnCount++;
           QuerySolution qs = rs.next();
           Object[] results = new String[listVars.size() + 1];
           if (solnCount == 1) results[0] = "new";
           else results[0] = "cont";
           for (int i = 1; i <= listVars.size(); i++) {
             Var var = listVars.get(i - 1);
             RDFNode value = qs.get(var.toString());
             if (value.isResource() || value.isAnon()) results[i] = value.asResource().toString();
             else if (value.isLiteral()) results[i] = value.asLiteral().toString();
           }
           collector.emit(new Values(results));
         }
         numOfReports += 1;
         startTime = currTime;
       }
       Thread.sleep(30000);
     }
   } catch (Exception e) {
     throw new TopologyException("Exception in query spout:: ", e);
   }
 }
Exemplo n.º 17
0
  protected String getTextForRow(QuerySolution row, boolean addSpace) {
    if (row == null) return "";

    StringBuffer text = new StringBuffer();
    Iterator<String> iter = row.varNames();
    while (iter.hasNext()) {
      String name = iter.next();
      RDFNode node = row.get(name);
      if (node != null) {
        String value = (node.isLiteral()) ? node.asLiteral().getString() : node.toString();
        if (StringUtils.isNotBlank(value)) {
          if (addSpace) {
            text.append(" ").append(value);
          } else {
            text.append(value);
          }
        }
      } else {
        log.debug(name + " is null");
      }
    }
    return text.toString();
  }
Exemplo n.º 18
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.º 19
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.º 20
0
 /* (non-Javadoc)
  * @see com.hp.hpl.jena.rdf.model.RDFNode#isLiteral()
  */
 @Override
 public boolean isLiteral() {
   return rdfNode.isLiteral();
 }
Exemplo n.º 21
0
  private static void addAdditionalProperties() {
    // Map <predicate,value> save each interesting predicate of the URI object
    Map<Property, List<RDFNode>> resourceInterestingInfoExtension =
        new HashMap<Property, List<RDFNode>>();
    // Map<object,objectResourceData> to save each object with its related data resource and be
    // retrieved whenever a URI object data needed to be added for extension
    Map<RDFNode, Map<Property, List<RDFNode>>> objectWithInfoAttached =
        new HashMap<RDFNode, Map<Property, List<RDFNode>>>();
    // Get list of unique URI objects in the data source as http://dbpedia.org/resource/XXXX
    List<RDFNode> urisObjects = getURIObjects();
    // Get information for each single distinct objectURI according to interesting predicates
    logger.info("Number of unique URI object to find extension: " + urisObjects.size());
    if (urisObjects.size() > 0) {
      // For each unique URI object, its predicate-value pairs are retrieved then add them attached
      // to their object in a map
      for (RDFNode uriObject : urisObjects) {
        // Retrieve all interesting <predicate,object> info. for current URI object
        resourceInterestingInfoExtension = getURIInfo(uriObject);
        // Add retrieved predicate-value pair attached to the object in the map
        objectWithInfoAttached.put(
            uriObject, resourceInterestingInfoExtension); // enriched list of objects
      }
    } else { // Otherwise no URI objects to be extended
      return;
    }

    List<Statement> triplesWithURIsObjects = getTriplesWithURIObjects();
    logger.info("Starting model enriching");
    if (triplesWithURIsObjects.size() > 0) {
      // iterate over each triple to add each URI object information to its resource subject
      for (Statement triple : triplesWithURIsObjects) {
        // put a hand over the required subject
        Resource enrichedResource = (Resource) triple.getObject();
        // for the subject's related object in the enriched list get the related predicate-value
        // pairs
        Map<Property, List<RDFNode>> objectPredicateValuePairs =
            objectWithInfoAttached.get(triple.getObject());
        int i = 0;
        for (Property predicate : objectPredicateValuePairs.keySet()) {
          for (RDFNode value : objectPredicateValuePairs.get(predicate)) {
            Property outputProperty =
                (i < outputProperties.size()) ? outputProperties.get(i) : defaultOutputProperty;
            if (value.isLiteral()) {
              localModel.add(enrichedResource, outputProperty, value.asLiteral().toString());
              logger.info(
                  "Triple found: <"
                      + enrichedResource
                      + "> <"
                      + outputProperty
                      + "> \""
                      + value.toString()
                      + "\"");
            } else {
              localModel.add(enrichedResource, outputProperty, value);
              logger.info(
                  "Triple found: <"
                      + enrichedResource
                      + "> <"
                      + outputProperty
                      + "> <"
                      + value
                      + ">");
            }
          }
          i++;
        }
      }
    }
  }
Exemplo n.º 22
0
 /**
  * @param predicates: list of targeted predicates to enrich the model It calls
  *     getTriplesWithObjectsAreURI() method retrieving list of triples in model having URI-typed
  *     objects. For each object of them, it is checked if it is in DBpedia (can be extended later)
  *     then calls getURIInfo() method to dereference the URI-typed object in HashMap and retrieve
  *     the targeted predicates values "if exist", it iterates over the HashMap and add them to the
  *     resources in the model.
  */
 private static void addAdditionalPropertiesUsingBlankNode(Map<String, String> predicates) {
   // Map <predicate,value> save each interesting predicate of the URI object
   Map<Property, List<RDFNode>> resourceInterestingInfoExtension =
       new HashMap<Property, List<RDFNode>>();
   // Map<object,objectResourceData> to save each object with its related data resource and be
   // retrieved whenever a URI object data needed to be added for extension
   Map<RDFNode, Resource> objectFilledResource = new HashMap<RDFNode, Resource>();
   // Get list of unique URI objects in the data source as http://dbpedia.org/resource/XXXX
   List<RDFNode> urisObjects = getURIObjects();
   // Get information for each single distinct objectURI according to interesting predicates
   logger.info("Number of unique URI object to find extension: " + urisObjects.size());
   if (urisObjects.size() > 0) {
     // The object resource that will have each URI object extended data
     Resource object = null;
     int count = 1;
     // For each URI object a resource is created, filled with information,add the object with its
     // data resource into map
     for (RDFNode uriObject : urisObjects) {
       logger.info("Predicate " + count++ + " of " + urisObjects.size() + ":" + uriObject);
       // Create a resource with empty node
       object = localModel.createResource();
       // Retrieve all interesting <predicate,object> info. for such URI object
       resourceInterestingInfoExtension = DereferencingModule.getURIInfo(uriObject);
       // Add information to the resource
       for (Property key : resourceInterestingInfoExtension.keySet()) {
         // add the new properties to the new triple
         List<RDFNode> subjects = resourceInterestingInfoExtension.get(key);
         for (RDFNode subject : subjects) {
           if (subject.isLiteral()) {
             object.addProperty(key, subject.asLiteral().toString());
           } else {
             object.addProperty(key, subject);
           }
         }
       }
       // Add to list of object's resource that is filled with information
       objectFilledResource.put(uriObject, object);
     }
   } else { // Otherwise no URI objects to be extended
     return;
   }
   List<Statement> triplesWithURIsObjects = getTriplesWithURIObjects();
   logger.info("Starting model enriching");
   if (triplesWithURIsObjects.size() > 0) {
     Resource object = null;
     // iterate over each triple to dereference each URI object and add its information to its
     // resource subject
     for (Statement triple : triplesWithURIsObjects) {
       // create new triple with empty node as its subject where this subject will be an object of
       // the targeted resource to be extended
       if (!objectFilledResource.containsKey(triple.getSubject())) {
         object = objectFilledResource.get(triple.getObject());
         objectsDerefModelAdded.put(triple.getObject(), object);
         // Attach the object's resource to this subject
         Resource resource = localModel.getResource(triple.getSubject().getURI());
         resource.addProperty(defaultOutputProperty, object);
         resourceInterestingInfoExtension = null;
       }
     }
   }
 }
Exemplo n.º 23
0
  /**
   * @param uri : the URI to be dereferenced
   * @param predicates : targeted predicates to be added to enrich the model
   * @return This method retrieves list of values for targeted predicates for a URI-typed object for
   *     each URI-typed object, through content negotiation an open connection is done retrieving
   *     its predicates/values. An iteration is made over targeted predicates. For each predicate
   *     list of statements with the targeted predicate is retrieved and extracting its value in
   *     order to be added to hashmap<predicate,Value>
   */
  @SuppressWarnings("unchecked")
  private static HashMap<Property, List<RDFNode>> getURIInfo(RDFNode p) {
    String uri = p.asResource().getURI();
    // to store each predicate and its value
    HashMap<Property, List<RDFNode>> resourceFocusedInfo = new HashMap<Property, List<RDFNode>>();

    if (demo) { // Deserialize the results if exists (For Demo purpose)
      if (useCache) {
        try {
          HashMap<String, List<String>> ser = new HashMap<String, List<String>>();
          File file = new File("resourceFocusedInfo.ser");
          if (file.exists()) {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            ser = (HashMap<String, List<String>>) in.readObject();
            in.close();
            // convert every object back from string
            for (String prop : ser.keySet()) {
              List<String> l = ser.get(prop);
              List<RDFNode> nodes = new ArrayList<RDFNode>();
              for (String n : l) {
                nodes.add(ResourceFactory.createResource(n));
              }
              resourceFocusedInfo.put(ResourceFactory.createProperty(prop), nodes);
            }
            return resourceFocusedInfo;
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    // define local model to have the data of the URI and extract focused info through built sparql
    // query
    List<RDFNode> values = new ArrayList<RDFNode>();
    try {
      URLConnection conn = new URL(uri).openConnection();
      conn.setRequestProperty("Accept", "application/rdf+xml");
      conn.setRequestProperty("Accept-Language", "en");
      Model model = ModelFactory.createDefaultModel();
      InputStream in = conn.getInputStream();
      model.read(in, null);
      for (Property inputProperty : inputProperties) {
        for (Statement st :
            model.listStatements(model.getResource(uri), inputProperty, (RDFNode) null).toList()) {
          RDFNode value = st.getObject();
          if (value.isLiteral()) {
            if (value.asLiteral().getLanguage().toLowerCase().equals("en")
                || value.asLiteral().getLanguage().toLowerCase().equals("")) {
              values.add(value);
            }
          } else {
            values.add(value);
          }
        }
        resourceFocusedInfo.put(inputProperty, values);
        values = new ArrayList<RDFNode>(); // create new list for new predicate
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (demo) { // serialize the output (for Demo purpose)
      try {
        HashMap<String, List<String>> ser = new HashMap<String, List<String>>();
        FileOutputStream fileOut = new FileOutputStream("resourceFocusedInfo.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        // convert to Serializabe Strings
        for (Property prop : resourceFocusedInfo.keySet()) {
          List<String> l = new ArrayList<String>();
          for (RDFNode n : resourceFocusedInfo.get(prop)) {
            l.add(n.toString());
          }
          ser.put(prop.toString(), l);
        }
        out.writeObject(ser);
        out.close();
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    }
    return resourceFocusedInfo;
  }
  /**
   * Get JSON map for a given resource by applying the river settings
   *
   * @param rs resource being processed
   * @param properties properties to be indexed
   * @param model model returned by the indexing query
   * @param getPropLabel if set to true all URI property values will be indexed as their label. The
   *     label is taken as the value of one of the properties set in {@link #uriDescriptionList}.
   * @return map of properties to be indexed for res
   */
  private Map<String, ArrayList<String>> getJsonMap(
      Resource rs, Set<Property> properties, Model model, boolean getPropLabel) {
    Map<String, ArrayList<String>> jsonMap = new HashMap<String, ArrayList<String>>();
    ArrayList<String> results = new ArrayList<String>();

    if (addUriForResource) {
      results.add("\"" + rs.toString() + "\"");
      jsonMap.put("http://www.w3.org/1999/02/22-rdf-syntax-ns#about", results);
    }

    Set<String> rdfLanguages = new HashSet<String>();

    for (Property prop : properties) {
      NodeIterator niter = model.listObjectsOfProperty(rs, prop);
      String property = prop.toString();
      results = new ArrayList<String>();

      String lang;
      String currValue;

      while (niter.hasNext()) {
        RDFNode node = niter.next();
        currValue = getStringForResult(node, getPropLabel);
        if (addLanguage) {
          if (node.isLiteral()) {
            lang = node.asLiteral().getLanguage();
            if (!lang.isEmpty()) {
              rdfLanguages.add("\"" + lang + "\"");
            }
          }
        }

        String shortValue = currValue;

        int currLen = currValue.length();
        // Unquote string
        if (currLen > 1) shortValue = currValue.substring(1, currLen - 1);

        // If either whiteMap does contains shortValue
        // or blackMap contains the value
        // skip adding it to the index
        boolean whiteMapCond =
            whiteMap.containsKey(property) && !whiteMap.get(property).contains(shortValue);
        boolean blackMapCond =
            blackMap.containsKey(property) && blackMap.get(property).contains(shortValue);

        if (whiteMapCond || blackMapCond) {
          continue;
        }

        if (normalizeObj.containsKey(shortValue)) {
          results.add("\"" + normalizeObj.get(shortValue) + "\"");
        } else {
          results.add(currValue);
        }
      }

      // Do not index empty properties
      if (results.isEmpty()) continue;

      if (normalizeProp.containsKey(property)) {
        property = normalizeProp.get(property);
        if (jsonMap.containsKey(property)) {
          jsonMap.get(property).addAll(results);
        } else {
          jsonMap.put(property, results);
        }
      } else {
        jsonMap.put(property, results);
      }
    }

    if (addLanguage) {
      if (rdfLanguages.isEmpty() && !language.isEmpty()) rdfLanguages.add(language);
      if (!rdfLanguages.isEmpty()) jsonMap.put("language", new ArrayList<String>(rdfLanguages));
    }

    for (Map.Entry<String, String> it : normalizeMissing.entrySet()) {
      if (!jsonMap.containsKey(it.getKey())) {
        ArrayList<String> res = new ArrayList<String>();
        res.add("\"" + it.getValue() + "\"");
        jsonMap.put(it.getKey(), res);
      }
    }

    return jsonMap;
  }
Exemplo n.º 25
0
  /**
   * Builds up statements like [] rr:template "http://data.example.com/department/{DEPTNO}" or []
   * rr:class ex:Department and returns them as a Statement List.
   *
   * @param r2rml the target com.hp.hpl.jena.rdf.model.Model
   * @param mappingData the target that should be mapped to relational structures (subject,
   *     predicate or object)
   * @param varDefs the construction definition of the target value based in the actual database
   *     value and some additional data like prefix strings or certain functions like uri( ... )
   * @return a List<Statement> containing all the subject map statements
   */
  private List<Statement> buildMapStatements(Model r2rml, Node mappingData, VarDefinition varDefs) {

    List<Statement> results = new ArrayList<Statement>();

    // a blank node []
    Resource mapSubject = ResourceFactory.createResource();
    // rr:template or rr:column or rr:constant
    Property mapPredicate;
    // a literal like "http://data.example.com/department/{DEPTNO}" or
    // simply "DEPTNO" (column name) or a constant "Foo bar!!"
    // (or in rare cases a URI, which is handled separately)
    Literal mapObject;

    // template or column or constant
    if (mappingData.isVariable()) {
      Collection<RestrictedExpr> restrictions = varDefs.getDefinitions((Var) mappingData);
      List<PredicateAndObject> mapPredicateAndObjects = processRestrictions(restrictions);

      for (PredicateAndObject result : mapPredicateAndObjects) {

        mapPredicate = result.getPrediacte();
        Statement resultStatement;
        RDFNode rawObject = result.getRawObject();

        // object is literal
        if (rawObject.isLiteral()) {
          mapObject = rawObject.asLiteral();
          resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapObject);

          // object is blank node
        } else if (rawObject.isAnon()) {
          Resource mapResObject = rawObject.asResource();
          resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapResObject);

          // object is resource
        } else {
          Resource mapResObject = rawObject.asResource();
          resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapResObject);
        }

        results.add(resultStatement);
      }

      // everything that is not a variable is handled as a constant
    } else if (mappingData.isConcrete()) {
      // URIs and Literals have to be handled separately since the methods
      // to retrieve the respective value are different

      Statement resultStatement;

      // URI
      if (mappingData.isURI()) {
        /*
         * This case is somewhat special since the mapObject is not a
         * Literal. So, this needs some special handling:
         * - the Literal mapObject will not be used
         * - a special mapObject_uri Resource will be created
         * - the result will be created, appended to the List and
         *   returned to not go through any further ordinary processing
         */

        Resource mapObject_uri = ResourceFactory.createResource(mappingData.getURI());
        mapPredicate = ResourceFactory.createProperty(rrNamespace, "constant");

        resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapObject_uri);
        results.add(resultStatement);

        return results;

        // Literal
      } else if (mappingData.isLiteral()) {

        mapObject = ResourceFactory.createPlainLiteral(mappingData.getLiteral().toString(false));

        // else (e.g. blank node)
      } else { // mapSubject.isBlank() == true
        /*
         * Hmm... I think this violates the standard. So lean back and
         *  enjoy the trace...
         */

        mapObject = null;
      }

      mapPredicate = ResourceFactory.createProperty(rrPrefix, "constant");

      resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapObject);
      results.add(resultStatement);
    }

    return results;
  }
Exemplo n.º 26
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;
  }