[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-23 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/incubator-rya/pull/206


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-22 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r134589415
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -723,29 +724,38 @@ public void handleStatement(final Statement 
statement) throws RDFHandlerExceptio
 }
 }
 }
-
-final List typeStatements = new ArrayList<>();
-ryaDaoQueryWrapper.queryAll(type, OWL.INTERSECTIONOF, null, 
new RDFHandlerBase() {
-@Override
-public void handleStatement(final Statement statement) 
throws RDFHandlerException {
-typeStatements.add(statement);
-}
-});
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, type);
+}
+}
+for (final Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
 
 final Set superClasses = getSuperClasses((URI) type);
+for (final URI superClass : superClasses) {
+// Add intersections to super classes if applicable.
+// IF:
+// :A intersectionOf[:B, :C]
+// AND
+// :A subclassOf :D
+// Then we can infer:
+// intersectionOf[:B, :C] subclassOf :D
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, superClass);
+}
+}
+// Check if other keys have any of the same intersections and 
infer
+// the same subclass logic to them that we know from the 
current
+// type. Propagating up through all the superclasses.
 for (final Set intersection : intersectionList) {
-addIntersection(intersection, type);
-for (final URI superClass : superClasses) {
-// Add intersections to super classes if applicable.
-// IF:
-// :A intersectionOf[:B, :C]
-// AND
-// :A subclassOf :D
-// Then we can infer:
-// intersectionOf[:B, :C] subclassOf :D
-for (final Statement statement : typeStatements) {
-final Resource intersectionOfBnode = (Resource) 
statement.getObject();
-addSubClassOf(intersectionOfBnode, superClass);
+final Set otherKeys = 
Sets.newHashSet(intersectionsProp.keySet());
+otherKeys.remove(type);
+for (final Resource otherKey : otherKeys) {
+if 
(intersectionsProp.get(otherKey).contains(intersection)) {
+for (final URI superClass : superClasses) {
--- End diff --

I updated this. otherKey and type are now inferred to be equivalentClasses 
so this changed some of the unit test expected output.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-22 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r134575148
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -653,170 +664,406 @@ private void 
refreshAllValuesFromRestrictions(Map restrictions) t
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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());
+}
+
+// head should point to a list of items that forms the
+// intersection.
+try {
+final Set 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> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+final Set otherTypes = new HashSet<>();
+// Combine all of a type's intersections together.
+for (final Set 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 intersection : intersectionList) {
+if (!intersection.contains(other)) {
+addIntersection(intersection, other);
+}
+}
+}
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, type);
+}
+}
+for (final Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+
+final Set superClasses = getSuperClasses((URI) type);
+for (final URI superClass : superClasses) {
+// Add intersections to super classes if applicable.
+// IF:
+// :A intersectionOf[:B, :C]
+// AND
+// :A subclassOf :D
+// Then we can infer:
+// intersectionOf[:B, :C] subclassOf :D
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, superClass);
+}
+}
+// Check if other keys have any of the same intersections and 
infer
+// the same subclass logic to them that we know from the 
current
+// type. Propagating 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-22 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r134537523
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -723,29 +724,38 @@ public void handleStatement(final Statement 
statement) throws RDFHandlerExceptio
 }
 }
 }
-
-final List typeStatements = new ArrayList<>();
-ryaDaoQueryWrapper.queryAll(type, OWL.INTERSECTIONOF, null, 
new RDFHandlerBase() {
-@Override
-public void handleStatement(final Statement statement) 
throws RDFHandlerException {
-typeStatements.add(statement);
-}
-});
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, type);
+}
+}
+for (final Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
 
 final Set superClasses = getSuperClasses((URI) type);
+for (final URI superClass : superClasses) {
+// Add intersections to super classes if applicable.
+// IF:
+// :A intersectionOf[:B, :C]
+// AND
+// :A subclassOf :D
+// Then we can infer:
+// intersectionOf[:B, :C] subclassOf :D
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, superClass);
+}
+}
+// Check if other keys have any of the same intersections and 
infer
+// the same subclass logic to them that we know from the 
current
+// type. Propagating up through all the superclasses.
 for (final Set intersection : intersectionList) {
-addIntersection(intersection, type);
-for (final URI superClass : superClasses) {
-// Add intersections to super classes if applicable.
-// IF:
-// :A intersectionOf[:B, :C]
-// AND
-// :A subclassOf :D
-// Then we can infer:
-// intersectionOf[:B, :C] subclassOf :D
-for (final Statement statement : typeStatements) {
-final Resource intersectionOfBnode = (Resource) 
statement.getObject();
-addSubClassOf(intersectionOfBnode, superClass);
+final Set otherKeys = 
Sets.newHashSet(intersectionsProp.keySet());
+otherKeys.remove(type);
+for (final Resource otherKey : otherKeys) {
+if 
(intersectionsProp.get(otherKey).contains(intersection)) {
+for (final URI superClass : superClasses) {
--- End diff --

I think if we get here we've found an equivalence between type and 
otherKey, not just a subclass relationship (if I'm following correctly, 
intersectionsProp has the direct definitions, as opposed to the indirect 
implications we're also going through, so at this point we have type and 
otherKey having the same definition).  If that's right, then instead of looping 
through the superclasses and adding them explicitly, we could do both 
"addSubClassOf(otherKey, type) ; addSubClassOf(type, otherKey)", and leave it 
up to whatever needs the superclass logic whether to traverse the graph 
recursively for the indirect superclasses.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-18 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r134081316
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -416,22 +425,131 @@ private void 
refreshHasValueRestrictions(Map restrictions) throws
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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 
RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement st1) throws 
Exception {
+final Resource type = st1.getSubject();
+// head will point to a type that is part of the 
intersection.
+URI head = (URI) st1.getObject();
+if (!intersectionsProp.containsKey(type)) {
+intersectionsProp.put(type, new 
ArrayList());
+}
+final Set intersection = new HashSet<>();
+// Go through and find all bnodes that are part of the 
defined
+// intersection.
+while (!RDF.NIL.equals(head)) {
+// rdf.first will point to a type item that is in the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.FIRST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st2) throws Exception{
+// The object found in the query represents a 
type
+// that should be included in the intersection.
+final URI obj2 = (URI) st2.getObject();
+intersection.add(obj2);
+}
+});
+final List headHolder = new ArrayList<>(1);
+// rdf.rest will point to the next bnode that's part 
of the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.REST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st3) throws Exception {
+// This object is the next bnode head to look 
for.
+final URI obj3 = (URI) st3.getObject();
+headHolder.add(obj3);
+}
+});
+// As long as we get a new head there are more bnodes 
that
+// are part of the intersection. Keep going until we 
reach
+// rdf.nil.
+if (!headHolder.isEmpty()) {
+head = headHolder.get(0);
+} else {
+head = RDF.NIL;
+}
+}
+// 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);
+}
+});
+
+for (final Map.Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+final Set otherTypes = new HashSet<>();
+// Combine all of a type's intersections together.
+for (final Set 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
+// 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-18 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r134081075
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -621,22 +631,189 @@ private void 
refreshHasValueRestrictions(Map restrictions) throws
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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());
+}
+
+// head should point to a list of items that forms the
+// intersection.
+try {
+final Set 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> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+final Set otherTypes = new HashSet<>();
+// Combine all of a type's intersections together.
+for (final Set 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 intersection : intersectionList) {
+if (!intersection.contains(other)) {
+addIntersection(intersection, other);
+}
+}
+}
+
+final List typeStatements = new ArrayList<>();
+ryaDaoQueryWrapper.queryAll(type, OWL.INTERSECTIONOF, null, 
new RDFHandlerBase() {
+@Override
+public void handleStatement(final Statement statement) 
throws RDFHandlerException {
+typeStatements.add(statement);
+}
+});
+
+final Set superClasses = getSuperClasses((URI) type);
+for (final Set intersection : intersectionList) {
+addIntersection(intersection, type);
+for (final URI superClass : superClasses) {
+// Add intersections to super classes if applicable.
+// IF:
+// :A intersectionOf[:B, :C]
+// AND
+// :A subclassOf :D
+// Then we can infer:
+// intersectionOf[:B, :C] subclassOf :D
+for (final Statement statement : typeStatements) {
+final Resource intersectionOfBnode = (Resource) 
statement.getObject();
+

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133567115
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -220,163 +229,163 @@ public void refreshGraph() throws 
InferenceEngineException {
 }
 }
 inverseOfMap = invProp;
-
-ValueFactory vf = ValueFactoryImpl.getInstance();
-iter = RyaDAOHelper.query(ryaDAO, null, 
-   
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
-   null, conf);
-Map propertyChainPropertiesToBNodes = new 
HashMap();
-propertyChainPropertyToChain = new HashMap();
+
+final ValueFactory vf = ValueFactoryImpl.getInstance();
+iter = RyaDAOHelper.query(ryaDAO, null,
+
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
+null, conf);
+final Map propertyChainPropertiesToBNodes = new 
HashMap<>();
+propertyChainPropertyToChain = new HashMap<>();
 try {
-   while (iter.hasNext()){
-   Statement st = iter.next();
-   
propertyChainPropertiesToBNodes.put((URI)st.getSubject(), (URI)st.getObject());
-   }
+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 (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
-   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);
-   TreeMap orderedProperties = new 
TreeMap();
-   // TODO refactor this.  Wish I could execute sparql
-   try {
-   while (iter.hasNext()){
- Statement st = iter.next();
- String indexedElement = 
st.getObject().stringValue();
- System.out.println(indexedElement);
- CloseableIteration  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()){
- 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()){
- 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()){
- Statement iter2Statement = 
iter2.next();
   

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133566726
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/evaluation/ReorderJoinVisitor.java
 ---
@@ -31,24 +31,24 @@
  * Date: Apr 11, 2011
  * Time: 10:16:15 PM
  */
-public class ReorderJoinVisitor extends QueryModelVisitorBase {
+public class ReorderJoinVisitor extends QueryModelVisitorBase {
--- End diff --

Removed a warning. The rest was save actions.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133566433
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
+checkNotNull(ryaDaoStatementIterHandler);
+final CloseableIteration iter 
= RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
+try {
+while (iter.hasNext()) {
+final Statement statement = iter.next();
+try {
+
ryaDaoStatementIterHandler.handleStatementIter(statement);
+} catch (final Exception e) {
+throw new QueryEvaluationException("Error handling 
statement.", e);
+}
+}
+} finally {
+if (iter != null) {
+iter.close();
+}
+}
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param statement the {@link Statement} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133566456
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoStatementIterHandler.java
 ---
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import org.apache.rya.api.persist.RyaDAO;
+import org.openrdf.model.Statement;
+
+/**
+ * Handles the statements returned from a {@link RyaDAO} query iterator.
+ */
+public abstract class RyaDaoStatementIterHandler {
--- End diff --

Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133566400
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
+checkNotNull(ryaDaoStatementIterHandler);
+final CloseableIteration iter 
= RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
+try {
+while (iter.hasNext()) {
+final Statement statement = iter.next();
+try {
+
ryaDaoStatementIterHandler.handleStatementIter(statement);
+} catch (final Exception e) {
+throw new QueryEvaluationException("Error handling 
statement.", e);
+}
+}
+} finally {
+if (iter != null) {
+iter.close();
+}
+}
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param statement the {@link Statement} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133566012
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
--- End diff --

Changed to RDFHandler in this class and RDFHandlerBase in the 
InferenceEngine.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133565628
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -467,126 +585,180 @@ private static void addParents(Vertex v, Set 
parents) {
 });
 }
 
-public boolean isSymmetricProperty(URI prop) {
+public boolean isSymmetricProperty(final URI prop) {
 return (symmetricPropertySet != null) && 
symmetricPropertySet.contains(prop);
 }
 
-public URI findInverseOf(URI prop) {
+public URI findInverseOf(final URI prop) {
 return (inverseOfMap != null) ? inverseOfMap.get(prop) : (null);
 }
 
-public boolean isTransitiveProperty(URI prop) {
+public boolean isTransitiveProperty(final URI prop) {
 return (transitivePropertySet != null) && 
transitivePropertySet.contains(prop);
 }
 
 /**
  * TODO: This chaining can be slow at query execution. the other 
option is to perform this in the query itself, but that will be constrained to 
how many levels we decide to go
  */
-public Set findTransitiveProperty(Resource subj, URI prop, 
Value obj, Resource... contxts) throws InferenceEngineException {
+public Set findTransitiveProperty(final Resource subj, 
final URI prop, final Value obj, final Resource... contxts) throws 
InferenceEngineException {
 if (transitivePropertySet.contains(prop)) {
-Set sts = new HashSet();
-boolean goUp = subj == null;
+final Set sts = new HashSet<>();
+final boolean goUp = subj == null;
 chainTransitiveProperty(subj, prop, obj, (goUp) ? (obj) : 
(subj), sts, goUp, contxts);
 return sts;
-} else
+} else {
 return null;
+}
 }
 
 /**
  * TODO: This chaining can be slow at query execution. the other 
option is to perform this in the query itself, but that will be constrained to 
how many levels we decide to go
  */
-public Set findSameAs(Resource value, Resource... contxts) 
throws InferenceEngineException{
-   Set sameAs = new HashSet();
-   sameAs.add(value);
-   findSameAsChaining(value, sameAs, contxts);
-   return sameAs;
+public Set findSameAs(final Resource value, final 
Resource... contxts) throws InferenceEngineException{
+final Set sameAs = new HashSet();
+sameAs.add(value);
+findSameAsChaining(value, sameAs, contxts);
+return sameAs;
+}
+
+public CloseableIteration 
queryDao(final Resource subject, final URI predicate, final Value object, final 
Resource... contexts) throws QueryEvaluationException {
+return RyaDAOHelper.query(ryaDAO, subject, predicate, object, 
conf, contexts);
 }
 
 /**
  * TODO: This chaining can be slow at query execution. the other 
option is to perform this in the query itself, but that will be constrained to 
how many levels we decide to go
  */
-public void findSameAsChaining(Resource subj, Set 
currentSameAs, Resource[] contxts) throws InferenceEngineException{
+public void findSameAsChaining(final Resource subj, final 
Set currentSameAs, final Resource[] contxts) throws 
InferenceEngineException{
+CloseableIteration subjIter = 
null;
+CloseableIteration objIter = 
null;
 try {
-   CloseableIteration 
subjIter = RyaDAOHelper.query(ryaDAO, subj, OWL.SAMEAS, null, conf, contxts);
-   while (subjIter.hasNext()){
-   Statement st = subjIter.next();
-   if (!currentSameAs.contains(st.getObject())){
-   Resource castedObj = (Resource) 
st.getObject();
-   currentSameAs.add(castedObj);
-   findSameAsChaining(castedObj, 
currentSameAs, contxts);
-   }
-   }
-   subjIter.close();
-   CloseableIteration 
objIter = RyaDAOHelper.query(ryaDAO, null, OWL.SAMEAS, subj, conf, contxts);
-   while (objIter.hasNext()){
-   Statement st = objIter.next();
-   if (!currentSameAs.contains(st.getSubject())){
-   Resource sameAsSubj = st.getSubject();
-   currentSameAs.add(sameAsSubj);
  

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133565270
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -220,163 +229,163 @@ public void refreshGraph() throws 
InferenceEngineException {
 }
 }
 inverseOfMap = invProp;
-
-ValueFactory vf = ValueFactoryImpl.getInstance();
-iter = RyaDAOHelper.query(ryaDAO, null, 
-   
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
-   null, conf);
-Map propertyChainPropertiesToBNodes = new 
HashMap();
-propertyChainPropertyToChain = new HashMap();
+
+final ValueFactory vf = ValueFactoryImpl.getInstance();
+iter = RyaDAOHelper.query(ryaDAO, null,
+
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
+null, conf);
+final Map propertyChainPropertiesToBNodes = new 
HashMap<>();
+propertyChainPropertyToChain = new HashMap<>();
 try {
-   while (iter.hasNext()){
-   Statement st = iter.next();
-   
propertyChainPropertiesToBNodes.put((URI)st.getSubject(), (URI)st.getObject());
-   }
+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 (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
-   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);
-   TreeMap orderedProperties = new 
TreeMap();
-   // TODO refactor this.  Wish I could execute sparql
-   try {
-   while (iter.hasNext()){
- Statement st = iter.next();
- String indexedElement = 
st.getObject().stringValue();
- System.out.println(indexedElement);
- CloseableIteration  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()){
- 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()){
- 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()){
- Statement iter2Statement = 
iter2.next();
   

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133565184
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -220,163 +229,163 @@ public void refreshGraph() throws 
InferenceEngineException {
 }
 }
 inverseOfMap = invProp;
-
-ValueFactory vf = ValueFactoryImpl.getInstance();
-iter = RyaDAOHelper.query(ryaDAO, null, 
-   
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
-   null, conf);
-Map propertyChainPropertiesToBNodes = new 
HashMap();
-propertyChainPropertyToChain = new HashMap();
+
+final ValueFactory vf = ValueFactoryImpl.getInstance();
+iter = RyaDAOHelper.query(ryaDAO, null,
+
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
+null, conf);
+final Map propertyChainPropertiesToBNodes = new 
HashMap<>();
+propertyChainPropertyToChain = new HashMap<>();
 try {
-   while (iter.hasNext()){
-   Statement st = iter.next();
-   
propertyChainPropertiesToBNodes.put((URI)st.getSubject(), (URI)st.getObject());
-   }
+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 (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
-   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);
-   TreeMap orderedProperties = new 
TreeMap();
-   // TODO refactor this.  Wish I could execute sparql
-   try {
-   while (iter.hasNext()){
- Statement st = iter.next();
- String indexedElement = 
st.getObject().stringValue();
- System.out.println(indexedElement);
- CloseableIteration  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()){
- 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()){
- 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()){
- Statement iter2Statement = 
iter2.next();
   

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread ejwhite922
Github user ejwhite922 commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133565124
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitor.java
 ---
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.rdftriplestore.inference;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.log4j.Logger;
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.openrdf.model.Resource;
+import org.openrdf.model.URI;
+import org.openrdf.model.vocabulary.RDF;
+import org.openrdf.query.algebra.StatementPattern;
+import org.openrdf.query.algebra.TupleExpr;
+import org.openrdf.query.algebra.Union;
+import org.openrdf.query.algebra.Var;
+
+/**
+ * Visitor for handling owl:intersectionOf inferencing on a node.
+ */
+public class IntersectionOfVisitor extends AbstractInferVisitor {
+private static final Logger log = 
Logger.getLogger(IntersectionOfVisitor.class);
+
+/**
+ * Creates a new instance of {@link IntersectionOfVisitor}.
+ * @param conf the {@link RdfCloudeTripleStoreConfiguration}.
+ * @param inferenceEngine the {@link InferenceEngine}.
+ */
+public IntersectionOfVisitor(final RdfCloudTripleStoreConfiguration 
conf, final InferenceEngine inferenceEngine) {
+super(conf, inferenceEngine);
+include = true;
+}
+
+@Override
+protected void meetSP(final StatementPattern node) throws Exception {
+final StatementPattern currentNode = node.clone();
+final Var subVar = node.getSubjectVar();
+final Var predVar = node.getPredicateVar();
+final Var objVar = node.getObjectVar();
+final Var conVar = node.getContextVar();
+if (predVar != null && objVar != null && objVar.getValue() != null 
&& RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
+final List intersections = 
inferenceEngine.getIntersectionsImplying((URI) objVar.getValue());
+if (intersections != null && !intersections.isEmpty()) {
+final Set combinedIntersections = new 
TreeSet<>(new ResourceComparator());
--- End diff --

Done.  Created a union tree of joins for each intersection.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133454880
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
+checkNotNull(ryaDaoStatementIterHandler);
+final CloseableIteration iter 
= RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
+try {
+while (iter.hasNext()) {
+final Statement statement = iter.next();
+try {
+
ryaDaoStatementIterHandler.handleStatementIter(statement);
+} catch (final Exception e) {
+throw new QueryEvaluationException("Error handling 
statement.", e);
+}
+}
+} finally {
+if (iter != null) {
+iter.close();
+}
+}
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param statement the {@link Statement} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133461212
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -178,11 +183,15 @@ public void refreshGraph() throws 
InferenceEngineException {
 
 subPropertyOfGraph = graph; //TODO: Should this be 
synchronized?
 
+
+refreshIntersectionOf();
--- End diff --

Awesome!  Thanks for separating out the intersection refresh logic here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133448942
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
--- End diff --

Yeah, I agree with Jesse here.  We should avoid creating interfaces/classes 
that provide the same functionality as preexisting interfaces/classes.  Use the 
RDFHandler here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133458015
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/evaluation/FilterRangeVisitor.java
 ---
@@ -27,63 +33,62 @@
 import org.openrdf.model.Value;
 import org.openrdf.model.impl.BooleanLiteralImpl;
 import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.algebra.*;
+import org.openrdf.query.algebra.Filter;
+import org.openrdf.query.algebra.FunctionCall;
+import org.openrdf.query.algebra.StatementPattern;
+import org.openrdf.query.algebra.ValueConstant;
+import org.openrdf.query.algebra.ValueExpr;
+import org.openrdf.query.algebra.Var;
 import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
 
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.apache.rya.api.RdfCloudTripleStoreConstants.RANGE;
-
 /**
  * Class FilterTimeIndexVisitor
  * Date: Apr 11, 2011
  * Time: 10:16:15 PM
  */
-public class FilterRangeVisitor extends QueryModelVisitorBase {
+public class FilterRangeVisitor extends QueryModelVisitorBase {
 
-private RdfCloudTripleStoreConfiguration conf;
-private Map rangeValues = new HashMap();
+private final RdfCloudTripleStoreConfiguration conf;
+private final Map rangeValues = new HashMap();
 
-public FilterRangeVisitor(RdfCloudTripleStoreConfiguration conf) {
+public FilterRangeVisitor(final RdfCloudTripleStoreConfiguration conf) 
{
 this.conf = conf;
 }
 
 @Override
-public void meet(Filter node) throws Exception {
+public void meet(final Filter node) throws Exception {
--- End diff --

Were any substantive changes made to this file?  Seems like you just added 
final everywhere.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133455337
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoStatementIterHandler.java
 ---
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import org.apache.rya.api.persist.RyaDAO;
+import org.openrdf.model.Statement;
+
+/**
+ * Handles the statements returned from a {@link RyaDAO} query iterator.
+ */
+public abstract class RyaDaoStatementIterHandler {
--- End diff --

Use the RDFHandler interface.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133478457
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -416,22 +425,131 @@ private void 
refreshHasValueRestrictions(Map restrictions) throws
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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 
RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement st1) throws 
Exception {
+final Resource type = st1.getSubject();
+// head will point to a type that is part of the 
intersection.
+URI head = (URI) st1.getObject();
+if (!intersectionsProp.containsKey(type)) {
+intersectionsProp.put(type, new 
ArrayList());
+}
+final Set intersection = new HashSet<>();
+// Go through and find all bnodes that are part of the 
defined
+// intersection.
+while (!RDF.NIL.equals(head)) {
+// rdf.first will point to a type item that is in the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.FIRST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st2) throws Exception{
+// The object found in the query represents a 
type
+// that should be included in the intersection.
+final URI obj2 = (URI) st2.getObject();
+intersection.add(obj2);
+}
+});
+final List headHolder = new ArrayList<>(1);
+// rdf.rest will point to the next bnode that's part 
of the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.REST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st3) throws Exception {
+// This object is the next bnode head to look 
for.
+final URI obj3 = (URI) st3.getObject();
+headHolder.add(obj3);
+}
+});
+// As long as we get a new head there are more bnodes 
that
+// are part of the intersection. Keep going until we 
reach
+// rdf.nil.
+if (!headHolder.isEmpty()) {
+head = headHolder.get(0);
+} else {
+head = RDF.NIL;
+}
+}
+// 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);
+}
+});
+
+for (final Map.Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+final Set otherTypes = new HashSet<>();
+// Combine all of a type's intersections together.
+for (final Set 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 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133458754
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/evaluation/ReorderJoinVisitor.java
 ---
@@ -31,24 +31,24 @@
  * Date: Apr 11, 2011
  * Time: 10:16:15 PM
  */
-public class ReorderJoinVisitor extends QueryModelVisitorBase {
+public class ReorderJoinVisitor extends QueryModelVisitorBase {
--- End diff --

Same as above.  Looks like you just added final everywhere.  Was this 
intentional or just a by-product of your eclipse formatting settings?  Are 
these visitors important for the intersectionOf query expansion?  Seems like 
that would just use the subClass visitor.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133450932
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
+checkNotNull(ryaDaoStatementIterHandler);
+final CloseableIteration iter 
= RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
+try {
+while (iter.hasNext()) {
+final Statement statement = iter.next();
+try {
+
ryaDaoStatementIterHandler.handleStatementIter(statement);
+} catch (final Exception e) {
+throw new QueryEvaluationException("Error handling 
statement.", e);
+}
+}
+} finally {
+if (iter != null) {
+iter.close();
+}
+}
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param statement the {@link Statement} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-16 Thread meiercaleb
Github user meiercaleb commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133465643
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -416,22 +425,131 @@ private void 
refreshHasValueRestrictions(Map restrictions) throws
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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 .
--- End diff --

Thanks for documenting this.  It really helps clarify the logic.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133323192
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -467,126 +585,180 @@ private static void addParents(Vertex v, Set 
parents) {
 });
 }
 
-public boolean isSymmetricProperty(URI prop) {
+public boolean isSymmetricProperty(final URI prop) {
 return (symmetricPropertySet != null) && 
symmetricPropertySet.contains(prop);
 }
 
-public URI findInverseOf(URI prop) {
+public URI findInverseOf(final URI prop) {
 return (inverseOfMap != null) ? inverseOfMap.get(prop) : (null);
 }
 
-public boolean isTransitiveProperty(URI prop) {
+public boolean isTransitiveProperty(final URI prop) {
 return (transitivePropertySet != null) && 
transitivePropertySet.contains(prop);
 }
 
 /**
  * TODO: This chaining can be slow at query execution. the other 
option is to perform this in the query itself, but that will be constrained to 
how many levels we decide to go
  */
-public Set findTransitiveProperty(Resource subj, URI prop, 
Value obj, Resource... contxts) throws InferenceEngineException {
+public Set findTransitiveProperty(final Resource subj, 
final URI prop, final Value obj, final Resource... contxts) throws 
InferenceEngineException {
 if (transitivePropertySet.contains(prop)) {
-Set sts = new HashSet();
-boolean goUp = subj == null;
+final Set sts = new HashSet<>();
+final boolean goUp = subj == null;
 chainTransitiveProperty(subj, prop, obj, (goUp) ? (obj) : 
(subj), sts, goUp, contxts);
 return sts;
-} else
+} else {
 return null;
+}
 }
 
 /**
  * TODO: This chaining can be slow at query execution. the other 
option is to perform this in the query itself, but that will be constrained to 
how many levels we decide to go
  */
-public Set findSameAs(Resource value, Resource... contxts) 
throws InferenceEngineException{
-   Set sameAs = new HashSet();
-   sameAs.add(value);
-   findSameAsChaining(value, sameAs, contxts);
-   return sameAs;
+public Set findSameAs(final Resource value, final 
Resource... contxts) throws InferenceEngineException{
+final Set sameAs = new HashSet();
+sameAs.add(value);
+findSameAsChaining(value, sameAs, contxts);
+return sameAs;
+}
+
+public CloseableIteration 
queryDao(final Resource subject, final URI predicate, final Value object, final 
Resource... contexts) throws QueryEvaluationException {
+return RyaDAOHelper.query(ryaDAO, subject, predicate, object, 
conf, contexts);
 }
 
 /**
  * TODO: This chaining can be slow at query execution. the other 
option is to perform this in the query itself, but that will be constrained to 
how many levels we decide to go
  */
-public void findSameAsChaining(Resource subj, Set 
currentSameAs, Resource[] contxts) throws InferenceEngineException{
+public void findSameAsChaining(final Resource subj, final 
Set currentSameAs, final Resource[] contxts) throws 
InferenceEngineException{
+CloseableIteration subjIter = 
null;
+CloseableIteration objIter = 
null;
 try {
-   CloseableIteration 
subjIter = RyaDAOHelper.query(ryaDAO, subj, OWL.SAMEAS, null, conf, contxts);
-   while (subjIter.hasNext()){
-   Statement st = subjIter.next();
-   if (!currentSameAs.contains(st.getObject())){
-   Resource castedObj = (Resource) 
st.getObject();
-   currentSameAs.add(castedObj);
-   findSameAsChaining(castedObj, 
currentSameAs, contxts);
-   }
-   }
-   subjIter.close();
-   CloseableIteration 
objIter = RyaDAOHelper.query(ryaDAO, null, OWL.SAMEAS, subj, conf, contxts);
-   while (objIter.hasNext()){
-   Statement st = objIter.next();
-   if (!currentSameAs.contains(st.getSubject())){
-   Resource sameAsSubj = st.getSubject();
-   

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133319932
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -416,22 +425,131 @@ private void 
refreshHasValueRestrictions(Map restrictions) throws
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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 
RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement st1) throws 
Exception {
+final Resource type = st1.getSubject();
+// head will point to a type that is part of the 
intersection.
+URI head = (URI) st1.getObject();
+if (!intersectionsProp.containsKey(type)) {
+intersectionsProp.put(type, new 
ArrayList());
+}
+final Set intersection = new HashSet<>();
+// Go through and find all bnodes that are part of the 
defined
+// intersection.
+while (!RDF.NIL.equals(head)) {
+// rdf.first will point to a type item that is in the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.FIRST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st2) throws Exception{
+// The object found in the query represents a 
type
+// that should be included in the intersection.
+final URI obj2 = (URI) st2.getObject();
+intersection.add(obj2);
+}
+});
+final List headHolder = new ArrayList<>(1);
+// rdf.rest will point to the next bnode that's part 
of the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.REST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st3) throws Exception {
+// This object is the next bnode head to look 
for.
+final URI obj3 = (URI) st3.getObject();
+headHolder.add(obj3);
+}
+});
+// As long as we get a new head there are more bnodes 
that
+// are part of the intersection. Keep going until we 
reach
+// rdf.nil.
+if (!headHolder.isEmpty()) {
+head = headHolder.get(0);
+} else {
+head = RDF.NIL;
+}
+}
+// 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);
+}
+});
+
+for (final Map.Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+final Set otherTypes = new HashSet<>();
+// Combine all of a type's intersections together.
+for (final Set 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
+// 

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133311778
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -220,163 +229,163 @@ public void refreshGraph() throws 
InferenceEngineException {
 }
 }
 inverseOfMap = invProp;
-
-ValueFactory vf = ValueFactoryImpl.getInstance();
-iter = RyaDAOHelper.query(ryaDAO, null, 
-   
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
-   null, conf);
-Map propertyChainPropertiesToBNodes = new 
HashMap();
-propertyChainPropertyToChain = new HashMap();
+
+final ValueFactory vf = ValueFactoryImpl.getInstance();
+iter = RyaDAOHelper.query(ryaDAO, null,
+
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
+null, conf);
+final Map propertyChainPropertiesToBNodes = new 
HashMap<>();
+propertyChainPropertyToChain = new HashMap<>();
 try {
-   while (iter.hasNext()){
-   Statement st = iter.next();
-   
propertyChainPropertiesToBNodes.put((URI)st.getSubject(), (URI)st.getObject());
-   }
+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 (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
-   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);
-   TreeMap orderedProperties = new 
TreeMap();
-   // TODO refactor this.  Wish I could execute sparql
-   try {
-   while (iter.hasNext()){
- Statement st = iter.next();
- String indexedElement = 
st.getObject().stringValue();
- System.out.println(indexedElement);
- CloseableIteration  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()){
- 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()){
- 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()){
- Statement iter2Statement = 
iter2.next();

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133310947
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -220,163 +229,163 @@ public void refreshGraph() throws 
InferenceEngineException {
 }
 }
 inverseOfMap = invProp;
-
-ValueFactory vf = ValueFactoryImpl.getInstance();
-iter = RyaDAOHelper.query(ryaDAO, null, 
-   
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
-   null, conf);
-Map propertyChainPropertiesToBNodes = new 
HashMap();
-propertyChainPropertyToChain = new HashMap();
+
+final ValueFactory vf = ValueFactoryImpl.getInstance();
+iter = RyaDAOHelper.query(ryaDAO, null,
+
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
+null, conf);
+final Map propertyChainPropertiesToBNodes = new 
HashMap<>();
+propertyChainPropertyToChain = new HashMap<>();
 try {
-   while (iter.hasNext()){
-   Statement st = iter.next();
-   
propertyChainPropertiesToBNodes.put((URI)st.getSubject(), (URI)st.getObject());
-   }
+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 (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
-   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);
-   TreeMap orderedProperties = new 
TreeMap();
-   // TODO refactor this.  Wish I could execute sparql
-   try {
-   while (iter.hasNext()){
- Statement st = iter.next();
- String indexedElement = 
st.getObject().stringValue();
- System.out.println(indexedElement);
- CloseableIteration  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()){
- 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()){
- 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()){
- Statement iter2Statement = 
iter2.next();

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133329139
  
--- Diff: 
common/rya.api/src/main/java/org/apache/rya/api/persist/utils/RyaDaoQueryWrapper.java
 ---
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.persist.utils;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
+import org.apache.rya.api.domain.RyaStatement;
+import org.apache.rya.api.persist.RyaDAO;
+import org.apache.rya.api.resolver.RyaToRdfConversions;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.QueryEvaluationException;
+
+import info.aduna.iteration.CloseableIteration;
+
+/**
+ * Wraps Rya DAO queries into a simpler interface that just passes in the
+ * statement to query for and a handler for dealing with each statement in 
the
+ * query result. This handles iterating over the query, throwing any 
exceptions,
+ * and closing the query iterator when done. The same wrapper can be 
re-used
+ * for multiple queries.
+ */
+public class RyaDaoQueryWrapper {
+private final RyaDAO ryaDao;
+private final RdfCloudTripleStoreConfiguration conf;
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ * @param conf the {@link RdfCloudTripleStoreConfiguration}.
+ * (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao, final 
RdfCloudTripleStoreConfiguration conf) {
+this.ryaDao = checkNotNull(ryaDao);
+this.conf = checkNotNull(conf);
+}
+
+/**
+ * Creates a new instance of {@link RyaDaoQueryWrapper}.
+ * @param ryaDao the {@link RyaDAO}. (not {@code null})
+ */
+public RyaDaoQueryWrapper(final RyaDAO ryaDao) {
+this(checkNotNull(ryaDao), ryaDao.getConf());
+}
+
+/**
+ * Handles all results of a query. Closes the query iterator when done.
+ * @param subject the subject {@link Resource} to query for.
+ * @param predicate the predicate {@link URI} to query for.
+ * @param object the object {@link Value} to query for.
+ * @param ryaDaoStatementIterHandler the {@link 
RyaDaoStatementIterHandler}
+ * to use for handling each statement returned. (not {@code null})
+ * @param contexts the context {@link Resource}s to query for.
+ * @throws QueryEvaluationException
+ */
+public void queryAll(final Resource subject, final URI predicate, 
final Value object, final RyaDaoStatementIterHandler 
ryaDaoStatementIterHandler, final Resource... contexts) throws 
QueryEvaluationException {
--- End diff --

Would it make sense to use the org.openrdf.rio.RDFHandler interface (and 
RDFHandlerBase) instead of a new handler class? That would let someone directly 
pass in something like an RDFWriter, if that were ever useful for something.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133312263
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -220,163 +229,163 @@ public void refreshGraph() throws 
InferenceEngineException {
 }
 }
 inverseOfMap = invProp;
-
-ValueFactory vf = ValueFactoryImpl.getInstance();
-iter = RyaDAOHelper.query(ryaDAO, null, 
-   
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
-   null, conf);
-Map propertyChainPropertiesToBNodes = new 
HashMap();
-propertyChainPropertyToChain = new HashMap();
+
+final ValueFactory vf = ValueFactoryImpl.getInstance();
+iter = RyaDAOHelper.query(ryaDAO, null,
+
vf.createURI("http://www.w3.org/2002/07/owl#propertyChainAxiom;),
+null, conf);
+final Map propertyChainPropertiesToBNodes = new 
HashMap<>();
+propertyChainPropertyToChain = new HashMap<>();
 try {
-   while (iter.hasNext()){
-   Statement st = iter.next();
-   
propertyChainPropertiesToBNodes.put((URI)st.getSubject(), (URI)st.getObject());
-   }
+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 (URI propertyChainProperty : 
propertyChainPropertiesToBNodes.keySet()){
-   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);
-   TreeMap orderedProperties = new 
TreeMap();
-   // TODO refactor this.  Wish I could execute sparql
-   try {
-   while (iter.hasNext()){
- Statement st = iter.next();
- String indexedElement = 
st.getObject().stringValue();
- System.out.println(indexedElement);
- CloseableIteration  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()){
- 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()){
- 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()){
- Statement iter2Statement = 
iter2.next();

[GitHub] incubator-rya pull request #206: RYA-292 Added owl:intersectionOf inference.

2017-08-15 Thread jessehatfield
Github user jessehatfield commented on a diff in the pull request:

https://github.com/apache/incubator-rya/pull/206#discussion_r133321220
  
--- Diff: 
sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java 
---
@@ -416,22 +425,131 @@ private void 
refreshHasValueRestrictions(Map restrictions) throws
 }
 }
 
-private static Vertex getVertex(Graph graph, Object id) {
-Iterator it = graph.vertices(id.toString());
+private void refreshIntersectionOf() throws QueryEvaluationException {
+final Map> 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 
RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement st1) throws 
Exception {
+final Resource type = st1.getSubject();
+// head will point to a type that is part of the 
intersection.
+URI head = (URI) st1.getObject();
+if (!intersectionsProp.containsKey(type)) {
+intersectionsProp.put(type, new 
ArrayList());
+}
+final Set intersection = new HashSet<>();
+// Go through and find all bnodes that are part of the 
defined
+// intersection.
+while (!RDF.NIL.equals(head)) {
+// rdf.first will point to a type item that is in the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.FIRST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st2) throws Exception{
+// The object found in the query represents a 
type
+// that should be included in the intersection.
+final URI obj2 = (URI) st2.getObject();
+intersection.add(obj2);
+}
+});
+final List headHolder = new ArrayList<>(1);
+// rdf.rest will point to the next bnode that's part 
of the
+// intersection.
+ryaDaoQueryWrapper.queryFirst(head, RDF.REST, null, 
new RyaDaoStatementIterHandler() {
+@Override
+public void handleStatementIter(final Statement 
st3) throws Exception {
+// This object is the next bnode head to look 
for.
+final URI obj3 = (URI) st3.getObject();
+headHolder.add(obj3);
+}
+});
+// As long as we get a new head there are more bnodes 
that
+// are part of the intersection. Keep going until we 
reach
+// rdf.nil.
+if (!headHolder.isEmpty()) {
+head = headHolder.get(0);
+} else {
+head = RDF.NIL;
+}
+}
+// 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);
+}
+});
+
+for (final Map.Entry> entry : 
intersectionsProp.entrySet()) {
+final Resource type = entry.getKey();
+final List intersectionList = entry.getValue();
+final Set otherTypes = new HashSet<>();
+// Combine all of a type's intersections together.
+for (final Set 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
+//