-
Notifications
You must be signed in to change notification settings - Fork 14
/
GraphDBStatementLoader.java
71 lines (62 loc) · 2.88 KB
/
GraphDBStatementLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cz.cvut.kbss.ontodriver.rdf4j.loader;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.rdf4j.connector.Connector;
import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
import cz.cvut.kbss.ontodriver.rdf4j.util.AxiomBuilder;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import java.net.URI;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* Statement loader for GraphDB repositories.
* <p>
* It differs from the basic {@link StatementLoader} in the way inferred statements are loaded. This is because GraphDB
* does not store inferred statements in the same context as the statements they are inferred from (as RDF4J does), but
* instead has a special {@code implicit} context for them.
*/
public class GraphDBStatementLoader extends StatementLoader {
/**
* Repository pseudo-context used by GraphDB to store inferred statements.
*/
static final URI GRAPHDB_IMPLICIT_CONTEXT = URI.create("http://www.ontotext.com/implicit");
/**
* Repository pseudo-context used by GraphDB to store explicit statements in the default context.
*/
static final URI GRAPHDB_EXPLICIT_CONTEXT = URI.create("http://www.ontotext.com/explicit");
public GraphDBStatementLoader(Connector connector, Resource subject, AxiomBuilder axiomBuilder) {
super(connector, subject, axiomBuilder);
}
@Override
protected Set<URI> resolveContexts(AxiomDescriptor descriptor, Assertion a) {
final Set<URI> contexts = new HashSet<>(super.resolveContexts(descriptor, a));
if (includeInferred) {
contexts.add(GRAPHDB_IMPLICIT_CONTEXT);
// Add explicit context for the cases when an explicit statement in default hides the inferred one.
// This is just to make the behavior consistent with contextMatches
contexts.add(GRAPHDB_EXPLICIT_CONTEXT);
}
return contexts;
}
@Override
protected boolean contextMatches(Set<URI> assertionCtx, Statement s, Assertion a) {
if (includeInferred && a.isInferred() && s.getContext() == null) {
// If the statement is inferred, its context is null in GraphDB (although when querying, one can ask the implicit context)
return true;
}
return super.contextMatches(assertionCtx, s, a);
}
@Override
public Collection<Axiom<?>> loadAxioms(Set<URI> contexts) throws Rdf4jDriverException {
if (includeInferred) {
final Set<URI> contextsToUse = new HashSet<>(contexts);
contextsToUse.add(GRAPHDB_IMPLICIT_CONTEXT);
contextsToUse.add(GRAPHDB_EXPLICIT_CONTEXT);
return super.loadAxioms(contextsToUse);
}
return super.loadAxioms(contexts);
}
}