Search in sources :

Example 81 with Label

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

the class ConceptController method getSchemaByLabel.

private String getSchemaByLabel(Request request, Response response) throws JsonProcessingException {
    Requests.validateRequest(request, APPLICATION_ALL, APPLICATION_JSON);
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    Label label = Label.of(mandatoryPathParameter(request, LABEL_PARAMETER));
    return getConcept(response, keyspace, (tx) -> tx.getSchemaConcept(label));
}
Also used : Keyspace(ai.grakn.Keyspace) Label(ai.grakn.concept.Label)

Example 82 with Label

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

the class ValidateGlobalRules method validateInstancePlaysAllRequiredRoles.

/**
 * @param thing The thing to be validated
 * @return An error message if the thing does not have all the required resources
 */
static Optional<String> validateInstancePlaysAllRequiredRoles(Thing thing) {
    TypeImpl<?, ?> currentConcept = (TypeImpl) thing.type();
    while (currentConcept != null) {
        Map<Role, Boolean> plays = currentConcept.directPlays();
        for (Map.Entry<Role, Boolean> playsEntry : plays.entrySet()) {
            if (playsEntry.getValue()) {
                Role role = playsEntry.getKey();
                // Assert there is a relationship for this type
                Stream<Relationship> relationships = thing.relationships(role);
                if (!CommonUtil.containsOnly(relationships, 1)) {
                    Label resourceTypeLabel = Schema.ImplicitType.explicitLabel(role.getLabel());
                    return Optional.of(VALIDATION_NOT_EXACTLY_ONE_KEY.getMessage(thing.getId(), resourceTypeLabel));
                }
            }
        }
        currentConcept = (TypeImpl) currentConcept.sup();
    }
    return Optional.empty();
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) Map(java.util.Map) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl)

Example 83 with Label

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

the class GlobalCache method populateSchemaTxCache.

void populateSchemaTxCache(TxCache txCache) {
    try {
        lock.writeLock().lock();
        Map<Label, SchemaConcept> cachedSchemaSnapshot = getCachedTypes();
        Map<Label, LabelId> cachedLabelsSnapshot = getCachedLabels();
        // Read central cache into txCache cloning only base concepts. Sets clones later
        for (SchemaConcept type : cachedSchemaSnapshot.values()) {
            txCache.cacheConcept(type);
        }
        // Load Labels Separately. We do this because the TypeCache may have expired.
        cachedLabelsSnapshot.forEach(txCache::cacheLabel);
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) LabelId(ai.grakn.concept.LabelId)

Example 84 with Label

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

the class ConceptControllerTest method whenCallingRoleEndpointAndRequestingJSON_ReturnJSON.

@Test
public void whenCallingRoleEndpointAndRequestingJSON_ReturnJSON() {
    Label label = MetaSchema.ROLE.getLabel();
    given().accept(ContentType.JSON).pathParam("keyspace", keyspace.getValue()).pathParam("label", label.getValue()).when().get("/kb/{keyspace}/role/{label}").then().statusCode(SC_OK).contentType(ContentType.JSON);
}
Also used : Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 85 with Label

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

the class DefineQueryTest method testDefineReferenceByName.

@Test
public void testDefineReferenceByName() {
    String roleTypeLabel = HAS_OWNER.getLabel("title").getValue();
    qb.define(label("new-type").sub(Schema.MetaSchema.ENTITY.getLabel().getValue()), label("new-type").plays(roleTypeLabel)).execute();
    qb.insert(var("x").isa("new-type")).execute();
    Match typeQuery = qb.match(var("n").label("new-type"));
    assertEquals(1, typeQuery.stream().count());
    // We checked count ahead of time
    // noinspection OptionalGetWithoutIsPresent
    EntityType newType = typeQuery.get("n").findFirst().get().asEntityType();
    assertTrue(newType.plays().anyMatch(role -> role.equals(movies.tx().getRole(roleTypeLabel))));
    assertExists(qb, var().isa("new-type"));
}
Also used : EntityType(ai.grakn.concept.EntityType) VarPattern(ai.grakn.graql.VarPattern) BOOLEAN(ai.grakn.concept.AttributeType.DataType.BOOLEAN) KEY_VALUE(ai.grakn.util.Schema.ImplicitType.KEY_VALUE) Graql(ai.grakn.graql.Graql) RELATIONSHIP(ai.grakn.util.Schema.MetaSchema.RELATIONSHIP) EntityType(ai.grakn.concept.EntityType) GraqlTestUtil.assertExists(ai.grakn.util.GraqlTestUtil.assertExists) ENTITY(ai.grakn.util.Schema.MetaSchema.ENTITY) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) After(org.junit.After) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Match(ai.grakn.graql.Match) KEY(ai.grakn.util.Schema.ImplicitType.KEY) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Var(ai.grakn.graql.Var) DefineQuery(ai.grakn.graql.DefineQuery) Matchers.is(org.hamcrest.Matchers.is) Matchers.anyOf(org.hamcrest.Matchers.anyOf) RULE(ai.grakn.util.Schema.MetaSchema.RULE) ValueProperty(ai.grakn.graql.internal.pattern.property.ValueProperty) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Role(ai.grakn.concept.Role) Answer(ai.grakn.graql.admin.Answer) ROLE(ai.grakn.util.Schema.MetaSchema.ROLE) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) AttributeType(ai.grakn.concept.AttributeType) CoreMatchers.allOf(org.hamcrest.CoreMatchers.allOf) ExpectedException(org.junit.rules.ExpectedException) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) KEY_OWNER(ai.grakn.util.Schema.ImplicitType.KEY_OWNER) Before(org.junit.Before) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.hasItemInArray(org.hamcrest.Matchers.hasItemInArray) Graql.label(ai.grakn.graql.Graql.label) MovieKB(ai.grakn.test.kbs.MovieKB) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Rule(org.junit.Rule) Graql.parse(ai.grakn.graql.Graql.parse) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty) GraqlTestUtil.assertNotExists(ai.grakn.util.GraqlTestUtil.assertNotExists) GraknException(ai.grakn.exception.GraknException) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Aggregations

Label (ai.grakn.concept.Label)87 Test (org.junit.Test)41 Role (ai.grakn.concept.Role)25 GraknTx (ai.grakn.GraknTx)22 ConceptId (ai.grakn.concept.ConceptId)21 Concept (ai.grakn.concept.Concept)20 Set (java.util.Set)20 RelationshipType (ai.grakn.concept.RelationshipType)19 SchemaConcept (ai.grakn.concept.SchemaConcept)19 AttributeType (ai.grakn.concept.AttributeType)17 EntityType (ai.grakn.concept.EntityType)17 Relationship (ai.grakn.concept.Relationship)14 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 Type (ai.grakn.concept.Type)12 Stream (java.util.stream.Stream)12 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)11 Schema (ai.grakn.util.Schema)11 Collection (java.util.Collection)11 Pattern (ai.grakn.graql.Pattern)10