Search in sources :

Example 1 with GraqlQueryException

use of ai.grakn.exception.GraqlQueryException in project grakn by graknlabs.

the class MatchBase method stream.

@Override
public Stream<Answer> stream(Optional<EmbeddedGraknTx<?>> optionalGraph) {
    EmbeddedGraknTx<?> tx = optionalGraph.orElseThrow(GraqlQueryException::noTx);
    validatePattern(tx);
    GraqlTraversal graqlTraversal = GreedyTraversalPlan.createTraversal(pattern, tx);
    LOG.trace("Created query plan");
    LOG.trace(graqlTraversal.toString());
    return streamWithTraversal(this.getPattern().commonVars(), tx, graqlTraversal);
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal)

Example 2 with GraqlQueryException

use of ai.grakn.exception.GraqlQueryException in project grakn by graknlabs.

the class RemoteGraknTxTest method whenAnErrorOccurs_TheTxCloses.

@Test
public void whenAnErrorOccurs_TheTxCloses() {
    Query<?> query = match(var("x")).get();
    TxRequest execQueryRequest = GrpcUtil.execQueryRequest(query);
    throwOn(execQueryRequest, ErrorType.GRAQL_QUERY_EXCEPTION, "well something went wrong");
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        try {
            tx.graql().match(var("x")).get().execute();
        } catch (GraqlQueryException e) {
        // Ignore
        }
        assertTrue(tx.isClosed());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) TxRequest(ai.grakn.rpc.generated.GrpcGrakn.TxRequest) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Test(org.junit.Test)

Example 3 with GraqlQueryException

use of ai.grakn.exception.GraqlQueryException in project grakn by graknlabs.

the class InsertQueryImpl method getSchemaConcepts.

@Override
public Set<SchemaConcept> getSchemaConcepts() {
    GraknTx theGraph = getTx().orElseThrow(GraqlQueryException::noTx);
    Set<SchemaConcept> types = allVarPatterns().map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).map(theGraph::<Type>getSchemaConcept).collect(Collectors.toSet());
    match().ifPresent(mq -> types.addAll(mq.admin().getSchemaConcepts()));
    return types;
}
Also used : GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Type(ai.grakn.concept.Type) GraqlQueryException(ai.grakn.exception.GraqlQueryException) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 4 with GraqlQueryException

use of ai.grakn.exception.GraqlQueryException in project grakn by graknlabs.

the class RelatesProperty method define.

@Override
public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException {
    Var roleVar = role().var();
    PropertyExecutor.Method relatesMethod = executor -> {
        Role role = executor.get(roleVar).asRole();
        executor.get(var).asRelationshipType().relates(role);
    };
    PropertyExecutor relatesExecutor = PropertyExecutor.builder(relatesMethod).requires(var, roleVar).build();
    // This allows users to skip stating `$roleVar sub role` when they say `$var relates $roleVar`
    PropertyExecutor.Method isRoleMethod = executor -> executor.builder(roleVar).isRole();
    PropertyExecutor isRoleExecutor = PropertyExecutor.builder(isRoleMethod).produces(roleVar).build();
    VarPatternAdmin superRoleVarPattern = superRole();
    if (superRoleVarPattern != null) {
        Var superRoleVar = superRoleVarPattern.var();
        PropertyExecutor.Method subMethod = executor -> {
            Role superRole = executor.get(superRoleVar).asRole();
            executor.builder(roleVar).sub(superRole);
        };
        PropertyExecutor subExecutor = PropertyExecutor.builder(subMethod).requires(superRoleVar).produces(roleVar).build();
        return ImmutableSet.of(relatesExecutor, isRoleExecutor, subExecutor);
    } else {
        return ImmutableSet.of(relatesExecutor, isRoleExecutor);
    }
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Role(ai.grakn.concept.Role) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) EquivalentFragmentSets.sub(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.sub) EquivalentFragmentSets.relates(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.relates) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) RelationshipType(ai.grakn.concept.RelationshipType) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) RelatesAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelatesAtom) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) Nullable(javax.annotation.Nullable) Role(ai.grakn.concept.Role) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var)

Example 5 with GraqlQueryException

use of ai.grakn.exception.GraqlQueryException in project grakn by graknlabs.

the class MatchInfer method stream.

@Override
public Stream<Answer> stream(Optional<EmbeddedGraknTx<?>> optionalGraph) {
    // If the tx is not embedded, treat it like there is no transaction
    // TODO: this is dodgy - when queries don't contain transactions this can be fixed
    EmbeddedGraknTx<?> tx = optionalOr(optionalGraph, inner.tx().filter(t -> t instanceof EmbeddedGraknTx).map(t -> (EmbeddedGraknTx<?>) t)).orElseThrow(GraqlQueryException::noTx);
    if (!RuleUtils.hasRules(tx))
        return inner.stream(optionalGraph);
    validatePattern(tx);
    try {
        Iterator<Conjunction<VarPatternAdmin>> conjIt = getPattern().getDisjunctiveNormalForm().getPatterns().iterator();
        Conjunction<VarPatternAdmin> conj = conjIt.next();
        ReasonerQuery conjQuery = ReasonerQueries.create(conj, tx);
        conjQuery.checkValid();
        Stream<Answer> answerStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
        while (conjIt.hasNext()) {
            conj = conjIt.next();
            conjQuery = ReasonerQueries.create(conj, tx);
            Stream<Answer> localStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
            answerStream = Stream.concat(answerStream, localStream);
        }
        return answerStream.map(result -> result.project(getSelectedNames()));
    } catch (GraqlQueryException e) {
        System.err.println(e.getMessage());
        return Stream.empty();
    }
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) Answer(ai.grakn.graql.admin.Answer) CommonUtil.optionalOr(ai.grakn.util.CommonUtil.optionalOr) Match(ai.grakn.graql.Match) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) Answer(ai.grakn.graql.admin.Answer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Conjunction(ai.grakn.graql.admin.Conjunction) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery)

Aggregations

GraqlQueryException (ai.grakn.exception.GraqlQueryException)6 GraknTx (ai.grakn.GraknTx)3 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)2 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 TxRequest (ai.grakn.rpc.generated.GrpcGrakn.TxRequest)2 Stream (java.util.stream.Stream)2 Test (org.junit.Test)2 ConceptId (ai.grakn.concept.ConceptId)1 Relationship (ai.grakn.concept.Relationship)1 RelationshipType (ai.grakn.concept.RelationshipType)1 Role (ai.grakn.concept.Role)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Type (ai.grakn.concept.Type)1 Match (ai.grakn.graql.Match)1 Var (ai.grakn.graql.Var)1 Answer (ai.grakn.graql.admin.Answer)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 EquivalentFragmentSet (ai.grakn.graql.internal.gremlin.EquivalentFragmentSet)1 GraqlTraversal (ai.grakn.graql.internal.gremlin.GraqlTraversal)1