use of ai.grakn.graql.Query in project grakn by graknlabs.
the class GraqlController method executeQuery.
/**
* Execute a query and return a response in the format specified by the request.
*
* @param tx open transaction to current graph
* @param queryString read query to be executed
* @param acceptType response format that the client will accept
* @param multi execute multiple statements
* @param parser
*/
private String executeQuery(EmbeddedGraknTx<?> tx, String queryString, String acceptType, boolean multi, boolean skipSerialisation, QueryParser parser) throws JsonProcessingException {
Printer printer = this.printer;
if (APPLICATION_TEXT.equals(acceptType))
printer = Printers.graql(false);
String formatted;
boolean commitQuery = true;
if (multi) {
Stream<Query<?>> query = parser.parseList(queryString);
List<?> collectedResults = query.map(this::executeAndMonitor).collect(Collectors.toList());
if (skipSerialisation) {
formatted = mapper.writeValueAsString(new Object[collectedResults.size()]);
} else {
formatted = printer.graqlString(collectedResults);
}
} else {
Query<?> query = parser.parseQuery(queryString);
if (skipSerialisation) {
formatted = "";
} else {
formatted = printer.graqlString(executeAndMonitor(query));
}
commitQuery = !query.isReadOnly();
}
if (commitQuery)
tx.commitSubmitNoLogs().ifPresent(postProcessor::submit);
return formatted;
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class QueryParserTest method testParseListMatchInsert.
@Test
public void testParseListMatchInsert() {
String matchString = "match $y isa movie; limit 1;";
String insertString = "insert $x isa movie;";
List<Query<?>> queries = Graql.parser().parseList(matchString + insertString).collect(toList());
assertEquals(ImmutableList.of(match(var("y").isa("movie")).limit(1).insert(var("x").isa("movie"))), queries);
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class QueryParserTest method whenParsingAnInfiniteListOfQueriesWithASyntaxError_Throw.
@Test
public void whenParsingAnInfiniteListOfQueriesWithASyntaxError_Throw() {
String queryText1 = "match $x isa movie; insert ($x, $x) isa has-genre;";
String queryText2 = "match $x isa person insert ($x, $x) isa has-genre;";
Query query1 = Graql.parse(queryText1);
char[] queryChars = (queryText1 + queryText2).toCharArray();
InputStream infStream = new InputStream() {
int pos = 0;
@Override
public int read() throws IOException {
char c = queryChars[pos];
pos += 1;
if (pos >= queryChars.length) {
pos -= queryChars.length;
}
return c;
}
};
Stream<Query<?>> queries = Graql.parser().parseList(new InputStreamReader(infStream));
Iterator<Query<?>> iterator = queries.iterator();
assertEquals(query1, iterator.next());
exception.expect(GraqlSyntaxException.class);
iterator.next();
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class QueryParserTest method testParseListOneMatch.
@Test
public void testParseListOneMatch() {
String getString = "match $y isa movie; limit 1; get;";
List<Query<?>> queries = Graql.parser().parseList(getString).collect(toList());
assertEquals(ImmutableList.of(match(var("y").isa("movie")).limit(1).get()), queries);
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class QueryParserTest method testParseManyMatchInsertWithoutStackOverflow.
@Test
public void testParseManyMatchInsertWithoutStackOverflow() {
int numQueries = 10_000;
String matchInsertString = "match $x; insert $y;";
String longQueryString = Strings.repeat(matchInsertString, numQueries);
Query<?> matchInsert = match(var("x")).insert(var("y"));
List<Query<?>> queries = Graql.parser().parseList(longQueryString).collect(toList());
assertEquals(Collections.nCopies(numQueries, matchInsert), queries);
}
Aggregations