private boolean isObsolete(Resource ctx) throws SailException { CloseableIteration<? extends Statement, SailException> stmts; stmts = super.getStatements(null, null, null, true, ctx); try { while (stmts.hasNext()) { Statement st = stmts.next(); URI pred = st.getPredicate(); Value obj = st.getObject(); String ns = pred.getNamespace(); if (Audit.NAMESPACE.equals(ns) || PROV.equals(ns) || AUDIT_2012.equals(ns)) continue; if (RDF.SUBJECT.equals(pred) || RDF.PREDICATE.equals(pred) || RDF.OBJECT.equals(pred)) continue; if (RDF.TYPE.equals(pred) && obj instanceof URI) { ns = ((URI) obj).getNamespace(); if (Audit.NAMESPACE.equals(ns) || PROV.equals(ns) || AUDIT_2012.equals(ns) || RDF.NAMESPACE.equals(ns)) continue; } return false; } } finally { stmts.close(); } return true; }
/** * Constant-valued term maps can be expressed more concisely using the constant shortcut * properties rr:subject, rr:predicate, rr:object and rr:graph. Occurrances of these properties * must be treated exactly as if the following triples were present in the mapping graph instead. * * @param r2rmlMappingGraph */ private static void replaceShortcuts(SesameDataSet r2rmlMappingGraph) { Map<URI, URI> shortcutPredicates = new HashMap<URI, URI>(); shortcutPredicates.put( vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.SUBJECT), vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.SUBJECT_MAP)); shortcutPredicates.put( vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.PREDICATE), vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.PREDICATE_MAP)); shortcutPredicates.put( vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.OBJECT), vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.OBJECT_MAP)); shortcutPredicates.put( vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.GRAPH), vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.GRAPH_MAP)); for (URI u : shortcutPredicates.keySet()) { List<Statement> shortcutTriples = r2rmlMappingGraph.tuplePattern(null, u, null); log.debug( "[RMLMappingFactory:replaceShortcuts] Number of R2RML shortcuts found " + "for " + u.getLocalName() + " : " + shortcutTriples.size()); for (Statement shortcutTriple : shortcutTriples) { r2rmlMappingGraph.remove( shortcutTriple.getSubject(), shortcutTriple.getPredicate(), shortcutTriple.getObject()); BNode blankMap = vf.createBNode(); URI pMap = vf.createURI(shortcutPredicates.get(u).toString()); URI pConstant = vf.createURI(Vocab.R2RML_NAMESPACE + R2RMLTerm.CONSTANT); r2rmlMappingGraph.add(shortcutTriple.getSubject(), pMap, blankMap); r2rmlMappingGraph.add(blankMap, pConstant, shortcutTriple.getObject()); } } }
private Pair toPair(Edge e, Graph graph) { URI predicate = e.getPredicate(); Value object = e.getObject(); String value = null; if (object instanceof Literal) { Literal literal = (Literal) object; String language = literal.getLanguage(); URI type = literal.getDatatype(); if (type.equals(XMLSchema.STRING)) { type = null; } StringBuilder builder = new StringBuilder(); builder.append('"'); builder.append(literal.getLabel()); builder.append('"'); if (language != null) { builder.append('@'); builder.append(language); } else if (type != null) { builder.append('^'); builder.append(type.stringValue()); } value = builder.toString(); } else if (object instanceof URI) { value = object.stringValue(); } else { Resource id = (Resource) object; Vertex v = graph.getVertex(id); value = createHash(predicate, v); } return new Pair(predicate, value); }
public boolean matches(final Vertex vertex, final Value value) { String kind = (String) vertex.getProperty(KIND); String val = (String) vertex.getProperty(VALUE); if (value instanceof URI) { return kind.equals(URI) && val.equals(value.stringValue()); } else if (value instanceof Literal) { if (kind.equals(LITERAL)) { if (!val.equals(((Literal) value).getLabel())) { return false; } String type = (String) vertex.getProperty(TYPE); String lang = (String) vertex.getProperty(LANG); URI vType = ((Literal) value).getDatatype(); String vLang = ((Literal) value).getLanguage(); return null == type && null == vType && null == lang && null == vLang || null != type && null != vType && type.equals(vType.stringValue()) || null != lang && null != vLang && lang.equals(vLang); } else { return false; } } else if (value instanceof BNode) { return kind.equals(BNODE) && ((BNode) value).getID().equals(val); } else { throw new IllegalStateException("value of unexpected kind: " + value); } }
private void upload(URI graphURI, Resource context) throws Exception { RepositoryConnection con = dataRep.getConnection(); con.setAutoCommit(false); try { RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString(), RDFFormat.TURTLE); RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory()); rdfParser.setVerifyData(false); rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE); // rdfParser.setPreserveBNodeIDs(true); RDFInserter rdfInserter = new RDFInserter(con); rdfInserter.enforceContext(context); rdfParser.setRDFHandler(rdfInserter); URL graphURL = new URL(graphURI.toString()); InputStream in = graphURL.openStream(); try { rdfParser.parse(in, graphURI.toString()); } finally { in.close(); } con.setAutoCommit(true); } finally { con.close(); } }
@Override public void handleStatement(Statement st) throws RDFHandlerException { URI predicate = st.getPredicate(); if (!CORE.NS.equals(predicate.getNamespace()) || !predicate.getLocalName().equals("localId")) { super.handleStatement(st); } }
/** * Describe the vocabularies which are in use in the KB based on the predicate partition * statistics. * * @param predicateParitionCounts The predicate partition statistics. */ protected void describeVocabularies(final IVCount[] predicatePartitionCounts) { // Find the distinct vocabularies in use. final Set<String> namespaces = new LinkedHashSet<String>(); { // property partitions. for (IVCount tmp : predicatePartitionCounts) { final URI p = (URI) tmp.getValue(); String namespace = p.getNamespace(); if (namespace.endsWith("#")) { // Strip trailing '#' per VoID specification. namespace = namespace.substring(0, namespace.length() - 1); } namespaces.add(namespace); } } // Sort into dictionary order. final String[] a = namespaces.toArray(new String[namespaces.size()]); Arrays.sort(a); for (String namespace : a) { g.add(aDataset, VoidVocabularyDecl.vocabulary, f.createURI(namespace)); } }
/** * Term constants for the DC music ontology * * @author Michael Grove * @since 0.1 */ public class MusicOntology extends Vocabulary { public static final String ONT_URI = "http://purl.org/ontology/mo/"; private static MusicOntology INSTANCE = null; private MusicOntology() { super(ONT_URI); } public static MusicOntology ontology() { if (INSTANCE == null) { INSTANCE = new MusicOntology(); } return INSTANCE; } // properties public final URI track = term("track"); public final URI release_type = term("release_type"); public final URI release_status = term("release_status"); public final URI track_number = term("track_number"); public final URI length = term("length"); public final URI made = term("made"); public final URI musicbrainz = term("musicbrainz"); public final URI olga = term("olga"); public final URI genre = term("genre"); public final URI sample_rate = term("sample_rate"); public final URI bitsPerSample = term("bitsPerSample"); // cp properties public final URI rating = term("rating"); public final URI albumRating = term("albumRating"); public final URI year = term("year"); public final URI location = term("location"); // classes public final URI Genre = term("Genre"); public final URI Record = term("Record"); public final URI Track = term("Track"); public final URI MusicArtist = term("MusicArtist"); public final URI MusicGroup = term("MusicGroup"); // individuals public final URI Metal = FACTORY.createURI(Genre.stringValue() + "/Metal"); public final URI Rock = FACTORY.createURI(Genre.stringValue() + "/Rock"); public final URI Alternative = FACTORY.createURI(Genre.stringValue() + "/Alternative"); public final URI Pop = FACTORY.createURI(Genre.stringValue() + "/Pop"); public final URI Punk = FACTORY.createURI(Genre.stringValue() + "/Punk"); public final URI Funk = FACTORY.createURI(Genre.stringValue() + "/Funk"); public final URI Soundtrack = FACTORY.createURI(Genre.stringValue() + "/Soundtrack"); public final URI Blues = FACTORY.createURI(Genre.stringValue() + "/Blues"); public final URI Jazz = FACTORY.createURI(Genre.stringValue() + "/Jazz"); public final URI Vocal = FACTORY.createURI(Genre.stringValue() + "/Vocal"); public final URI Country = FACTORY.createURI(Genre.stringValue() + "/Country"); public final URI album = term("album"); public final URI official = term("official"); }
private Collection<String> getEntryForUriSuffix(Map<URI, Collection<String>> map, String suffix) { for (URI resource : map.keySet()) { if (resource.stringValue().endsWith(suffix)) { return map.get(resource); } } return null; }
protected String predicateToID(URI predicate, SailConnection connection) { if (predicate.equals(s_type)) { return "pub-type"; } else if (predicate.equals(s_title)) { return null; } return super.predicateToID(predicate, connection); }
private Resource getContainerURI(Resource subj) { if (subj instanceof URI) { URI uri = (URI) subj; String ns = uri.getNamespace(); if (ns.charAt(ns.length() - 1) == '#') return vf.createURI(ns.substring(0, ns.length() - 1)); } return subj; }
@Override public IV get(final IBindingSet bs) { URI datatype = null; String lang = null; boolean allSame = true; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < arity(); i++) { @SuppressWarnings("rawtypes") final IV v = getAndCheckLiteral(i, bs); String label = null; if (allSame) { final Literal lit = asLiteral(v); label = lit.getLabel(); if (lit.getDatatype() != null) { if (lang != null) { allSame = false; } else if (datatype == null) { if (i == 0) { datatype = lit.getDatatype(); } else { allSame = false; } } else if (!datatype.equals(lit.getDatatype())) { allSame = false; } } else if (lit.getLanguage() != null) { if (datatype != null) { allSame = false; } else if (lang == null) { if (i == 0) { lang = lit.getLanguage(); } else { allSame = false; } } else if (!lang.equals(lit.getLanguage())) { allSame = false; } } else { allSame = false; } } else { label = literalLabel(v); } sb.append(label); } if (allSame) { if (datatype != null) { return super.asIV(getValueFactory().createLiteral(sb.toString(), datatype), bs); } else if (lang != null) { return super.asIV(getValueFactory().createLiteral(sb.toString(), lang), bs); } } return super.asIV(getValueFactory().createLiteral(sb.toString()), bs); }
private static String getSuffix(URI plainURI, URI baseURI) { if (baseURI == null) return null; String b = baseURI.toString(); String p = plainURI.toString(); if (p.equals(b)) { return null; } else if (p.startsWith(b)) { return p.substring(b.length()); } else { return null; } }
private static ReferencingObjectMap extractReferencingObjectMap( SesameDataSet r2rmlMappingGraph, Resource object, Set<GraphMap> graphMaps, Map<Resource, TriplesMap> triplesMapResources) throws InvalidR2RMLStructureException, InvalidR2RMLSyntaxException { log.debug("[RMLMappingFactory:extractReferencingObjectMap] Extract referencing object map.."); URI parentTriplesMap = (URI) extractValueFromTermMap(r2rmlMappingGraph, object, R2RMLTerm.PARENT_TRIPLES_MAP); Set<JoinCondition> joinConditions = extractJoinConditions(r2rmlMappingGraph, object); if (parentTriplesMap == null && !joinConditions.isEmpty()) { throw new InvalidR2RMLStructureException( "[RMLMappingFactory:extractReferencingObjectMap] " + object.stringValue() + " has no parentTriplesMap map defined whereas one or more joinConditions exist" + " : exactly one parentTripleMap is required."); } if (parentTriplesMap == null && joinConditions.isEmpty()) { log.debug( "[RMLMappingFactory:extractReferencingObjectMap] This object map is not a referencing object map."); return null; } // Extract parent boolean contains = false; TriplesMap parent = null; for (Resource triplesMapResource : triplesMapResources.keySet()) { if (triplesMapResource.stringValue().equals(parentTriplesMap.stringValue())) { contains = true; parent = triplesMapResources.get(triplesMapResource); log.debug( "[RMLMappingFactory:extractReferencingObjectMap] Parent triples map found : " + triplesMapResource.stringValue()); break; } } if (!contains) { throw new InvalidR2RMLStructureException( "[RMLMappingFactory:extractReferencingObjectMap] " + object.stringValue() + " reference to parent triples maps is broken : " + parentTriplesMap.stringValue() + " not found."); } // Link between this reerencing object and its triplesMap parent will be // performed // at the end f treatment. ReferencingObjectMap refObjectMap = new StdReferencingObjectMap(null, parent, joinConditions); log.debug( "[RMLMappingFactory:extractReferencingObjectMap] Extract referencing object map done."); return refObjectMap; }
public boolean equals(final Object o) { if (this == o) return true; if (o instanceof FullyInlineURIIV<?>) { return uri.stringValue().equals(((FullyInlineURIIV<?>) o).stringValue()); } return false; }
/** * Insert Triple/Statement into graph * * @param s subject uriref * @param p predicate uriref * @param o value object (URIref or Literal) * @param contexts varArgs context objects (use default graph if null) */ public void add(Resource s, URI p, Value o, Resource... contexts) { if (log.isDebugEnabled()) log.debug( "[SesameDataSet:add] Add triple (" + s.stringValue() + ", " + p.stringValue() + ", " + o.stringValue() + ")."); try { RepositoryConnection con = currentRepository.getConnection(); try { ValueFactory myFactory = con.getValueFactory(); Statement st = myFactory.createStatement((Resource) s, p, (Value) o); con.add(st, contexts); con.commit(); } catch (Exception e) { e.printStackTrace(); } finally { con.close(); } } catch (Exception e) { // handle exception } }
@Test public void testSize() throws Exception { assertEquals("Size of empty repository should be 0", 0, con.size()); // Add some data to the repository con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.addStatement(picasso, paints, guernica, context1); con.commit(); assertEquals("Size of repository should be 5", 5, con.size()); assertEquals("Size of named context should be 3", 3, con.size(context1)); URI unknownContext = new URIImpl(EXAMPLE_NS + "unknown"); assertEquals("Size of unknown context should be 0", 0, con.size(unknownContext)); URIImpl uriImplContext1 = new URIImpl(context1.toString()); assertEquals( "Size of named context (defined as URIImpl) should be 3", 3, con.size(uriImplContext1)); }
private static String expandBaseURI(URI baseURI) { String s = baseURI.toString(); if (s.matches(".*[A-Za-z0-9\\-_]")) { s += "."; } return s; }
public static String normalize(URI uri, String hash) { String s = uri.toString(); if (s.indexOf('\n') > -1 || s.indexOf('\t') > -1) { throw new RuntimeException("Newline or tab character in URI: " + s); } if (hash == null) return s; return s.replace(hash, " "); }
private void storeStatement(Resource subj, URI pred, Value obj, Resource... contexts) throws SailException { if (subj.equals(currentTrx)) { subj = getTrx(); } if (obj.equals(currentTrx)) { obj = getTrx(); } if (contexts != null && contexts.length == 1) { if (currentTrx.equals(contexts[0])) { contexts[0] = getTrx(); } } else if (contexts != null) { for (int i = 0; i < contexts.length; i++) { if (currentTrx.equals(contexts[i])) { contexts[i] = getTrx(); } } } if (contexts == null || contexts.length == 0 || contexts.length == 1 && contexts[0] == null) { addRevision(subj); super.addStatement(subj, pred, obj, getTrx()); } else if (contexts.length == 1) { if (contexts[0].equals(trx)) { addRevision(subj); } super.addStatement(subj, pred, obj, contexts); Resource ctx = contexts[0]; if (isURI(ctx) && !ctx.equals(trx) && modified.add(ctx)) { addMetadata(currentTrx, MODIFIED, ctx, currentTrx); } } else { for (Resource ctx : contexts) { if (ctx == null || ctx.equals(trx)) { addRevision(subj); break; } } super.addStatement(subj, pred, obj, contexts); for (Resource ctx : contexts) { if (isURI(ctx) && !ctx.equals(trx) && modified.add(ctx)) { addMetadata(currentTrx, MODIFIED, ctx, currentTrx); } } } }
public static void setLabel( RepositoryConnection repCon, org.openrdf.model.URI concept, org.openrdf.model.URI labelType, Literal newlabel, PersistAndNotifyProvider persistAndNotifyProvider) throws RepositoryException { repCon.add(concept, labelType, newlabel); persistAndNotifyProvider.persistAndNotify( Helper.createChangeSetModel( concept.stringValue(), labelType.stringValue(), newlabel, ChangeTripleService.CHANGETYPE_ADD), true); }
// TODO: MimeType detector to null forces the execution of all extractors, but extraction // tests should be based on mimetype detection. protected void extract(String resource) throws ExtractionException, IOException { SingleDocumentExtraction ex = new SingleDocumentExtraction( new HTMLFixture(copyResourceToTempFile(resource)).getOpener(baseURI.toString()), getExtractorFactory(), new RepositoryWriter(conn)); ex.setMIMETypeDetector(null); report = ex.run(); }
private Response get(URI user) { if (userService.isAnonymous(user)) { AccountPoJo apj = new AccountPoJo(Namespaces.ANONYMOUS_LOGIN, user.stringValue()); return Response.ok(apj, Namespaces.MIME_TYPE_JSON) .location(java.net.URI.create(user.stringValue())) .build(); } try { RepositoryConnection conn = sesameService.getConnection(); try { final UserAccount a = accountService.getAccount(user); if (a != null) { AccountPoJo apj = new AccountPoJo(a.getLogin(), a.getWebId()); apj.setRoles(a.getRoles()); for (Statement t : ResourceUtils.listOutgoing(conn, conn.getValueFactory().createURI(a.getWebId()))) { String prop = t.getPredicate().stringValue(); if (prop.startsWith(Namespaces.NS_FOAF)) { Value object = t.getObject(); if (object instanceof org.openrdf.model.URI) { apj.setFoaf(prop, String.format("<%s>", object)); } else if (object instanceof Literal) { apj.setFoaf(prop, object.toString()); } } } return Response.ok(apj, Namespaces.MIME_TYPE_JSON) .location(java.net.URI.create(user.stringValue())) .build(); } return Response.status(Status.NOT_FOUND) .entity("Could not find account data of " + user) .build(); } finally { conn.commit(); conn.close(); } } catch (RepositoryException e) { // This must not happen! return Response.serverError().entity(e).build(); } }
private void reify(URI activity, URI entity, Resource subj, URI pred, Value obj, Resource ctx) throws SailException { String ns = activity.stringValue(); if (ctx instanceof URI) { super.addStatement(activity, informedBy, ctx, activity); } if (entity == null || GENERATED_BY.equals(pred.stringValue())) return; URI operation = vf.createURI(ns + "#" + hash(ctx, entity)); if (ctx instanceof URI) { super.addStatement(ctx, qualifiedUsage, operation, activity); } super.addStatement(activity, qualifiedUsage, operation, activity); super.addStatement(operation, usedEntity, entity, activity); Resource node = vf.createBNode(); super.addStatement(operation, changed, node, activity); super.addStatement(node, subject, subj, activity); super.addStatement(node, predicate, pred, activity); super.addStatement(node, object, obj, activity); }
@Override public int compareTo(Pair other) { int result = predicate.stringValue().compareTo(other.predicate.stringValue()); if (result == 0) { result = value.compareTo(other.value); } return result; }
/** * LDP-Style to serialize a resource. * * @param writer the writer to serialize to * @param subject the resource to serialize * @param iteration the Iteration containing the data * @throws RDFHandlerException * @throws RepositoryException */ public static void exportIteration( RDFWriter writer, URI subject, CloseableIteration<Statement, RepositoryException> iteration) throws RDFHandlerException, RepositoryException { writer.startRDF(); writer.handleNamespace(LDP.PREFIX, LDP.NAMESPACE); writer.handleNamespace(RDF.PREFIX, RDF.NAMESPACE); writer.handleNamespace(XSD.PREFIX, XSD.NAMESPACE); writer.handleNamespace(DCTERMS.PREFIX, DCTERMS.NAMESPACE); writer.handleNamespace("parent", subject.getNamespace()); writer.handleNamespace("child", subject.stringValue().replaceFirst("/*$", "/")); writer.handleNamespace("this", subject.stringValue().replaceFirst("/*$", "#")); while (iteration.hasNext()) { writer.handleStatement(iteration.next()); } writer.endRDF(); }
@Override public int _compareTo(final IV o) { final FullyInlineURIIV<?> t = (FullyInlineURIIV<?>) o; return IVUnicode.IVUnicodeComparator.INSTANCE.compare(uri.stringValue(), t.uri.stringValue()); // return uri.stringValue().compareTo(id2); // return id == id2 ? 0 : id < id2 ? -1 : 1; }
@SuppressWarnings("unchecked") public V asValue(final LexiconRelation lex) { V v = getValueCache(); if (v == null) { final BigdataValueFactory f = lex.getValueFactory(); v = (V) f.createURI(uri.stringValue()); v.setIV(this); setValue(v); } return v; }
public static URI getHashURI( Resource resource, URI baseURI, String hash, Map<String, Integer> blankNodeMap) { if (resource == null) { return null; } else if (resource instanceof URI) { URI plainURI = (URI) resource; if (plainURI.toString().matches(".*(\\n|\\t).*")) { throw new RuntimeException("Newline or tab character in URI: " + plainURI.toString()); } String suffix = getSuffix(plainURI, baseURI); if (suffix == null && !plainURI.equals(baseURI)) { return plainURI; } return new URIImpl(getHashURIString(baseURI, hash, suffix)); } else { BNode blankNode = (BNode) resource; int n = getBlankNodeNumber(blankNode, blankNodeMap); return new URIImpl(expandBaseURI(baseURI) + hash + ".." + n); } }
public synchronized BlobObject getBlobObject(final String uri) throws RepositoryException { if (blobs == null) throw new RepositoryException("No configured blob store"); try { if (blobVersion == null && isAutoCommit()) { return blobs.open(uri); } else if (blobVersion == null) { URI version = getVersionBundle(); if (version == null) { blobVersion = blobs.newVersion(); } else { blobVersion = blobs.newVersion(version.stringValue()); } return blobVersion.open(uri); } else { return blobVersion.open(uri); } } catch (IOException exc) { throw new RepositoryException(exc); } }