Skip to content

Commit

Permalink
Don't throw exception when encountering invalid query terms (#1541)
Browse files Browse the repository at this point in the history
* Don't throw exception when encountering invalid query terms

* Order map entries

* Factor out PropertyLike (#1543)

---------

Co-authored-by: Olov Ylinenpää <[email protected]>
  • Loading branch information
kwahlin and olovy authored Dec 19, 2024
1 parent 94c12e2 commit bb09334
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
18 changes: 18 additions & 0 deletions whelk-core/src/main/groovy/whelk/search2/querytree/InvalidKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package whelk.search2.querytree;

import java.util.LinkedHashMap;
import java.util.Map;

import static whelk.JsonLd.TYPE_KEY;

public sealed interface InvalidKey extends PropertyLike {
record UnrecognizedKey(String name) implements InvalidKey {}
record AmbiguousKey(String name) implements InvalidKey {}

default Map<String, Object> definition() {
var m = new LinkedHashMap<String, Object>();
m.put(TYPE_KEY, "_Invalid");
m.put("label", name());
return m;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package whelk.search2.querytree;

import java.util.LinkedHashMap;
import java.util.Map;

import static whelk.JsonLd.TYPE_KEY;

sealed interface InvalidValue extends Value {
record ForbiddenValue(String string) implements InvalidValue {}
record AmbiguousValue(String string) implements InvalidValue {}

@Override
String string();

@Override
default Object description() {
var m = new LinkedHashMap<String, Object>();
m.put(TYPE_KEY, "_Invalid");
m.put("label", string());
return m;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Path(List<Object> path) {
@Override
public String toString() {
return path.stream()
.map(x -> x instanceof Property ? ((Property) x).name() : (String) x)
.map(x -> x instanceof PropertyLike p ? p.name() : (String) x)
.map(this::substitute)
.collect(Collectors.joining("."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ public Map<String, Object> toSearchMapping(QueryTree qt, Map<String, String> non
var propertyChainAxiom = new LinkedList<>();

for (int i = getPath().size() - 1; i >= 0; i--) {
var property = Optional.of(getPath().get(i))
.filter(x -> x instanceof Property)
.map(Property.class::cast);

if (property.isPresent()) {
if (getPath().get(i) instanceof PropertyLike property) {
propertyChainAxiom.push(i > 0 && getPath().get(i - 1).equals(JsonLd.REVERSE_KEY)
? Map.of("inverseOf", property.get().definition())
: property.get().definition());
? Map.of("inverseOf", property.definition())
: property.definition());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import static whelk.search2.Disambiguate.RDF_TYPE;

public class Property {
public class Property implements PropertyLike {
public enum DomainCategory {
ADMIN_METADATA,
WORK,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package whelk.search2.querytree;

import java.util.Map;

public interface PropertyLike {
String name();
Map<String, Object> definition();
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ private static PathValue buildPathValue(Ast.Code c, Disambiguate disambiguate) t
} else {
var ambiguous = disambiguate.getAmbiguousPropertyMapping(part);
if (ambiguous.isEmpty()) {
throw new InvalidQueryException("Unrecognized property alias: " + part);
path.add(new InvalidKey.UnrecognizedKey(part));
} else {
throw new InvalidQueryException("\"" + part + "\" maps to multiple properties: " + ambiguous + "," +
" please specify which one is meant.");
path.add(new InvalidKey.AmbiguousKey(part));
}
}
}
Expand All @@ -117,26 +116,18 @@ private static Value buildValue(Property property, String value, Disambiguate di
if (mappedType.isPresent()) {
return new VocabTerm(mappedType.get(), disambiguate.getDefinition(mappedType.get()));
} else {
var ambiguous = disambiguate.getAmbiguousClassMapping(value);
if (ambiguous.isEmpty()) {
throw new InvalidQueryException("Unrecognized type: " + value);
} else {
throw new InvalidQueryException("\"" + value + "\" maps to multiple types: " + ambiguous + "," +
" please specify which one is meant.");
}
return disambiguate.getAmbiguousClassMapping(value).isEmpty()
? new InvalidValue.ForbiddenValue(value)
: new InvalidValue.AmbiguousValue(value);
}
} else if (property.isVocabTerm()) {
Optional<String> mappedEnum = disambiguate.mapToEnum(value);
if (mappedEnum.isPresent()) {
return new VocabTerm(mappedEnum.get(), disambiguate.getDefinition(mappedEnum.get()));
} else {
var ambiguous = disambiguate.getAmbiguousEnumMapping(value);
if (ambiguous.isEmpty()) {
throw new InvalidQueryException("Invalid value " + value + " for property " + property);
} else {
throw new InvalidQueryException("\"" + value + "\" maps to multiple types: " + ambiguous + "," +
" please specify which one is meant.");
}
return disambiguate.getAmbiguousEnumMapping(value).isEmpty()
? new InvalidValue.ForbiddenValue(value)
: new InvalidValue.AmbiguousValue(value);
}
}
// Expand and encode URIs, e.g. sao:Hästar -> https://id.kb.se/term/sao/H%C3%A4star
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package whelk.search2.querytree;

public sealed interface Value permits Link, Literal, VocabTerm {
public sealed interface Value permits Link, Literal, InvalidValue, VocabTerm {
String string();

Object description();
Expand Down

0 comments on commit bb09334

Please sign in to comment.