use of org.apache.stanbol.entityhub.core.query.FieldQueryImpl in project stanbol by apache.
the class RdfResultListTest method testRdfResultSorting.
/**
* Providing a sorted Iteration over query results stored in an RDF
* graph is not something trivial. Therefore this test
*/
@Test
public void testRdfResultSorting() {
SortedMap<Double, RdfRepresentation> sorted = new TreeMap<Double, RdfRepresentation>();
Graph resultGraph = new IndexedGraph();
RdfValueFactory vf = new RdfValueFactory(resultGraph);
IRI resultListNode = new IRI(RdfResourceEnum.QueryResultSet.getUri());
IRI resultProperty = new IRI(RdfResourceEnum.queryResult.getUri());
for (int i = 0; i < 100; i++) {
Double rank;
do {
//avoid duplicate keys
rank = Math.random();
} while (sorted.containsKey(rank));
RdfRepresentation r = vf.createRepresentation("urn:sortTest:rep." + i);
//link the representation with the query result set
resultGraph.add(new TripleImpl(resultListNode, resultProperty, r.getNode()));
r.set(RdfResourceEnum.resultScore.getUri(), rank);
sorted.put(rank, r);
}
RdfQueryResultList resultList = new RdfQueryResultList(new FieldQueryImpl(), resultGraph);
if (log.isDebugEnabled()) {
log.debug("---DEBUG Sorting ---");
for (Iterator<Representation> it = resultList.iterator(); it.hasNext(); ) {
Representation r = it.next();
log.debug("{}: {}", r.getFirst(RdfResourceEnum.resultScore.getUri()), r.getId());
}
}
log.debug("---ASSERT Sorting ---");
for (Iterator<Representation> it = resultList.iterator(); it.hasNext(); ) {
Representation r = it.next();
Double lastkey = sorted.lastKey();
Representation last = sorted.get(lastkey);
Assert.assertEquals("score: " + r.getFirst(RdfResourceEnum.resultScore.getUri()) + " of Representation " + r.getId() + " is not as expected " + last.getFirst(RdfResourceEnum.resultScore.getUri()) + " of Representation " + last.getId() + "!", r, last);
sorted.remove(lastkey);
}
Assert.assertTrue(sorted.isEmpty());
}
use of org.apache.stanbol.entityhub.core.query.FieldQueryImpl in project stanbol by apache.
the class FieldQueryReader method fromJSON.
/**
*
* @param queryFactory
* @param jsonQueryString
* @param acceptedMediaType used to add the accept header to Error responses
* @return
* @throws JSONException
* @throws WebApplicationException
*/
public static FieldQuery fromJSON(String jsonQueryString, MediaType acceptedMediaType, NamespacePrefixService nsPrefixService) throws JSONException, WebApplicationException {
if (jsonQueryString == null) {
throw new IllegalArgumentException("The parsed JSON object MUST NOT be NULL!");
}
JSONObject jQuery = new JSONObject(jsonQueryString);
FieldQuery query;
if (jQuery.has("ldpath")) {
//STANBOL-417: support for using LDPath as select
LDPathFieldQueryImpl ldPathQuery = new LDPathFieldQueryImpl();
ldPathQuery.setLDPathSelect(jQuery.getString("ldpath"));
query = ldPathQuery;
} else {
query = new FieldQueryImpl();
}
if (!jQuery.has("constraints")) {
StringBuilder message = new StringBuilder();
message.append("The parsed Field Query MUST contain at least a single 'constraints'\n");
message.append("Parsed Query:\n");
message.append(jQuery.toString(4));
log.warn(message.toString());
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(message.toString()).header(HttpHeaders.ACCEPT, acceptedMediaType.toString()).build());
}
JSONArray constraints = jQuery.getJSONArray("constraints");
//collect all parsing Errors to report a complete set of all errors
boolean parsingError = false;
StringBuilder parsingErrorMessages = new StringBuilder();
parsingErrorMessages.append("Constraint parsing Errors:\n");
for (int i = 0; i < constraints.length(); i++) {
JSONObject jConstraint = constraints.getJSONObject(i);
if (jConstraint.has("field")) {
String field = jConstraint.getString("field");
//check if there is already a constraint for that field
if (field == null || field.isEmpty()) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Each Field Query Constraint MUST define a value for 'field'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
String fieldUri = nsPrefixService.getFullName(field);
if (fieldUri == null) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("The 'field' '").append(field).append("uses an unknown namespace prefix '");
parsingErrorMessages.append(NamespaceMappingUtils.getPrefix(field)).append("'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
} else if (query.isConstrained(fieldUri)) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("The parsed Query defines multiple constraints fr the field '").append(fieldUri).append("'!\n");
parsingErrorMessages.append("FieldQuery allows only a single Constraint for a field\n");
parsingErrorMessages.append("Parsed Constraints:\n");
parsingErrorMessages.append(constraints.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
} else {
try {
query.setConstraint(fieldUri, parseConstraint(jConstraint, nsPrefixService));
} catch (IllegalArgumentException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append(e.getMessage());
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
}
} else {
//empty field
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Constraints MUST define a value for 'field'\n");
parsingErrorMessages.append("Parsed Constraint:\n");
parsingErrorMessages.append(jConstraint.toString(4));
parsingErrorMessages.append('\n');
parsingError = true;
continue;
}
}
if (parsingError) {
String message = parsingErrorMessages.toString();
log.warn(message);
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(message).header(HttpHeaders.ACCEPT, acceptedMediaType.toString()).build());
}
//parse selected fields
JSONArray selected = jQuery.optJSONArray("selected");
if (selected != null) {
for (int i = 0; i < selected.length(); i++) {
String selectedField = selected.getString(i);
selectedField = nsPrefixService.getFullName(selectedField);
if (selectedField != null && !selectedField.isEmpty()) {
query.addSelectedField(selectedField);
}
}
}
//parse limit and offset
if (jQuery.has("limit") && !jQuery.isNull("limit")) {
try {
query.setLimit(jQuery.getInt("limit"));
} catch (JSONException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Property \"limit\" MUST BE a valid integer number!\n");
parsingErrorMessages.append("Parsed Value:");
parsingErrorMessages.append(jQuery.get("init"));
parsingErrorMessages.append('\n');
parsingError = true;
}
}
if (jQuery.has("offset") && !jQuery.isNull("offset")) {
try {
query.setOffset(jQuery.getInt("offset"));
} catch (JSONException e) {
parsingErrorMessages.append('\n');
parsingErrorMessages.append("Property \"offset\" MUST BE a valid integer number!\n");
parsingErrorMessages.append("Parsed Value:");
parsingErrorMessages.append(jQuery.get("init"));
parsingErrorMessages.append('\n');
parsingError = true;
}
}
return query;
}
Aggregations