Skip to contentMethod: static {...}
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.loader;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.rdf4j.connector.RepoConnection;
24: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
25: import cz.cvut.kbss.ontodriver.rdf4j.util.AxiomBuilder;
26: import org.eclipse.rdf4j.model.Resource;
27: import org.eclipse.rdf4j.model.Statement;
28:
29: import java.net.URI;
30: import java.util.Collection;
31: import java.util.HashSet;
32: import java.util.Set;
33:
34: /**
35: * Statement loader for GraphDB repositories.
36: * <p>
37: * It differs from the basic {@link StatementLoader} in the way inferred statements are loaded. This is because GraphDB
38: * does not store inferred statements in the same context as the statements they are inferred from (as RDF4J does), but
39: * instead has a special {@code implicit} context for them.
40: */
41: public class GraphDBStatementLoader extends StatementLoader {
42:
43: /**
44: * Repository pseudo-context used by GraphDB to store inferred statements.
45: */
46: static final URI GRAPHDB_IMPLICIT_CONTEXT = URI.create("http://www.ontotext.com/implicit");
47:
48: /**
49: * Repository pseudo-context used by GraphDB to store explicit statements in the default context.
50: */
51: static final URI GRAPHDB_EXPLICIT_CONTEXT = URI.create("http://www.ontotext.com/explicit");
52:
53: public GraphDBStatementLoader(RepoConnection connector, Resource subject, AxiomBuilder axiomBuilder) {
54: super(connector, subject, axiomBuilder);
55: }
56:
57: @Override
58: protected Set<URI> resolveContexts(AxiomDescriptor descriptor, Assertion a) {
59: final Set<URI> contexts = new HashSet<>(super.resolveContexts(descriptor, a));
60: if (includeInferred) {
61: contexts.add(GRAPHDB_IMPLICIT_CONTEXT);
62: // Add explicit context for the cases when an explicit statement in default hides the inferred one.
63: // This is just to make the behavior consistent with contextMatches
64: contexts.add(GRAPHDB_EXPLICIT_CONTEXT);
65: }
66: return contexts;
67: }
68:
69: @Override
70: protected boolean contextMatches(Set<URI> assertionCtx, Statement s, Assertion a) {
71: if (includeInferred && a.isInferred() && s.getContext() == null) {
72: // If the statement is inferred, its context is null in GraphDB (although when querying, one can ask the implicit context)
73: return true;
74: }
75: return super.contextMatches(assertionCtx, s, a);
76: }
77:
78: @Override
79: public Collection<Axiom<?>> loadAxioms(Set<URI> contexts) throws Rdf4jDriverException {
80: if (includeInferred) {
81: final Set<URI> contextsToUse = new HashSet<>(contexts);
82: contextsToUse.add(GRAPHDB_IMPLICIT_CONTEXT);
83: contextsToUse.add(GRAPHDB_EXPLICIT_CONTEXT);
84: return super.loadAxioms(contextsToUse);
85: }
86: return super.loadAxioms(contexts);
87: }
88: }