Skip to content

Method: loadAxioms(NamedResource, boolean, URI)

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