Skip to content

Commit

Permalink
RYA-292 PR Update #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ejwhite922 committed Aug 22, 2017
1 parent c44d559 commit 062bc68
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.rya.api.persist.utils.RyaDAOHelper;
import org.apache.rya.api.persist.utils.RyaDaoQueryWrapper;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
Expand All @@ -63,6 +62,8 @@
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.helpers.RDFHandlerBase;

import com.google.common.collect.Sets;

import info.aduna.iteration.CloseableIteration;

/**
Expand Down Expand Up @@ -562,7 +563,7 @@ private void refreshDomainRange() throws QueryEvaluationException {
// make sure that the consequent of a domain/range inference goes on to apply any more
// general classes as well.
for (final URI subtype : domainRangeTypeSet) {
final Set<URI> supertypes = findChildren(subClassOfGraph, subtype);
final Set<URI> supertypes = getSuperClasses(subtype);
final Set<URI> propertiesWithDomain = domainByTypePartial.getOrDefault(subtype, new HashSet<>());
final Set<URI> propertiesWithRange = rangeByTypePartial.getOrDefault(subtype, new HashSet<>());
for (final URI supertype : supertypes) {
Expand Down Expand Up @@ -646,7 +647,7 @@ private void refreshAllValuesFromRestrictions(final Map<Resource, URI> restricti
final Set<Resource> restrictionClasses = new HashSet<>();
restrictionClasses.add(st.getSubject());
if (st.getSubject() instanceof URI) {
restrictionClasses.addAll(findParents(subClassOfGraph, (URI) st.getSubject()));
restrictionClasses.addAll(getSubClasses((URI) st.getSubject()));
}
for (final Resource restrictionClass : restrictionClasses) {
if (!allValuesFromByValueType.containsKey(valueClass)) {
Expand Down Expand Up @@ -723,29 +724,38 @@ public void handleStatement(final Statement statement) throws RDFHandlerExceptio
}
}
}

final List<Statement> 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<Resource> intersection : intersectionList) {
addIntersection(intersection, type);
}
}
for (final Entry<Resource, List<Set<Resource>>> entry : intersectionsProp.entrySet()) {
final Resource type = entry.getKey();
final List<Set<Resource>> intersectionList = entry.getValue();

final Set<URI> superClasses = getSuperClasses((URI) type);
for (final URI superClass : superClasses) {
// 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<Resource> 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<Resource> 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<Resource> otherKeys = Sets.newHashSet(intersectionsProp.keySet());
otherKeys.remove(type);
for (final Resource otherKey : otherKeys) {
if (intersectionsProp.get(otherKey).contains(intersection)) {
for (final URI superClass : superClasses) {
addSubClassOf(otherKey, superClass);
}
}
}
}
Expand Down Expand Up @@ -838,7 +848,7 @@ private static Vertex getVertex(final Graph graph, final Object id) {
return null;
}

private void addStatementEdge(final Graph graph, final String edgeName, final Statement st) {
private static void addStatementEdge(final Graph graph, final String edgeName, final Statement st) {
final Resource subj = st.getSubject();
Vertex a = getVertex(graph, subj);
if (a == null) {
Expand All @@ -852,17 +862,47 @@ private void addStatementEdge(final Graph graph, final String edgeName, final St
b.property(URI_PROP, obj);
}
a.addEdge(edgeName, b);
}
}

public Set<URI> findParents(final Graph graph, final URI vertexId) {
return findConnected(graph, vertexId, Direction.IN);
/**
* Returns all super class types of the specified type based on the
* internal subclass graph.
* @param type the type {@link URI} to find super classes for.
* @return the {@link Set} of {@link URI} types that are super classes types
* of the specified {@code type}. Returns an empty set if nothing was found.
*/
public Set<URI> getSuperClasses(final URI type) {
return findChildren(subClassOfGraph, type);
}

public Set<URI> findChildren(final Graph graph, final URI vertexId) {
return findConnected(graph, vertexId, Direction.OUT);
/**
* Returns all sub class types of the specified type based on the
* internal subclass graph.
* @param type the type {@link URI} to find sub classes for.
* @return the {@link Set} of {@link URI} types that are sub classes types
* of the specified {@code type}. Returns an empty set if nothing was found.
*/
public Set<URI> getSubClasses(final URI type) {
return findParents(subClassOfGraph, type);
}

private Set<URI> findConnected(final Graph graph, final URI vertexId, final Direction traversal) {
public static Set<URI> findParents(final Graph graph, final URI vertexId) {
return findParents(graph, vertexId, true);
}

public static Set<URI> findParents(final Graph graph, final URI vertexId, final boolean isRecursive) {
return findConnected(graph, vertexId, Direction.IN, isRecursive);
}

public static Set<URI> findChildren(final Graph graph, final URI vertexId) {
return findChildren(graph, vertexId, true);
}

public static Set<URI> findChildren(final Graph graph, final URI vertexId, final boolean isRecursive) {
return findConnected(graph, vertexId, Direction.OUT, isRecursive);
}

private static Set<URI> findConnected(final Graph graph, final URI vertexId, final Direction traversal, final boolean isRecursive) {
final Set<URI> connected = new HashSet<>();
if (graph == null) {
return connected;
Expand All @@ -871,19 +911,21 @@ private Set<URI> findConnected(final Graph graph, final URI vertexId, final Dire
if (v == null) {
return connected;
}
addConnected(v, connected, traversal);
addConnected(v, connected, traversal, isRecursive);
return connected;
}

private static void addConnected(final Vertex v, final Set<URI> connected, final Direction traversal) {
private static void addConnected(final Vertex v, final Set<URI> connected, final Direction traversal, final boolean isRecursive) {
v.edges(traversal).forEachRemaining(edge -> {
final Vertex ov = edge.vertices(traversal.opposite()).next();
final Object o = ov.property(URI_PROP).value();
if (o != null && o instanceof URI) {
final boolean contains = connected.contains(o);
if (!contains) {
connected.add((URI) o);
addConnected(ov, connected, traversal);
if (isRecursive) {
addConnected(ov, connected, traversal, isRecursive);
}
}
}
});
Expand Down Expand Up @@ -1029,32 +1071,6 @@ public Graph getSubClassOfGraph() {
return subClassOfGraph;
}

/**
* Returns all super class types of the specified type based on the
* internal subclass graph.
* @param type the type {@link URI} to find super classes for.
* @return the {@link Set} of {@link URI} types that are super classes types
* of the specified {@code type}. Returns an empty set if nothing was found.
*/
public Set<URI> getSuperClasses(final URI type) {
final Set<URI> superClassUris = new HashSet<>();
if (type != null) {
final Iterator<Vertex> vertexIterator = subClassOfGraph.vertices(type.stringValue());
if (vertexIterator.hasNext()) {
final Vertex vertex = vertexIterator.next();
final ValueFactory vf = ValueFactoryImpl.getInstance();
final Iterator<Edge> edgeIterator = vertex.edges(Direction.OUT, RDFS.SUBCLASSOF.stringValue());
while (edgeIterator.hasNext()) {
final Edge edge = edgeIterator.next();
final Vertex inVertex = edge.inVertex();
final String id = (String) inVertex.id();
superClassUris.add(vf.createURI(id));
}
}
}
return superClassUris;
}

public Map<URI, List<URI>> getPropertyChainMap() {
return propertyChainPropertyToChain;
}
Expand Down Expand Up @@ -1126,7 +1142,7 @@ public Map<URI, Set<Value>> getHasValueByType(final Resource type) {
final Set<Resource> types = new HashSet<>();
types.add(type);
if (type instanceof URI) {
types.addAll(findParents(subClassOfGraph, (URI) type));
types.addAll(getSubClasses((URI) type));
}
for (final Resource relevantType : types) {
if (hasValueByType.containsKey(relevantType)) {
Expand Down Expand Up @@ -1163,7 +1179,7 @@ public Map<Resource, Set<Value>> getHasValueByProperty(final URI property) {
}
implications.get(type).add(typeToValue.getValue());
if (type instanceof URI) {
for (final URI subtype : findParents(subClassOfGraph, (URI) type)) {
for (final URI subtype : getSubClasses((URI) type)) {
if (!implications.containsKey(subtype)) {
implications.put(subtype, new HashSet<>());
}
Expand Down Expand Up @@ -1234,7 +1250,7 @@ public Map<Resource, Set<URI>> getAllValuesFromByValueType(final Resource valueT
final HashSet<Resource> valueTypes = new HashSet<>();
valueTypes.add(valueType);
if (valueType instanceof URI) {
valueTypes.addAll(findParents(subClassOfGraph, (URI) valueType));
valueTypes.addAll(getSubClasses((URI) valueType));
}
for (final Resource valueSubType : valueTypes) {
if (allValuesFromByValueType.containsKey(valueSubType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected void meetSP(final StatementPattern node) throws Exception {
// node.replaceWith(join);

final URI subclassof_uri = (URI) objVar.getValue();
final Collection<URI> parents = inferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), subclassof_uri);
final Collection<URI> parents = InferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), subclassof_uri);
if (parents != null && parents.size() > 0) {
final String s = UUID.randomUUID().toString();
final Var typeVar = new Var(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected void meetSP(final StatementPattern node) throws Exception {
// }

final URI subprop_uri = (URI) predVar.getValue();
final Set<URI> parents = inferenceEngine.findParents(inferenceEngine.getSubPropertyOfGraph(), subprop_uri);
final Set<URI> parents = InferenceEngine.findParents(inferenceEngine.getSubPropertyOfGraph(), subprop_uri);
if (parents != null && parents.size() > 0) {
final String s = UUID.randomUUID().toString();
final Var typeVar = new Var(s);
Expand Down

0 comments on commit 062bc68

Please sign in to comment.