Search in sources :

Example 51 with ConceptId

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

the class MatchTest method testLookupResourcesOnId.

@Test
public void testLookupResourcesOnId() {
    Thing godfather = movieKB.tx().getAttributeType("title").getAttribute("Godfather").owner();
    ConceptId id = godfather.getId();
    Match query = qb.match(var().id(id).has("title", x));
    assertThat(query, variable(x, contains(hasValue("Godfather"))));
}
Also used : Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 52 with ConceptId

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

the class PostProcessingTest method whenCreatingDuplicateResources_EnsureTheyAreMergedInPost.

@Test
public void whenCreatingDuplicateResources_EnsureTheyAreMergedInPost() throws InvalidKBException, InterruptedException, JsonProcessingException {
    String value = "1";
    String sample = "Sample";
    // Create GraknTx With Duplicate Resources
    EmbeddedGraknTx<?> tx = session.open(GraknTxType.WRITE);
    AttributeType<String> attributeType = tx.putAttributeType(sample, AttributeType.DataType.STRING);
    Attribute<String> attribute = attributeType.putAttribute(value);
    tx.commitSubmitNoLogs();
    tx = session.open(GraknTxType.WRITE);
    assertEquals(1, attributeType.instances().count());
    // Check duplicates have been created
    Set<Vertex> resource1 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource2 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource3 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource4 = createDuplicateResource(tx, attributeType, attribute);
    assertEquals(5, attributeType.instances().count());
    // Attribute vertex index
    String resourceIndex = resource1.iterator().next().value(INDEX.name()).toString();
    // Merge the attribute sets
    Set<Vertex> merged = Sets.newHashSet();
    merged.addAll(resource1);
    merged.addAll(resource2);
    merged.addAll(resource3);
    merged.addAll(resource4);
    tx.close();
    // Casting sets as ConceptIds
    Set<ConceptId> resourceConcepts = merged.stream().map(c -> ConceptId.of(Schema.PREFIX_VERTEX + c.id().toString())).collect(toSet());
    // Create Commit Log
    CommitLog commitLog = CommitLog.createDefault(tx.keyspace());
    commitLog.attributes().put(resourceIndex, resourceConcepts);
    // Submit it
    postProcessor.submit(commitLog);
    // Force running the PP job
    engine.server().backgroundTaskRunner().tasks().forEach(BackgroundTask::run);
    Thread.sleep(2000);
    tx = session.open(GraknTxType.READ);
    // Check it's fixed
    assertEquals(1, tx.getAttributeType(sample).instances().count());
    tx.close();
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) CountPostProcessor(ai.grakn.engine.task.postprocessing.CountPostProcessor) SampleKBLoader(ai.grakn.util.SampleKBLoader) Keyspace(ai.grakn.Keyspace) Concept(ai.grakn.concept.Concept) PostProcessingTestUtils.createDuplicateResource(ai.grakn.test.engine.postprocessing.PostProcessingTestUtils.createDuplicateResource) EntityType(ai.grakn.concept.EntityType) GraknConfig(ai.grakn.engine.GraknConfig) Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) After(org.junit.After) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) RedisCountStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisCountStorage) INDEX(ai.grakn.util.Schema.VertexProperty.INDEX) Collectors.toSet(java.util.stream.Collectors.toSet) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) MetricRegistry(com.codahale.metrics.MetricRegistry) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Sets(com.google.common.collect.Sets) PostProcessor(ai.grakn.engine.task.postprocessing.PostProcessor) CommitLog(ai.grakn.kb.log.CommitLog) GraknConfigKey(ai.grakn.GraknConfigKey) IndexPostProcessor(ai.grakn.engine.task.postprocessing.IndexPostProcessor) BackgroundTask(ai.grakn.engine.task.BackgroundTask) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Assume.assumeTrue(org.junit.Assume.assumeTrue) RedisIndexStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisIndexStorage) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CommitLog(ai.grakn.kb.log.CommitLog) ConceptId(ai.grakn.concept.ConceptId) BackgroundTask(ai.grakn.engine.task.BackgroundTask) Test(org.junit.Test)

Example 53 with ConceptId

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

the class BenchmarkIT method testJoinRuleChain.

/**
 * Scalability test defined in terms of number of rules in the system. Creates a simple rule chain based on join operation on two relations:
 *
 * R_i(x, y) := R_1(x, z), R_{i-1}(z, y);    i e [2, N],
 *
 * initialised with a chain of N x base relation R_1instances.
 *
 * The rules are defined in such a way that relation R_j spans j hops of base relations and has (N + 1 - j) instances.
 * Consequently R_N has a single instance linking the first and last entities in the base relation chain.
 */
@Test
public void testJoinRuleChain() {
    final int N = 100;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    loadRuleChainData(N);
    try (GraknTx tx = session.open(GraknTxType.READ)) {
        ConceptId firstId = Iterables.getOnlyElement(tx.graql().<GetQuery>parse("match $x has index 'first';get;").execute()).get("x").getId();
        ConceptId lastId = Iterables.getOnlyElement(tx.graql().<GetQuery>parse("match $x has index '" + N + "';get;").execute()).get("x").getId();
        String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
        String queryString = "match " + queryPattern + " get;";
        String subbedQueryString = "match " + queryPattern + "$x id '" + firstId.getValue() + "';" + "get;";
        String subbedQueryString2 = "match " + queryPattern + "$y id '" + lastId.getValue() + "';" + "get;";
        String limitedQueryString = "match " + queryPattern + "limit 1;" + "get;";
        assertEquals(executeQuery(queryString, tx, "full").size(), 1);
        assertEquals(executeQuery(subbedQueryString, tx, "first argument bound").size(), 1);
        assertEquals(executeQuery(subbedQueryString2, tx, "second argument bound").size(), 1);
        assertEquals(executeQuery(limitedQueryString, tx, "limit ").size(), 1);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 54 with ConceptId

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

the class BenchmarkIT method testRandomSetLinearTransitivity.

/**
 * 2-rule transitive test with transitivity expressed in terms of two linear rules.
 * Data arranged randomly with N number of db relation instances.
 */
@Test
public void testRandomSetLinearTransitivity() {
    final int N = 2000;
    final int limit = 100;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    loadTransitivityData(N);
    try (GraknTx tx = session.open(GraknTxType.READ)) {
        ConceptId entityId = tx.getEntityType("a-entity").instances().findFirst().get().getId();
        String queryPattern = "(P-from: $x, P-to: $y) isa P;";
        String queryString = "match " + queryPattern + " get;";
        String subbedQueryString = "match " + queryPattern + "$x id '" + entityId.getValue() + "';" + "get;";
        String subbedQueryString2 = "match " + queryPattern + "$y id '" + entityId.getValue() + "';" + "get;";
        String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
        executeQuery(queryString, tx, "full");
        executeQuery(subbedQueryString, tx, "first argument bound");
        executeQuery(subbedQueryString2, tx, "second argument bound");
        executeQuery(limitedQueryString, tx, "limit " + limit);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 55 with ConceptId

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

the class BenchmarkIT method loadRandomisedRelationInstances.

private void loadRandomisedRelationInstances(String entityLabel, String fromRoleLabel, String toRoleLabel, String relationLabel, int N, GraknSession session, GraknClient graknClient, Keyspace keyspace) {
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        GraknTx tx = session.open(GraknTxType.READ);
        Var entityVar = var().asUserDefined();
        ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
        assertEquals(instances.length, N);
        Role fromRole = tx.getRole(fromRoleLabel);
        Role toRole = tx.getRole(toRoleLabel);
        RelationshipType relationType = tx.getRelationshipType(relationLabel);
        Random rand = new Random();
        Multimap<Integer, Integer> assignmentMap = HashMultimap.create();
        for (int i = 0; i < N; i++) {
            int from = rand.nextInt(N - 1);
            int to = rand.nextInt(N - 1);
            while (to == from && assignmentMap.get(from).contains(to)) to = rand.nextInt(N - 1);
            Var fromRolePlayer = Graql.var();
            Var toRolePlayer = Graql.var();
            Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(relationType.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[from])).and(toRolePlayer.asUserDefined().id(instances[to]));
            loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
        }
        tx.close();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Random(java.util.Random) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Aggregations

ConceptId (ai.grakn.concept.ConceptId)80 Test (org.junit.Test)55 Concept (ai.grakn.concept.Concept)23 Role (ai.grakn.concept.Role)22 RelationshipType (ai.grakn.concept.RelationshipType)19 GraknTx (ai.grakn.GraknTx)18 EntityType (ai.grakn.concept.EntityType)18 Label (ai.grakn.concept.Label)16 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)14 Var (ai.grakn.graql.Var)12 List (java.util.List)12 Entity (ai.grakn.concept.Entity)10 AttributeType (ai.grakn.concept.AttributeType)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 ClassRule (org.junit.ClassRule)9 GraknTxType (ai.grakn.GraknTxType)8 Keyspace (ai.grakn.Keyspace)8 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)8