Skip to content

Commit

Permalink
Introduced the explicit parsing of generated SPARQL queries and their…
Browse files Browse the repository at this point in the history
… transformation into an object oriented representation that is then checked with respect to faulty filter statements.
  • Loading branch information
MichaelRoeder committed Nov 4, 2024
1 parent 5ec9ead commit ac08fa3
Show file tree
Hide file tree
Showing 6 changed files with 731 additions and 30 deletions.
51 changes: 27 additions & 24 deletions src/main/java/org/dice_research/cel/PruneCEL.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.dice_research.cel.expression.NamedClass;
import org.dice_research.cel.expression.ScoredCEComparatorForRefinement;
import org.dice_research.cel.expression.ScoredClassExpression;
import org.dice_research.cel.expression.SimpleQuantifiedRole;
import org.dice_research.cel.io.IntermediateResultPrinter;
import org.dice_research.cel.io.LearningProblem;
import org.dice_research.cel.io.csv.CSVIntermediateResultPrinter;
Expand Down Expand Up @@ -256,21 +255,25 @@ public static void main(String[] args) throws Exception {
factory = new AvoidingPickySolutionsDecorator.Factory(factory);

boolean useCache = true;
boolean debugMode = false;
boolean debugMode = true;
boolean skipNonImproving = true;
boolean recursive = false;

try (SparqlBasedSuggestor suggestor = SparqlBasedSuggestor.create(endpoint, logic, useCache)) {
suggestor.addToClassBlackList(OWL2.NamedIndividual.getURI());
suggestor.addToPropertyBlackList(RDF.type.getURI());

boolean printLogs = true;

// XXX We should use PruneCEL for now, you can try Recursive later
// PruneCEL cel = new PruneCEL(suggestor, logic, factory);
PruneCEL cel = null;
if(recursive) {
cel = new SimpleRecursivePruneCEL(suggestor, logic, factory, suggestor);
} else {
cel = new PruneCEL(suggestor, logic, factory);
}
// PruneCEL cel = new RecursivePruneCEL(suggestor, logic, factory, suggestor);
// PruneCEL cel = new SingleThreadRecursivePruneCEL(suggestor, logic, factory,
// suggestor);
PruneCEL cel = new SimpleRecursivePruneCEL(suggestor, logic, factory, suggestor);
// XXX Max iterations of the refinement
// cel.setMaxIterations(1000);
// XXX Maximum time (in ms)
Expand All @@ -296,19 +299,19 @@ public static void main(String[] args) throws Exception {
// .readProblems("/home/micha/Downloads/TandF_ganswer_reverse.json");
// Collection<LearningProblem> problems =
// reader.readProblems("LPs/QA/TandF_MST5_reverse.json");
Collection<LearningProblem> problems = reader.readProblems("/home/micha/Downloads/AuntTrain_Fold_2.json");
Collection<LearningProblem> problems = reader.readProblems("/home/micha/Downloads/QALD10Train_Fold_0.json");

// DEBUG CODE!!!
ClassExpression ce;
ce = new Junction(true, new Junction(false,
new SimpleQuantifiedRole(true, "http://www.benchmark.org/family#married", false,
new NamedClass("http://www.benchmark.org/family#Brother")),
new Junction(true,
new SimpleQuantifiedRole(true, "http://www.benchmark.org/family#hasSibling", false,
new Junction(false, new NamedClass("http://www.benchmark.org/family#Mother"),
new NamedClass("http://www.benchmark.org/family#Father"))),
new NamedClass("http://www.benchmark.org/family#Daughter"))),
new NamedClass("http://www.benchmark.org/family#Female"));
// ClassExpression ce;
// ce = new Junction(true, new Junction(false,
// new SimpleQuantifiedRole(true, "http://www.benchmark.org/family#married", false,
// new NamedClass("http://www.benchmark.org/family#Brother")),
// new Junction(true,
// new SimpleQuantifiedRole(true, "http://www.benchmark.org/family#hasSibling", false,
// new Junction(false, new NamedClass("http://www.benchmark.org/family#Mother"),
// new NamedClass("http://www.benchmark.org/family#Father"))),
// new NamedClass("http://www.benchmark.org/family#Daughter"))),
// new NamedClass("http://www.benchmark.org/family#Female"));

// new Junction(false,
// new Junction(true, new NamedClass("http://www.benchmark.org/family#Son", true),
Expand Down Expand Up @@ -346,21 +349,21 @@ public static void main(String[] args) throws Exception {
// new NamedClass("http://www.w3.org/2004/02/skos/core#Concept"),
// new NamedClass("http://dbpedia.org/ontology/Agent"))))));

LearningProblem prob = problems.iterator().next();
ScoreCalculator scoreCalculator = factory.create(prob.getPositiveExamples().size(),
prob.getNegativeExamples().size());
RefinementOperator rho = new SuggestorBasedRefinementOperator(suggestor, logic, scoreCalculator,
prob.getPositiveExamples(), prob.getNegativeExamples());
Set<ScoredClassExpression> expressions = rho.refine(ce, System.currentTimeMillis() + 60000);
System.out.println(expressions.size());
// LearningProblem prob = problems.iterator().next();
// ScoreCalculator scoreCalculator = factory.create(prob.getPositiveExamples().size(),
// prob.getNegativeExamples().size());
// RefinementOperator rho = new SuggestorBasedRefinementOperator(suggestor, logic, scoreCalculator,
// prob.getPositiveExamples(), prob.getNegativeExamples());
// Set<ScoredClassExpression> expressions = rho.refine(ce, System.currentTimeMillis() + 60000);
// System.out.println(expressions.size());

// System.out.println(suggestor.suggestClass(prob.getPositiveExamples(), prob.getNegativeExamples(), ce));
// System.out.println(suggestor.scoreExpression(ce, prob.getPositiveExamples(), prob.getNegativeExamples()));
// System.out.println(ce);
// ce = (new NegatingVisitor()).negateExpression(ce);
// System.out.println(suggestor.scoreExpression(ce, prob.getPositiveExamples(), prob.getNegativeExamples()));
// DEBUG CODE END!!!

try (PrintStream pout = new PrintStream("results.txt")) {
// for (int i = 0; i < names.size(); ++i) {
for (LearningProblem problem : problems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp;
import org.aksw.jenax.arq.connection.core.QueryExecutionFactory;
import org.aksw.jenax.stmt.parser.query.SparqlQueryParser;
import org.aksw.jenax.stmt.parser.query.SparqlQueryParserImpl;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
Expand All @@ -27,6 +30,7 @@
import org.dice_research.cel.refine.suggest.SelectionScores;
import org.dice_research.cel.refine.suggest.Suggestor;
import org.dice_research.cel.sparql.InstanceRetriever;
import org.dice_research.cel.sparql.MultiNotExistFilterFixingVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -43,6 +47,11 @@ public class SparqlBasedSuggestor implements ExtendedSuggestor, InstanceRetrieve
protected DisjunctionCheckingVisitor checker = new DisjunctionCheckingVisitor();
protected SuggestionCheckingVisitor sugChecker = new SuggestionCheckingVisitor();
protected ExpressionPreProcessor preprocessor = new ExpressionPreProcessor();
protected SparqlQueryParser queryParser = new SparqlQueryParserImpl();
protected MultiNotExistFilterFixingVisitor classQueryVisitor = new MultiNotExistFilterFixingVisitor("?class",
OWL.Class.getURI());
protected MultiNotExistFilterFixingVisitor propertyQueryVisitor = new MultiNotExistFilterFixingVisitor("?prop",
RDF.Property.getURI());

public SparqlBasedSuggestor(QueryExecutionFactory queryExecFactory, DescriptionLogic logic) {
this.queryExecFactory = queryExecFactory;
Expand Down Expand Up @@ -185,19 +194,28 @@ public Collection<ScoredIRI> suggestClass(Collection<String> positive, Collectio
ClassExpression context) {
LOGGER.trace("Suggesting classes for {}", context);
SuggestionData data = prepareForSuggestion(context, positive.size(), negative.size());
String suggestionQuery;
if (logic.supportsComplexConceptNegation()) {
data.suggestionQuery = generateClassQueryForGeneralNegation(positive, negative, data.suggestionPart, null);
suggestionQuery = generateClassQueryForGeneralNegation(positive, negative, data.suggestionPart, null);
} else {
data.suggestionQuery = generateClassQuery(positive, negative, data.suggestionPart, null);
suggestionQuery = generateClassQuery(positive, negative, data.suggestionPart, null);
}
data.suggestionQuery = prepareQuery(suggestionQuery, classQueryVisitor);
return performClassSelection(data, positive, negative);
}

protected Query prepareQuery(String queryString, MultiNotExistFilterFixingVisitor visitor) {
Query query = queryParser.apply(queryString);
visitor.fixQuery(query);
return query;
}

public Collection<ScoredIRI> suggestNegatedClass(Collection<String> positive, Collection<String> negative,
ClassExpression context) {
LOGGER.trace("Suggesting negated classes for {}", context);
SuggestionData data = prepareForSuggestion(context, positive.size(), negative.size());
data.suggestionQuery = generateNegatedClassQuery(positive, negative, data.suggestionPart, null);
data.suggestionQuery = prepareQuery(generateNegatedClassQuery(positive, negative, data.suggestionPart, null),
classQueryVisitor);
return performClassSelection(data, positive, negative);
}

Expand Down Expand Up @@ -331,12 +349,14 @@ public Collection<ScoredIRI> suggestProperty(Collection<String> positive, Collec
protected Collection<ScoredIRI> suggestProperty(SuggestionData data, Collection<String> positive,
Collection<String> negative, boolean inverted) {
List<ScoredIRI> results = new ArrayList<>();
String suggestionQuery;
if (logic.supportsAtomicNegation()) {
data.suggestionQuery = generatePropertyQuery(positive, negative, data.suggestionPart, null, inverted);
suggestionQuery = generatePropertyQuery(positive, negative, data.suggestionPart, null, inverted);
} else {
data.suggestionQuery = generatePropertyQueryWithoutNegation(positive, negative, data.suggestionPart, null,
suggestionQuery = generatePropertyQueryWithoutNegation(positive, negative, data.suggestionPart, null,
inverted);
}
data.suggestionQuery = prepareQuery(suggestionQuery, propertyQueryVisitor);
performQuery(data, positive, negative, new ScoredIriQuerySolutionMapper("?prop", propertyBlackList), results);
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.apache.jena.query.Query;
import org.dice_research.cel.expression.ClassExpression;
import org.dice_research.cel.refine.suggest.ScoredIRI;
import org.dice_research.cel.refine.suggest.SelectionScores;
Expand All @@ -10,7 +11,7 @@ public class SuggestionData {

public ClassExpression suggestionPart;
public ClassExpression basePart;
public String suggestionQuery;
public Query suggestionQuery;
public int posCount;
public int negCount;
public int maxPos;
Expand Down
Loading

0 comments on commit ac08fa3

Please sign in to comment.