Skip to content

Method: processAssertions(AxiomDescriptor)

1: /**
2: * Copyright (C) 2016 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.connector.Connector;
22: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
23: import cz.cvut.kbss.ontodriver.sesame.util.AxiomBuilder;
24: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
25: import org.eclipse.rdf4j.model.IRI;
26: import org.eclipse.rdf4j.model.Resource;
27: import org.eclipse.rdf4j.model.Statement;
28: import org.eclipse.rdf4j.model.ValueFactory;
29:
30: import java.util.*;
31: import java.util.stream.Collectors;
32:
33: class AxiomLoader {
34:
35: private final Connector connector;
36: private final ValueFactory valueFactory;
37:
38: private Map<IRI, Assertion> propertyToAssertion;
39: private Map<IRI, Assertion> explicitAssertions;
40: private Map<IRI, Assertion> inferredAssertions;
41:
42: private final String language;
43:
44: AxiomLoader(Connector connector, ValueFactory valueFactory, String language) {
45: this.connector = connector;
46: this.valueFactory = valueFactory;
47: this.propertyToAssertion = new HashMap<>();
48: this.language = language;
49: }
50:
51: Collection<Axiom<?>> loadAxioms(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
52: return findStatements(axiomDescriptor);
53: }
54:
55: private Collection<Axiom<?>> findStatements(AxiomDescriptor descriptor) throws SesameDriverException {
56: final Collection<Axiom<?>> result = new HashSet<>();
57: final Resource subject = SesameUtils.toSesameIri(descriptor.getSubject().getIdentifier(), valueFactory);
58: final Assertion unspecified = processAssertions(descriptor);
59: final AxiomBuilder axiomBuilder = new AxiomBuilder(descriptor.getSubject(), propertyToAssertion, unspecified,
60: language);
61: final StatementLoader statementLoader = new StatementLoader(descriptor, connector, subject, 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: this.explicitAssertions = new HashMap<>(descriptor.getAssertions().size());
81: this.inferredAssertions = new HashMap<>(descriptor.getAssertions().size());
82: Assertion unspecified = null;
83:• for (Assertion a : descriptor.getAssertions()) {
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), language);
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: }