Reputation: 51
I am trying to figure out a Graql code. where I need to list all the attributes for an entity. Please suggest.
I need to list all the attributes of an entity.
for ex: a person has age, gender, and height. I need to get o/p as age, gender, and height.
Upvotes: 2
Views: 983
Reputation: 920
You have a couple of options if you use Graql to ask for the owned attributes directly.
Firstly, to ask for all owned attributes, you can do this:
match $p isa person, has attribute $a; get $a;
Now you will get back the attributes $p
owns as $a
. You will see the types of those attributes when viewed in the Grakn console. Using a Grakn client for Python/Node.js/Java etc. you need to then use concept.type().label()
on each Concept that you get back in order to find out their types.
Alternatively, you can find the owned attributes and their types directly using:
match $p isa person, has attribute $a; $a isa! $t; get $a, $t;
This also queries for the type of $a
, giving that type as $t
. You notice here that this is exactly the same as a normal statement, but we have replaced declaring a specific type with a variable.
In this query, the !
of isa!
is important here. When using isa
, $t
will yield the type of $a
and all of its super types as well.
Instead, using isa!
asks for only the direct type, so you will get only the actual type of the attribute back, not the super types, which means you most likely want to use isa!
.
Upvotes: 2
Reputation: 504
In general on any Concept you get back from a Graql query you can use the .attributes()
method and then on every attribute you can use .value()
and .type().label()
to see what type of attribute you're handling, example:
sudo code (as I don't know which language you want to use)
var attributes = person.attributes();
for(attribute in attributes){
print("Attribute "+attribute.type().label()+ "with value: "+ attribute.value())
}
Please check official documentation fro Concept API: https://dev.grakn.ai/docs/concept-api/thing
Some examples: https://dev.grakn.ai/docs/examples/phone-calls-queries and the blog with many examples: https://blog.grakn.ai/
Upvotes: 2