Search code examples
pythonvaticle-typedbvaticle-typeql

How to compute connected-component using Python Client in Grakn


I want to get clusters(or connected components) using Python client. I can do it with graql with following:

compute cluster in [company, c2c], using connected-component, where contains=V86179944;

I can run the query with Python as well:

query = "compute cluster in [company, c2c], using connected-component, where contains=V86179944;"
with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace=keyspace) as session:
        with session.transaction().read() as transaction:
            answer_iterator = transaction.query(query)
            # What to do here??          

However, I don't know how to access the results. According to python client docs, there are 2 ways of getting results:

  • iterate over
  • use collect_concepts()

When I iterate over, I can't use .map() I get AttributeError: 'ConceptSet' object has no attribute 'map'

When I try collect_concepts, I get GraknError: Only use .collect_concepts on ConceptMaps returned by query()


Solution

  • map() and collect_concepts (which will be removed in the next release of Client Python) are methods of the ConceptMap answer type. What you're getting back as the result of a compute cluster query is the ConceptSet answer type. ConceptSet has the set() method that returns the set of ids of concepts after the cluster computation.

    Here you'll find the query types and their corresponding answer type and here you'll find the documentation on the set() method available on a ConceptSet.