use of org.openrdf.model.Statement in project incubator-rya by apache.
the class InferenceEngine method refreshPropertyChainPropertyToChain.
private void refreshPropertyChainPropertyToChain() throws QueryEvaluationException {
CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDAO, null, VF.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom"), null, conf);
final Map<URI, URI> propertyChainPropertiesToBNodes = new HashMap<>();
final Map<URI, List<URI>> tempPropertyChainPropertyToChain = new HashMap<>();
try {
while (iter.hasNext()) {
final Statement st = iter.next();
propertyChainPropertiesToBNodes.put((URI) st.getSubject(), (URI) st.getObject());
}
} finally {
if (iter != null) {
iter.close();
}
}
// now for each property chain bNode, get the indexed list of properties associated with that chain
for (final URI propertyChainProperty : propertyChainPropertiesToBNodes.keySet()) {
final URI bNode = propertyChainPropertiesToBNodes.get(propertyChainProperty);
// query for the list of indexed properties
iter = RyaDAOHelper.query(ryaDAO, bNode, VF.createURI("http://www.w3.org/2000/10/swap/list#index"), null, conf);
final TreeMap<Integer, URI> orderedProperties = new TreeMap<>();
// TODO refactor this. Wish I could execute sparql
try {
while (iter.hasNext()) {
final Statement st = iter.next();
final String indexedElement = st.getObject().stringValue();
log.info(indexedElement);
CloseableIteration<Statement, QueryEvaluationException> iter2 = RyaDAOHelper.query(ryaDAO, VF.createURI(st.getObject().stringValue()), RDF.FIRST, null, conf);
String integerValue = "";
Value anonPropNode = null;
Value propURI = null;
if (iter2 != null) {
while (iter2.hasNext()) {
final Statement iter2Statement = iter2.next();
integerValue = iter2Statement.getObject().stringValue();
break;
}
iter2.close();
}
iter2 = RyaDAOHelper.query(ryaDAO, VF.createURI(st.getObject().stringValue()), RDF.REST, null, conf);
if (iter2 != null) {
while (iter2.hasNext()) {
final Statement iter2Statement = iter2.next();
anonPropNode = iter2Statement.getObject();
break;
}
iter2.close();
if (anonPropNode != null) {
iter2 = RyaDAOHelper.query(ryaDAO, VF.createURI(anonPropNode.stringValue()), RDF.FIRST, null, conf);
while (iter2.hasNext()) {
final Statement iter2Statement = iter2.next();
propURI = iter2Statement.getObject();
break;
}
iter2.close();
}
}
if (!integerValue.isEmpty() && propURI != null) {
try {
final int indexValue = Integer.parseInt(integerValue);
final URI chainPropURI = VF.createURI(propURI.stringValue());
orderedProperties.put(indexValue, chainPropURI);
} catch (final Exception e) {
log.error("Error adding chain property to ordered properties", e);
}
}
}
} finally {
if (iter != null) {
iter.close();
}
}
final List<URI> properties = new ArrayList<>();
for (final Map.Entry<Integer, URI> entry : orderedProperties.entrySet()) {
properties.add(entry.getValue());
}
tempPropertyChainPropertyToChain.put(propertyChainProperty, properties);
}
// could also be represented as a list of properties (some of which may be blank nodes)
for (final URI propertyChainProperty : propertyChainPropertiesToBNodes.keySet()) {
final List<URI> existingChain = tempPropertyChainPropertyToChain.get(propertyChainProperty);
// if we didn't get a chain, try to get it through following the collection
if ((existingChain == null) || existingChain.isEmpty()) {
CloseableIteration<Statement, QueryEvaluationException> iter2 = RyaDAOHelper.query(ryaDAO, propertyChainPropertiesToBNodes.get(propertyChainProperty), RDF.FIRST, null, conf);
final List<URI> properties = new ArrayList<>();
URI previousBNode = propertyChainPropertiesToBNodes.get(propertyChainProperty);
if (iter2.hasNext()) {
Statement iter2Statement = iter2.next();
Value currentPropValue = iter2Statement.getObject();
while ((currentPropValue != null) && (!currentPropValue.stringValue().equalsIgnoreCase(RDF.NIL.stringValue()))) {
if (currentPropValue instanceof URI) {
iter2 = RyaDAOHelper.query(ryaDAO, VF.createURI(currentPropValue.stringValue()), RDF.FIRST, null, conf);
if (iter2.hasNext()) {
iter2Statement = iter2.next();
if (iter2Statement.getObject() instanceof URI) {
properties.add((URI) iter2Statement.getObject());
}
} else // otherwise see if there is an inverse declaration
{
iter2 = RyaDAOHelper.query(ryaDAO, VF.createURI(currentPropValue.stringValue()), OWL.INVERSEOF, null, conf);
if (iter2.hasNext()) {
iter2Statement = iter2.next();
if (iter2Statement.getObject() instanceof URI) {
properties.add(new InverseURI((URI) iter2Statement.getObject()));
}
}
}
// get the next prop pointer
iter2 = RyaDAOHelper.query(ryaDAO, previousBNode, RDF.REST, null, conf);
if (iter2.hasNext()) {
iter2Statement = iter2.next();
previousBNode = (URI) currentPropValue;
currentPropValue = iter2Statement.getObject();
} else {
currentPropValue = null;
}
} else {
currentPropValue = null;
}
}
tempPropertyChainPropertyToChain.put(propertyChainProperty, properties);
}
}
}
synchronized (propertyChainPropertyToChain) {
propertyChainPropertyToChain.clear();
propertyChainPropertyToChain.putAll(tempPropertyChainPropertyToChain);
}
}
use of org.openrdf.model.Statement in project incubator-rya by apache.
the class InferenceEngine method refreshHasSelfRestrictions.
private void refreshHasSelfRestrictions(final Map<Resource, URI> restrictions) throws QueryEvaluationException {
hasSelfByType.clear();
hasSelfByProperty.clear();
for (final Resource type : restrictions.keySet()) {
final URI property = restrictions.get(type);
final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDAO, type, HAS_SELF, null, conf);
try {
if (iter.hasNext()) {
Set<URI> typeSet = hasSelfByType.get(type);
Set<Resource> propSet = hasSelfByProperty.get(property);
if (typeSet == null) {
typeSet = new HashSet<>();
}
if (propSet == null) {
propSet = new HashSet<>();
}
typeSet.add(property);
propSet.add(type);
hasSelfByType.put(type, typeSet);
hasSelfByProperty.put(property, propSet);
}
} finally {
if (iter != null) {
iter.close();
}
}
}
}
use of org.openrdf.model.Statement in project incubator-rya by apache.
the class InferenceEngine method refreshAllValuesFromRestrictions.
private void refreshAllValuesFromRestrictions(final Map<Resource, URI> restrictions) throws QueryEvaluationException {
allValuesFromByValueType.clear();
ryaDaoQueryWrapper.queryAll(null, OWL.ALLVALUESFROM, null, new RDFHandlerBase() {
@Override
public void handleStatement(final Statement statement) throws RDFHandlerException {
final Resource directRestrictionClass = statement.getSubject();
if (restrictions.containsKey(directRestrictionClass) && statement.getObject() instanceof Resource) {
final URI property = restrictions.get(directRestrictionClass);
final Resource valueClass = (Resource) statement.getObject();
// Should also be triggered by subclasses of the property restriction
final Set<Resource> restrictionClasses = new HashSet<>();
restrictionClasses.add(directRestrictionClass);
if (directRestrictionClass instanceof URI) {
restrictionClasses.addAll(getSubClasses((URI) directRestrictionClass));
}
for (final Resource restrictionClass : restrictionClasses) {
if (!allValuesFromByValueType.containsKey(valueClass)) {
allValuesFromByValueType.put(valueClass, new ConcurrentHashMap<>());
}
allValuesFromByValueType.get(valueClass).put(restrictionClass, property);
}
}
}
});
}
use of org.openrdf.model.Statement in project incubator-rya by apache.
the class InferenceEngine method fetchInstances.
/**
* Query for and collect all instances of a given type. Should only be called for types expected
* to have few members, such as ontology vocabulary terms, as instances will be collected in
* memory.
*/
private Set<URI> fetchInstances(final URI type) throws QueryEvaluationException {
final Set<URI> instances = new HashSet<>();
ryaDaoQueryWrapper.queryAll(null, RDF.TYPE, type, new RDFHandlerBase() {
@Override
public void handleStatement(final Statement st) throws RDFHandlerException {
if (st.getSubject() instanceof URI) {
instances.add((URI) st.getSubject());
}
}
});
return instances;
}
use of org.openrdf.model.Statement in project incubator-rya by apache.
the class InferenceEngine method refreshPropertyRestrictions.
private void refreshPropertyRestrictions() throws QueryEvaluationException {
// Get a set of all property restrictions of any type
final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDAO, null, OWL.ONPROPERTY, null, conf);
final Map<Resource, URI> restrictions = new HashMap<>();
try {
while (iter.hasNext()) {
final Statement st = iter.next();
restrictions.put(st.getSubject(), (URI) st.getObject());
}
} finally {
if (iter != null) {
iter.close();
}
}
// Query for specific types of restriction and add their details to the schema
refreshHasValueRestrictions(restrictions);
refreshSomeValuesFromRestrictions(restrictions);
refreshAllValuesFromRestrictions(restrictions);
refreshHasSelfRestrictions(restrictions);
}
Aggregations