use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class KrillQuery method _operationRepetitionFromJson.
// Deserialize operation:repetition
private SpanQueryWrapper _operationRepetitionFromJson(JsonNode json, JsonNode operands) throws QueryException {
if (operands.size() != 1)
throw new QueryException(705, "Number of operands is not acceptable");
int min = 0, max = 100;
if (json.has("boundary")) {
Boundary b = new Boundary(json.get("boundary"), 0, 100);
min = b.min;
max = b.max;
} else // <legacyCode>
{
this.addMessage(0, "Setting boundary by min and max is deprecated");
// Set minimum value
if (json.has("min"))
min = json.get("min").asInt(0);
// Set maximum value
if (json.has("max"))
max = json.get("max").asInt(100);
}
;
// Sanitize max
if (max < 0)
max = 100;
else if (max > 100)
max = 100;
// Sanitize min
if (min < 0)
min = 0;
else if (min > 100)
min = 100;
// Check relation between min and max
if (min > max)
max = max;
SpanQueryWrapper sqw = this._fromKoral(operands.get(0));
if (sqw.maybeExtension())
return sqw.setMin(min).setMax(max);
return new SpanRepetitionQueryWrapper(sqw, min, max);
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class KrillCollection method _fromKoral.
// Create collection from KoralQuery
private CollectionBuilder.Interface _fromKoral(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 (type.equals("koral:doc")) {
String key = "tokens";
String valtype = "type:string";
String match = "match:eq";
if (json.has("key"))
key = json.get("key").asText();
if (json.has("type"))
valtype = json.get("type").asText();
// Filter based on date
if (valtype.equals("type:date")) {
if (!json.has("value"))
throw new QueryException(820, "Dates require value fields");
String dateStr = json.get("value").asText();
if (json.has("match"))
match = json.get("match").asText();
// TODO: This isn't stable yet
switch(match) {
case "match:eq":
return this.cb.date(key, dateStr);
case "match:ne":
return this.cb.date(key, dateStr).not();
case "match:geq":
return this.cb.since(key, dateStr);
case "match:leq":
return this.cb.till(key, dateStr);
}
;
throw new QueryException(841, "Match relation unknown for type");
} else // Filter based on string
if (valtype.equals("type:string")) {
if (json.has("match"))
match = json.get("match").asText();
switch(match) {
case "match:eq":
return this.cb.term(key, json.get("value").asText());
case "match:ne":
return this.cb.term(key, json.get("value").asText()).not();
// string fields
case "match:contains":
return this.cb.text(key, json.get("value").asText());
case "match:containsnot":
return this.cb.text(key, json.get("value").asText()).not();
// <LEGACY>
case "match:excludes":
return this.cb.text(key, json.get("value").asText()).not();
}
;
throw new QueryException(841, "Match relation unknown for type");
} else // Filter based on regex
if (valtype.equals("type:regex")) {
if (json.has("match"))
match = json.get("match").asText();
if (match.equals("match:eq")) {
return this.cb.re(key, json.get("value").asText());
} else if (match.equals("match:ne")) {
return this.cb.re(key, json.get("value").asText()).not();
} else // with .*
if (match.equals("match:contains")) {
return this.cb.re(key, json.get("value").asText());
} else if (match.equals("match:containsnot")) {
return this.cb.re(key, json.get("value").asText());
} else // <LEGACY>
if (match.equals("match:excludes")) {
return this.cb.re(key, json.get("value").asText()).not();
}
;
throw new QueryException(841, "Match relation unknown for type");
}
;
throw new QueryException(843, "Document type is not supported");
} else // nested group
if (type.equals("koral:docGroup")) {
if (!json.has("operands") || !json.get("operands").isArray())
throw new QueryException(842, "Document group needs operand list");
CollectionBuilder.Group group;
String operation = "operation:and";
if (json.has("operation"))
operation = json.get("operation").asText();
if (operation.equals("operation:or"))
group = this.cb.orGroup();
else if (operation.equals("operation:and"))
group = this.cb.andGroup();
else
throw new QueryException(810, "Unknown document group operation");
for (JsonNode operand : json.get("operands")) {
group.with(this._fromKoral(operand));
}
;
return group;
}
// Unknown type
throw new QueryException(813, "Collection type is not supported");
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class FrameConstraint method add.
/**
* Add a new valid condition to the Frame Constraint.
*
* @param condition
* A string representing a valid condition.
* See the synopsis for valid condition names.
* @return The {@link FrameConstraint} for chaining.
* @throws QueryException
*/
public FrameConstraint add(String condition) throws QueryException {
int or = FRAME.get(condition);
if (or == 0)
throw new QueryException(706, "Frame type is unknown");
this.vector |= or;
return this;
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class TestKrillQueryJSON method queryJSONelement.
@Test
public void queryJSONelement() throws QueryException {
// <base/s=s>
try {
String json = getString(getClass().getResource("/queries/element/simple-element.jsonld").getFile());
KrillQuery kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(), "<tokens:base/s:s />");
} catch (QueryException e) {
fail(e.getMessage());
}
;
}
use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.
the class TestKrillQueryJSON method queryJSONflags2.
@Test
public void queryJSONflags2() throws QueryException {
// buchstabe/i
try {
String json = getString(getClass().getResource("/queries/flags/unknown1.jsonld").getFile());
KrillQuery kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(), "tokens:s:buchstabe");
assertEquals(kq.getWarning(0).getCode(), 748);
json = getString(getClass().getResource("/queries/flags/unknown2.jsonld").getFile());
kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(), "tokens:i:buchstabe");
assertEquals(kq.getWarning(0).getCode(), 748);
json = getString(getClass().getResource("/queries/flags/unknown3.jsonld").getFile());
kq = new KrillQuery("tokens");
assertEquals(kq.fromKoral(json).toQuery().toString(), "tokens:i:buchstabe");
assertEquals(kq.getWarning(0).getCode(), 748);
} catch (QueryException e) {
fail(e.getMessage());
}
;
}
Aggregations