This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eclipse-rdf4j/rdf4j#1298 initial implementation of a rdfs reasoner fo…
…r the shacl engine Signed-off-by: Håvard Ottestad <hmottestad@gmail.com>
- Loading branch information
1 parent
11e0d51
commit 5f32fc6
Showing
30 changed files
with
698 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
shacl/src/main/java/org/eclipse/rdf4j/sail/shacl/RdfsSubClassOfReasoner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.eclipse.rdf4j.sail.shacl; | ||
|
||
import org.eclipse.rdf4j.common.iteration.Iterations; | ||
import org.eclipse.rdf4j.model.Resource; | ||
import org.eclipse.rdf4j.model.Statement; | ||
import org.eclipse.rdf4j.model.impl.SimpleValueFactory; | ||
import org.eclipse.rdf4j.model.vocabulary.RDF; | ||
import org.eclipse.rdf4j.model.vocabulary.RDFS; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
public class RdfsSubClassOfReasoner { | ||
|
||
private final Collection<Statement> subClassOfStatements = new ArrayList<>(); | ||
private final Collection<Resource> types = new ArrayList<>(); | ||
|
||
private final Map<Resource, Set<Resource>> forwardChainCache = new HashMap<>(); | ||
private final Map<Resource, Set<Resource>> backwardsChainCache = new HashMap<>(); | ||
|
||
|
||
public Stream<Statement> forwardChain(Statement statement) { | ||
SimpleValueFactory vf = SimpleValueFactory.getInstance(); | ||
if (statement.getPredicate().equals(RDF.TYPE) && forwardChainCache.containsKey(statement.getObject())) { | ||
return forwardChainCache.get(statement.getObject()).stream().map(r -> vf.createStatement(statement.getSubject(), RDF.TYPE, r, statement.getContext())); | ||
} | ||
return Stream.of(statement); | ||
} | ||
|
||
public Set<Resource> backwardsChain(Resource type) { | ||
Set<Resource> resources = backwardsChainCache.get(type); | ||
if(resources != null){ | ||
return resources; | ||
} | ||
return Collections.emptySet(); | ||
} | ||
|
||
void addSubClassOfStatement(Statement st) { | ||
subClassOfStatements.add(st); | ||
types.add(st.getSubject()); | ||
types.add((Resource) st.getObject()); | ||
} | ||
|
||
private void calculateSubClassOf(Collection<Statement> subClassOfStatements) { | ||
types.forEach(type -> { | ||
if (!forwardChainCache.containsKey(type)) { | ||
forwardChainCache.put(type, new HashSet<>()); | ||
} | ||
if (!backwardsChainCache.containsKey(type)) { | ||
backwardsChainCache.put(type, new HashSet<>()); | ||
} | ||
|
||
forwardChainCache.get(type).add(type); | ||
backwardsChainCache.get(type).add(type); | ||
|
||
}); | ||
|
||
subClassOfStatements.forEach(s -> { | ||
Resource subClass = s.getSubject(); | ||
Resource supClass = (Resource) s.getObject(); | ||
if (!forwardChainCache.containsKey(subClass)) { | ||
forwardChainCache.put(subClass, new HashSet<>()); | ||
} | ||
if (!backwardsChainCache.containsKey(supClass)) { | ||
backwardsChainCache.put(supClass, new HashSet<>()); | ||
} | ||
|
||
forwardChainCache.get(subClass).add((Resource) s.getObject()); | ||
backwardsChainCache.get(supClass).add((Resource) s.getSubject()); | ||
|
||
}); | ||
|
||
forwardChainUntilFixPoint(forwardChainCache); | ||
forwardChainUntilFixPoint(backwardsChainCache); | ||
|
||
|
||
} | ||
|
||
private void forwardChainUntilFixPoint(Map<Resource, Set<Resource>> forwardChainCache) { | ||
// Fixed point approach to finding all sub-classes. | ||
// prevSize is the size of the previous application of the function | ||
// newSize is the size of the current application of the function | ||
// Fixed point is reached when they are the same. | ||
// Eg. Two consecutive applications return the same number of subclasses | ||
long prevSize = 0; | ||
final long[] newSize = {-1}; | ||
while (prevSize != newSize[0]) { | ||
|
||
prevSize = newSize[0]; | ||
|
||
newSize[0] = 0; | ||
|
||
forwardChainCache.forEach((key, value) -> { | ||
List<Resource> temp = new ArrayList<>(); | ||
value.forEach(superClass -> temp.addAll(resolveTypes(superClass, forwardChainCache))); | ||
|
||
value.addAll(temp); | ||
newSize[0] += value.size(); | ||
}); | ||
|
||
} | ||
} | ||
|
||
private Set<Resource> resolveTypes(Resource value, Map<Resource, Set<Resource>> forwardChainCache) { | ||
Set<Resource> iris = forwardChainCache.get(value); | ||
return iris != null ? iris : Collections.emptySet(); | ||
} | ||
|
||
|
||
static RdfsSubClassOfReasoner createReasoner(ShaclSailConnection shaclSailConnection) { | ||
RdfsSubClassOfReasoner rdfsSubClassOfReasoner = new RdfsSubClassOfReasoner(); | ||
|
||
try (Stream<? extends Statement> stream = Iterations.stream(shaclSailConnection.getStatements(null, RDFS.SUBCLASSOF, null, false))) { | ||
stream.forEach(rdfsSubClassOfReasoner::addSubClassOfStatement); | ||
} | ||
|
||
rdfsSubClassOfReasoner.calculateSubClassOf(rdfsSubClassOfReasoner.subClassOfStatements); | ||
return rdfsSubClassOfReasoner; | ||
} | ||
} | ||
|
||
/* | ||
*/ |
Oops, something went wrong.