Java Code Examples for org.eclipse.rdf4j.model.Statement#getSubject()

The following examples show how to use org.eclipse.rdf4j.model.Statement#getSubject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RepositoryConnectionTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBNodeSerialization() throws Exception {
	testCon.add(bob, name, nameBob);

	Statement st;
	try (RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, false);) {
		st = statements.next();
	}

	BNode bnode = (BNode) st.getSubject();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream out = new ObjectOutputStream(baos);
	out.writeObject(bnode);
	out.close();

	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
	ObjectInputStream in = new ObjectInputStream(bais);
	BNode deserializedBNode = (BNode) in.readObject();
	in.close();

	assertThat(deserializedBNode).isEqualTo(bnode);

	assertThat(testCon.hasStatement(bnode, name, nameBob, true)).isTrue();
	assertThat(testCon.hasStatement(deserializedBNode, name, nameBob, true)).isTrue();
}
 
Example 2
Source File: ExploreServlet.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Gets whether this is the first time the result quad has been seen.
 *
 * @param patternPredicate the predicate asked for, or null if another quad element was asked for
 * @param patternObject    the object asked for, or null if another quad element was asked for
 * @param result           the result statement to determine if we've already seen
 * @param patternContext   the context asked for, or null if another quad element was asked for
 * @return true, if this is the first time the quad has been seen, false otherwise
 */
private boolean isFirstTimeSeen(Statement result, IRI patternPredicate, Value patternObject,
		Resource... patternContext) {
	Resource resultSubject = result.getSubject();
	IRI resultPredicate = result.getPredicate();
	Value resultObject = result.getObject();
	boolean firstTimeSeen;
	if (1 == patternContext.length) {
		// I.e., when context matches explore value.
		Resource ctx = patternContext[0];
		firstTimeSeen = !(ctx.equals(resultSubject) || ctx.equals(resultPredicate) || ctx.equals(resultObject));
	} else if (null != patternObject) {
		// I.e., when object matches explore value.
		firstTimeSeen = !(resultObject.equals(resultSubject) || resultObject.equals(resultPredicate));
	} else if (null != patternPredicate) {
		// I.e., when predicate matches explore value.
		firstTimeSeen = !(resultPredicate.equals(resultSubject));
	} else {
		// I.e., when subject matches explore value.
		firstTimeSeen = true;
	}
	return firstTimeSeen;
}
 
Example 3
Source File: ConformanceTest.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Determine that a statement is trivially true for purposes of entailment
 * tests, such as an implicit "[bnode] type Ontology" triple or a
 * "[class] type Class" triple as long as the class exists.
 */
boolean triviallyTrue(final Statement triple, final Schema schema) {
    final Resource s = triple.getSubject();
    final IRI p = triple.getPredicate();
    final Value o = triple.getObject();
    if (p.equals(RDF.TYPE)) {
        if (o.equals(OWL.ONTOLOGY)) {
            return true;
        }
        else if (o.equals(OWL.CLASS)) {
            return schema.hasClass(s);
        }
        else if ((o.equals(OWL.OBJECTPROPERTY)
            || o.equals(OWL.DATATYPEPROPERTY))
            && s instanceof IRI) {
            // Distinction not maintained, irrelevant to RL rules
            return schema.hasProperty((IRI) s);
        }
    }
    return false;
}
 
Example 4
Source File: InferenceEngine.java    From rya with Apache License 2.0 5 votes vote down vote up
private static void addStatementEdge(final Graph graph, final String edgeName, final Statement st) {
    final Resource subj = st.getSubject();
    Vertex a = getVertex(graph, subj);
    if (a == null) {
        a = graph.addVertex(T.id, subj.toString());
        a.property(URI_PROP, subj);
    }
    final Value obj = st.getObject();
    Vertex b = getVertex(graph, obj);
    if (b == null) {
        b = graph.addVertex(T.id, obj.toString());
        b.property(URI_PROP, obj);
    }
    a.addEdge(edgeName, b);
}
 
Example 5
Source File: CombineContextsRdfInserter.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void handleStatement(Statement st)
        throws RDFHandlerException {
    Resource subj = st.getSubject();
    IRI pred = st.getPredicate();
    Value obj = st.getObject();
    Resource ctxt = st.getContext();

    if (!preserveBNodeIDs) {
        if (subj instanceof BNode) {
            subj = mapBNode((BNode) subj);
        }

        if (obj instanceof BNode) {
            obj = mapBNode((BNode) obj);
        }

        if (!enforcesContext() && ctxt instanceof BNode) {
            ctxt = mapBNode((BNode) ctxt);
        }
    }

    try {
        if (enforcesContext()) {
            Resource[] ctxts = contexts;
            if (ctxt != null) {
                ctxts = combineContexts(contexts, ctxt);
            }
            con.add(subj, pred, obj, ctxts);
        } else {
            con.add(subj, pred, obj, ctxt);
        }
    } catch (RepositoryException e) {
        throw new RDFHandlerException(e);
    }
}
 
Example 6
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs9_1() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.SUBCLASSOF, null);

	for (Statement nt : ntIter) {
		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (yyy instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, RDF.TYPE, xxx, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource aaa = t1.getSubject();

				boolean added = addInferredStatement(aaa, RDF.TYPE, yyy);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 7
Source File: RepositoryConfigUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static RepositoryConfig getRepositoryConfig(Model model, String repositoryID) {
	Statement idStatement = getIDStatement(model, repositoryID);
	if (idStatement == null) {
		// No such config
		return null;
	}
	Resource repositoryNode = idStatement.getSubject();
	Resource context = idStatement.getContext();
	Model contextGraph = model.filter(null, null, null, context);
	return RepositoryConfig.create(contextGraph, repositoryNode);
}
 
Example 8
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs7_1() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, null, null);

	for (Statement nt : ntIter) {
		Resource xxx = nt.getSubject();
		IRI aaa = nt.getPredicate();
		Value yyy = nt.getObject();

		CloseableIteration<? extends Statement, SailException> t1Iter;
		t1Iter = getWrappedConnection().getStatements(aaa, RDFS.SUBPROPERTYOF, null, true);

		while (t1Iter.hasNext()) {
			Statement t1 = t1Iter.next();

			Value bbb = t1.getObject();
			if (bbb instanceof IRI) {
				boolean added = addInferredStatement(xxx, (IRI) bbb, yyy);
				if (added) {
					nofInferred++;
				}
			}
		}
		t1Iter.close();
	}

	return nofInferred;
}
 
Example 9
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs6() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> iter = newThisIteration.getStatements(null, RDF.TYPE, RDF.PROPERTY);

	for (Statement st : iter) {
		Resource xxx = st.getSubject();
		boolean added = addInferredStatement(xxx, RDFS.SUBPROPERTYOF, xxx);
		if (added) {
			nofInferred++;
		}
	}

	return nofInferred;
}
 
Example 10
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs5_2() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.SUBPROPERTYOF, null);

	for (Statement nt : ntIter) {
		Resource bbb = nt.getSubject();
		Value ccc = nt.getObject();

		if (ccc instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, RDFS.SUBPROPERTYOF, bbb, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource aaa = t1.getSubject();
				boolean added = addInferredStatement(aaa, RDFS.SUBPROPERTYOF, ccc);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 11
Source File: LinkedHashModel.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean add(Statement statement) {
	Resource subj = statement.getSubject();
	IRI pred = statement.getPredicate();
	Value obj = statement.getObject();
	Resource context = statement.getContext();

	ModelNode<Resource> s = asNode(subj);
	ModelNode<IRI> p = asNode(pred);
	ModelNode<Value> o = asNode(obj);
	ModelNode<Resource> c = asNode(context);
	ModelStatement modelStatement = new ModelStatement(s, p, o, c, statement);
	return addModelStatement(modelStatement);

}
 
Example 12
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs3_2() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.RANGE, null);

	for (Statement nt : ntIter) {
		Resource aaa = nt.getSubject();
		Value zzz = nt.getObject();

		if (aaa instanceof IRI && zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (IRI) aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value uuu = t1.getObject();
				if (uuu instanceof Resource) {
					boolean added = addInferredStatement((Resource) uuu, RDF.TYPE, zzz);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;

}
 
Example 13
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs2_1() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, null, null);

	for (Statement nt : ntIter) {
		Resource xxx = nt.getSubject();
		IRI aaa = nt.getPredicate();

		CloseableIteration<? extends Statement, SailException> t1Iter;
		t1Iter = getWrappedConnection().getStatements(aaa, RDFS.DOMAIN, null, true);

		while (t1Iter.hasNext()) {
			Statement t1 = t1Iter.next();

			Value zzz = t1.getObject();
			if (zzz instanceof Resource) {
				boolean added = addInferredStatement(xxx, RDF.TYPE, zzz);
				if (added) {
					nofInferred++;
				}
			}
		}
		t1Iter.close();
	}

	return nofInferred;
}
 
Example 14
Source File: MemoryOverflowModel.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeObject(ObjectOutputStream s) throws IOException {
	// Write out any hidden serialization magic
	s.defaultWriteObject();
	// Write in size
	Model delegate = getDelegate();
	s.writeInt(delegate.size());
	// Write in all elements
	for (Statement st : delegate) {
		Resource subj = st.getSubject();
		IRI pred = st.getPredicate();
		Value obj = st.getObject();
		Resource ctx = st.getContext();
		s.writeObject(new ContextStatementImpl(subj, pred, obj, ctx));
	}
}
 
Example 15
Source File: AbstractRDFWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void handleStatementEncodeRDFStar(Statement st) {
	Resource s = st.getSubject();
	Value o = st.getObject();
	if (s instanceof Triple || o instanceof Triple) {
		consumeStatement(new RDFStarEncodingStatement(st));
	} else {
		consumeStatement(st);
	}
}
 
Example 16
Source File: AbstractRDFInserter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
	Resource subj = st.getSubject();
	IRI pred = st.getPredicate();
	Value obj = st.getObject();
	Resource ctxt = st.getContext();

	if (!preserveBNodeIDs) {
		if (subj instanceof BNode) {
			subj = mapBNode((BNode) subj);
		}

		if (obj instanceof BNode) {
			obj = mapBNode((BNode) obj);
		}

		if (!enforcesContext() && ctxt instanceof BNode) {
			ctxt = mapBNode((BNode) ctxt);
		}
	}

	try {
		addStatement(subj, pred, obj, ctxt);
	} catch (RDF4JException e) {
		throw new RDFHandlerException(e);
	}
}
 
Example 17
Source File: Schema.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Determine whether a fact is contained in the Schema object
 * relationships or implied by schema rules.
 * @return  True if this schema contains the semantics of the triple
 */
public boolean containsTriple(Statement triple) {
    // The schema certainly doesn't contain it if it's not a
    // schema-relevant triple at all.
    if (isSchemaTriple(triple)) {
        Resource s = triple.getSubject();
        IRI p = triple.getPredicate();
        Value o = triple.getObject();
        // If this is telling us something about a property:
        if (properties.containsKey(s)) {
            OwlProperty prop = properties.get(s);
            // Property types:
            if (p.equals(RDF.TYPE)) {
                if ((o.equals(OWL.TRANSITIVEPROPERTY)
                        && prop.isTransitive())
                    || (o.equals(OWL2.IRREFLEXIVEPROPERTY)
                        && prop.isIrreflexive())
                    || (o.equals(OWL.SYMMETRICPROPERTY)
                        && prop.isSymmetric())
                    || (o.equals(OWL2.ASYMMETRICPROPERTY)
                        && prop.isAsymmetric())
                    || (o.equals(OWL.FUNCTIONALPROPERTY)
                        && prop.isFunctional())
                    || (o.equals(OWL.INVERSEFUNCTIONALPROPERTY)
                        && prop.isInverseFunctional())) {
                    return true;
                }
            }
            // Relationships with other properties:
            if ((p.equals(RDFS.SUBPROPERTYOF)
                    && prop.getSuperProperties().contains(o))
                || (p.equals(OWL2.PROPERTYDISJOINTWITH)
                    && prop.getDisjointProperties().contains(o))
                || (p.equals(OWL.EQUIVALENTPROPERTY)
                    && prop.getEquivalentProperties().contains(o))
                || (p.equals(OWL.INVERSEOF)
                    && prop.getInverseProperties().contains(o))) {
                return true;
            }
            // Relationships with classes:
            if ((p.equals(RDFS.DOMAIN)
                    && prop.getDomain().contains(o))
                || (p.equals(RDFS.RANGE)
                    && prop.getRange().contains(o))) {
                return true;
            }
        }
        // If this is about a class relationship:
        if (classes.containsKey(s)) {
            OwlClass subject = classes.get(s);
            if ((p.equals(OWL.EQUIVALENTCLASS)
                    && (subject.getEquivalentClasses().contains(o)))
                || (p.equals(OWL.DISJOINTWITH)
                    && (subject.getDisjointClasses().contains(o)))
                || (p.equals(OWL.COMPLEMENTOF)
                    && (subject.getComplementaryClasses().contains(o)))
                || (p.equals(RDFS.SUBCLASSOF)
                    && (subject.getSuperClasses().contains(o)))) {
                return true;
            }
        }
    }
    return false;
}
 
Example 18
Source File: RepositoryUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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<>(model1);
	LinkedList<Statement> copy2 = new LinkedList<>(model2);

	Collection<Statement> result = new ArrayList<>();

	// 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<>();
	// 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;
}
 
Example 19
Source File: InferenceEngine.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Add unions to the subclass graph: if c owl:unionOf LIST(c1, c2, ... cn),
 * then any instances of c1, c2, ... or cn are also instances of c, meaning
 * c is a superclass of all the rest.
 * (In principle, an instance of c is likewise implied to be at least one of
 * the other types, but this fact is ignored for now to avoid
 * nondeterministic reasoning.)
 * @param graph the {@link Graph} to add to.
 * @throws QueryEvaluationException
 */
private void addUnions(final Graph graph) throws QueryEvaluationException {
    final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDAO, null, OWL.UNIONOF, null, conf);
    try {
        while (iter.hasNext()) {
            final Statement st = iter.next();
            final Value unionType = st.getSubject();
            // Traverse the list of types constituting the union
            Value current = st.getObject();
            while (current instanceof Resource && !RDF.NIL.equals(current)) {
                final Resource listNode = (Resource) current;
                CloseableIteration<Statement, QueryEvaluationException> listIter = RyaDAOHelper.query(ryaDAO,
                        listNode, RDF.FIRST, null, conf);
                try {
                    if (listIter.hasNext()) {
                        final Statement firstStatement = listIter.next();
                        if (firstStatement.getObject() instanceof Resource) {
                            final Resource subclass = (Resource) firstStatement.getObject();
                            final Statement subclassStatement = VF.createStatement(subclass, RDFS.SUBCLASSOF, unionType);
                            addStatementEdge(graph, RDFS.SUBCLASSOF.stringValue(), subclassStatement);
                        }
                    }
                } finally {
                    listIter.close();
                }
                listIter = RyaDAOHelper.query(ryaDAO, listNode, RDF.REST, null, conf);
                try {
                    if (listIter.hasNext()) {
                        current = listIter.next().getObject();
                    }
                    else {
                        current = RDF.NIL;
                    }
                } finally {
                    listIter.close();
                }
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example 20
Source File: ArrangedWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private SubjectInContext(Statement st) {
	this(st.getSubject(), st.getContext());
}