Search in sources :

Example 86 with Statement

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);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) URI(org.openrdf.model.URI) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 87 with Statement

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

Example 88 with Statement

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);
                }
            }
        }
    });
}
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 89 with Statement

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;
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) Statement(org.openrdf.model.Statement) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) URI(org.openrdf.model.URI) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 90 with Statement

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);
}
Also used : QueryEvaluationException(org.openrdf.query.QueryEvaluationException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI)

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