// keyToUse is the URI of the next instance that can be found in // myRepository public SpinInferencingRuleImpl( final Collection<Statement> inputStatements, final URI keyToUse, final int modelVersion) throws OpenRDFException { super(inputStatements, keyToUse, modelVersion); final Collection<Statement> currentUnrecognisedStatements = this.resetUnrecognisedStatements(); for (final Statement nextStatement : currentUnrecognisedStatements) { if (nextStatement.getPredicate().equals(RDF.TYPE) && nextStatement .getObject() .equals(SpinInferencingRuleSchema.getSpinInferencingRuleTypeUri())) { if (SpinInferencingRuleImpl.TRACE) { SpinInferencingRuleImpl.log.trace( "SparqlNormalisationRuleImpl: found valid type predicate for URI: " + keyToUse); } // isValid = true; this.setKey(keyToUse); } else { if (SpinInferencingRuleImpl.DEBUG) { SpinInferencingRuleImpl.log.debug( "SparqlNormalisationRuleImpl: unrecognisedStatement nextStatement: " + nextStatement.toString()); } this.addUnrecognisedStatement(nextStatement); } } if (SpinInferencingRuleImpl.DEBUG) { SpinInferencingRuleImpl.log.debug( "SparqlNormalisationRuleImpl constructor: toString()=" + this.toString()); } }
/* (non-Javadoc) * @see org.openrdf.rio.helpers.RDFHandlerBase#handleStatement(org.openrdf.model.Statement) */ @Override public void handleStatement(final Statement stmt) throws RDFHandlerException { final String predicate = stmt.getPredicate().stringValue(); if (!predicate.startsWith("http://psi.ontopia.net/rdf2tm/#")) { return; } final String subject = stmt.getSubject().stringValue(); if (!(stmt.getObject() instanceof URI)) { throw new RDFHandlerException( new MIOException("The object of the RDF mapping must be an IRI")); } final String object = stmt.getObject().stringValue(); if (RDF2TM.MAPS_TO.equals(predicate)) { if (RDF2TM.BASENAME.equals(object)) { getMapping(subject).kind = NAME; } else if (RDF2TM.OCCURRENCE.equals(object)) { getMapping(subject).kind = OCCURRENCE; } else if (RDF2TM.INSTANCE_OF.equals(object)) { getMapping(subject).kind = TYPE_INSTANCE; } else if (RDF2TM.ASSOCIATION.equals(object)) { getMapping(subject).kind = ASSOCIATION; } else if (RDF2TM.SUBJECT_IDENTIFIER.equals(object)) { try { _handler.handleSubjectIdentifier(subject); } catch (MIOException ex) { throw new RDFHandlerException(ex); } } else if (RDF2TM.SUBJECT_LOCATOR.equals(object)) { try { _handler.handleSubjectLocator(subject); } catch (MIOException ex) { throw new RDFHandlerException(ex); } } else if (RDF2TM.SOURCE_LOCATOR.equals(object)) { try { _handler.handleItemIdentifier(subject); } catch (MIOException ex) { throw new RDFHandlerException(ex); } } else { throw new RDFHandlerException( new MIOException( "Unknown object of a rtm:maps-to mapping. Object: <" + object + ">")); } } else if (RDF2TM.TYPE.equals(predicate)) { getMapping(subject).type = object; } else if (RDF2TM.IN_SCOPE.equals(predicate)) { Mapping mapping = getMapping(subject); if (mapping.scope == null) { mapping.scope = new ArrayList<String>(); } mapping.scope.add(object); } else if (RDF2TM.SUBJECT_ROLE.equals(predicate)) { getMapping(subject).subject = object; } else if (RDF2TM.OBJECT_ROLE.equals(predicate)) { getMapping(subject).object = object; } else { throw new RDFHandlerException(new MIOException("Unknown predicate <" + predicate + ">")); } }
private static Set<JoinCondition> extractJoinConditions( SesameDataSet r2rmlMappingGraph, Resource object) throws InvalidR2RMLStructureException, InvalidR2RMLSyntaxException { log.debug("[RMLMappingFactory:extractJoinConditions] Extract join conditions.."); Set<JoinCondition> result = new HashSet<JoinCondition>(); // Extract predicate-object maps URI p = r2rmlMappingGraph.URIref(Vocab.R2RML_NAMESPACE + R2RMLTerm.JOIN_CONDITION); List<Statement> statements = r2rmlMappingGraph.tuplePattern(object, p, null); try { for (Statement statement : statements) { Resource jc = (Resource) statement.getObject(); String child = extractLiteralFromTermMap(r2rmlMappingGraph, jc, R2RMLTerm.CHILD); String parent = extractLiteralFromTermMap(r2rmlMappingGraph, jc, R2RMLTerm.PARENT); if (parent == null || child == null) { throw new InvalidR2RMLStructureException( "[RMLMappingFactory:extractReferencingObjectMap] " + object.stringValue() + " must have exactly two properties child and parent. "); } result.add(new StdJoinCondition(child, parent)); } } catch (ClassCastException e) { throw new InvalidR2RMLStructureException( "[RMLMappingFactory:extractJoinConditions] " + "A resource was expected in object of predicateMap of " + object.stringValue()); } log.debug("[RMLMappingFactory:extractJoinConditions] Extract join conditions done."); return result; }
protected List<Statement> nextNonAggregateDocument() throws QueryEvaluationException { try { List<Statement> document = new ArrayList<Statement>(); if (!statefulHasNext()) return document; Statement stmt = peekNextStatement(); if (stmt == null) return document; Resource subject = stmt.getSubject(); Resource current = subject; document.add(stmt); while ((current.equals(subject) && statefulHasNext())) { advance(); current = subject; stmt = peekNextStatement(); if (stmt != null) { subject = stmt.getSubject(); if (subject.equals(current)) document.add(stmt); } else subject = null; } // System.out.println(document); return document; } catch (Exception e) { throw new QueryEvaluationException(e); } }
private void removeInforming( URI activity, URI entity, Resource subj, URI pred, Value obj, Resource... contexts) throws SailException { if (contexts != null && contexts.length == 0) { CloseableIteration<? extends Statement, SailException> stmts; stmts = super.getStatements(subj, pred, obj, false, contexts); try { while (stmts.hasNext()) { Statement st = stmts.next(); Resource ctx = st.getContext(); subj = st.getSubject(); pred = st.getPredicate(); obj = st.getObject(); removeInformingGraph(activity, entity, subj, pred, obj, ctx); } } finally { stmts.close(); } } else if (contexts == null) { removeInformingGraph(activity, entity, subj, pred, obj, null); } else { for (Resource ctx : contexts) { removeInformingGraph(activity, entity, subj, pred, obj, ctx); } } }
/** * Construct TriplesMap objects rule. A triples map is represented by a resource that references * the following other resources : - It must have exactly one subject map * using the * rr:subjectMap property. * * @param r2rmlMappingGraph * @return * @throws InvalidR2RMLStructureException */ private static Map<Resource, TriplesMap> extractTripleMapResources( SesameDataSet r2rmlMappingGraph) throws InvalidR2RMLStructureException { // A triples map is represented by a resource that references the // following other resources : // - It must have exactly one subject map Map<Resource, TriplesMap> triplesMapResources = new HashMap<Resource, TriplesMap>(); URI p = r2rmlMappingGraph.URIref(Vocab.R2RML_NAMESPACE + Vocab.R2RMLTerm.SUBJECT_MAP); List<Statement> statements = r2rmlMappingGraph.tuplePattern(null, p, null); if (statements.isEmpty()) { log.warn("[RMLMappingFactory:extractRMLMapping] No subject statement found. Exit..."); } /* * throw new InvalidR2RMLStructureException( * "[RMLMappingFactory:extractRMLMapping]" + * " One subject statement is required."); */ else // No subject map, Many shortcuts subjects { for (Statement s : statements) { List<Statement> otherStatements = r2rmlMappingGraph.tuplePattern(s.getSubject(), p, null); if (otherStatements.size() > 1) { throw new InvalidR2RMLStructureException( "[RMLMappingFactory:extractRMLMapping] " + s.getSubject() + " has many subjectMap " + "(or subject) but only one is required."); } else // First initialization of triples map : stored to link them // with referencing objects { triplesMapResources.put( s.getSubject(), new StdTriplesMap(null, null, null, s.getSubject().stringValue())); } } } return triplesMapResources; }
private void populateBindingSets( List<QueryBindingSet> results, Var predVar, Var objVar, Collection<Statement> stmts) { if (predVar == null || objVar == null || stmts == null || stmts.size() == 0) return; List<QueryBindingSet> copyOf = new ArrayList<QueryBindingSet>(results); int i = copyOf.size(); int j = 0; for (Iterator<Statement> iter = stmts.iterator(); iter.hasNext(); ) { Statement st = iter.next(); int k = 0; for (QueryBindingSet result : results) { if (!result.hasBinding(predVar.getName()) || k >= i) { String name = predVar.getName(); org.openrdf.model.Value val = st.getPredicate(); addBinding(result, name, val); } if (!result.hasBinding(objVar.getName()) || k >= i) addBinding(result, objVar.getName(), st.getObject()); k++; } i = copyOf.size() + j * copyOf.size(); j++; if (iter.hasNext()) { // copy results for (QueryBindingSet copy : copyOf) { results.add(new QueryBindingSet(copy)); } } } }
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; }
@Override public void handleStatement(Statement st) { String stSubject = st.getSubject().toString(); String stPredicate = st.getPredicate().toString(); String stObject = st.getObject().toString(); saveStatement(stSubject, stPredicate, stObject); }
/** **********************PROPERTIES************************ */ public Collection<String> getAllObjectProperties() { Set<String> allObjectProperties = new HashSet<>(); // check if property is object property URI rangeOfUri = this.repository.getValueFactory().createURI(rangeAsText); try { RepositoryConnection repoConn = this.repository.getConnection(); RepositoryResult<Statement> results = repoConn.getStatements(null, rangeOfUri, null, false); while (results.hasNext()) { Statement tempResult = results.next(); // if range is not rdf:resource literal means that is an object property if (!tempResult.getObject().stringValue().equals(literalAsText)) { allObjectProperties.add(tempResult.getSubject().stringValue()); } } repoConn.close(); } catch (RepositoryException ex) { } URI typeOfUri = this.repository.getValueFactory().createURI(typeOfUriAsText); URI objectProperyUri = this.repository.getValueFactory().createURI(owlObjectProperyUriAsText); try { RepositoryConnection repoConn = this.repository.getConnection(); RepositoryResult<Statement> results = repoConn.getStatements(null, typeOfUri, objectProperyUri, false); while (results.hasNext()) { allObjectProperties.add(results.next().getSubject().stringValue()); } repoConn.close(); } catch (RepositoryException ex) { } return allObjectProperties; }
@Override public void handleStatement(Statement st) throws RDFHandlerException { if (!writingStarted) { throw new RuntimeException("Document writing has not yet been started"); } try { // SUBJECT NTriplesUtil.append(st.getSubject(), writer); writer.write(" "); // PREDICATE NTriplesUtil.append(st.getPredicate(), writer); writer.write(" "); // OBJECT NTriplesUtil.append(st.getObject(), writer); if (null != st.getContext()) { writer.write(" "); NTriplesUtil.append(st.getContext(), writer); } writer.write(" .\n"); } catch (IOException e) { throw new RDFHandlerException(e); } }
private int findSavedIndex(ReloRdfRepository repo, Resource codeRes) { Statement indexWhenSavedStmt = repo.getStatement(codeRes, RJCore.index, null); int indexWhenSaved = (indexWhenSavedStmt == null || indexWhenSavedStmt.getObject() == null) ? -1 : Integer.parseInt(indexWhenSavedStmt.getObject().toString()); return indexWhenSaved; }
private Object encodeStatement(final Statement statement) { return SerializerAvro.newGenericRecord( Schemas.STATEMENT, encodeIdentifier(statement.getSubject()), encodeIdentifier(statement.getPredicate()), // encodeValue(statement.getObject()), // encodeIdentifier(statement.getContext())); }
// TODO: bnode check is too weak, introduce graph omomorphism check. protected void assertContains(Statement statement) throws RepositoryException { Assert.assertTrue( "Cannot find statement " + statement + " in model.", conn.hasStatement( statement.getSubject() instanceof BNode ? null : statement.getSubject(), statement.getPredicate(), statement.getObject() instanceof BNode ? null : statement.getObject(), false)); }
@Override public void overlay(Model m, IEngine engine) { labelcache.setEngine(engine); List<Value[]> valdata = new ArrayList<>(); for (Statement s : m) { valdata.add(new Value[] {s.getSubject(), s.getPredicate(), s.getObject()}); } overlay(valdata, Arrays.asList("Subject", "Predicate", "Object"), engine); }
@Override public boolean equals(Object other) { if (this == other) return true; if (other instanceof Statement) { Statement otherSt = (Statement) other; return this.hashCode() == otherSt.hashCode(); } else { return false; } }
/** * Compares two models, defined by two statement collections, and returns the difference between * the first and the second model (that is, all statements that are present in model1 but not in * model2). Blank node IDs are not relevant for model equality, they are mapped from one model to * the other by using the attached properties. * * * <p><b>NOTE: this algorithm is currently broken; it doesn't actually map blank nodes between the * two models.</b> * * @return The collection of statements that is the difference between model1 and model2. */ public static Collection<? extends Statement> difference( Collection<? extends Statement> model1, Collection<? extends Statement> model2) { // Create working copies LinkedList<Statement> copy1 = new LinkedList<Statement>(model1); LinkedList<Statement> copy2 = new LinkedList<Statement>(model2); Collection<Statement> result = new ArrayList<Statement>(); // Compare statements that don't contain bNodes Iterator<Statement> iter1 = copy1.iterator(); while (iter1.hasNext()) { Statement st = iter1.next(); if (st.getSubject() instanceof BNode || st.getObject() instanceof BNode) { // One or more of the statement's components is a bNode, // these statements are handled later continue; } // Try to remove the statement from model2 boolean removed = copy2.remove(st); if (!removed) { // statement was not present in model2 and is part of the difference result.add(st); } iter1.remove(); } // FIXME: this algorithm is broken: bNodeMapping is assumed to contain a // bnode mapping while in reallity it is an empty map HashMap<BNode, BNode> bNodeMapping = new HashMap<BNode, BNode>(); // mapBlankNodes(copy1, copy2, bNodeMapping, 0); for (Statement st1 : copy1) { boolean foundMatch = false; for (Statement st2 : copy2) { if (statementsMatch(st1, st2, bNodeMapping)) { // Found a matching statement foundMatch = true; break; } } if (!foundMatch) { // No statement matching st1 was found in model2, st1 is part of // the difference. result.add(st1); } } return result; }
private URI getTrx() throws SailException { if (trx == null) { trx = sail.nextTransaction(); synchronized (metadata) { for (Statement st : metadata) { storeStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext()); } metadata.clear(); } } return trx; }
protected BindingSet populateBindingSet(Statement st, List<Map.Entry<Var, Var>> predObjVar) { QueryBindingSet result = new QueryBindingSet(bindings); for (Map.Entry<Var, Var> entry : predObjVar) { Var predVar = entry.getKey(); Var objVar = entry.getValue(); if (predVar != null && !result.hasBinding(predVar.getName())) result.addBinding(predVar.getName(), st.getPredicate()); if (objVar != null && !result.hasBinding(objVar.getName())) result.addBinding(objVar.getName(), st.getObject()); } return result; }
/** * Returns the blank subject matching the pattern <code>(_:b p o)</code>, it is expected to exists * and be just one. * * @param p predicate. * @param o object. * @return the matching blank subject. * @throws RepositoryException */ protected Resource findExactlyOneBlankSubject(URI p, Value o) throws RepositoryException { RepositoryResult<Statement> it = conn.getStatements(null, p, o, false); try { Assert.assertTrue(getFailedExtractionMessage(), it.hasNext()); Statement stmt = it.next(); Resource result = stmt.getSubject(); Assert.assertTrue(getFailedExtractionMessage(), result instanceof BNode); Assert.assertFalse(getFailedExtractionMessage(), it.hasNext()); return result; } finally { it.close(); } }
private void compareGraphs(Set<Statement> queryResult, Set<Statement> expectedResult) throws Exception { if (!ModelUtil.equals(expectedResult, queryResult)) { // Don't use RepositoryUtil.difference, it reports incorrect diffs /* * Collection<? extends Statement> unexpectedStatements = * RepositoryUtil.difference(queryResult, expectedResult); * Collection<? extends Statement> missingStatements = * RepositoryUtil.difference(expectedResult, queryResult); * StringBuilder message = new StringBuilder(128); * message.append("\n=======Diff: "); message.append(getName()); * message.append("========================\n"); if * (!unexpectedStatements.isEmpty()) { message.append("Unexpected * statements in result: \n"); for (Statement st : * unexpectedStatements) { message.append(st.toString()); * message.append("\n"); } message.append("============="); for (int * i = 0; i < getName().length(); i++) { message.append("="); } * message.append("========================\n"); } if * (!missingStatements.isEmpty()) { message.append("Statements * missing in result: \n"); for (Statement st : missingStatements) { * message.append(st.toString()); message.append("\n"); } * message.append("============="); for (int i = 0; i < * getName().length(); i++) { message.append("="); } * message.append("========================\n"); } */ StringBuilder message = new StringBuilder(128); message.append("\n============ "); message.append(getName()); message.append(" =======================\n"); message.append("Expected result: \n"); for (Statement st : expectedResult) { message.append(st.toString()); message.append("\n"); } message.append("============="); StringUtil.appendN('=', getName().length(), message); message.append("========================\n"); message.append("Query result: \n"); for (Statement st : queryResult) { message.append(st.toString()); message.append("\n"); } message.append("============="); StringUtil.appendN('=', getName().length(), message); message.append("========================\n"); logger.error(message.toString()); fail(message.toString()); } }
/** * Returns all the objects matching the pattern <code>(s p _)</code>. * * @param s predicate. * @param p predicate. * @return list of matching objects. * @throws RepositoryException */ protected List<Value> findObjects(Resource s, URI p) throws RepositoryException { RepositoryResult<Statement> it = conn.getStatements(s, p, null, false); List<Value> objects = new ArrayList<Value>(); try { Statement statement; while (it.hasNext()) { statement = it.next(); objects.add(statement.getObject()); } } finally { it.close(); } return objects; }
/** * Returns all the subjects matching the pattern <code>(s? p o)</code>. * * @param p predicate. * @param o object. * @return list of matching subjects. * @throws RepositoryException */ protected List<Resource> findSubjects(URI p, Value o) throws RepositoryException { RepositoryResult<Statement> it = conn.getStatements(null, p, o, false); List<Resource> subjects = new ArrayList<Resource>(); try { Statement statement; while (it.hasNext()) { statement = it.next(); subjects.add(statement.getSubject()); } } finally { it.close(); } return subjects; }
public void graphQuery() throws RepositoryException, MalformedQueryException, QueryEvaluationException { String queryString = "CONSTRUCT {?x a ?y} WHERE { ?x a ?y} "; // String queryString = // "SELECT ?x ?y WHERE { ?x a onto:Person. ?x onto:age ?y } "; GraphQuery graphQuery = (con).prepareGraphQuery(QueryLanguage.SPARQL, queryString); GraphQueryResult result = graphQuery.evaluate(); while (result.hasNext()) { Statement st = result.next(); System.out.println(st.toString()); } result.close(); }
private List<XMLGregorianCalendar> getInstantaneousTimes(List<Statement> statements) { List<XMLGregorianCalendar> times = new ArrayList<XMLGregorianCalendar>(); for (Statement statement : statements) { QualifiedName predQ = convertURIToQualifiedName(statement.getPredicate()); Value value = statement.getObject(); if (value instanceof Literal) { if (predQ.equals(onto.QualifiedName_PROVO_atTime)) { times.add((XMLGregorianCalendar) super.decodeLiteral((Literal) value)); } } } return times; }
/** * @return string containing human readable statements. * @throws RepositoryException */ protected String dumpHumanReadableTriples() throws RepositoryException { StringBuilder sb = new StringBuilder(); RepositoryResult<Statement> result = conn.getStatements(null, null, null, false); while (result.hasNext()) { Statement statement = result.next(); sb.append( String.format( "%s %s %s %s\n", statement.getSubject(), statement.getPredicate(), statement.getObject(), statement.getContext())); } return sb.toString(); }
public Collection<String> getAllTriples() { Set<String> all = new HashSet<>(); try { RepositoryConnection repoConn = this.repository.getConnection(); RepositoryResult<Statement> results = repoConn.getStatements(null, null, null, false); while (results.hasNext()) { Statement tempResult = results.next(); all.add(tempResult.toString()); } repoConn.close(); } catch (RepositoryException ex) { ex.printStackTrace(); } return all; }
@Override public List<URI> collectVertices(final String type) throws RepositoryException { final URI typeURI = vf.createURI(BASE_PREFIX + type); final List<URI> vertices = new ArrayList<>(); final RepositoryResult<Statement> statements = connection.getStatements(null, RDF.TYPE, typeURI, true); while (statements.hasNext()) { final Statement s = statements.next(); final URI uri = (URI) s.getSubject(); vertices.add(uri); } return vertices; }
@Override public void handleStatement(Statement st) throws RDFHandlerException { if (!this.samplesData.containsKey(st.getSubject())) { this.samplesData.put(st.getSubject(), new HashMap<URI, HashSet<Value>>()); } if (!this.samplesData.get(st.getSubject()).containsKey(st.getPredicate())) { this.samplesData.get(st.getSubject()).put(st.getPredicate(), new HashSet<Value>()); } this.samplesData.get(st.getSubject()).get(st.getPredicate()).add(st.getObject()); }
@Test public void testStatementSerialization() throws Exception { Statement st = vf.createStatement(picasso, RDF.TYPE, painter); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(st); out.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); Statement deserializedStatement = (Statement) in.readObject(); in.close(); assertTrue(st.equals(deserializedStatement)); }