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);
}
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;
}
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)));
}
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]));
}
}
}
}
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]));
}
}
}
}
Aggregations