use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class KrillQuery method _handleAttrGroup.
// Deserialize attribute groups
private SpanQueryWrapper _handleAttrGroup(SpanQueryWrapper elementWithIdWrapper, JsonNode attrNode) throws QueryException {
if (!attrNode.has("relation")) {
throw new QueryException(743, "Term group expects a relation");
}
if (!attrNode.has("operands")) {
throw new QueryException(742, "Term group needs operand list");
}
String relation = attrNode.get("relation").asText();
JsonNode operands = attrNode.get("operands");
SpanQueryWrapper attrWrapper;
if ("relation:and".equals(relation)) {
List<SpanQueryWrapper> wrapperList = new ArrayList<SpanQueryWrapper>();
for (JsonNode operand : operands) {
attrWrapper = _termFromJson(operand);
if (attrWrapper == null) {
throw new QueryException(747, "Attribute is null");
}
wrapperList.add(attrWrapper);
}
if (elementWithIdWrapper != null) {
return new SpanWithAttributeQueryWrapper(elementWithIdWrapper, wrapperList);
} else {
return new SpanWithAttributeQueryWrapper(wrapperList);
}
} else if ("relation:or".equals(relation)) {
SpanAlterQueryWrapper saq = new SpanAlterQueryWrapper(field);
SpanWithAttributeQueryWrapper saqw;
for (JsonNode operand : operands) {
attrWrapper = _termFromJson(operand);
if (attrWrapper == null) {
throw new QueryException(747, "Attribute is null");
}
if (elementWithIdWrapper != null) {
saqw = new SpanWithAttributeQueryWrapper(elementWithIdWrapper, attrWrapper);
} else {
saqw = new SpanWithAttributeQueryWrapper(attrWrapper);
}
saq.or(saqw);
}
return saq;
} else {
throw new QueryException(716, "Unknown relation");
}
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class KrillQuery method _operationRelationFromJson.
private SpanQueryWrapper _operationRelationFromJson(JsonNode operands, JsonNode relation) throws QueryException {
if (operands.size() < 2) {
throw new QueryException(705, "Number of operands is not acceptable");
}
SpanQueryWrapper operand1 = this._fromKoral(operands.get(0), true);
SpanQueryWrapper operand2 = this._fromKoral(operands.get(1), true);
RelationDirection direction;
if (operand1.isEmpty() && !operand2.isEmpty()) {
// "<:";
direction = RelationDirection.LEFT;
} else {
// ">:"
direction = RelationDirection.RIGHT;
}
if (!relation.has("@type")) {
throw new QueryException(701, "JSON-LD group has no @type attribute");
}
if (relation.get("@type").asText().equals("koral:relation")) {
SpanRelationWrapper spanRelationWrapper;
SpanQueryWrapper relationTermWrapper;
if (!relation.has("wrap")) {
throw new QueryException(718, "Missing relation term.");
} else {
relationTermWrapper = _termFromJson(relation.get("wrap"), false, direction);
spanRelationWrapper = new SpanRelationWrapper(relationTermWrapper, operand1, operand2);
}
spanRelationWrapper.setDirection(direction);
return spanRelationWrapper;
} else {
throw new QueryException(713, "Query type is not supported");
}
// if (relation.has("boundary")){
// _operationRepetitionFromJson(relation, operands);
// }
// else{
//
// }
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class KrillQuery method _segFromJson.
// Deserialize koral:token
private SpanQueryWrapper _segFromJson(JsonNode json) throws QueryException {
if (!json.has("@type"))
throw new QueryException(701, "JSON-LD group has no @type attribute");
String type = json.get("@type").asText();
if (DEBUG)
log.trace("Wrap new token definition by {}", type);
// Branch on type
switch(type) {
case "koral:term":
// case "match:eq":
return this._termFromJson(json);
case "koral:termGroup":
if (!json.has("operands"))
throw new QueryException(742, "Term group needs operand list");
// Get operands
JsonNode operands = json.get("operands");
SpanSegmentQueryWrapper ssegqw = this.builder().seg();
if (!json.has("relation"))
throw new QueryException(743, "Term group expects a relation");
switch(json.get("relation").asText()) {
case "relation:and":
for (JsonNode operand : operands) {
SpanQueryWrapper part = this._segFromJson(operand);
if (part instanceof SpanAlterQueryWrapper) {
ssegqw.with((SpanAlterQueryWrapper) part);
} else if (part instanceof SpanRegexQueryWrapper) {
ssegqw.with((SpanRegexQueryWrapper) part);
} else if (part instanceof SpanSegmentQueryWrapper) {
ssegqw.with((SpanSegmentQueryWrapper) part);
} else {
throw new QueryException(744, "Operand not supported in term group");
}
;
}
;
return ssegqw;
case "relation:or":
SpanAlterQueryWrapper ssaq = new SpanAlterQueryWrapper(this.field);
for (JsonNode operand : operands) {
ssaq.or(this._segFromJson(operand));
}
;
return ssaq;
}
;
}
;
throw new QueryException(745, "Token type is not supported");
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class TestKrillQueryJSON method queryJSONinfiniteExpansion.
@Test
public void queryJSONinfiniteExpansion() throws QueryException {
// der []*
try {
String json = getString(getClass().getResource("/queries/bugs/expansion_bug_3.jsonld").getFile());
KrillQuery kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(), "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:c, []{0, 4}, right)}))");
} catch (QueryException e) {
fail(e.getMessage());
}
;
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class TestKrillQueryJSON method queryJSONdistancesWithRegexes.
@Test
public void queryJSONdistancesWithRegexes() throws QueryException {
// "der" []{2,3} [opennlp/p="NN"]
try {
String json = getString(getClass().getResource("/queries/bugs/distances_with_regex_bug.jsonld").getFile());
KrillQuery kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(), "spanDistance(SpanMultiTermQueryWrapper(tokens:/s:der/), SpanMultiTermQueryWrapper(tokens:/opennlp/p:NN/), [(w[3:4], ordered, notExcluded)])");
} catch (QueryException e) {
fail(e.getMessage());
}
;
}
Aggregations