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