-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an more in-depth check whether FILTER NOT EXISTS statements mig…
…ht be nested and, hence, shouldn't be created.
- Loading branch information
1 parent
2faae5b
commit 4e903ba
Showing
4 changed files
with
145 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/org/dice_research/cel/refine/suggest/sparql/FilterNotExistsChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.dice_research.cel.refine.suggest.sparql; | ||
|
||
import org.dice_research.cel.expression.ClassExpression; | ||
import org.dice_research.cel.expression.ClassExpressionVisitingCreator; | ||
import org.dice_research.cel.expression.Junction; | ||
import org.dice_research.cel.expression.NamedClass; | ||
import org.dice_research.cel.expression.SimpleQuantifiedRole; | ||
|
||
/** | ||
* This class is a visitor that returns {@code true} if the given class | ||
* expression should not be put into a {@code FILTER NOT EXISTS} (FNE) statement | ||
* since the statement itself will consist of one (or several) FNE statements. | ||
* Instead, such an expression should be simply negated. | ||
* | ||
* For example, trying to add an FNE statement with the expression ∀r.⊥ leads to | ||
* an FNE statement within an FNE, without any additional triple pattern in the | ||
* outer filter. That means that the variables within the filter and outside of | ||
* the filter are not connected, anymore. Hence, it is easier to add ∃r.⊤ | ||
* instead. The same holds for negated, named classes (¬A) or a conjunction of | ||
* these two types of statements. | ||
* | ||
* Note that the given statement should already have been preprocessed for being | ||
* transformed into a SPARQL query. | ||
* | ||
* @author Michael Röder ([email protected]) | ||
* | ||
*/ | ||
public class FilterNotExistsChecker implements ClassExpressionVisitingCreator<Boolean> { | ||
|
||
@Override | ||
public Boolean visitNamedClass(NamedClass node) { | ||
return node.isNegated(); | ||
} | ||
|
||
@Override | ||
public Boolean visitJunction(Junction node) { | ||
for (ClassExpression child : node.getChildren()) { | ||
if (!child.accept(this)) { | ||
return Boolean.FALSE; | ||
} | ||
} | ||
return Boolean.TRUE; | ||
} | ||
|
||
@Override | ||
public Boolean visitSimpleQuantificationRole(SimpleQuantifiedRole node) { | ||
return !node.isExists(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/org/dice_research/cel/refine/suggest/sparql/FilterNotExistsCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.dice_research.cel.refine.suggest.sparql; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.dice_research.cel.expression.ClassExpression; | ||
import org.dice_research.cel.expression.Junction; | ||
import org.dice_research.cel.expression.NamedClass; | ||
import org.dice_research.cel.expression.SimpleQuantifiedRole; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
@RunWith(Parameterized.class) | ||
public class FilterNotExistsCheckerTest { | ||
|
||
private ClassExpression givenExpression; | ||
private Boolean expectedResult; | ||
|
||
public FilterNotExistsCheckerTest(ClassExpression givenExpression, Boolean expectedResult) { | ||
super(); | ||
this.givenExpression = givenExpression; | ||
this.expectedResult = expectedResult; | ||
} | ||
|
||
@Test | ||
public void test() { | ||
FilterNotExistsChecker checker = new FilterNotExistsChecker(); | ||
if (expectedResult) { | ||
Assert.assertTrue(givenExpression.accept(checker)); | ||
} else { | ||
Assert.assertFalse(givenExpression.accept(checker)); | ||
} | ||
} | ||
|
||
@Parameters | ||
public static List<Object[]> parameters() { | ||
List<Object[]> testCases = new ArrayList<>(); | ||
|
||
testCases.add(new Object[] { new NamedClass("A"), Boolean.FALSE }); | ||
testCases.add(new Object[] { new NamedClass("A", true), Boolean.TRUE }); | ||
testCases.add(new Object[] { new SimpleQuantifiedRole(true, "r", false, new NamedClass("A")), Boolean.FALSE }); | ||
testCases.add(new Object[] { new SimpleQuantifiedRole(false, "r", false, new NamedClass("A")), Boolean.TRUE }); | ||
|
||
testCases.add(new Object[] { new Junction(true, new NamedClass("A"), new NamedClass("B")), Boolean.FALSE }); | ||
testCases.add( | ||
new Object[] { new Junction(true, new NamedClass("A", true), new NamedClass("B")), Boolean.FALSE }); | ||
testCases.add( | ||
new Object[] { new Junction(true, new NamedClass("A"), new NamedClass("B", true)), Boolean.FALSE }); | ||
testCases.add(new Object[] { new Junction(true, new NamedClass("A", true), new NamedClass("B", true)), | ||
Boolean.TRUE }); | ||
testCases | ||
.add(new Object[] { | ||
new Junction(true, new NamedClass("A", true), | ||
new Junction(true, new NamedClass("B", true), | ||
new SimpleQuantifiedRole(false, "r", false, new NamedClass("A")))), | ||
Boolean.TRUE }); | ||
|
||
return testCases; | ||
} | ||
|
||
} |