Search in sources :

Example 31 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class PatternPropertyTests method patternsWithDifferentVariablesAndTheSameProperties_ShouldNotBeEquivalent.

@Property
public void patternsWithDifferentVariablesAndTheSameProperties_ShouldNotBeEquivalent(Var var1, Var var2, Set<VarProperty> properties) {
    assumeThat(var1, not(is(var2)));
    VarPatternAdmin varPattern1 = Patterns.varPattern(var1, properties);
    VarPatternAdmin varPattern2 = Patterns.varPattern(var2, properties);
    assertNotEquals(varPattern1, varPattern2);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Property(com.pholser.junit.quickcheck.Property) VarProperty(ai.grakn.graql.admin.VarProperty)

Example 32 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin 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)

Example 33 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class RelatesProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    Var varName = var.var().asUserDefined();
    VarPatternAdmin roleVar = this.role();
    Var roleVariable = roleVar.var().asUserDefined();
    IdPredicate predicate = getIdPredicate(roleVariable, roleVar, vars, parent);
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    return RelatesAtom.create(varName, roleVariable, predicateId, parent);
}
Also used : IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId)

Example 34 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class HasAttributeTypeProperty method of.

/**
 * @throws GraqlQueryException if no label is specified on {@code resourceType}
 */
public static HasAttributeTypeProperty of(VarPatternAdmin resourceType, boolean required) {
    Label resourceLabel = resourceType.getTypeLabel().orElseThrow(() -> GraqlQueryException.noLabelSpecifiedForHas(resourceType));
    VarPattern role = Graql.label(Schema.MetaSchema.ROLE.getLabel());
    VarPatternAdmin ownerRole = var().sub(role).admin();
    VarPatternAdmin valueRole = var().sub(role).admin();
    VarPattern relationType = var().sub(Graql.label(Schema.MetaSchema.RELATIONSHIP.getLabel()));
    // If a key, limit only to the implicit key type
    if (required) {
        ownerRole = ownerRole.label(KEY_OWNER.getLabel(resourceLabel)).admin();
        valueRole = valueRole.label(KEY_VALUE.getLabel(resourceLabel)).admin();
        relationType = relationType.label(KEY.getLabel(resourceLabel));
    }
    VarPatternAdmin relationOwner = relationType.relates(ownerRole).admin();
    VarPatternAdmin relationValue = relationType.admin().var().relates(valueRole).admin();
    return new AutoValue_HasAttributeTypeProperty(resourceType, ownerRole, valueRole, relationOwner, relationValue, required);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Label(ai.grakn.concept.Label) VarPattern(ai.grakn.graql.VarPattern)

Example 35 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class HasAttributeTypeProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // NB: HasResourceType is a special case and it doesn't allow variables as resource types
    Var varName = var.var().asUserDefined();
    Label label = this.resourceType().getTypeLabel().orElse(null);
    Var predicateVar = var().asUserDefined();
    SchemaConcept schemaConcept = parent.tx().getSchemaConcept(label);
    ConceptId predicateId = schemaConcept != null ? schemaConcept.getId() : null;
    // isa part
    VarPatternAdmin resVar = varName.has(Graql.label(label)).admin();
    return HasAtom.create(resVar, predicateVar, predicateId, parent);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) ConceptId(ai.grakn.concept.ConceptId)

Aggregations

VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)55 Test (org.junit.Test)20 Var (ai.grakn.graql.Var)17 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)16 Unifier (ai.grakn.graql.admin.Unifier)15 ConceptId (ai.grakn.concept.ConceptId)13 Label (ai.grakn.concept.Label)13 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)13 Role (ai.grakn.concept.Role)11 GraknTx (ai.grakn.GraknTx)10 VarProperty (ai.grakn.graql.admin.VarProperty)10 Collection (java.util.Collection)10 Set (java.util.Set)10 Stream (java.util.stream.Stream)10 SchemaConcept (ai.grakn.concept.SchemaConcept)9 GraqlQueryException (ai.grakn.exception.GraqlQueryException)9 VarPattern (ai.grakn.graql.VarPattern)9 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)9 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)9