Search in sources :

Example 91 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class InferenceEngine method addUnions.

/**
 * 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();
        }
    }
}
Also used : QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) Resource(org.openrdf.model.Resource)

Example 92 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class InferenceEngine method refreshSomeValuesFromRestrictions.

private void refreshSomeValuesFromRestrictions(final Map<Resource, URI> restrictions) throws QueryEvaluationException {
    someValuesFromByRestrictionType.clear();
    ryaDaoQueryWrapper.queryAll(null, OWL.SOMEVALUESFROM, null, new RDFHandlerBase() {

        @Override
        public void handleStatement(final Statement statement) throws RDFHandlerException {
            final Resource restrictionClass = statement.getSubject();
            if (restrictions.containsKey(restrictionClass) && statement.getObject() instanceof Resource) {
                final URI property = restrictions.get(restrictionClass);
                final Resource valueClass = (Resource) statement.getObject();
                // Should also be triggered by subclasses of the value class
                final Set<Resource> valueClasses = new HashSet<>();
                valueClasses.add(valueClass);
                if (valueClass instanceof URI) {
                    valueClasses.addAll(getSubClasses((URI) valueClass));
                }
                for (final Resource valueSubClass : valueClasses) {
                    if (!someValuesFromByRestrictionType.containsKey(restrictionClass)) {
                        someValuesFromByRestrictionType.put(restrictionClass, new ConcurrentHashMap<>());
                    }
                    someValuesFromByRestrictionType.get(restrictionClass).put(valueSubClass, property);
                }
            }
        }
    });
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) RDFHandlerException(org.openrdf.rio.RDFHandlerException) Statement(org.openrdf.model.Statement) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) Resource(org.openrdf.model.Resource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URI(org.openrdf.model.URI)

Example 93 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class InferenceEngine method refreshDomainRange.

/**
 * Queries domain and range information, then populates the inference engine with direct
 * domain/range relations and any that can be inferred from the subclass graph, subproperty
 * graph, and inverse property map. Should be called after that class and property information
 * has been refreshed.
 *
 * Computes indirect domain/range:
 *  - If p1 has domain c, and p2 is a subproperty of p1, then p2 also has domain c.
 *  - If p1 has range c, and p2 is a subproperty of p1, then p2 also has range c.
 *  - If p1 has domain c, and p2 is the inverse of p1, then p2 has range c.
 *  - If p1 has range c, and p2 is the inverse of p1, then p2 has domain c.
 *  - If p has domain c1, and c1 is a subclass of c2, then p also has domain c2.
 *  - If p has range c1, and c1 is a subclass of c2, then p also has range c2.
 * @throws QueryEvaluationException
 */
private void refreshDomainRange() throws QueryEvaluationException {
    final Map<URI, Set<URI>> domainByTypePartial = new ConcurrentHashMap<>();
    final Map<URI, Set<URI>> rangeByTypePartial = new ConcurrentHashMap<>();
    // First, populate domain and range based on direct domain/range triples.
    CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDAO, null, RDFS.DOMAIN, null, conf);
    try {
        while (iter.hasNext()) {
            final Statement st = iter.next();
            final Resource property = st.getSubject();
            final Value domainType = st.getObject();
            if (domainType instanceof URI && property instanceof URI) {
                if (!domainByTypePartial.containsKey(domainType)) {
                    domainByTypePartial.put((URI) domainType, new HashSet<>());
                }
                domainByTypePartial.get(domainType).add((URI) property);
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
    iter = RyaDAOHelper.query(ryaDAO, null, RDFS.RANGE, null, conf);
    try {
        while (iter.hasNext()) {
            final Statement st = iter.next();
            final Resource property = st.getSubject();
            final Value rangeType = st.getObject();
            if (rangeType instanceof URI && property instanceof URI) {
                if (!rangeByTypePartial.containsKey(rangeType)) {
                    rangeByTypePartial.put((URI) rangeType, new HashSet<>());
                }
                rangeByTypePartial.get(rangeType).add((URI) property);
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
    // Then combine with the subclass/subproperty graphs and the inverse property map to compute
    // the closure of domain and range per class.
    final Set<URI> domainRangeTypeSet = new HashSet<>(domainByTypePartial.keySet());
    domainRangeTypeSet.addAll(rangeByTypePartial.keySet());
    // These two rules can recursively trigger one another.
    for (final URI domainRangeType : domainRangeTypeSet) {
        final Set<URI> propertiesWithDomain = domainByTypePartial.getOrDefault(domainRangeType, new HashSet<>());
        final Set<URI> propertiesWithRange = rangeByTypePartial.getOrDefault(domainRangeType, new HashSet<>());
        // Since findParents will traverse the subproperty graph and find all indirect
        // subproperties, the subproperty rule does not need to trigger itself directly.
        // And since no more than one inverseOf relationship is stored for any property, the
        // inverse property rule does not need to trigger itself directly. However, each rule
        // can trigger the other, so keep track of how the inferred domains/ranges were
        // discovered so we can apply only those rules that might yield new information.
        final Stack<URI> domainViaSuperProperty = new Stack<>();
        final Stack<URI> rangeViaSuperProperty = new Stack<>();
        final Stack<URI> domainViaInverseProperty = new Stack<>();
        final Stack<URI> rangeViaInverseProperty = new Stack<>();
        // Start with the direct domain/range assertions, which can trigger any rule.
        domainViaSuperProperty.addAll(propertiesWithDomain);
        domainViaInverseProperty.addAll(propertiesWithDomain);
        rangeViaSuperProperty.addAll(propertiesWithRange);
        rangeViaInverseProperty.addAll(propertiesWithRange);
        // information can be generated.
        while (!(domainViaSuperProperty.isEmpty() && rangeViaSuperProperty.isEmpty() && domainViaInverseProperty.isEmpty() && rangeViaInverseProperty.isEmpty())) {
            // inverse of p. Would be redundant for properties discovered via inverseOf.
            while (!domainViaSuperProperty.isEmpty()) {
                final URI property = domainViaSuperProperty.pop();
                final URI inverseProperty = findInverseOf(property);
                if (inverseProperty != null && propertiesWithRange.add(inverseProperty)) {
                    rangeViaInverseProperty.push(inverseProperty);
                }
            }
            // inverse of p. Would be redundant for properties discovered via inverseOf.
            while (!rangeViaSuperProperty.isEmpty()) {
                final URI property = rangeViaSuperProperty.pop();
                final URI inverseProperty = findInverseOf(property);
                if (inverseProperty != null && propertiesWithDomain.add(inverseProperty)) {
                    domainViaInverseProperty.push(inverseProperty);
                }
            }
            // p's subproperties. Would be redundant for properties discovered via this rule.
            while (!domainViaInverseProperty.isEmpty()) {
                final URI property = domainViaInverseProperty.pop();
                final Set<URI> subProperties = getSubProperties(property);
                subProperties.removeAll(propertiesWithDomain);
                propertiesWithDomain.addAll(subProperties);
                domainViaSuperProperty.addAll(subProperties);
            }
            // p's subproperties. Would be redundant for properties discovered via this rule.
            while (!rangeViaInverseProperty.isEmpty()) {
                final URI property = rangeViaInverseProperty.pop();
                final Set<URI> subProperties = getSubProperties(property);
                subProperties.removeAll(propertiesWithRange);
                propertiesWithRange.addAll(subProperties);
                rangeViaSuperProperty.addAll(subProperties);
            }
        }
        if (!propertiesWithDomain.isEmpty()) {
            domainByTypePartial.put(domainRangeType, propertiesWithDomain);
        }
        if (!propertiesWithRange.isEmpty()) {
            rangeByTypePartial.put(domainRangeType, propertiesWithRange);
        }
    }
    // general classes as well.
    for (final URI subtype : domainRangeTypeSet) {
        final Set<URI> supertypes = getSuperClasses(subtype);
        final Set<URI> propertiesWithDomain = domainByTypePartial.getOrDefault(subtype, new HashSet<>());
        final Set<URI> propertiesWithRange = rangeByTypePartial.getOrDefault(subtype, new HashSet<>());
        for (final URI supertype : supertypes) {
            // For a property p and its domain c: all of c's superclasses are also domains of p.
            if (!propertiesWithDomain.isEmpty() && !domainByTypePartial.containsKey(supertype)) {
                domainByTypePartial.put(supertype, new HashSet<>());
            }
            for (final URI property : propertiesWithDomain) {
                domainByTypePartial.get(supertype).add(property);
            }
            // For a property p and its range c: all of c's superclasses are also ranges of p.
            if (!propertiesWithRange.isEmpty() && !rangeByTypePartial.containsKey(supertype)) {
                rangeByTypePartial.put(supertype, new HashSet<>());
            }
            for (final URI property : propertiesWithRange) {
                rangeByTypePartial.get(supertype).add(property);
            }
        }
    }
    synchronized (domainByType) {
        domainByType.clear();
        domainByType.putAll(domainByTypePartial);
    }
    synchronized (rangeByType) {
        rangeByType.clear();
        rangeByType.putAll(rangeByTypePartial);
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) Stack(java.util.Stack) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 94 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class InferenceEngine method refreshIntersectionOf.

private void refreshIntersectionOf() throws QueryEvaluationException {
    final Map<Resource, List<Set<Resource>>> intersectionsProp = new HashMap<>();
    // First query for all the owl:intersectionOf's.
    // If we have the following intersectionOf:
    // :A owl:intersectionOf[:B, :C]
    // It will be represented by triples following a pattern similar to:
    // <:A> owl:intersectionOf _:bnode1 .
    // _:bnode1 rdf:first <:B> .
    // _:bnode1 rdf:rest _:bnode2 .
    // _:bnode2 rdf:first <:C> .
    // _:bnode2 rdf:rest rdf:nil .
    ryaDaoQueryWrapper.queryAll(null, OWL.INTERSECTIONOF, null, new RDFHandlerBase() {

        @Override
        public void handleStatement(final Statement statement) throws RDFHandlerException {
            final Resource type = statement.getSubject();
            // head will point to a type that is part of the intersection.
            final URI head = (URI) statement.getObject();
            if (!intersectionsProp.containsKey(type)) {
                intersectionsProp.put(type, new ArrayList<Set<Resource>>());
            }
            // intersection.
            try {
                final Set<Resource> intersection = new LinkedHashSet<>(getList(head));
                if (!intersection.isEmpty()) {
                    // Add this intersection for this type. There may be more
                    // intersections for this type so each type has a list of
                    // intersection sets.
                    intersectionsProp.get(type).add(intersection);
                }
            } catch (final QueryEvaluationException e) {
                throw new RDFHandlerException("Error getting intersection list.", e);
            }
        }
    });
    intersections.clear();
    for (final Entry<Resource, List<Set<Resource>>> entry : intersectionsProp.entrySet()) {
        final Resource type = entry.getKey();
        final List<Set<Resource>> intersectionList = entry.getValue();
        final Set<Resource> otherTypes = new HashSet<>();
        // Combine all of a type's intersections together.
        for (final Set<Resource> intersection : intersectionList) {
            otherTypes.addAll(intersection);
        }
        for (final Resource other : otherTypes) {
            // :A intersectionOf[:B, :C] implies that
            // :A subclassOf :B
            // :A subclassOf :C
            // So add each type that's part of the intersection to the
            // subClassOf graph.
            addSubClassOf(type, other);
            for (final Set<Resource> intersection : intersectionList) {
                if (!intersection.contains(other)) {
                    addIntersection(intersection, other);
                }
            }
        }
        for (final Set<Resource> intersection : intersectionList) {
            addIntersection(intersection, type);
        }
    }
    for (final Entry<Resource, List<Set<Resource>>> entry : intersectionsProp.entrySet()) {
        final Resource type = entry.getKey();
        final List<Set<Resource>> intersectionList = entry.getValue();
        final Set<URI> superClasses = getSuperClasses((URI) type);
        for (final URI superClass : superClasses) {
            // intersectionOf[:B, :C] subclassOf :D
            for (final Set<Resource> intersection : intersectionList) {
                addIntersection(intersection, superClass);
            }
        }
        // type. Propagating up through all the superclasses.
        for (final Set<Resource> intersection : intersectionList) {
            final Set<Resource> otherKeys = Sets.newHashSet(intersectionsProp.keySet());
            otherKeys.remove(type);
            for (final Resource otherKey : otherKeys) {
                if (intersectionsProp.get(otherKey).contains(intersection)) {
                    addSubClassOf(otherKey, type);
                    addSubClassOf(type, otherKey);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) ArrayList(java.util.ArrayList) URI(org.openrdf.model.URI) RDFHandlerException(org.openrdf.rio.RDFHandlerException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 95 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class SameAsTest method testGraphConfiguration.

@Test
public // This isn't a good test.  It's simply a cut-and-paste from a test that was failing in a different package in the SameAsVisitor.
void testGraphConfiguration() throws Exception {
    URI a = vf.createURI(namespace, "a");
    Statement statement = new StatementImpl(a, vf.createURI(namespace, "p"), vf.createLiteral("l"));
    Statement statement2 = new StatementImpl(a, vf.createURI(namespace, "p2"), vf.createLiteral("l"));
    ryaDAO.add(RdfToRyaConversions.convertStatement(statement));
    ryaDAO.add(RdfToRyaConversions.convertStatement(statement2));
    ryaDAO.add(RdfToRyaConversions.convertStatement(new StatementImpl(vf.createURI(namespace, "b"), vf.createURI(namespace, "p"), vf.createLiteral("l"))));
    ryaDAO.add(RdfToRyaConversions.convertStatement(new StatementImpl(vf.createURI(namespace, "c"), vf.createURI(namespace, "n"), vf.createLiteral("l"))));
    // build a connection
    RdfCloudTripleStore store = new RdfCloudTripleStore();
    store.setConf(conf);
    store.setRyaDAO(ryaDAO);
    InferenceEngine inferenceEngine = new InferenceEngine();
    inferenceEngine.setRyaDAO(ryaDAO);
    store.setInferenceEngine(inferenceEngine);
    store.initialize();
    System.out.println(Iterations.asList(store.getConnection().getStatements(a, vf.createURI(namespace, "p"), vf.createLiteral("l"), false, new Resource[0])).size());
}
Also used : RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) Statement(org.openrdf.model.Statement) StatementImpl(org.openrdf.model.impl.StatementImpl) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

Statement (org.openrdf.model.Statement)359 Test (org.junit.Test)209 ValueFactory (org.openrdf.model.ValueFactory)114 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)106 URI (org.openrdf.model.URI)96 HashSet (java.util.HashSet)88 Resource (org.openrdf.model.Resource)69 Value (org.openrdf.model.Value)67 BindingSet (org.openrdf.query.BindingSet)60 RyaStatement (org.apache.rya.api.domain.RyaStatement)56 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)54 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)53 MapBindingSet (org.openrdf.query.impl.MapBindingSet)43 ArrayList (java.util.ArrayList)41 LiteralImpl (org.openrdf.model.impl.LiteralImpl)40 StatementImpl (org.openrdf.model.impl.StatementImpl)40 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)36 URIImpl (org.openrdf.model.impl.URIImpl)30 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24