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