Search in sources :

Example 1 with SchemaConcept

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

the class SchemaConceptMapper method formatBase.

/**
 * Create a var with the information underlying all Types
 * @param schemaConcept type to be mapped
 * @return {@link VarPattern} containing basic information about the given type
 */
private static VarPattern formatBase(SchemaConcept schemaConcept) {
    VarPattern var = var().label(schemaConcept.getLabel());
    SchemaConcept superType = schemaConcept.sup();
    if (schemaConcept.sup() != null) {
        var = var.sub(Graql.label(superType.getLabel()));
    }
    if (schemaConcept.isType()) {
        Type type = schemaConcept.asType();
        var = plays(var, type);
        var = isAbstract(var, type);
    }
    return var;
}
Also used : AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Type(ai.grakn.concept.Type) VarPattern(ai.grakn.graql.VarPattern) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 2 with SchemaConcept

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

the class ConceptBuilder method putSchemaConcept.

private SchemaConcept putSchemaConcept() {
    SchemaConcept superConcept = use(SUPER_CONCEPT);
    Label label = use(LABEL);
    SchemaConcept concept;
    if (superConcept.isEntityType()) {
        concept = executor.tx().putEntityType(label);
    } else if (superConcept.isRelationshipType()) {
        concept = executor.tx().putRelationshipType(label);
    } else if (superConcept.isRole()) {
        concept = executor.tx().putRole(label);
    } else if (superConcept.isAttributeType()) {
        AttributeType attributeType = superConcept.asAttributeType();
        AttributeType.DataType<?> dataType = useOrDefault(DATA_TYPE, attributeType.getDataType());
        concept = executor.tx().putAttributeType(label, dataType);
    } else if (superConcept.isRule()) {
        concept = executor.tx().putRule(label, use(WHEN), use(THEN));
    } else {
        throw GraqlQueryException.insertMetaType(label, superConcept);
    }
    setSuper(concept, superConcept);
    return concept;
}
Also used : AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 3 with SchemaConcept

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

the class InferenceRule method propagateConstraints.

/**
 * @param parentAtom atom containing constraints (parent)
 * @param unifier unifier unifying parent with the rule
 * @return rule with propagated constraints from parent
 */
public InferenceRule propagateConstraints(Atom parentAtom, Unifier unifier) {
    if (!parentAtom.isRelation() && !parentAtom.isResource())
        return this;
    Atom headAtom = head.getAtom();
    Set<Atomic> bodyAtoms = new HashSet<>(body.getAtoms());
    // transfer value predicates
    parentAtom.getPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).forEach(bodyAtoms::add);
    // if head is a resource merge vps into head
    if (headAtom.isResource() && ((ResourceAtom) headAtom).getMultiPredicate().isEmpty()) {
        ResourceAtom resourceHead = (ResourceAtom) headAtom;
        Set<ValuePredicate> innerVps = parentAtom.getInnerPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).peek(bodyAtoms::add).collect(toSet());
        headAtom = ResourceAtom.create(headAtom.getPattern(), headAtom.getPredicateVariable(), resourceHead.getRelationVariable(), resourceHead.getTypeId(), innerVps, headAtom.getParentQuery());
    }
    Set<TypeAtom> unifiedTypes = parentAtom.getTypeConstraints().flatMap(type -> type.unify(unifier).stream()).collect(toSet());
    // set rule body types to sub types of combined query+rule types
    Set<TypeAtom> ruleTypes = body.getAtoms(TypeAtom.class).filter(t -> !t.isRelation()).collect(toSet());
    Set<TypeAtom> allTypes = Sets.union(unifiedTypes, ruleTypes);
    allTypes.stream().filter(ta -> {
        SchemaConcept schemaConcept = ta.getSchemaConcept();
        SchemaConcept subType = allTypes.stream().map(Atom::getSchemaConcept).filter(Objects::nonNull).filter(t -> ReasonerUtils.supers(t).contains(schemaConcept)).findFirst().orElse(null);
        return schemaConcept == null || subType == null;
    }).forEach(t -> bodyAtoms.add(t.copy(body)));
    return new InferenceRule(ReasonerQueries.atomic(headAtom), ReasonerQueries.create(bodyAtoms, tx), ruleId, tx);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) Rule(ai.grakn.concept.Rule) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Conjunction(ai.grakn.graql.admin.Conjunction) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Atomic(ai.grakn.graql.admin.Atomic) SchemaConcept(ai.grakn.concept.SchemaConcept) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Objects(java.util.Objects) HashSet(java.util.HashSet)

Example 4 with SchemaConcept

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

the class ReasonerUtils method supers.

/**
 * @param schemaConcept input type
 * @return set of all non-meta super types of the role
 */
public static Set<SchemaConcept> supers(SchemaConcept schemaConcept) {
    Set<SchemaConcept> superTypes = new HashSet<>();
    SchemaConcept superType = schemaConcept.sup();
    while (superType != null && !Schema.MetaSchema.isMetaLabel(superType.getLabel())) {
        superTypes.add(superType);
        superType = superType.sup();
    }
    return superTypes;
}
Also used : SchemaConcept(ai.grakn.concept.SchemaConcept) HashSet(java.util.HashSet)

Example 5 with SchemaConcept

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

the class ResourceAtom method toRelationshipAtom.

@Override
public RelationshipAtom toRelationshipAtom() {
    SchemaConcept type = getSchemaConcept();
    if (type == null)
        throw GraqlQueryException.illegalAtomConversion(this);
    GraknTx tx = getParentQuery().tx();
    Label typeLabel = Schema.ImplicitType.HAS.getLabel(type.getLabel());
    return RelationshipAtom.create(Graql.var().rel(Schema.ImplicitType.HAS_OWNER.getLabel(type.getLabel()).getValue(), getVarName()).rel(Schema.ImplicitType.HAS_VALUE.getLabel(type.getLabel()).getValue(), getPredicateVariable()).isa(typeLabel.getValue()).admin(), getPredicateVariable(), tx.getSchemaConcept(typeLabel).getId(), getParentQuery());
}
Also used : GraknTx(ai.grakn.GraknTx) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept)

Aggregations

SchemaConcept (ai.grakn.concept.SchemaConcept)51 Label (ai.grakn.concept.Label)19 Set (java.util.Set)15 Type (ai.grakn.concept.Type)14 GraknTx (ai.grakn.GraknTx)12 HashSet (java.util.HashSet)12 ConceptId (ai.grakn.concept.ConceptId)11 Stream (java.util.stream.Stream)11 Test (org.junit.Test)11 AttributeType (ai.grakn.concept.AttributeType)10 Property (com.pholser.junit.quickcheck.Property)10 Role (ai.grakn.concept.Role)9 Concept (ai.grakn.concept.Concept)8 Sets (com.google.common.collect.Sets)8 Optional (java.util.Optional)8 RelationshipType (ai.grakn.concept.RelationshipType)7 Rule (ai.grakn.concept.Rule)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)7 ErrorMessage (ai.grakn.util.ErrorMessage)7 Var (ai.grakn.graql.Var)6