Discussion Forum

How to get player id of a relation with python grakn.client 1.7.0

with console, if query:

hydro> match $r (up:$a,down:$b,step:$c) isa FLOOD_PROPAGATION; $a has name 'ABC';$b has name 'DEF'; get $r;

console will reply:

{$r id V49256 (down: id V40968304, step: id V41029768, up: id V40992880) isa FLOOD_PROPAGATION;}
{$r id V98528 (down: id V40968304, step: id V41009232, up: id V40992880) isa FLOOD_PROPAGATION;}

however, I don’t know how to get ‘step’ id ‘V41029768’ with python client, what I’ve done as follows:

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="hydro") as session:
        transaction = session.transaction().read()
        re_iter = transaction.query(cmd)
        for concept in re_iter:
            c = concept.map()
            for i in c.values():
                print(i.id)
                print(i.type().label())

Based on Grakn’s concept API document, I have no idea how to get the players of a queried relation. From github, I found some example code with ‘role_players()’ and ‘collect_concepts()’ function, But those are not working in latest grakn.client.

Hi yiouyo! transaction.query would return a list of concept maps, each concept map contains all the variable you specified in the get clause. Since you only specified get $r in the query, you would only get variable r in the concept map. You can just specify get $c if you only care about the steps. Simply say get without listing variables is also a valid query, which returns all the variables defined in the query ($a. $b, $c and $r).

Given that, a patched version of your python code would be:

cmd = "match $r (up:$a,down:$b,step:$c) isa FLOOD_PROPAGATION; $a has name 'ABC';$b has name 'DEF'; get $c;"
re_iter = transaction.query(cmd)
for concept in re_iter:
  c = concept.map().get('c')
  print(c.id)
  print(c.type().label())

See this bit of documentation for more info https://dev.grakn.ai/docs/client-api/python#conceptmap.

I found some example code with ‘role_players()’ and ‘collect_concepts()’ function, But those are not working in latest grakn.client

Could you please elaborate on where you find the example code? Can you give some sample code that doesn’t work? Which client version are you using?

@rui Thanks for you help. I see your points about ‘get $c’ and will try to explore it. The example code is here:

In which, there is collect_concept() in line 67/83, and role_players() in line 108. I’ve tried the code on grakn.client 1.6 with grakn 1.6.2, but not working in grakn.client 1.7 with grakn 1.7.1