Skip to content

Method: processAssertions(AxiomDescriptor)

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: final StatementLoader statementLoader = new StatementLoader(config, descriptor, connector, subject,
60: axiomBuilder);
61: if (unspecified == null || !unspecified.isInferred()) {
62: statementLoader.setIncludeInferred(false);
63: result.addAll(statementLoader.loadAxioms(explicitAssertions));
64: }
65: statementLoader.setIncludeInferred(true);
66: result.addAll(statementLoader.loadAxioms(inferredAssertions));
67: return result;
68: }
69:
70: /**
71: * Processes assertions in the specified descriptor.
72: * <p>
73: * Splits them into explicit and inferred and returns unspecified property, if it is present in the descriptor.
74: *
75: * @param descriptor The descriptor to process
76: * @return Unspecified property, if it is present
77: */
78: private Assertion processAssertions(AxiomDescriptor descriptor) {
79: final Set<Assertion> assertions = descriptor.getAssertions();
80: this.explicitAssertions = new HashMap<>(assertions.size());
81: this.inferredAssertions = new HashMap<>(assertions.size());
82: Assertion unspecified = null;
83:• for (Assertion a : assertions) {
84: final IRI property = SesameUtils.toSesameIri(a.getIdentifier(), valueFactory);
85: propertyToAssertion.put(property, a);
86:• if (a.equals(Assertion.createUnspecifiedPropertyAssertion(a.isInferred()))) {
87: unspecified = a;
88: }
89:• if (a.isInferred()) {
90: inferredAssertions.put(property, a);
91: } else {
92: explicitAssertions.put(property, a);
93: }
94: }
95: return unspecified;
96: }
97:
98: Collection<Axiom<?>> loadAxioms(NamedResource individual, boolean includeInferred, java.net.URI context)
99: throws SesameDriverException {
100: final IRI sesameContext = SesameUtils.toSesameIri(context, valueFactory);
101: final IRI subject = SesameUtils.toSesameIri(individual.getIdentifier(), valueFactory);
102: final AxiomBuilder axiomBuilder = new AxiomBuilder(individual, Collections.emptyMap(),
103: Assertion.createUnspecifiedPropertyAssertion(includeInferred));
104: final Collection<Statement> statements = connector
105: .findStatements(subject, null, null, includeInferred, sesameContext);
106: return statements.stream().map(axiomBuilder::statementToAxiom).collect(Collectors.toSet());
107: }
108: }