Discussion Forum

Client Java - answer statements

I’m still trying to reproduce the BioGrakn Text Mining Use Case from the White Paper “Text Mined Knowledge Graphs” with the aim of building a text mined knowledge graph out of my (non-biomedical) document collection later on, using Client Java. Now I’m trying to reproduce the queries from the White Paper. The first one aks what knowledge is extracted from a specific PubMed article:

GraqlGet knowledgeFromPubMedArticleQuery = Graql.match(
		var("p").has("pmid", "27246822"),
		var("r").isa("knowledge-extraction").rel("p").rel("k")
		).get();
    
    List<ConceptMap> answers_knowledgeFromPubMedArticleQuery = transaction.execute(knowledgeFromPubMedArticleQuery);
    
    for (ConceptMap answer : answers_knowledgeFromPubMedArticleQuery) {
        System.out.println(answer.get("p").asEntity());
        System.out.println(answer.get("r").asRelation());
        
        System.out.println(answer.map().values());
        System.out.println(answer.explanation());
    }

This is my output:

RemoteEntity{tx=grakn.client.GraknClient$Transaction@5852c06f, id=V41024}
RemoteRelation{tx=grakn.client.GraknClient$Transaction@5852c06f, id=V4184}
[RemoteEntity{tx=grakn.client.GraknClient$Transaction@5852c06f, id=V41024}, RemoteRelation{tx=grakn.client.GraknClient$Transaction@5852c06f, id=V4184}, RemoteRelation{tx=grakn.client.GraknClient$Transaction@5852c06f, id=V557280}]
grakn.core.concept.answer.Explanation@b7c4869

My question is: How do I have to reformulate my answer statements in order to receive something insightful from the query, like running the query in the Workbase (see image)? Any help is highly appreciated.

Unbenannt

I think the inconsistency that you are seeing here is related to the functionality that workbase does automatically for you.

Let’s start by looking at the query in plain Graql:

match $p has pmid 27246822;
$r ($p, $k) isa knowledge-extraction;
get;

We expect to get 3 variables back from this in each answer, $p, $k, $r.
$k will be any entity/relation/attribute that is in a knowledge-extraction relation with the punned article, $p.

As a sanity check, if you run this query in the Grakn console, you should see the same results that you see from client-java.
Your results show that you found a result for $p, $k, $r. The interesting result is $k, which is a relation. So, you got an answer for all of the variables you asked for, but of course this wasn’t what you wanted.

In workbase these variables map to these elements:
31

Workbase does the same as the clients, except that when it displays a relation, it automatically issues another query to fetch the roleplayers of that relation, to avoid displaying a lonely circle as the relation.

Effectively, it transforms your query to look like this:

match $p has pmid 27246822;
$r ($p, $k) isa knowledge-extraction;
$k($e);
get;

Hopefully this is the query that you want :slight_smile:

Thank you very much for your detailed and enlightening answer! :bulb: Now many things are clearer to me. My real question, however, was - I should have explained myself better - how can I “access” and output the following knowledge using client Java:

  • Dabrafenib is a drug
  • Trametinib is a drug
  • Melanoma is a disease
  • There is a treatment relation between the three mentioned entities, whose subject is Melanoma, having two objects: Dabrafenib and Trametinib

In my use case, similar to the one in the BioGrakn White Paper, the user wants a quick overview of large amounts of textual data and infer (new) knowledge from it. In this case, I inferred the knowledge by literally browsing the graph in the Workbase. How could I output it in a way that is easy to interpret using client Java? Right now, the answer.get("r").asRelation() method call returns the node ID, which is not that insightful for my human user…

Ok great, as long as we are on the road to full understanding!

To be precise, answer.get("r").asRelation() is a Relation object, which has the id of the relation as a property. Upon this object, or any other from Grakn, you can access the Concept API methods.

Remember that in Grakn, Attributes (such as the name of a disease, like “Dabrafenib”) are first-class citizens, and therefore nodes on their own.

One of your options to get the attributes that an Entity or Relation has is to use answer.get("r").asRelation().attributes()
Documented in Concept API

Alternatively, extend your query to also retrieve the attributes that you are interested in.

Something like:

match $p has pmid 27246822;
$r ($p, $k) isa knowledge-extraction;
$k($e);
$e has name $n;
get;

In this case, $n will contain an Attribute.

Using either method, once you have the Attribute you should retrieve its value as attribute.value()

Remember that in Grakn, Attributes (such as the name of a disease, like “Dabrafenib”) are first-class citizens, and therefore nodes on their own.

I wasn’t fully aware of that - altough it’s all documented in the Concept API obviously. Thank you very much for the explanation!!