Skip to content

Method: loadAxioms(AxiomDescriptor, Map)

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.config.Constants;
24: import cz.cvut.kbss.ontodriver.rdf4j.connector.RepoConnection;
25: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
26: import cz.cvut.kbss.ontodriver.rdf4j.util.AxiomBuilder;
27: import cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils;
28: import org.eclipse.rdf4j.model.IRI;
29: import org.eclipse.rdf4j.model.Resource;
30: import org.eclipse.rdf4j.model.Statement;
31: import org.eclipse.rdf4j.model.ValueFactory;
32:
33: import java.net.URI;
34: import java.util.Collection;
35: import java.util.HashSet;
36: import java.util.Map;
37: import java.util.Set;
38: import java.util.stream.Collectors;
39:
40: public class StatementLoader {
41:
42: private final RepoConnection connector;
43: private final Resource subject;
44: private final ValueFactory vf;
45: private final AxiomBuilder axiomBuilder;
46:
47: private int loadAllThreshold = Constants.DEFAULT_LOAD_ALL_THRESHOLD;
48: private boolean loadAll;
49: boolean includeInferred;
50:
51: public StatementLoader(RepoConnection connector, Resource subject, AxiomBuilder axiomBuilder) {
52: this.connector = connector;
53: this.vf = connector.getValueFactory();
54: this.subject = subject;
55: this.axiomBuilder = axiomBuilder;
56: }
57:
58: public void setLoadAllThreshold(int loadAllThreshold) {
59: this.loadAllThreshold = loadAllThreshold;
60: }
61:
62: public void setIncludeInferred(boolean includeInferred) {
63: this.includeInferred = includeInferred;
64: }
65:
66: public Collection<Axiom<?>> loadAxioms(AxiomDescriptor descriptor,
67: Map<IRI, Assertion> properties) throws Rdf4jDriverException {
68: this.loadAll = properties.containsValue(Assertion.createUnspecifiedPropertyAssertion(includeInferred));
69:• if (properties.size() < loadAllThreshold && !loadAll) {
70: return loadOneByOne(descriptor, properties);
71: } else {
72: return loadAll(descriptor, properties);
73: }
74: }
75:
76: private Collection<Axiom<?>> loadOneByOne(AxiomDescriptor descriptor,
77: Map<IRI, Assertion> assertions) throws Rdf4jDriverException {
78: final Collection<Axiom<?>> result = new HashSet<>();
79: for (Map.Entry<IRI, Assertion> e : assertions.entrySet()) {
80: final Set<IRI> contexts = resolveContexts(descriptor, e.getValue()).stream()
81: .map(uri -> Rdf4jUtils.toRdf4jIri(uri,
82: vf))
83: .collect(Collectors.toSet());
84:
85: final Collection<Statement> statements;
86: statements = connector.findStatements(subject, e.getKey(), null, includeInferred, contexts);
87: for (Statement s : statements) {
88: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s, e.getValue());
89: if (axiom != null) {
90: result.add(axiom);
91: }
92: }
93: }
94: return result;
95: }
96:
97: protected Set<URI> resolveContexts(AxiomDescriptor descriptor, Assertion a) {
98: return descriptor.getAssertionContexts(a);
99: }
100:
101: private Collection<Axiom<?>> loadAll(AxiomDescriptor descriptor,
102: Map<IRI, Assertion> properties) throws Rdf4jDriverException {
103: final Collection<Statement> statements = connector.findStatements(subject, null, null, includeInferred);
104: final Collection<Axiom<?>> result = new HashSet<>(statements.size());
105: final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
106: for (Statement s : statements) {
107: if (!properties.containsKey(s.getPredicate()) && !loadAll) {
108: continue;
109: }
110: final Assertion a = getAssertion(properties, s);
111: if (!contextMatches(descriptor.getAssertionContexts(a), s, a) &&
112: !(loadAll && contextMatches(descriptor.getAssertionContexts(unspecified), s, a))) {
113: continue;
114: }
115: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s);
116: if (axiom != null) {
117: result.add(axiom);
118: }
119: }
120: return result;
121: }
122:
123: private Assertion getAssertion(Map<IRI, Assertion> properties, Statement s) {
124: if (properties.containsKey(s.getPredicate())) {
125: return properties.get(s.getPredicate());
126: }
127: return Assertion.createUnspecifiedPropertyAssertion(includeInferred);
128: }
129:
130: protected boolean contextMatches(Set<URI> assertionCtx, Statement s, Assertion a) {
131: if (assertionCtx.isEmpty()) {
132: // If the assertion should be in default, we don't care about the context of the statement, because
133: // the default is a union of all the contexts
134: return true;
135: }
136: final Resource statementContext = s.getContext();
137: return statementContext != null && assertionCtx.contains(URI.create(statementContext.stringValue()));
138: }
139:
140: public Collection<Axiom<?>> loadAxioms(Set<URI> contexts) throws Rdf4jDriverException {
141: final Collection<Statement> statements =
142: connector.findStatements(subject, null, null, includeInferred, contexts.stream()
143: .map(uri -> Rdf4jUtils.toRdf4jIri(
144: uri, vf))
145: .collect(Collectors.toSet()));
146: return statements.stream().map(axiomBuilder::statementToAxiom).collect(Collectors.toSet());
147: }
148: }