Modelization question, you have 3 cats instances: bill, joe, lucy how do you modelize "i love cats"?


#1

modelization question…
you have 3 cats instances: bill, joe, lucy. How do you modelize “i love cats”?


#2

Hi @johnwoo1

First let’s model the entity types.

person sub entity,
has name;

cat sub entity,
has name;

name sub resource, datatype string;

Then, there are 2 ways (at least) to model the loves relationship. It depends on your assumption whether your cat loves you back! :smiley:

A. First assumption: lets assume that if you love your cat, the cat loves you back. :slight_smile:
Then you can model the loves relationship (and the complete ontology) would look like below.

person sub entity,
has name,
plays lover;

cat sub entity,
has name,
plays lover;

lover sub role;
loves sub relation,
relates lover;

name sub resource, datatype string;

B. Second assumption: if you love your cat, it does not mean the cat loves you back, but the cat is simply being loved. :slight_smile:

person sub entity,
has name,
plays lover;

cat sub entity,
has name,
plays loved;

lover sub role;
loved sub role;
loves sub relation,
relates lover,
relates loved;

name sub resource, datatype string;

I hope that makes sense, John! :slight_smile:


#3

thanks @haikal


#4

and if you want to insert data that follows the model above, @johnwoo1

Scenario A: where the cat loves you back

insert $x isa person, has name "Alice";
$y isa cat, has name "Billy";
(lover: $x, lover: $y) isa loves;

Scenario B: where the cat doesn’t necessarily loves you back

insert $x isa person, has name "Alice";
$y isa cat, has name "Billy";
(lover: $x, loved: $y) isa loves;

#5

I realised I didn’t exactly answer your question, @johnwoo1.

If you have 3 instances of a cat:

insert 
$x isa cat, has name "bill";
$y isa cat, has name "joe";
$y isa cat, has name "lucy";

and one instance of a person:

insert $x isa person, has name "Alice";

You can’t make a relation for a an instance of a person, say Alice, to love all cats at once. You will need to create individual relationships of from Alice to each cat. That is because Grakn is a database, thus, you can only create assertions / facts at the data instance layer and not at the data type layer.

Let me know if that makes sense!


Inference problem?
#6

all you answers helped, thank you very much :relaxed:


#7

Sorry, can’t I do this?:

person sub entity,
has name,
plays catsLover;

cat sub entity,
has name;

catsLover sub role;
lovesCats sub relation,
relates catsLover,
relates cat;

name sub resource, datatype string;

insert $x isa person, has name "Alice";
$x isa cat, has name "bill";
$y isa cat, has name "joe";
$y isa cat, has name "lucy";

(catsLover: $x, cat: cat) isa lovesCats;

#8

You have a possible misunderstanding in your graql file:

insert $x isa person, has name "Alice";
$x isa cat, has name "bill";
$y isa cat, has name "joe";
$y isa cat, has name "lucy";

By the end of these statements x and y are cats. This has led to a tiny error here:

(catsLover: $x, cat: cat) isa lovesCats;

Here you are saying x = bill loves a cat without specifying the cat

When creating a relation you need to specify the other role player. In this case you need to specify the cat:

(catsLover: $x, cat: $y) isa lovesCats;

If there are multiple cats then you need to insert multiple lovesCats relations.


#9

Thanks for the correction

What about this?

livingBeing sub entity,
has name;

person sub livingBeing,
plays human,
plays catsLover;

cat sub livingBeing,
plays animal;


human sub role;
animal sub role;
catsLover sub role;

lovesCats sub relation,
relates catsLover,
relates animal;

name sub resource, datatype string;

insert $x isa person, has name "Alice";
$y isa cat, has name "bill";
$y isa cat, has name "joe";
$y isa cat, has name "lucy";


(catsLover: $x, animal: cat) isa lovesCats;

Here I’m trying to use the type “cat” as the role player, is this valid?

Thanks.


#10

Here I’m trying to use the type “cat” as the role player, is this valid?

Currently no this it not possible. Role-players must be instances (i.e. entities, resources, and relations). So in your case you would have to do the following:

insert $x isa person, has name "Alice";
$a isa cat, has name "bill";
$b isa cat, has name "joe";
$c isa cat, has name "lucy";

(catsLover: $x, animal: $a) isa lovesCats;
(catsLover: $x, animal: $b) isa lovesCats;
(catsLover: $x, animal: $c) isa lovesCats;

#11

Understood, Thank you!


#12

I may be wrong but I think the following does what OP wants to do. (I’m using the updated syntax BTW):

define

# Entities

person sub entity,
has name,
plays lover;

cat sub entity,
has name,
plays loved;



# Resources
name sub attribute datatype string;

# Roles and Relations
loves sub relationship,
    relates lover,
    relates loved;

lover sub role;
loved sub role;

# FACTS
insert

$alice isa person, has name "Alice";
$billy isa cat, has name "Billy";


## RULES
define

aliceLovesCats sub rule
when
{$cat isa cat;
$person isa person, has name "Alice";}
then
{(lover: $person, loved: $cat) isa loves;};

Save it in a file called “cat-love.gql”. Then let’s start the grakn server, load the file and start the graql console with the -n (–infer) option.

./grakn server start
./graql console -f ../testing/cat-love.gql 
 ./graql console -n

Now let’s do a query to see if anyone is loved by someone else:

match $loves (lover: $lover , loved: $loved) isa loves; $lover has name $lover-name; $loved has name $loved-name; get;
$loved id V28736 isa cat; $loved-name val “Billy” isa name; $lover-name val “Alice” isa name; $lover id V24648 isa person; $loves id V16600 (loved: id V28736, lover: id V24648) isa loves;

And there it is, graql inferred that Alice loves Billy. That’s because Billy is a cat, and the rule says Alice loves cats. Correct?


#13

Haikal’s answer on Apr 12 was the right one. My question was about relationships between the instances layer and the data type layer.


#14

Oh, OK. Since you asked for the way to modelize “I love cats” (or “Alice loves cats”, etc), it sounded like you wanted a rule so that all cats are loved automatically, instead of having to state it “manually” for each cat, and again each time you insert a new cat. Anyway, I’m glad your doubts were solved.


#15

@martinodb thanks for trying to help :blush:, appreciated