Search code examples
vaticle-typedbvaticle-typeql

Are attribute instances in grakn.ai supposed to be singleton?


According to grakn docs:

Attributes in a Grakn knowledge graph are modeled differently to columns in a relational database. In this example, the attribute start-date with the value of, for instance 2019-01-01, exists only once in the knowledge graph and shared among any number of instances that may own it. This is useful when we need to query the knowledge graph for anything that has the start-date attribute with value 2019-01-01. In this case, we would get all the residencies and travels that started on the first day of 2019. It's important to remember this when performing write operations on instances of an attribute type.

This would make me think grakn.ai basically only ever stores one attribute value exactly once in its database.

Now I have run the following twice:

define unit sub attribute, datatype string;
define mass sub attribute, has unit, datatype double;
define modelName sub attribute, datatype string;

define product sub entity, abstract;
define cellphone sub product, has mass, has modelName;

insert
   $y 10 isa mass, has unit "kg";
   $x isa cellphone, has modelName "Cat Phone", has mass $y;

As expected, this creates only one "Cat Phone" instance of the "modelName" attribute, however, two instances of "mass" with value 10 and unit "kg" are created. (see screenshot)

output

Is this the intended behaviour or are attribute instances (including those with "nested" attributes) in grakn.ai supposed to be singleton?


Solution

  • You have interpreted the Grakn documentation correctly! An important feature to understand, Grakn does indeed store each "fact" as a singleton, per type. In this case, the unit "kg" should be stored exactly once, same as the mass 10 **

    It appears you've uncovered a bug in Grakn's handling of re-definition of schema elements. The problem is caused by the following portion of your code being run twice:

    define unit sub attribute, datatype string;
    define mass sub attribute, has unit, datatype double;
    define modelName sub attribute, datatype string;
    
    define product sub entity, abstract;
    define cellphone sub product, has mass, has modelName;
    

    Grakn behaves as it should when not re-defining the schema twice, just running the insert... statements twice:

    Workbase screenshot

    Thank you for providing a reproducible example! An issue has been created at https://github.com/graknlabs/grakn/issues/5411 and should be handled soon.

    ** Note that if you get some interesting cases when you want to create a mass with value 10 with a different unit, say lb. Both kg and lb will connect to the same instance 10. If you find this case troubling you head over to the friendly Grakn Slack community to find some solutions!