/** * 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); } }
/** * Selects a triple by providing an RDFStatement object collection * * @param stmts * @return Query result */ public List<RDFStatement> selectTriples(Collection<? extends RDFStatement> stmts) { if (stmts == null || stmts.isEmpty()) { return null; } RDFStatement rdfs = stmts.stream().findFirst().get(); String si = rdfs.getSubject(), pi = rdfs.getPredicate(), oi = rdfs.getObject(); // doesnt get value StringBuilder sb = new StringBuilder(); stmts .stream() .forEach( s -> { s.setObject(RDFUtils.escapeString(s.getObject())); sb.append(s.getSubject()) .append(" ") .append(s.getPredicate()) .append(" ") .append(s.getObject()) .append(" .\n"); }); String query = String.format( this.defaultPrefices + "SELECT * FROM <%s> WHERE { %s }", this.graphName, sb.toString()); Query sparqlQuery = QueryFactory.create(query); VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparqlQuery, this.graph); List<RDFStatement> stmtsList = new ArrayList<>(); ResultSet rs = vqe.execSelect(); while (rs.hasNext()) { QuerySolution qs = rs.nextSolution(); RDFNode s = qs.get(si); RDFNode p = qs.get(pi); RDFNode o = qs.get(oi); RDFStatement stmt = new RDFStatement( s != null ? s.toString() : "null", p != null ? p.toString() : "null", o != null ? RDFUtils.escapeString(o.toString()) : "null"); stmtsList.add(stmt); logger.info("fetched: {}", stmt.toString()); } return stmtsList; }
/** * 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; }
@SuppressWarnings("unchecked") private static List<RDFNode> getURIObjects() { List<RDFNode> objectsURIs = new ArrayList<RDFNode>(); if (demo) { // Deserialize the results if exists (For Demo purpose) if (useCache) { try { List<String> ser = new ArrayList<String>(); File file = new File("URIObjects.ser"); if (file.exists()) { ObjectInputStream in; in = new ObjectInputStream(new FileInputStream(file)); ser = (List<String>) in.readObject(); in.close(); // convert every object back from string for (String n : ser) { objectsURIs.add(ResourceFactory.createResource(n)); } return objectsURIs; } } catch (Exception e) { e.printStackTrace(); } } } // create a query to retrieve URIs objects String queryString = "SELECT * " + "WHERE { ?s ?p ?o . FILTER (isURI(?o)) . " + "FILTER (STRSTARTS(STR(?o), \"" + resourcePrefix + "\"))}"; Query query = QueryFactory.create(queryString); QueryExecution exec = QueryExecutionFactory.create(query, localModel); ResultSet rs = exec.execSelect(); while (rs.hasNext()) { QuerySolution sol = rs.next(); RDFNode object = sol.get("?o"); objectsURIs.add(object); } if (demo) { // serialize the output (for Demo purpose) try { FileOutputStream fileOut = new FileOutputStream("URIObjects.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); // convert to Serializabe Strings List<String> l = new ArrayList<String>(); for (RDFNode n : objectsURIs) { l.add(n.toString()); } out.writeObject(l); out.close(); } catch (Exception e2) { e2.printStackTrace(); } } return objectsURIs; }
@Test(enabled = true) public void selectAllNamedGraphs() { List<String> namedGraphs = new ArrayList<String>(); String query = "SELECT DISTINCT(?g) WHERE { GRAPH ?g { ?s ?p ?o } }"; ResultSet results = Util.executeQuery(query, virtDataset); while (results.hasNext()) { QuerySolution sol = results.next(); RDFNode nodeVar = sol.get("g"); namedGraphs.add(nodeVar.toString()); } System.out.println(namedGraphs); }
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; }
@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(); }
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(); }
public static void main(String[] args) { List<String> obj = new ArrayList<String>(); Scanner input = new Scanner(System.in); System.out.print("Enter URI: "); String userIn = input.nextLine(); // create an empty Model Model model = ModelFactory.createDefaultModel(); // read the RDF/XML file model.read(userIn); // write it to standard out // model.write(System.out); // list the statements in the Model StmtIterator iter = model.listStatements(); System.out.println(); // print out the predicate, subject and object of each statement while (iter.hasNext()) { Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object System.out.print(subject.toString()); System.out.print(" -> " + predicate.toString() + " -> "); if (object instanceof Resource) { System.out.print(object.toString() + "\n"); } else { // object is a literal System.out.print(" \"" + object.toString() + "\"\n"); } } /* for(int i = 0; i < (obj.size()); i++){ String sparqlQueryString1= "SELECT ?s ?o "+ "WHERE {"+ "?s ?p ?o ."+ "?o <bif:contains> \""+obj.get(i)+"\" ."+ "}"+ "limit 10"; Query query = QueryFactory.create(sparqlQueryString1); QueryExecution qexec = QueryExecutionFactory.sparqlService("http://pubmed.bio2rdf.org/sparql", query); ResultSet results = qexec.execSelect(); System.out.println("Query: "+obj.get(i)); ResultSetFormatter.out(System.out, results, query); qexec.close() ; } */ }
/** * @return list of triples having URI-typed objects it queries the model for all its URI-typed * objects as it check every object if it is a resource (URI). if so then add them to the * list. */ @SuppressWarnings("unchecked") private static List<Statement> getTriplesWithURIObjects() { List<Statement> triplesWithURIObjects = new ArrayList<Statement>(); if (demo) { // Deserialize the results if exists (For Demo purpose) if (useCache) { try { List<String> ser = new ArrayList<String>(); File file = new File("triplesWithURIObjects.ser"); if (file.exists()) { ObjectInputStream in; in = new ObjectInputStream(new FileInputStream(file)); ser = (List<String>) in.readObject(); in.close(); // convert every object back from string for (String st : ser) { triplesWithURIObjects.add( ResourceFactory.createStatement( ResourceFactory.createResource(st.split(" ")[0]), ResourceFactory.createProperty(st.split(" ")[1]), ResourceFactory.createResource(st.split(" ")[2]))); } return triplesWithURIObjects; } } catch (Exception e) { e.printStackTrace(); } } } // create a query to retrieve URIs objects String queryString = "SELECT * " + "WHERE { ?s ?p ?o . FILTER (isURI(?o)) . " + "FILTER (STRSTARTS(STR(?o), \"" + resourcePrefix + "\"))}"; Query query = QueryFactory.create(queryString); QueryExecution exec = QueryExecutionFactory.create(query, localModel); ResultSet rs = exec.execSelect(); while (rs.hasNext()) { QuerySolution sol = rs.next(); RDFNode s = sol.get("?s"); RDFNode p = sol.get("?p"); RDFNode o = sol.get("?o"); Statement triple = ResourceFactory.createStatement( s.asResource(), ResourceFactory.createProperty(p.toString()), o); triplesWithURIObjects.add(triple); } if (demo) { // serialize the output (for Demo purpose) try { FileOutputStream fileOut = new FileOutputStream("triplesWithURIObjects.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); List<String> l = new ArrayList<String>(); for (Statement s : triplesWithURIObjects) { l.add(s.getSubject() + " " + s.getPredicate() + " " + s.getObject()); } out.writeObject(l); out.close(); } catch (Exception e2) { e2.printStackTrace(); } } return triplesWithURIObjects; }
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++; } } } }
/** * @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; }