Search in sources :

Example 21 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class ReasonerTest method testReasoningWithQueryContainingRelationTypeVar.

@Test
public void testReasoningWithQueryContainingRelationTypeVar() {
    GraknTx graph = nonMaterialisedGeoKB.tx();
    String queryString = "match (geo-entity: $x) isa $type;$type label 'is-located-in'; get $x;";
    String queryString2 = "match (geo-entity: $x, entity-location: $y)isa is-located-in; get $x;";
    QueryBuilder iqb = graph.graql().infer(true);
    GetQuery query = iqb.parse(queryString);
    GetQuery query2 = iqb.parse(queryString2);
    assertQueriesEqual(query, query2);
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 22 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class MigrationCLI method printWholeCompletionMessage.

public static void printWholeCompletionMessage(MigrationOptions options) {
    System.out.println("Migration complete.");
    if (options.isVerbose()) {
        System.out.println("Gathering information about migrated data. If in a hurry, you can ctrl+c now.");
        GraknTx graph = Grakn.session(options.getUri(), options.getKeyspace()).open(GraknTxType.WRITE);
        QueryBuilder qb = graph.graql();
        StringBuilder builder = new StringBuilder();
        builder.append("Graph schema contains:\n");
        builder.append("\t ").append(graph.admin().getMetaEntityType().instances().count()).append(" entity types\n");
        builder.append("\t ").append(graph.admin().getMetaRelationType().instances().count()).append(" relation types\n");
        builder.append("\t ").append(graph.admin().getMetaRole().subs().count()).append(" roles\n\n");
        builder.append("\t ").append(graph.admin().getMetaAttributeType().instances().count()).append(" resource types\n");
        builder.append("\t ").append(graph.admin().getMetaRule().subs().count()).append(" rules\n\n");
        builder.append("Graph data contains:\n");
        builder.append("\t ").append(qb.match(var("x").isa(label(ENTITY.getLabel()))).aggregate(count()).execute()).append(" entities\n");
        builder.append("\t ").append(qb.match(var("x").isa(label(RELATIONSHIP.getLabel()))).aggregate(count()).execute()).append(" relations\n");
        builder.append("\t ").append(qb.match(var("x").isa(label(ATTRIBUTE.getLabel()))).aggregate(count()).execute()).append(" resources\n");
        builder.append("\t ").append(qb.match(var("x").isa(label(RULE.getLabel()))).aggregate(count()).execute()).append(" rules\n\n");
        System.out.println(builder);
        graph.close();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) QueryBuilder(ai.grakn.graql.QueryBuilder)

Example 23 with GraknTx

use of ai.grakn.GraknTx 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 24 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class BenchmarkIT method loadOntology.

private void loadOntology(String fileName, GraknSession session) {
    try {
        File graqlFile = new File(GraknSystemProperty.PROJECT_RELATIVE_DIR.value() + "/grakn-test-tools/src/main/graql/" + fileName);
        String s = Files.toString(graqlFile, Charset.forName("UTF-8"));
        GraknTx tx = session.open(GraknTxType.WRITE);
        tx.graql().parser().parseQuery(s).execute();
        tx.commit();
        tx.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) File(java.io.File)

Example 25 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class MigratorTestUtils method getResources.

public static Stream<Attribute> getResources(GraknTx graph, Thing thing, Label label) {
    Role roleOwner = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(label));
    Role roleOther = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(label));
    Stream<Relationship> relations = thing.relationships(roleOwner);
    return relations.flatMap(r -> r.rolePlayers(roleOther)).map(Concept::asAttribute);
}
Also used : Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) GraknTxType(ai.grakn.GraknTxType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Schema(ai.grakn.util.Schema) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17