Skip to content

Commit c54b025

Browse files
committed
wip
1 parent b64f565 commit c54b025

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

core/sail/shacl/src/main/java/org/eclipse/rdf4j/sail/shacl/wrapper/data/VerySimpleRdfsBackwardsChainingConnection.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ public VerySimpleRdfsBackwardsChainingConnection(SailConnection wrappedCon,
5555
public boolean hasStatement(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts)
5656
throws SailException {
5757

58-
if (rdfsSubClassOfReasoner == null && includeInferredStatements) {
59-
return super.hasStatement(subj, pred, obj, includeInferred, contexts);
58+
boolean includeBaseInferred = includeInferredStatements && includeInferred;
59+
if (rdfsSubClassOfReasoner == null) {
60+
return super.hasStatement(subj, pred, obj, includeBaseInferred, contexts);
6061
}
6162

62-
boolean hasStatement = super.hasStatement(subj, pred, obj, false, contexts);
63+
boolean hasStatement = super.hasStatement(subj, pred, obj, includeBaseInferred, contexts);
6364

6465
if (hasStatement) {
6566
return true;
6667
}
6768

68-
if (rdfsSubClassOfReasoner != null && includeInferred && obj != null && obj.isResource()
69-
&& RDF.TYPE.equals(pred)) {
69+
if (obj != null && obj.isResource() && RDF.TYPE.equals(pred)) {
7070

7171
Set<Resource> types = rdfsSubClassOfReasoner.backwardsChain((Resource) obj);
7272
if (types.size() == 1) {
7373
return hasStatement;
7474
}
7575
if (types.size() > 10) {
7676
try (CloseableIteration<? extends Statement> statements = super.getStatements(subj,
77-
RDF.TYPE, null, false, contexts)) {
77+
RDF.TYPE, null, includeBaseInferred, contexts)) {
7878
return statements.stream()
7979
.map(Statement::getObject)
8080
.filter(Value::isResource)
@@ -84,7 +84,7 @@ public boolean hasStatement(Resource subj, IRI pred, Value obj, boolean includeI
8484
} else {
8585
return types
8686
.stream()
87-
.anyMatch(type -> super.hasStatement(subj, pred, type, false, contexts));
87+
.anyMatch(type -> super.hasStatement(subj, pred, type, includeBaseInferred, contexts));
8888
}
8989

9090
}
@@ -96,13 +96,14 @@ public boolean hasStatement(Resource subj, IRI pred, Value obj, boolean includeI
9696
public CloseableIteration<? extends Statement> getStatements(Resource subj, IRI pred, Value obj,
9797
boolean includeInferred, Resource... contexts) throws SailException {
9898

99-
if (rdfsSubClassOfReasoner != null && includeInferred && obj != null && obj.isResource()
99+
boolean includeBaseInferred = includeInferredStatements && includeInferred;
100+
if (rdfsSubClassOfReasoner != null && obj != null && obj.isResource()
100101
&& RDF.TYPE.equals(pred)) {
101102
Set<Resource> inferredTypes = rdfsSubClassOfReasoner.backwardsChain((Resource) obj);
102103
if (inferredTypes.size() > 1) {
103104

104105
CloseableIteration<Statement>[] statementsMatchingInferredTypes = inferredTypes.stream()
105-
.map(r -> super.getStatements(subj, pred, r, false, contexts))
106+
.map(r -> super.getStatements(subj, pred, r, includeBaseInferred, contexts))
106107
.toArray(CloseableIteration[]::new);
107108

108109
return new LookAheadIteration<>() {
@@ -154,6 +155,6 @@ protected void handleClose() throws SailException {
154155
}
155156
}
156157

157-
return super.getStatements(subj, pred, obj, includeInferred, contexts);
158+
return super.getStatements(subj, pred, obj, includeBaseInferred, contexts);
158159
}
159160
}

core/sail/shacl/src/test/java/org/eclipse/rdf4j/sail/shacl/RdfsShaclConnectionTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ public void testHasStatement() {
6060

6161
}
6262

63+
@Test
64+
public void testHasStatementWithoutInferredKeepsRdfsReasoning() {
65+
66+
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
67+
shaclSail.init();
68+
69+
fill(shaclSail);
70+
71+
try (NotifyingSailConnection connection = shaclSail.getConnection()) {
72+
((ShaclSailConnection) connection).rdfsSubClassOfReasoner = RdfsSubClassOfReasoner
73+
.createReasoner((ShaclSailConnection) connection, new ValidationSettings());
74+
VerySimpleRdfsBackwardsChainingConnection connection2 = new VerySimpleRdfsBackwardsChainingConnection(
75+
connection,
76+
((ShaclSailConnection) connection).getRdfsSubClassOfReasoner());
77+
78+
Assertions.assertTrue(connection2.hasStatement(aSubSub, RDF.TYPE, sup, false));
79+
}
80+
shaclSail.shutDown();
81+
82+
}
83+
6384
@Test
6485
public void testGetStatement() {
6586

@@ -102,6 +123,34 @@ public void testGetStatement() {
102123

103124
}
104125

126+
@Test
127+
public void testGetStatementWithoutInferredKeepsRdfsReasoning() {
128+
129+
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
130+
shaclSail.init();
131+
132+
fill(shaclSail);
133+
134+
try (NotifyingSailConnection connection = shaclSail.getConnection()) {
135+
((ShaclSailConnection) connection).rdfsSubClassOfReasoner = RdfsSubClassOfReasoner
136+
.createReasoner((ShaclSailConnection) connection, new ValidationSettings());
137+
138+
VerySimpleRdfsBackwardsChainingConnection connection2 = new VerySimpleRdfsBackwardsChainingConnection(
139+
connection,
140+
((ShaclSailConnection) connection).getRdfsSubClassOfReasoner());
141+
142+
try (Stream<? extends Statement> stream = connection2.getStatements(aSubSub, RDF.TYPE, sup, false)
143+
.stream()) {
144+
Set<? extends Statement> collect = stream.collect(Collectors.toSet());
145+
Set<Statement> expected = Set.of(vf.createStatement(aSubSub, RDF.TYPE, sup));
146+
Assertions.assertEquals(expected, collect);
147+
}
148+
}
149+
150+
shaclSail.shutDown();
151+
152+
}
153+
105154
@Test
106155
public void testGetStatementNoDuplicates() {
107156

0 commit comments

Comments
 (0)