public static Map<String, RDFNode> solutionMap(QuerySolution qs, List<String> vars) { Map<String, RDFNode> result = new HashMap<String, RDFNode>(); for (String var : vars) { RDFNode val = qs.get(var); result.put(var, val.isAnon() ? DUMMY_FOR_BNODE : val); } return result; }
public static boolean isNativeRule(RDFNode node) { if (node != null && node.isAnon()) { if (((Resource) node).hasProperty(RDF.type, SH.NativeRule)) { return true; } if (!((Resource) node).hasProperty(RDF.type)) { return SH.NativeRule.equals(SHACLUtil.getDefaultTemplateType((Resource) node)); } } return false; }
public boolean doGet( MappedResource resource, Property property, boolean isInverse, HttpServletRequest request, HttpServletResponse response, Configuration config) throws IOException { Model descriptions = getAnonymousPropertyValues(resource, property, isInverse); if (descriptions.size() == 0) { return false; } Resource r = descriptions.getResource(resource.getWebURI()); List resourceDescriptions = new ArrayList(); StmtIterator it = isInverse ? descriptions.listStatements(null, property, r) : r.listProperties(property); while (it.hasNext()) { Statement stmt = it.nextStatement(); RDFNode value = isInverse ? stmt.getSubject() : stmt.getObject(); if (!value.isAnon()) { continue; } resourceDescriptions.add( new ResourceDescription((Resource) value.as(Resource.class), descriptions, config)); } Model description = getResourceDescription(resource); ResourceDescription resourceDescription = new ResourceDescription(resource, description, config); String title = resourceDescription.getLabel() + (isInverse ? " ? " : " ? ") + config.getPrefixes().getNsURIPrefix(property.getNameSpace()) + ":" + property.getLocalName(); VelocityHelper template = new VelocityHelper(getServletContext(), response); Context context = template.getVelocityContext(); context.put("project_name", config.getProjectName()); context.put("project_link", config.getProjectLink()); context.put("title", title); context.put("server_base", config.getWebApplicationBaseURI()); context.put("sparql_endpoint", resource.getDataset().getDataSource().getEndpointURL()); context.put("back_uri", resource.getWebURI()); context.put("back_label", resourceDescription.getLabel()); context.put( "rdf_link", isInverse ? resource.getInversePathDataURL(property) : resource.getPathDataURL(property)); context.put("resources", resourceDescriptions); template.renderXHTML("pathpage.vm"); return true; }
/** * 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; }
/** * Checks if a given node is a Shape. Note this is just an approximation based on a couple of * hard-coded properties. It should really rely on sh:defaultValueType. * * @param node the node to test * @return true if node is a Shape */ public static boolean isShape(RDFNode node) { if (node instanceof Resource) { if (JenaUtil.hasIndirectType((Resource) node, SH.Shape)) { return true; } else if (node.isAnon() && !((Resource) node).hasProperty(RDF.type)) { if (node.getModel().contains(null, SH.shape, node) || node.getModel().contains(null, SH.filterShape, node)) { return true; } } } return false; }
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"); } }
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()); } }
@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); } }
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--; }
/** * 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; }
/* (non-Javadoc) * @see com.hp.hpl.jena.rdf.model.RDFNode#isAnon() */ @Override public boolean isAnon() { return rdfNode.isAnon(); }