|
| 1 | +/* |
| 2 | + * Licensed to the Technische Universität Darmstadt under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The Technische Universität Darmstadt |
| 6 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.tudarmstadt.ukp.inception.kb.querybuilder; |
| 19 | + |
| 20 | +import static de.tudarmstadt.ukp.inception.kb.IriConstants.PREFIX_GRAPHDB; |
| 21 | +import static de.tudarmstadt.ukp.inception.kb.querybuilder.SPARQLQueryBuilder.convertToRequiredTokenPrefixMatchingQuery; |
| 22 | +import static de.tudarmstadt.ukp.inception.kb.querybuilder.SPARQLQueryBuilder.Priority.PRIMARY; |
| 23 | +import static org.apache.commons.lang3.StringUtils.isBlank; |
| 24 | +import static org.eclipse.rdf4j.sparqlbuilder.constraint.Expressions.and; |
| 25 | +import static org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder.prefix; |
| 26 | +import static org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatterns.and; |
| 27 | +import static org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatterns.union; |
| 28 | +import static org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf.iri; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | + |
| 32 | +import org.eclipse.rdf4j.sparqlbuilder.constraint.Expression; |
| 33 | +import org.eclipse.rdf4j.sparqlbuilder.core.Prefix; |
| 34 | +import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPattern; |
| 35 | + |
| 36 | +public class FtsAdapterGraphDb |
| 37 | + implements FtsAdapter |
| 38 | +{ |
| 39 | + private static final String MULTI_CHAR_WILDCARD = "*"; |
| 40 | + |
| 41 | + private static final Prefix PREFIX_GRAPHDB_SEARCH = prefix("onto", iri(PREFIX_GRAPHDB)); |
| 42 | + |
| 43 | + private final SPARQLQueryBuilder builder; |
| 44 | + |
| 45 | + public FtsAdapterGraphDb(SPARQLQueryBuilder aBuilder) |
| 46 | + { |
| 47 | + builder = aBuilder; |
| 48 | + builder.addPrefix(PREFIX_GRAPHDB_SEARCH); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void withLabelMatchingExactlyAnyOf(String... aValues) |
| 53 | + { |
| 54 | + var kb = builder.getKnowledgeBase(); |
| 55 | + |
| 56 | + var valuePatterns = new ArrayList<GraphPattern>(); |
| 57 | + for (var value : aValues) { |
| 58 | + var sanitizedValue = builder.sanitizeQueryString_FTS(value); |
| 59 | + |
| 60 | + if (isBlank(sanitizedValue)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); |
| 65 | + |
| 66 | + valuePatterns.add(new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, |
| 67 | + SPARQLQueryBuilder.VAR_SCORE, SPARQLQueryBuilder.VAR_MATCH_TERM, |
| 68 | + SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, sanitizedValue) // |
| 69 | + .withLimit(builder.getLimit()) // |
| 70 | + .filter(builder.equalsPattern(SPARQLQueryBuilder.VAR_MATCH_TERM, value, |
| 71 | + kb))); |
| 72 | + } |
| 73 | + |
| 74 | + if (valuePatterns.isEmpty()) { |
| 75 | + builder.noResult(); |
| 76 | + } |
| 77 | + |
| 78 | + builder.addPattern(PRIMARY, and( // |
| 79 | + builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), // |
| 80 | + union(valuePatterns.toArray(GraphPattern[]::new)))); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void withLabelContainingAnyOf(String... aValues) |
| 85 | + { |
| 86 | + var valuePatterns = new ArrayList<GraphPattern>(); |
| 87 | + for (var value : aValues) { |
| 88 | + var sanitizedValue = builder.sanitizeQueryString_FTS(value); |
| 89 | + |
| 90 | + if (isBlank(sanitizedValue)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); |
| 95 | + |
| 96 | + valuePatterns.add(new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, |
| 97 | + SPARQLQueryBuilder.VAR_SCORE, SPARQLQueryBuilder.VAR_MATCH_TERM, |
| 98 | + SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, sanitizedValue) // |
| 99 | + .withLimit(builder.getLimit()) // |
| 100 | + .filter(builder.containsPattern(SPARQLQueryBuilder.VAR_MATCH_TERM, |
| 101 | + value))); |
| 102 | + } |
| 103 | + |
| 104 | + if (valuePatterns.isEmpty()) { |
| 105 | + builder.noResult(); |
| 106 | + } |
| 107 | + |
| 108 | + builder.addPattern(PRIMARY, |
| 109 | + and(builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), |
| 110 | + union(valuePatterns.toArray(GraphPattern[]::new)))); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void withLabelStartingWith(String aPrefixQuery) |
| 115 | + { |
| 116 | + // Strip single quotes and asterisks because they have special semantics |
| 117 | + var queryString = builder.sanitizeQueryString_FTS(aPrefixQuery); |
| 118 | + |
| 119 | + if (isBlank(queryString)) { |
| 120 | + builder.noResult(); |
| 121 | + } |
| 122 | + |
| 123 | + // If the query string entered by the user does not end with a space character, then |
| 124 | + // we assume that the user may not yet have finished writing the word and add a |
| 125 | + // wildcard |
| 126 | + if (!aPrefixQuery.endsWith(" ")) { |
| 127 | + queryString += MULTI_CHAR_WILDCARD; |
| 128 | + } |
| 129 | + |
| 130 | + builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); |
| 131 | + |
| 132 | + // Locate all entries where the label contains the prefix (using the FTS) and then |
| 133 | + // filter them by those which actually start with the prefix. |
| 134 | + builder.addPattern(PRIMARY, and( // |
| 135 | + builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), // |
| 136 | + new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, SPARQLQueryBuilder.VAR_SCORE, |
| 137 | + SPARQLQueryBuilder.VAR_MATCH_TERM, |
| 138 | + SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, queryString) // |
| 139 | + .withLimit(builder.getLimit()) // |
| 140 | + .filter(builder.startsWithPattern(SPARQLQueryBuilder.VAR_MATCH_TERM, |
| 141 | + aPrefixQuery)))); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void withLabelMatchingAnyOf(String... aValues) |
| 146 | + { |
| 147 | + var valuePatterns = new ArrayList<GraphPattern>(); |
| 148 | + for (var value : aValues) { |
| 149 | + var sanitizedValue = builder.sanitizeQueryString_FTS(value); |
| 150 | + |
| 151 | + if (isBlank(sanitizedValue)) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + var fuzzyQuery = convertToRequiredTokenPrefixMatchingQuery(sanitizedValue, "", |
| 156 | + MULTI_CHAR_WILDCARD); |
| 157 | + |
| 158 | + if (isBlank(fuzzyQuery)) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + |
| 162 | + builder.addProjection(SPARQLQueryBuilder.VAR_SCORE); |
| 163 | + |
| 164 | + var labelFilterExpressions = new ArrayList<Expression<?>>(); |
| 165 | + labelFilterExpressions.add(builder.matchKbLanguage(VAR_MATCH_TERM)); |
| 166 | + |
| 167 | + valuePatterns.add(new GraphDbFtsQuery(SPARQLQueryBuilder.VAR_SUBJECT, |
| 168 | + SPARQLQueryBuilder.VAR_SCORE, SPARQLQueryBuilder.VAR_MATCH_TERM, |
| 169 | + SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY, fuzzyQuery) // |
| 170 | + .withLimit(builder.getLimit()) // |
| 171 | + .filter(and(labelFilterExpressions.toArray(Expression[]::new)))); |
| 172 | + } |
| 173 | + |
| 174 | + if (valuePatterns.isEmpty()) { |
| 175 | + builder.noResult(); |
| 176 | + } |
| 177 | + |
| 178 | + builder.addPattern(PRIMARY, |
| 179 | + and(builder.bindMatchTermProperties(SPARQLQueryBuilder.VAR_MATCH_TERM_PROPERTY), |
| 180 | + union(valuePatterns.toArray(GraphPattern[]::new)))); |
| 181 | + } |
| 182 | +} |
0 commit comments