use of ai.grakn.graql.QueryParser in project grakn by graknlabs.
the class GraqlControllerReadOnlyTest method setupMock.
@Before
public void setupMock() {
mockQueryBuilder = mock(QueryBuilder.class);
when(mockQueryBuilder.materialise(anyBoolean())).thenReturn(mockQueryBuilder);
when(mockQueryBuilder.infer(anyBoolean())).thenReturn(mockQueryBuilder);
when(printer.graqlString(any())).thenReturn(Json.object().toString());
QueryParser mockParser = mock(QueryParser.class);
when(mockQueryBuilder.parser()).thenReturn(mockParser);
when(mockParser.parseQuery(any())).thenAnswer(invocation -> sampleKB.tx().graql().parse(invocation.getArgument(0)));
mockTx = mock(EmbeddedGraknTx.class, RETURNS_DEEP_STUBS);
when(mockTx.keyspace()).thenReturn(Keyspace.of("randomkeyspace"));
when(mockTx.graql()).thenReturn(mockQueryBuilder);
when(mockFactory.tx(eq(mockTx.keyspace()), any())).thenReturn(mockTx);
when(mockFactory.keyspaceStore()).thenReturn(mockGraknKeyspaceStore);
when(mockFactory.config()).thenReturn(sparkContext.config());
}
use of ai.grakn.graql.QueryParser in project grakn by graknlabs.
the class QueryParserTest method whenParsingAQueryAndDefiningAllVars_AllVarsExceptLabelsAreDefined.
@Test
public void whenParsingAQueryAndDefiningAllVars_AllVarsExceptLabelsAreDefined() {
QueryParser parser = Graql.parser();
parser.defineAllVars(true);
GetQuery query = parser.parseQuery("match ($x, $y) isa foo; get;");
System.out.println(query);
Conjunction<PatternAdmin> conjunction = query.match().admin().getPattern();
Set<PatternAdmin> patterns = conjunction.getPatterns();
VarPatternAdmin pattern = Iterables.getOnlyElement(patterns).asVarPattern();
assertTrue(pattern.var().isUserDefinedName());
IsaProperty property = pattern.getProperty(IsaProperty.class).get();
assertFalse(property.type().var().isUserDefinedName());
}
use of ai.grakn.graql.QueryParser in project grakn by graknlabs.
the class QueryParserTest method testCustomAggregate.
@Test
public void testCustomAggregate() {
QueryBuilder qb = withoutGraph();
QueryParser parser = qb.parser();
parser.registerAggregate("get-any", args -> new GetAny((Var) args.get(0)));
AggregateQuery<Concept> expected = qb.match(var("x").isa("movie")).aggregate(new GetAny(Graql.var("x")));
AggregateQuery<Concept> parsed = qb.parse("match $x isa movie; aggregate get-any $x;");
assertEquals(expected, parsed);
}
use of ai.grakn.graql.QueryParser in project grakn by graknlabs.
the class GraqlController method executeGraql.
@POST
@Path("/kb/{keyspace}/graql")
private String executeGraql(Request request, Response response) throws RetryException, ExecutionException {
Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
String queryString = mandatoryBody(request);
// Run the query with reasoning on or off
Optional<Boolean> infer = queryParameter(request, EXECUTE_WITH_INFERENCE).map(Boolean::parseBoolean);
// Allow multiple queries to be executed
boolean multiQuery = parseBoolean(queryParameter(request, ALLOW_MULTIPLE_QUERIES).orElse("false"));
// Define all anonymous variables in the query
Optional<Boolean> defineAllVars = queryParameter(request, DEFINE_ALL_VARS).map(Boolean::parseBoolean);
// Used to check if serialisation of results is needed. When loading we skip this for the sake of speed
boolean skipSerialisation = parseBoolean(queryParameter(request, LOADING_DATA).orElse("false"));
// Check the transaction type to use
GraknTxType txType = queryParameter(request, TX_TYPE).map(String::toUpperCase).map(GraknTxType::valueOf).orElse(GraknTxType.WRITE);
// This is used to determine the response format
// TODO: Maybe we should really try to stick with one representation? This would require dashboard console interpreting the json representation
final String acceptType;
if (APPLICATION_TEXT.equals(Requests.getAcceptType(request))) {
acceptType = APPLICATION_TEXT;
} else {
acceptType = APPLICATION_JSON;
}
response.type(APPLICATION_JSON);
// Execute the query and get the results
LOG.debug("Executing graql query: {}", StringUtils.abbreviate(queryString, 100));
LOG.trace("Full query: {}", queryString);
return executeFunctionWithRetrying(() -> {
try (EmbeddedGraknTx<?> tx = factory.tx(keyspace, txType);
Timer.Context context = executeGraql.time()) {
QueryBuilder builder = tx.graql();
infer.ifPresent(builder::infer);
QueryParser parser = builder.parser();
defineAllVars.ifPresent(parser::defineAllVars);
response.status(SC_OK);
return executeQuery(tx, queryString, acceptType, multiQuery, skipSerialisation, parser);
} finally {
LOG.debug("Executed graql query");
}
});
}
Aggregations