Search in sources :

Example 1 with ConceptId

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

the class BenchmarkIT method testMultiJoin.

/**
 * Scalable multi-join test defined as a non-recursive tree of binary joins,
 * which is expressed using the following inference rules:
 *
 * a(X,Y)  :- b1(X,Z), b2(Z,Y).
 * b1(X,Y) :- c1(X,Z), c2(Z,Y).
 * b2(X,Y) :- c3(X,Z), c4(Z,Y).
 * c1(X,Y) :- d1(X,Z), d2(Z,Y).
 *
 * The base relations, c2, c3, c4, d1 and d2 are randomly generated
 * with 1/5 * N instances (reaching a total of N instances) each defined over 1/5 * N entities.
 * The query is based on the final derived predicate.
 */
@Test
public void testMultiJoin() {
    final int N = 10000;
    final int limit = 100;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    loadJoinData(N);
    try (GraknTx tx = session.open(GraknTxType.READ)) {
        ConceptId entityId = tx.getEntityType("genericEntity").instances().findFirst().get().getId();
        String queryPattern = "(fromRole: $x, toRole: $y) isa A;";
        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 2 with ConceptId

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

the class DegreeTest method testDegreesSimple.

@Test
public void testDegreesSimple() {
    // create instances
    EntityType thingy = tx.putEntityType("thingy");
    EntityType anotherThing = tx.putEntityType("another");
    ConceptId entity1 = thingy.addEntity().getId();
    ConceptId entity2 = thingy.addEntity().getId();
    ConceptId entity3 = thingy.addEntity().getId();
    ConceptId entity4 = anotherThing.addEntity().getId();
    Role role1 = tx.putRole("role1");
    Role role2 = tx.putRole("role2");
    thingy.plays(role1).plays(role2);
    anotherThing.plays(role1).plays(role2);
    RelationshipType related = tx.putRelationshipType("related").relates(role1).relates(role2);
    // relate them
    related.addRelationship().addRolePlayer(role1, tx.getConcept(entity1)).addRolePlayer(role2, tx.getConcept(entity2));
    related.addRelationship().addRolePlayer(role1, tx.getConcept(entity2)).addRolePlayer(role2, tx.getConcept(entity3));
    related.addRelationship().addRolePlayer(role1, tx.getConcept(entity2)).addRolePlayer(role2, tx.getConcept(entity4));
    tx.commit();
    tx = session.open(GraknTxType.READ);
    Map<ConceptId, Long> correctDegrees = new HashMap<>();
    correctDegrees.put(entity1, 1L);
    correctDegrees.put(entity2, 3L);
    correctDegrees.put(entity3, 1L);
    correctDegrees.put(entity4, 1L);
    // compute degrees
    List<Long> list = new ArrayList<>(4);
    long workerNumber = 4L;
    if (GraknTestUtil.usingTinker())
        workerNumber = 1L;
    for (long i = 0L; i < workerNumber; i++) {
        list.add(i);
    }
    tx.close();
    Set<Map<Long, Set<String>>> result = list.parallelStream().map(i -> {
        try (GraknTx graph = session.open(GraknTxType.READ)) {
            return graph.graql().compute().centrality().usingDegree().execute();
        }
    }).collect(Collectors.toSet());
    assertEquals(1, result.size());
    Map<Long, Set<String>> degrees0 = result.iterator().next();
    assertEquals(2, degrees0.size());
    degrees0.forEach((key, value) -> value.forEach(id -> {
        assertTrue(correctDegrees.containsKey(ConceptId.of(id)));
        assertEquals(correctDegrees.get(ConceptId.of(id)), key);
    }));
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        Map<Long, Set<String>> degrees1 = graph.graql().compute().centrality().usingDegree().of("thingy").execute();
        assertEquals(2, degrees1.size());
        assertEquals(2, degrees1.get(1L).size());
        assertEquals(1, degrees1.get(3L).size());
        degrees1.forEach((key, value) -> value.forEach(id -> {
            assertTrue(correctDegrees.containsKey(ConceptId.of(id)));
            assertEquals(correctDegrees.get(ConceptId.of(id)), key);
        }));
        Map<Long, Set<String>> degrees2 = graph.graql().compute().centrality().usingDegree().of("thingy", "related").execute();
        assertEquals(degrees1, degrees2);
        degrees2 = graph.graql().compute().centrality().usingDegree().of().execute();
        assertEquals(degrees0, degrees2);
        // compute degrees on subgraph
        Map<Long, Set<String>> degrees3 = graph.graql().compute().centrality().usingDegree().in("thingy", "related").execute();
        assertEquals(degrees1, degrees3);
        degrees3 = graph.graql().compute().centrality().usingDegree().of("thingy").in("related").execute();
        assertEquals(degrees1, degrees3);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) HashMap(java.util.HashMap) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with ConceptId

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

the class ConceptController method getConceptById.

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

Example 4 with ConceptId

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

the class CountPostProcessorTest method whenBreachingTheShardingThreshold_ShardingHappens.

@Test
public void whenBreachingTheShardingThreshold_ShardingHappens() {
    // Configure mock to return value which breaches threshold
    ConceptId id = ConceptId.of("e");
    newInstanceCounts.put(id, 6L);
    when(countStorage.incrementInstanceCount(keyspace, id, 6L)).thenReturn(6L);
    when(countStorage.incrementInstanceCount(keyspace, id, 0L)).thenReturn(6L);
    // Create fake commit log
    CommitLog commitLog = CommitLog.create(keyspace, newInstanceCounts, Collections.emptyMap());
    // Update The Counts
    countPostProcessor.updateCounts(commitLog);
    // Check Sharding Takes Place
    verify(factoryMock, Mockito.times(1)).tx(keyspace, GraknTxType.WRITE);
    verify(countStorage, Mockito.times(1)).incrementShardCount(keyspace, id, 1);
}
Also used : CommitLog(ai.grakn.kb.log.CommitLog) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 5 with ConceptId

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

the class RemoteGraknTxTest method whenPuttingRole_EnsureCorrectRequestIsSent.

@Test
public void whenPuttingRole_EnsureCorrectRequestIsSent() {
    ConceptId id = ConceptId.of(V123.getValue());
    Label label = Label.of("foo");
    try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
        // The open request
        verify(server.requests()).onNext(any());
        Concept concept = RemoteConcepts.createRole(tx, id);
        server.setResponse(GrpcUtil.putRoleRequest(label), GrpcUtil.conceptResponse(concept));
        assertEquals(concept, tx.putRole(label));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

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