Skip to contentMethod: processAssertions(AxiomDescriptor)
1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.Axiom;
20: import cz.cvut.kbss.ontodriver.model.NamedResource;
21: import cz.cvut.kbss.ontodriver.sesame.config.RuntimeConfiguration;
22: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
23: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
24: import cz.cvut.kbss.ontodriver.sesame.util.AxiomBuilder;
25: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
26: import org.eclipse.rdf4j.model.IRI;
27: import org.eclipse.rdf4j.model.Resource;
28: import org.eclipse.rdf4j.model.Statement;
29: import org.eclipse.rdf4j.model.ValueFactory;
30:
31: import java.util.*;
32: import java.util.stream.Collectors;
33:
34: class AxiomLoader {
35:
36: private final Connector connector;
37: private final ValueFactory valueFactory;
38:
39: private final Map<IRI, Assertion> propertyToAssertion;
40: private Map<IRI, Assertion> explicitAssertions;
41: private Map<IRI, Assertion> inferredAssertions;
42:
43: private final RuntimeConfiguration config;
44:
45: AxiomLoader(Connector connector, ValueFactory valueFactory, RuntimeConfiguration config) {
46: this.connector = connector;
47: this.valueFactory = valueFactory;
48: this.propertyToAssertion = new HashMap<>();
49: this.config = config;
50: }
51:
52: Collection<Axiom<?>> loadAxioms(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
53: return findStatements(axiomDescriptor);
54: }
55:
56: private Collection<Axiom<?>> findStatements(AxiomDescriptor descriptor) throws SesameDriverException {
57: final Collection<Axiom<?>> result = new HashSet<>();
58: final Resource subject = SesameUtils.toSesameIri(descriptor.getSubject().getIdentifier(), valueFactory);
59: final Assertion unspecified = processAssertions(descriptor);
60: final AxiomBuilder axiomBuilder = new AxiomBuilder(descriptor.getSubject(), propertyToAssertion, unspecified);
61: final StatementLoader statementLoader = new StatementLoader(config, descriptor, connector, subject,
62: axiomBuilder);
63: if (unspecified == null || !unspecified.isInferred()) {
64: statementLoader.setIncludeInferred(false);
65: result.addAll(statementLoader.loadAxioms(explicitAssertions));
66: }
67: statementLoader.setIncludeInferred(true);
68: result.addAll(statementLoader.loadAxioms(inferredAssertions));
69: return result;
70: }
71:
72: /**
73: * Processes assertions in the specified descriptor.
74: * <p>
75: * Splits them into explicit and inferred and returns unspecified property, if it is present in the descriptor.
76: *
77: * @param descriptor The descriptor to process
78: * @return Unspecified property, if it is present
79: */
80: private Assertion processAssertions(AxiomDescriptor descriptor) {
81: final Set<Assertion> assertions = descriptor.getAssertions();
82: this.explicitAssertions = new HashMap<>(assertions.size());
83: this.inferredAssertions = new HashMap<>(assertions.size());
84: Assertion unspecified = null;
85:• for (Assertion a : assertions) {
86: final IRI property = SesameUtils.toSesameIri(a.getIdentifier(), valueFactory);
87: propertyToAssertion.put(property, a);
88:• if (a.equals(Assertion.createUnspecifiedPropertyAssertion(a.isInferred()))) {
89: unspecified = a;
90: }
91:• if (a.isInferred()) {
92: inferredAssertions.put(property, a);
93: } else {
94: explicitAssertions.put(property, a);
95: }
96: }
97: return unspecified;
98: }
99:
100: Collection<Axiom<?>> loadAxioms(NamedResource individual, boolean includeInferred, java.net.URI context)
101: throws SesameDriverException {
102: final IRI sesameContext = SesameUtils.toSesameIri(context, valueFactory);
103: final IRI subject = SesameUtils.toSesameIri(individual.getIdentifier(), valueFactory);
104: final AxiomBuilder axiomBuilder = new AxiomBuilder(individual, Collections.emptyMap(),
105: Assertion.createUnspecifiedPropertyAssertion(includeInferred));
106: final Collection<Statement> statements = connector
107: .findStatements(subject, null, null, includeInferred, sesameContext);
108: return statements.stream().map(axiomBuilder::statementToAxiom).collect(Collectors.toSet());
109: }
110: }