Search in sources :

Example 46 with ConceptId

use of ai.grakn.concept.ConceptId in project grakn by graknlabs.

the class IsaAtomBase method getTypedPair.

Pair<VarPattern, IdPredicate> getTypedPair(SchemaConcept type) {
    ConceptId typeId = type.getId();
    Var typeVariable = getPredicateVariable().getValue().isEmpty() ? Graql.var().asUserDefined() : getPredicateVariable();
    IdPredicate newPredicate = IdPredicate.create(typeVariable.id(typeId).admin(), getParentQuery());
    return new Pair<>(getPattern(), newPredicate);
}
Also used : IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 47 with ConceptId

use of ai.grakn.concept.ConceptId in project grakn by graknlabs.

the class ReasonerQueryImpl method idTransform.

/**
 * returns id transform that would convert this query to a query alpha-equivalent to the query,
 * provided they are structurally equivalent
 * @param query for which the transform is to be constructed
 * @param unifier between this query and provided query
 * @return id transform
 */
public Map<Var, ConceptId> idTransform(ReasonerQueryImpl query, Unifier unifier) {
    Map<Var, ConceptId> transform = new HashMap<>();
    this.getAtoms(IdPredicate.class).forEach(thisP -> {
        Collection<Var> vars = unifier.get(thisP.getVarName());
        Var var = !vars.isEmpty() ? Iterators.getOnlyElement(vars.iterator()) : thisP.getVarName();
        IdPredicate p2 = query.getIdPredicate(var);
        if (p2 != null)
            transform.put(thisP.getVarName(), p2.getPredicate());
    });
    return transform;
}
Also used : IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId)

Example 48 with ConceptId

use of ai.grakn.concept.ConceptId in project grakn by graknlabs.

the class StructuralCache method get.

/**
 * @param query to be retrieved
 * @return answer stream of provided query
 */
public Stream<Answer> get(Q query) {
    Equivalence.Wrapper<Q> structQuery = equivalence.wrap(query);
    EmbeddedGraknTx<?> tx = query.tx();
    CacheEntry<Q, GraqlTraversal> match = structCache.get(structQuery);
    if (match != null) {
        Q equivalentQuery = match.query();
        GraqlTraversal traversal = match.cachedElement();
        Unifier unifier = equivalentQuery.getMultiUnifier(query, UnifierType.STRUCTURAL).getAny();
        Map<Var, ConceptId> idTransform = equivalentQuery.idTransform(query, unifier);
        ReasonerQueryImpl transformedQuery = equivalentQuery.transformIds(idTransform);
        return MatchBase.streamWithTraversal(transformedQuery.getPattern().commonVars(), tx, traversal.transform(idTransform)).map(ans -> ans.unify(unifier)).map(a -> a.explain(new LookupExplanation(query)));
    }
    GraqlTraversal traversal = GreedyTraversalPlan.createTraversal(query.getPattern(), tx);
    structCache.put(structQuery, new CacheEntry<>(query, traversal));
    return MatchBase.streamWithTraversal(query.getPattern().commonVars(), tx, traversal).map(a -> a.explain(new LookupExplanation(query)));
}
Also used : UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Equivalence(com.google.common.base.Equivalence) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) MatchBase(ai.grakn.graql.internal.query.match.MatchBase) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Map(java.util.Map) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Unifier(ai.grakn.graql.admin.Unifier) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Equivalence(com.google.common.base.Equivalence) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Unifier(ai.grakn.graql.admin.Unifier) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 49 with ConceptId

use of ai.grakn.concept.ConceptId in project grakn by graknlabs.

the class TransitivityMatrixKB method buildExtensionalDB.

private void buildExtensionalDB(GraknTx graph, int n, int m) {
    Role qfrom = graph.getRole("Q-from");
    Role qto = graph.getRole("Q-to");
    EntityType aEntity = graph.getEntityType("a-entity");
    RelationshipType q = graph.getRelationshipType("Q");
    Thing aInst = putEntityWithResource(graph, "a", graph.getEntityType("entity2"), key);
    ConceptId[][] aInstanceIds = new ConceptId[n][m];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            aInstanceIds[i][j] = putEntityWithResource(graph, "a" + i + "," + j, aEntity, key).getId();
        }
    }
    q.addRelationship().addRolePlayer(qfrom, aInst).addRolePlayer(qto, graph.getConcept(aInstanceIds[0][0]));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i < n - 1) {
                q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i][j])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i + 1][j]));
            }
            if (j < m - 1) {
                q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i][j])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i][j + 1]));
            }
        }
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId)

Example 50 with ConceptId

use of ai.grakn.concept.ConceptId in project grakn by graknlabs.

the class LinearTransitivityMatrixKB method buildExtensionalDB.

private void buildExtensionalDB(GraknTx graph, int n, int m) {
    Role Qfrom = graph.getRole("Q-from");
    Role Qto = graph.getRole("Q-to");
    EntityType aEntity = graph.getEntityType("a-entity");
    RelationshipType Q = graph.getRelationshipType("Q");
    ConceptId[][] aInstancesIds = new ConceptId[n + 1][m + 1];
    Thing aInst = putEntityWithResource(graph, "a", graph.getEntityType("entity2"), key);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            aInstancesIds[i][j] = putEntityWithResource(graph, "a" + i + "," + j, aEntity, key).getId();
        }
    }
    Q.addRelationship().addRolePlayer(Qfrom, aInst).addRolePlayer(Qto, graph.getConcept(aInstancesIds[1][1]));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i < n) {
                Q.addRelationship().addRolePlayer(Qfrom, graph.getConcept(aInstancesIds[i][j])).addRolePlayer(Qto, graph.getConcept(aInstancesIds[i + 1][j]));
            }
            if (j < m) {
                Q.addRelationship().addRolePlayer(Qfrom, graph.getConcept(aInstancesIds[i][j])).addRolePlayer(Qto, graph.getConcept(aInstancesIds[i][j + 1]));
            }
        }
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId)

Aggregations

ConceptId (ai.grakn.concept.ConceptId)80 Test (org.junit.Test)55 Concept (ai.grakn.concept.Concept)23 Role (ai.grakn.concept.Role)22 RelationshipType (ai.grakn.concept.RelationshipType)19 GraknTx (ai.grakn.GraknTx)18 EntityType (ai.grakn.concept.EntityType)18 Label (ai.grakn.concept.Label)16 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)14 Var (ai.grakn.graql.Var)12 List (java.util.List)12 Entity (ai.grakn.concept.Entity)10 AttributeType (ai.grakn.concept.AttributeType)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 ClassRule (org.junit.ClassRule)9 GraknTxType (ai.grakn.GraknTxType)8 Keyspace (ai.grakn.Keyspace)8 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)8