Search in sources :

Example 1 with VarProperty

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

the class GraqlTraversalPlanner method planFromTraversal.

/**
 * @param atoms list of current atoms of interest
 * @param queryPattern corresponding pattern
 * @return an optimally ordered list of provided atoms
 */
static ImmutableList<Atom> planFromTraversal(List<Atom> atoms, PatternAdmin queryPattern, EmbeddedGraknTx<?> tx) {
    Multimap<VarProperty, Atom> propertyMap = HashMultimap.create();
    atoms.stream().filter(at -> !(at instanceof OntologicalAtom)).forEach(at -> at.getVarProperties().forEach(p -> propertyMap.put(p, at)));
    Set<VarProperty> properties = propertyMap.keySet();
    GraqlTraversal graqlTraversal = GreedyTraversalPlan.createTraversal(queryPattern, tx);
    ImmutableList<Fragment> fragments = graqlTraversal.fragments().iterator().next();
    ImmutableList.Builder<Atom> builder = ImmutableList.builder();
    builder.addAll(atoms.stream().filter(at -> at instanceof OntologicalAtom).iterator());
    builder.addAll(fragments.stream().map(Fragment::varProperty).filter(Objects::nonNull).filter(properties::contains).distinct().flatMap(p -> propertyMap.get(p).stream()).distinct().iterator());
    return builder.build();
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) HashMultimap(com.google.common.collect.HashMultimap) ImmutableList(com.google.common.collect.ImmutableList) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Nullable(javax.annotation.Nullable) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Comparator(java.util.Comparator) VarProperty(ai.grakn.graql.admin.VarProperty) ImmutableList(com.google.common.collect.ImmutableList) Objects(java.util.Objects) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom)

Example 2 with VarProperty

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

the class VarPatternImpl method toString.

@Override
public final String toString() {
    Collection<VarPatternAdmin> innerVars = innerVarPatterns();
    innerVars.remove(this);
    getProperties(HasAttributeProperty.class).map(HasAttributeProperty::attribute).flatMap(r -> r.innerVarPatterns().stream()).forEach(innerVars::remove);
    if (innerVars.stream().anyMatch(VarPatternImpl::invalidInnerVariable)) {
        LOG.warn("printing a query with inner variables, which is not supported in native Graql");
    }
    StringBuilder builder = new StringBuilder();
    String name = var().isUserDefinedName() ? var().toString() : "";
    builder.append(name);
    if (var().isUserDefinedName() && !properties().isEmpty()) {
        // Add a space after the var name
        builder.append(" ");
    }
    boolean first = true;
    for (VarProperty property : properties()) {
        if (!first) {
            builder.append(" ");
        }
        first = false;
        property.buildString(builder);
    }
    return builder.toString();
}
Also used : VarProperty(ai.grakn.graql.admin.VarProperty) Logger(org.slf4j.Logger) VarPattern(ai.grakn.graql.VarPattern) LabelProperty(ai.grakn.graql.internal.pattern.property.LabelProperty) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty) Collection(java.util.Collection) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) LoggerFactory(org.slf4j.LoggerFactory) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Set(java.util.Set) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) VarProperty(ai.grakn.graql.admin.VarProperty) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty)

Example 3 with VarProperty

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

the class DefineQueryPropertyTest method aDefineQueryWithoutASubOrPlaysOrRelatesProperty_CannotBeInserted.

@Ignore("Currently no error message is returned when trying to insert an empty set of propoerties. I am not entirely sure this is correct")
@Property
public void aDefineQueryWithoutASubOrPlaysOrRelatesProperty_CannotBeInserted(@Open GraknTx tx, @Size(max = 5) Set<VarProperty> properties) {
    boolean containsSub = properties.stream().anyMatch(SubProperty.class::isInstance);
    boolean containsPlays = properties.stream().anyMatch(PlaysProperty.class::isInstance);
    boolean containsRelates = properties.stream().anyMatch(RelatesProperty.class::isInstance);
    assumeFalse(containsSub || containsPlays || containsRelates);
    VarPatternAdmin pattern = Patterns.varPattern(Graql.var("x"), properties);
    exception.expect(GraqlQueryException.class);
    tx.graql().define(pattern).execute();
}
Also used : RelatesProperty(ai.grakn.graql.internal.pattern.property.RelatesProperty) SubProperty(ai.grakn.graql.internal.pattern.property.SubProperty) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) PlaysProperty(ai.grakn.graql.internal.pattern.property.PlaysProperty) Ignore(org.junit.Ignore) Property(com.pholser.junit.quickcheck.Property) SubProperty(ai.grakn.graql.internal.pattern.property.SubProperty) VarProperty(ai.grakn.graql.admin.VarProperty) PlaysProperty(ai.grakn.graql.internal.pattern.property.PlaysProperty) RelatesProperty(ai.grakn.graql.internal.pattern.property.RelatesProperty)

Example 4 with VarProperty

use of ai.grakn.graql.admin.VarProperty 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 5 with VarProperty

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

the class InsertQueryPropertyTest method anInsertQueryWithoutAnIsaProperty_CannotBeInserted.

@Ignore("Currently no error message is returned when trying to insert an empty set of propoerties. I am not entirely sure this is correct")
@Property
public void anInsertQueryWithoutAnIsaProperty_CannotBeInserted(@Open GraknTx tx, @Size(max = 5) Set<VarProperty> properties) {
    boolean containsIsa = properties.stream().anyMatch(IsaProperty.class::isInstance);
    assumeFalse(containsIsa);
    VarPatternAdmin pattern = Patterns.varPattern(Graql.var("x"), properties);
    exception.expect(GraqlQueryException.class);
    tx.graql().insert(pattern).execute();
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Ignore(org.junit.Ignore) Property(com.pholser.junit.quickcheck.Property) VarProperty(ai.grakn.graql.admin.VarProperty) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty)

Aggregations

VarProperty (ai.grakn.graql.admin.VarProperty)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)4 Property (com.pholser.junit.quickcheck.Property)3 Set (java.util.Set)2 Ignore (org.junit.Ignore)2 ConceptId (ai.grakn.concept.ConceptId)1 Var (ai.grakn.graql.Var)1 VarPattern (ai.grakn.graql.VarPattern)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)1 GraqlTraversal (ai.grakn.graql.internal.gremlin.GraqlTraversal)1 GreedyTraversalPlan (ai.grakn.graql.internal.gremlin.GreedyTraversalPlan)1 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)1 Patterns (ai.grakn.graql.internal.pattern.Patterns)1 HasAttributeProperty (ai.grakn.graql.internal.pattern.property.HasAttributeProperty)1 IsaProperty (ai.grakn.graql.internal.pattern.property.IsaProperty)1 LabelProperty (ai.grakn.graql.internal.pattern.property.LabelProperty)1 PlaysProperty (ai.grakn.graql.internal.pattern.property.PlaysProperty)1 RelatesProperty (ai.grakn.graql.internal.pattern.property.RelatesProperty)1