Search in sources :

Example 26 with SchemaConcept

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

the class EmbeddedGraknTx method getSchemaConcept.

private <T extends SchemaConcept> T getSchemaConcept(Label label, Schema.BaseType baseType) {
    // Makes sure the graph is open
    operateOnOpenGraph(() -> null);
    SchemaConcept schemaConcept = buildSchemaConcept(label, () -> getSchemaConcept(convertToId(label)));
    return validateSchemaConcept(schemaConcept, baseType, () -> null);
}
Also used : SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 27 with SchemaConcept

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

the class ConceptMethods method fromGrpc.

public static ConceptMethod<?> fromGrpc(GrpcConceptConverter converter, GrpcConcept.ConceptMethod conceptMethod) {
    Role[] roles;
    switch(conceptMethod.getConceptMethodCase()) {
        case GETVALUE:
            return GET_VALUE;
        case GETDATATYPEOFTYPE:
            return GET_DATA_TYPE_OF_TYPE;
        case GETDATATYPEOFATTRIBUTE:
            return GET_DATA_TYPE_OF_ATTRIBUTE;
        case GETLABEL:
            return GET_LABEL;
        case SETLABEL:
            return setLabel(convert(conceptMethod.getSetLabel()));
        case ISIMPLICIT:
            return IS_IMPLICIT;
        case ISINFERRED:
            return IS_INFERRED;
        case ISABSTRACT:
            return IS_ABSTRACT;
        case GETWHEN:
            return GET_WHEN;
        case GETTHEN:
            return GET_THEN;
        case GETREGEX:
            return GET_REGEX;
        case GETROLEPLAYERS:
            return GET_ROLE_PLAYERS;
        case GETATTRIBUTETYPES:
            return GET_ATTRIBUTE_TYPES;
        case SETATTRIBUTETYPE:
            return setAttributeType(converter.convert(conceptMethod.getSetAttributeType()).asAttributeType());
        case UNSETATTRIBUTETYPE:
            return unsetAttributeType(converter.convert(conceptMethod.getUnsetAttributeType()).asAttributeType());
        case GETKEYTYPES:
            return GET_KEY_TYPES;
        case GETDIRECTTYPE:
            return GET_DIRECT_TYPE;
        case GETDIRECTSUPERCONCEPT:
            return GET_DIRECT_SUPER;
        case SETDIRECTSUPERCONCEPT:
            GrpcConcept.Concept setDirectSuperConcept = conceptMethod.getSetDirectSuperConcept();
            SchemaConcept schemaConcept = converter.convert(setDirectSuperConcept).asSchemaConcept();
            return setDirectSuperConcept(schemaConcept);
        case UNSETROLEPLAYER:
            return removeRolePlayer(converter.convert(conceptMethod.getUnsetRolePlayer()));
        case DELETE:
            return DELETE;
        case GETATTRIBUTE:
            return getAttribute(convert(conceptMethod.getGetAttribute()));
        case GETOWNERS:
            return GET_OWNERS;
        case GETTYPESTHATPLAYROLE:
            return GET_TYPES_THAT_PLAY_ROLE;
        case GETROLESPLAYEDBYTYPE:
            return GET_ROLES_PLAYED_BY_TYPE;
        case GETINSTANCES:
            return GET_INSTANCES;
        case GETRELATEDROLES:
            return GET_RELATED_ROLES;
        case GETATTRIBUTES:
            return GET_ATTRIBUTES;
        case GETSUPERCONCEPTS:
            return GET_SUPER_CONCEPTS;
        case GETRELATIONSHIPTYPESTHATRELATEROLE:
            return GET_RELATIONSHIP_TYPES_THAT_RELATE_ROLE;
        case GETATTRIBUTESBYTYPES:
            GrpcConcept.Concepts getAttributeTypes = conceptMethod.getGetAttributesByTypes();
            AttributeType<?>[] attributeTypes = convert(converter, getAttributeTypes).toArray(AttributeType[]::new);
            return getAttributesByTypes(attributeTypes);
        case GETRELATIONSHIPS:
            return GET_RELATIONSHIPS;
        case GETSUBCONCEPTS:
            return GET_SUB_CONCEPTS;
        case GETRELATIONSHIPSBYROLES:
            roles = convert(converter, conceptMethod.getGetRelationshipsByRoles()).toArray(Role[]::new);
            return getRelationshipsByRoles(roles);
        case GETROLESPLAYEDBYTHING:
            return GET_ROLES_PLAYED_BY_THING;
        case GETKEYS:
            return GET_KEYS;
        case GETKEYSBYTYPES:
            GrpcConcept.Concepts getKeyTypes = conceptMethod.getGetAttributesByTypes();
            AttributeType<?>[] keyTypes = convert(converter, getKeyTypes).toArray(AttributeType[]::new);
            return getKeysByTypes(keyTypes);
        case GETROLEPLAYERSBYROLES:
            roles = convert(converter, conceptMethod.getGetRolePlayersByRoles()).toArray(Role[]::new);
            return getRolePlayersByRoles(roles);
        case SETKEYTYPE:
            return setKeyType(converter.convert(conceptMethod.getSetKeyType()).asAttributeType());
        case UNSETKEYTYPE:
            return unsetKeyType(converter.convert(conceptMethod.getUnsetKeyType()).asAttributeType());
        case SETABSTRACT:
            return setAbstract(conceptMethod.getSetAbstract());
        case SETROLEPLAYEDBYTYPE:
            return setRolePlayedByType(converter.convert(conceptMethod.getSetRolePlayedByType()).asRole());
        case UNSETROLEPLAYEDBYTYPE:
            return unsetRolePlayedByType(converter.convert(conceptMethod.getUnsetRolePlayedByType()).asRole());
        case ADDENTITY:
            return ADD_ENTITY;
        case SETRELATEDROLE:
            return setRelatedRole(converter.convert(conceptMethod.getSetRelatedRole()).asRole());
        case UNSETRELATEDROLE:
            return unsetRelatedRole(converter.convert(conceptMethod.getUnsetRelatedRole()).asRole());
        case PUTATTRIBUTE:
            return putAttribute(convert(conceptMethod.getPutAttribute()));
        case SETREGEX:
            return setRegex(convert(conceptMethod.getSetRegex()));
        case SETATTRIBUTE:
            return setAttribute(converter.convert(conceptMethod.getSetAttribute()).asAttribute());
        case UNSETATTRIBUTE:
            return unsetAttribute(converter.convert(conceptMethod.getUnsetAttribute()).asAttribute());
        case ADDRELATIONSHIP:
            return ADD_RELATIONSHIP;
        case SETROLEPLAYER:
            return setRolePlayer(converter.convert(conceptMethod.getSetRolePlayer()));
        default:
        case CONCEPTMETHOD_NOT_SET:
            throw new IllegalArgumentException("Unrecognised " + conceptMethod);
    }
}
Also used : Role(ai.grakn.concept.Role) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) AttributeType(ai.grakn.concept.AttributeType) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 28 with SchemaConcept

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

the class TxCache method cacheConcept.

/**
 * Caches a concept so it does not have to be rebuilt later.
 *
 * @param concept The concept to be cached.
 */
public void cacheConcept(Concept concept) {
    conceptCache.put(concept.getId(), concept);
    if (concept.isSchemaConcept()) {
        SchemaConcept schemaConcept = concept.asSchemaConcept();
        schemaConceptCache.put(schemaConcept.getLabel(), schemaConcept);
        labelCache.put(schemaConcept.getLabel(), schemaConcept.getLabelId());
    }
}
Also used : SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 29 with SchemaConcept

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

the class MatchTest method testGraqlPlaysSemanticsMatchGraphAPI.

@Test
public void testGraqlPlaysSemanticsMatchGraphAPI() {
    GraknTx tx = emptyKB.tx();
    QueryBuilder qb = tx.graql();
    Label a = Label.of("a");
    Label b = Label.of("b");
    Label c = Label.of("c");
    Label d = Label.of("d");
    Label e = Label.of("e");
    Label f = Label.of("f");
    qb.define(Graql.label(c).sub(Graql.label(b).sub(Graql.label(a).sub("entity"))), Graql.label(f).sub(Graql.label(e).sub(Graql.label(d).sub("role"))), Graql.label(b).plays(Graql.label(e))).execute();
    Stream.of(a, b, c, d, e, f).forEach(type -> {
        Set<Concept> graqlPlays = qb.match(Graql.label(type).plays(x)).get(x).collect(Collectors.toSet());
        Collection<Role> graphAPIPlays;
        SchemaConcept schemaConcept = tx.getSchemaConcept(type);
        if (schemaConcept.isType()) {
            graphAPIPlays = schemaConcept.asType().plays().collect(toSet());
        } else {
            graphAPIPlays = Collections.EMPTY_SET;
        }
        assertEquals(graqlPlays, graphAPIPlays);
    });
    Stream.of(d, e, f).forEach(type -> {
        Set<Concept> graqlPlayedBy = qb.match(x.plays(Graql.label(type))).get(x).collect(toSet());
        Collection<Type> graphAPIPlayedBy = tx.<Role>getSchemaConcept(type).playedByTypes().collect(toSet());
        assertEquals(graqlPlayedBy, graphAPIPlayedBy);
    });
}
Also used : Concept(ai.grakn.concept.Concept) MatchableConcept(ai.grakn.matcher.MatchableConcept) SchemaConcept(ai.grakn.concept.SchemaConcept) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) AttributeType(ai.grakn.concept.AttributeType) GraknMatchers.hasType(ai.grakn.matcher.GraknMatchers.hasType) Type(ai.grakn.concept.Type) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 30 with SchemaConcept

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

the class ConceptPropertyTest method assumeDeletable.

private static void assumeDeletable(GraknTx graph, Concept concept) {
    // TODO: A better way to handle these assumptions?
    if (concept.isSchemaConcept()) {
        SchemaConcept schemaConcept = concept.asSchemaConcept();
        assumeThat(schemaConcept.subs().collect(toSet()), contains(schemaConcept));
        if (schemaConcept.isType()) {
            Type type = schemaConcept.asType();
            assumeThat(type.instances().collect(toSet()), empty());
            assumeThat(type.getRulesOfHypothesis().collect(toSet()), empty());
            assumeThat(type.getRulesOfConclusion().collect(toSet()), empty());
        }
        if (schemaConcept.isRole()) {
            Role role = schemaConcept.asRole();
            assumeThat(role.playedByTypes().collect(toSet()), empty());
            assumeThat(role.relationshipTypes().collect(toSet()), empty());
            Stream<Relationship> allRelations = graph.admin().getMetaRelationType().instances();
            Set<Role> allRolesPlayed = allRelations.flatMap(relation -> relation.allRolePlayers().keySet().stream()).collect(toSet());
            assumeThat(allRolesPlayed, not(hasItem(role)));
        } else if (schemaConcept.isRelationshipType()) {
            assumeThat(schemaConcept.asRelationshipType().relates().collect(toSet()), empty());
        }
    }
}
Also used : Role(ai.grakn.concept.Role) FromTx(ai.grakn.generator.FromTxGenerator.FromTx) Assume.assumeThat(org.junit.Assume.assumeThat) Matchers.isA(org.hamcrest.Matchers.isA) Methods.mockParamsOf(ai.grakn.generator.Methods.mockParamsOf) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) MethodOf(ai.grakn.generator.Methods.MethodOf) RunWith(org.junit.runner.RunWith) Matchers.not(org.hamcrest.Matchers.not) Type(ai.grakn.concept.Type) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Open(ai.grakn.generator.GraknTxs.Open) NonMeta(ai.grakn.generator.AbstractSchemaConceptGenerator.NonMeta) JUnitQuickcheck(com.pholser.junit.quickcheck.runner.JUnitQuickcheck) GraknTx(ai.grakn.GraknTx) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) ExpectedException(org.junit.rules.ExpectedException) Method(java.lang.reflect.Method) Collectors.toSet(java.util.stream.Collectors.toSet) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) Matchers.empty(org.hamcrest.Matchers.empty) Property(com.pholser.junit.quickcheck.Property) Set(java.util.Set) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) InvocationTargetException(java.lang.reflect.InvocationTargetException) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Ignore(org.junit.Ignore) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertFalse(org.junit.Assert.assertFalse) GraknTxs.allConceptsFrom(ai.grakn.generator.GraknTxs.allConceptsFrom) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) Type(ai.grakn.concept.Type) Relationship(ai.grakn.concept.Relationship) 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