Search in sources :

Example 1 with QueryParser

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());
}
Also used : QueryParser(ai.grakn.graql.QueryParser) QueryBuilder(ai.grakn.graql.QueryBuilder) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Before(org.junit.Before)

Example 2 with QueryParser

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());
}
Also used : QueryParser(ai.grakn.graql.QueryParser) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GetQuery(ai.grakn.graql.GetQuery) PatternAdmin(ai.grakn.graql.admin.PatternAdmin) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Test(org.junit.Test)

Example 3 with QueryParser

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);
}
Also used : Concept(ai.grakn.concept.Concept) QueryParser(ai.grakn.graql.QueryParser) Var(ai.grakn.graql.Var) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 4 with QueryParser

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");
        }
    });
}
Also used : GraknTxType(ai.grakn.GraknTxType) QueryBuilder(ai.grakn.graql.QueryBuilder) QueryParser(ai.grakn.graql.QueryParser) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

QueryParser (ai.grakn.graql.QueryParser)4 QueryBuilder (ai.grakn.graql.QueryBuilder)3 Test (org.junit.Test)2 GraknTxType (ai.grakn.GraknTxType)1 Keyspace (ai.grakn.Keyspace)1 Concept (ai.grakn.concept.Concept)1 GetQuery (ai.grakn.graql.GetQuery)1 Var (ai.grakn.graql.Var)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1 IsaProperty (ai.grakn.graql.internal.pattern.property.IsaProperty)1 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)1 Timer (com.codahale.metrics.Timer)1 Boolean.parseBoolean (java.lang.Boolean.parseBoolean)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Before (org.junit.Before)1