-
Notifications
You must be signed in to change notification settings - Fork 14
/
TypesHandler.java
72 lines (62 loc) · 3.08 KB
/
TypesHandler.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
package cz.cvut.kbss.ontodriver.jena;
import cz.cvut.kbss.ontodriver.Types;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.model.*;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class TypesHandler implements Types {
// TODO We are not dealing with inferred statements, yet
private final StorageConnector connector;
TypesHandler(StorageConnector connector) {
this.connector = connector;
}
@Override
public Set<Axiom<URI>> getTypes(NamedResource individual, URI context, boolean includeInferred) {
final Collection<Statement> statements = getStatements(individual, context);
final Assertion assertion = Assertion.createClassAssertion(includeInferred);
// Skip possible non-resources and anonymous resources (not likely to appear, but safety first)
return statements.stream().filter(s -> s.getObject().isResource() && !s.getObject().isAnon())
.map(s -> new AxiomImpl<>(individual, assertion,
new Value<>(URI.create(s.getObject().asResource().getURI())))).collect(
Collectors.toSet());
}
private Collection<Statement> getStatements(NamedResource individual, URI context) {
final Resource subject = ResourceFactory.createResource(individual.getIdentifier().toString());
final Property property = ResourceFactory.createProperty(Vocabulary.RDF_TYPE);
return context != null ? connector.find(subject, property, null, context.toString()) :
connector.find(subject, property, null);
}
@Override
public void addTypes(NamedResource individual, URI context, Set<URI> types) {
final List<Statement> statements = generateStatementsForTypes(individual, types);
if (context != null) {
connector.add(statements, context.toString());
} else {
connector.add(statements);
}
}
private List<Statement> generateStatementsForTypes(NamedResource individual, Set<URI> types) {
final Resource subject = ResourceFactory.createResource(individual.getIdentifier().toString());
final Property property = ResourceFactory.createProperty(Vocabulary.RDF_TYPE);
return types.stream().map(t -> ResourceFactory
.createStatement(subject, property, ResourceFactory.createResource(t.toString()))).collect(
Collectors.toList());
}
@Override
public void removeTypes(NamedResource individual, URI context, Set<URI> types) {
final List<Statement> statements = generateStatementsForTypes(individual, types);
if (context != null) {
connector.remove(statements, context.toString());
} else {
connector.remove(statements);
}
}
}