-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4931 - Support for GraphDB knowledge bases
- Tentative support - fails many tests
- Loading branch information
Showing
7 changed files
with
495 additions
and
1 deletion.
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
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
182 changes: 182 additions & 0 deletions
182
...tion-kb/src/main/java/de/tudarmstadt/ukp/inception/kb/querybuilder/FtsAdapterGraphDb.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,182 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.kb.querybuilder; | ||
|
||
import static de.tudarmstadt.ukp.inception.kb.IriConstants.PREFIX_GRAPHDB; | ||
import static de.tudarmstadt.ukp.inception.kb.querybuilder.SPARQLQueryBuilder.convertToRequiredTokenPrefixMatchingQuery; | ||
import static de.tudarmstadt.ukp.inception.kb.querybuilder.SPARQLQueryBuilder.Priority.PRIMARY; | ||
import static org.apache.commons.lang3.StringUtils.isBlank; | ||
import static org.eclipse.rdf4j.sparqlbuilder.constraint.Expressions.and; | ||
import static org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder.prefix; | ||
import static org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatterns.and; | ||
import static org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatterns.union; | ||
import static org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf.iri; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.eclipse.rdf4j.sparqlbuilder.constraint.Expression; | ||
import org.eclipse.rdf4j.sparqlbuilder.core.Prefix; | ||
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPattern; | ||
|
||
public class FtsAdapterGraphDb | ||
implements FtsAdapter | ||
{ | ||
private static final String MULTI_CHAR_WILDCARD = "*"; | ||
|
||
private static final Prefix PREFIX_GRAPHDB_SEARCH = prefix("onto", iri(PREFIX_GRAPHDB)); | ||
|
||
private final SPARQLQueryBuilder builder; | ||
|
||
public FtsAdapterGraphDb(SPARQLQueryBuilder aBuilder) | ||
{ | ||
builder = aBuilder; | ||
builder.addPrefix(PREFIX_GRAPHDB_SEARCH); | ||
} | ||
|
||
@Override | ||
public void withLabelMatchingExactlyAnyOf(String... aValues) | ||
{ | ||
var kb = builder.getKnowledgeBase(); | ||
|
||
var valuePatterns = new ArrayList<GraphPattern>(); | ||
for (var value : aValues) { | ||
var sanitizedValue = builder.sanitizeQueryString_FTS(value); | ||
|
||
if (isBlank(sanitizedValue)) { | ||
continue; | ||
} | ||
|
||
builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); | ||
|
||
valuePatterns.add(new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, | ||
SPARQLQueryBuilder.VAR_SCORE, SPARQLQueryBuilder.VAR_MATCH_TERM, | ||
SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, sanitizedValue) // | ||
.withLimit(builder.getLimit()) // | ||
.filter(builder.equalsPattern(SPARQLQueryBuilder.VAR_MATCH_TERM, value, | ||
kb))); | ||
} | ||
|
||
if (valuePatterns.isEmpty()) { | ||
builder.noResult(); | ||
} | ||
|
||
builder.addPattern(PRIMARY, and( // | ||
builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), // | ||
union(valuePatterns.toArray(GraphPattern[]::new)))); | ||
} | ||
|
||
@Override | ||
public void withLabelContainingAnyOf(String... aValues) | ||
{ | ||
var valuePatterns = new ArrayList<GraphPattern>(); | ||
for (var value : aValues) { | ||
var sanitizedValue = builder.sanitizeQueryString_FTS(value); | ||
|
||
if (isBlank(sanitizedValue)) { | ||
continue; | ||
} | ||
|
||
builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); | ||
|
||
valuePatterns.add(new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, | ||
SPARQLQueryBuilder.VAR_SCORE, SPARQLQueryBuilder.VAR_MATCH_TERM, | ||
SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, sanitizedValue) // | ||
.withLimit(builder.getLimit()) // | ||
.filter(builder.containsPattern(SPARQLQueryBuilder.VAR_MATCH_TERM, | ||
value))); | ||
} | ||
|
||
if (valuePatterns.isEmpty()) { | ||
builder.noResult(); | ||
} | ||
|
||
builder.addPattern(PRIMARY, | ||
and(builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), | ||
union(valuePatterns.toArray(GraphPattern[]::new)))); | ||
} | ||
|
||
@Override | ||
public void withLabelStartingWith(String aPrefixQuery) | ||
{ | ||
// Strip single quotes and asterisks because they have special semantics | ||
var queryString = builder.sanitizeQueryString_FTS(aPrefixQuery); | ||
|
||
if (isBlank(queryString)) { | ||
builder.noResult(); | ||
} | ||
|
||
// If the query string entered by the user does not end with a space character, then | ||
// we assume that the user may not yet have finished writing the word and add a | ||
// wildcard | ||
if (!aPrefixQuery.endsWith(" ")) { | ||
queryString += MULTI_CHAR_WILDCARD; | ||
} | ||
|
||
builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); | ||
|
||
// Locate all entries where the label contains the prefix (using the FTS) and then | ||
// filter them by those which actually start with the prefix. | ||
builder.addPattern(PRIMARY, and( // | ||
builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), // | ||
new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, SPARQLQueryBuilder.VAR_SCORE, | ||
SPARQLQueryBuilder.VAR_MATCH_TERM, | ||
SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, queryString) // | ||
.withLimit(builder.getLimit()) // | ||
.filter(builder.startsWithPattern(SPARQLQueryBuilder.VAR_MATCH_TERM, | ||
aPrefixQuery)))); | ||
} | ||
|
||
@Override | ||
public void withLabelMatchingAnyOf(String... aValues) | ||
{ | ||
var valuePatterns = new ArrayList<GraphPattern>(); | ||
for (var value : aValues) { | ||
var sanitizedValue = builder.sanitizeQueryString_FTS(value); | ||
|
||
if (isBlank(sanitizedValue)) { | ||
continue; | ||
} | ||
|
||
var fuzzyQuery = convertToRequiredTokenPrefixMatchingQuery(sanitizedValue, "", | ||
MULTI_CHAR_WILDCARD); | ||
|
||
if (isBlank(fuzzyQuery)) { | ||
continue; | ||
} | ||
|
||
builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); | ||
|
||
var labelFilterExpressions = new ArrayList<Expression<?>>(); | ||
labelFilterExpressions.add(builder.matchKbLanguage(VAR_MATCH_TERM)); | ||
|
||
valuePatterns.add(new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, | ||
SPARQLQueryBuilder.VAR_SCORE, SPARQLQueryBuilder.VAR_MATCH_TERM, | ||
SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, fuzzyQuery) // | ||
.withLimit(builder.getLimit()) // | ||
.filter(and(labelFilterExpressions.toArray(Expression[]::new)))); | ||
} | ||
|
||
if (valuePatterns.isEmpty()) { | ||
builder.noResult(); | ||
} | ||
|
||
builder.addPattern(PRIMARY, | ||
and(builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), | ||
union(valuePatterns.toArray(GraphPattern[]::new)))); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...eption-kb/src/main/java/de/tudarmstadt/ukp/inception/kb/querybuilder/GraphDbFtsQuery.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,86 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.kb.querybuilder; | ||
|
||
import static de.tudarmstadt.ukp.inception.kb.querybuilder.RdfCollection.collectionOf; | ||
import static org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder.prefix; | ||
import static org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf.iri; | ||
import static org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf.literalOf; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.eclipse.rdf4j.sparqlbuilder.core.Prefix; | ||
import org.eclipse.rdf4j.sparqlbuilder.core.QueryElement; | ||
import org.eclipse.rdf4j.sparqlbuilder.core.Variable; | ||
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPattern; | ||
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatterns; | ||
import org.eclipse.rdf4j.sparqlbuilder.rdf.Iri; | ||
|
||
import de.tudarmstadt.ukp.inception.kb.IriConstants; | ||
|
||
public class GraphDbFtsQuery | ||
implements GraphPattern | ||
{ | ||
public static final Prefix PREFIX_GRAPHDB_FTS = prefix("onto", | ||
iri(IriConstants.PREFIX_GRAPHDB)); | ||
public static final Iri GRAPHDB_FTS = PREFIX_GRAPHDB_FTS.iri("fts"); | ||
|
||
private final Variable subject; | ||
private final Variable score; | ||
private final Variable matchTerm; | ||
private final Variable matchTermProperty; | ||
private final String query; | ||
private int limit = 0; | ||
|
||
public GraphDbFtsQuery(Variable aSubject, Variable aScore, Variable aMatchTerm, | ||
Variable aMatchTermProperty, String aQuery) | ||
{ | ||
subject = aSubject; | ||
score = aScore; | ||
matchTerm = aMatchTerm; | ||
matchTermProperty = aMatchTermProperty; | ||
query = aQuery; | ||
} | ||
|
||
public GraphDbFtsQuery withLimit(int aLimit) | ||
{ | ||
limit = aLimit; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String getQueryString() | ||
{ | ||
var queryElements = new ArrayList<QueryElement>(); | ||
queryElements.add(literalOf(query)); | ||
if (limit > 0) { | ||
queryElements.add(literalOf(2 * limit)); | ||
} | ||
|
||
return GraphPatterns.and( // | ||
matchTerm.has(GRAPHDB_FTS, collectionOf(queryElements)), // | ||
subject.has(matchTermProperty, matchTerm)) // | ||
.getQueryString(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() | ||
{ | ||
return false; | ||
} | ||
} |
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
Oops, something went wrong.