Search in sources :

Example 16 with SchemaConcept

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

the class LabelFragmentSet method tryExpandSubs.

/**
 * Expand a {@link LabelFragmentSet} to match all sub-concepts of the single existing {@link Label}.
 *
 * Returns null if there is not exactly one label any of the {@link Label}s mentioned are not in the knowledge base.
 */
@Nullable
LabelFragmentSet tryExpandSubs(Var typeVar, GraknTx tx) {
    if (labels().size() != 1)
        return null;
    Label oldLabel = Iterables.getOnlyElement(labels());
    SchemaConcept concept = tx.getSchemaConcept(oldLabel);
    if (concept == null)
        return null;
    Set<Label> newLabels = concept.subs().map(SchemaConcept::getLabel).collect(toSet());
    return new AutoValue_LabelFragmentSet(varProperty(), typeVar, ImmutableSet.copyOf(newLabels));
}
Also used : Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) Nullable(javax.annotation.Nullable)

Example 17 with SchemaConcept

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

the class SchemaConceptImpl method superLoops.

private boolean superLoops() {
    // Check For Loop
    HashSet<SchemaConcept> foundTypes = new HashSet<>();
    SchemaConcept currentSuperType = sup();
    while (currentSuperType != null) {
        foundTypes.add(currentSuperType);
        currentSuperType = currentSuperType.sup();
        if (foundTypes.contains(currentSuperType)) {
            return true;
        }
    }
    return false;
}
Also used : SchemaConcept(ai.grakn.concept.SchemaConcept) HashSet(java.util.HashSet)

Example 18 with SchemaConcept

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

the class GraknTxTest method checkThatMainCentralCacheIsNotAffectedByTransactionModifications.

@Test
public void checkThatMainCentralCacheIsNotAffectedByTransactionModifications() throws InvalidKBException, ExecutionException, InterruptedException {
    // Check Central cache is empty
    assertCacheOnlyContainsMetaTypes();
    Role r1 = tx.putRole("r1");
    Role r2 = tx.putRole("r2");
    EntityType e1 = tx.putEntityType("e1").plays(r1).plays(r2);
    RelationshipType rel1 = tx.putRelationshipType("rel1").relates(r1).relates(r2);
    // Purge the above concepts into the main cache
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    // Check cache is in good order
    Collection<SchemaConcept> cachedValues = tx.getGlobalCache().getCachedTypes().values();
    assertTrue("Type [" + r1 + "] was not cached", cachedValues.contains(r1));
    assertTrue("Type [" + r2 + "] was not cached", cachedValues.contains(r2));
    assertTrue("Type [" + e1 + "] was not cached", cachedValues.contains(e1));
    assertTrue("Type [" + rel1 + "] was not cached", cachedValues.contains(rel1));
    assertThat(e1.plays().collect(toSet()), containsInAnyOrder(r1, r2));
    ExecutorService pool = Executors.newSingleThreadExecutor();
    // Mutate Schema in a separate thread
    pool.submit(() -> {
        GraknTx innerGraph = Grakn.session(Grakn.IN_MEMORY, tx.keyspace()).open(GraknTxType.WRITE);
        EntityType entityType = innerGraph.getEntityType("e1");
        Role role = innerGraph.getRole("r1");
        entityType.deletePlays(role);
    }).get();
    // Check the above mutation did not affect central repo
    SchemaConcept foundE1 = tx.getGlobalCache().getCachedTypes().get(e1.getLabel());
    assertTrue("Main cache was affected by transaction", foundE1.asType().plays().anyMatch(role -> role.equals(r1)));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) CoreMatchers.is(org.hamcrest.CoreMatchers.is) InvalidKBException(ai.grakn.exception.InvalidKBException) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) Entity(ai.grakn.concept.Entity) Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ExecutorService(java.util.concurrent.ExecutorService) Collectors.toSet(java.util.stream.Collectors.toSet) Grakn(ai.grakn.Grakn) IsInstanceOf(org.hamcrest.core.IsInstanceOf) GraknTxType(ai.grakn.GraknTxType) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) Matchers.empty(org.hamcrest.Matchers.empty) VerificationException(org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.VerificationException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Shard(ai.grakn.kb.internal.structure.Shard) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Stream(java.util.stream.Stream) Assert.assertNull(org.junit.Assert.assertNull) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) RelationshipType(ai.grakn.concept.RelationshipType) ExecutorService(java.util.concurrent.ExecutorService) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Example 19 with SchemaConcept

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

the class GrpcServerIT method whenGettingASchemaConcept_TheInformationOnTheSchemaConceptIsCorrect.

@Test
public void whenGettingASchemaConcept_TheInformationOnTheSchemaConceptIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").label("actor")).get();
        SchemaConcept remoteConcept = query.stream().findAny().get().get("x").asSchemaConcept();
        SchemaConcept localConcept = localTx.getConcept(remoteConcept.getId()).asSchemaConcept();
        assertEquals(localConcept.isImplicit(), remoteConcept.isImplicit());
        assertEquals(localConcept.getLabel(), remoteConcept.getLabel());
        assertEquals(localConcept.sup().getId(), remoteConcept.sup().getId());
        assertEqualConcepts(localConcept, remoteConcept, SchemaConcept::sups);
        assertEqualConcepts(localConcept, remoteConcept, SchemaConcept::subs);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Example 20 with SchemaConcept

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

the class GrpcServerIT method whenDeletingAConcept_TheConceptIsDeleted.

@Test
public void whenDeletingAConcept_TheConceptIsDeleted() {
    Label label = Label.of("hello");
    try (GraknTx tx = localSession.open(GraknTxType.WRITE)) {
        tx.putEntityType(label);
        tx.commit();
    }
    try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
        SchemaConcept schemaConcept = tx.getSchemaConcept(label);
        assertFalse(schemaConcept.isDeleted());
        schemaConcept.delete();
        assertTrue(schemaConcept.isDeleted());
        tx.commit();
    }
    try (GraknTx tx = localSession.open(GraknTxType.WRITE)) {
        assertNull(tx.getSchemaConcept(label));
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

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