Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

read an complete .gql file | insert behaviour #41

Closed
@kristall

Description

@kristall

Problem to Solve

If you have an .gql file, that contains definitions and inserts then this causes an exception

with open("/path/grakn/grakn-core-all-linux-1.5.0/tests/foo.gpl") as f:
    definitions_and_inserts = f.read()

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="test") as session:
        with session.transaction().write() as write_transaction:
            write_transaction.query(definitions_and_inserts)
            write_transaction.commit() 

but from console this works:

./grakn console --keyspace test --file tests/foo.gql

Current Workaround

To avoid such behavior the original .gql file need to be split in two parts, definitions as one and inserts as the other like this:

with open("/path/grakn/grakn-core-all-linux-1.5.0/tests/foo_d.gpl") as f:
    definitions = f.read()

with open("/path/grakn/grakn-core-all-linux-1.5.0/tests/foo_i.gpl") as f:
    inserts = f.read()

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="test") as session:
        with session.transaction().write() as write_transaction:
            write_transaction.query(definitions)  
            write_transaction.query(insert)
            write_transaction.commit()

Now the definitions are committable, but unfortunately the insert part still fails with exceptions.

So one needs to do the following:

with open("/path/grakn/grakn-core-all-linux-1.5.0/tests/foo_i.gpl") as f:
    inserts = [e for e in f.read().split("\n\n") if e.strip() and not e.startswith("#")] 
    # Attention see below

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="test") as session:
        with session.transaction().write() as write_transaction:
            write_transaction.query(definitions)  
            for insert in inserts:
                write_transaction.query(insert)
            write_transaction.commit()

But since in the foo_i.gql some of the inserts are multilines (match\n..\n..insert\n..\n) one need to insert blank lines to make an easy split with little logic (not empty and not just a comment which also causes exceptions) and split at \n\n. This is painful since one needs to also split up single-line inserts with blank lines.

Example:

insert $n isa NNP, has Lemma "Geldstrafe";

insert $z isa NZP, has Lemma "Jahre", has Eigenschaft "bis zu fünf (<=5)";

match 
    $NO isa NEP, has Lemma "Wer";
    $DO isa NEP, has Lemma "anderer";
    $AO isa NNP, has Lemma "Sache";
    $PO isa NNP, has Lemma "in der Absicht";
insert
    $VP (NO: $NO, DO: $DO, AO: $AO, PO: $PO) isa VP;
    $VP has Lemma "wegnehmen";

match 
    $A1 isa NEP, has Lemma "Wer";
    $A2 isa NEP, has Lemma "Dritter";
insert
    $Alt (Alt_1: $A1, Alt_2: $A2) isa Alternative;
    $Alt has Lemma "oder";

(the blank line between the first two inserts is painful especially if you have a lot of them).

Proposed Solution

Since my guess is that you don't want to change the behaviour of write_transaction.query() it be great to have a write_transaction.from_file() which would take the path to the original foo.gql and read it without exceptions like:

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="test") as session:
        with session.transaction().write() as write_transaction:
            write_transaction.from_file("/path/grakn/grakn-core-all-linux-1.5.0/tests/foo.gpl")
            write_transaction.commit()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions