set client_min_messages = warning;

drop extension if exists pg_liquid cascade;

create extension pg_liquid;

select count(*) as seeded_claim_count
from liquid.query($$
DefPred("onto/preferred_label", "1", "liquid/node", "0", "liquid/string").
DefPred("onto/subclass_of", "0", "liquid/node", "0", "liquid/node").
DefPred("onto/instance_of", "0", "liquid/node", "0", "liquid/node").
DefPred("meta/compound_subject_class", "0", "liquid/node", "0", "liquid/node").

DefCompound("UserSignal", "user", "0", "liquid/node").
DefCompound("UserSignal", "intent", "0", "liquid/string").
DefCompound("UserSignal", "value", "0", "liquid/string").
DefCompound("UserSignal", "source", "0", "liquid/string").
Edge("UserSignal", "liquid/mutable", "false").

DefCompound("AgentExtraction", "user", "0", "liquid/node").
DefCompound("AgentExtraction", "fact_key", "0", "liquid/string").
DefCompound("AgentExtraction", "fact_value", "0", "liquid/string").
DefCompound("AgentExtraction", "agent", "0", "liquid/node").
DefCompound("AgentExtraction", "thread", "0", "liquid/node").
Edge("AgentExtraction", "liquid/mutable", "false").

DefCompound("OntologyClaim", "user", "0", "liquid/node").
DefCompound("OntologyClaim", "subject", "0", "liquid/node").
DefCompound("OntologyClaim", "predicate", "0", "liquid/node").
DefCompound("OntologyClaim", "object", "0", "liquid/node").
DefCompound("OntologyClaim", "author", "0", "liquid/node").
DefCompound("OntologyClaim", "source", "0", "liquid/node").
DefCompound("OntologyClaim", "confidence", "0", "liquid/string").
Edge("OntologyClaim", "liquid/mutable", "false").

CompoundReadByRole@(compound_type="UserSignal", role="user").
CompoundReadByRole@(compound_type="AgentExtraction", role="user").
CompoundReadByRole@(compound_type="OntologyClaim", role="user").

Edge("session:alice-app", "liquid/acts_for", "user:alice").
Edge("session:bob-app", "liquid/acts_for", "user:bob").
Edge("session:alice-agent", "liquid/acts_for", "agent:extractor:alice").
Edge("agent:extractor:alice", "liquid/acts_for", "user:alice").
Edge("session:bob-agent", "liquid/acts_for", "agent:extractor:bob").
Edge("agent:extractor:bob", "liquid/acts_for", "user:bob").

Edge("class/Thing", "onto/preferred_label", "Thing").
Edge("class/User", "onto/subclass_of", "class/Thing").
Edge("class/Food", "onto/subclass_of", "class/Thing").
Edge("class/FermentedDairy", "onto/subclass_of", "class/Food").
Edge("concept/greek_yogurt", "onto/instance_of", "class/FermentedDairy").
Edge("concept/banana", "onto/instance_of", "class/Food").
Edge("concept/greek_yogurt", "onto/preferred_label", "Greek yogurt").
Edge("concept/banana", "onto/preferred_label", "Banana").

Edge("UserSignal", "meta/compound_subject_class", "class/User").
Edge("AgentExtraction", "meta/compound_subject_class", "class/User").
Edge("OntologyClaim", "meta/compound_subject_class", "class/Food").

UserSignal@(user="user:alice", intent="goal", value="increase_protein", source="chat").
UserSignal@(user="user:bob", intent="goal", value="reduce_sugar", source="chat").

AgentExtraction@(user="user:alice", fact_key="allergy", fact_value="peanut", agent="agent:extractor:alice", thread="thread:alice:1").
AgentExtraction@(user="user:bob", fact_key="avoid", fact_value="lactose", agent="agent:extractor:bob", thread="thread:bob:1").

OntologyClaim@(user="user:alice", subject="concept/greek_yogurt", predicate="onto/has_property", object="property/high_protein", author="agent:extractor:alice", source="thread:alice:1", confidence="0.88").
OntologyClaim@(user="user:alice", subject="concept/banana", predicate="onto/has_property", object="property/high_potassium", author="user:alice", source="thread:alice:2", confidence="0.93").
OntologyClaim@(user="user:bob", subject="concept/banana", predicate="onto/has_property", object="property/easy_digest", author="agent:extractor:bob", source="thread:bob:1", confidence="0.84").

OntologyClaim@(cid=cid, user=user_id, subject=subject_id, predicate=predicate_id, object=object_id, author=author_id, source=source_id, confidence=confidence)?
$$) as t(cid text, user_id text, subject_id text, predicate_id text, object_id text, author_id text, source_id text, confidence text);

select compound_type, role
from liquid.query($$
CompoundReadByRole@(cid=cid, compound_type=compound_type, role=role)?
$$) as t(cid text, compound_type text, role text)
where compound_type in ('UserSignal', 'AgentExtraction', 'OntologyClaim')
order by 1, 2;

select compound_type, subject_class
from liquid.query($$
Edge(compound_type, "meta/compound_subject_class", subject_class)?
$$) as t(compound_type text, subject_class text)
where compound_type in ('UserSignal', 'AgentExtraction', 'OntologyClaim')
order by 1, 2;

select count(*) as alice_agent_extraction_count
from liquid.query_as('session:alice-agent', $$
AgentExtraction@(user="user:alice", fact_key="avoid", fact_value="shellfish", agent="agent:extractor:alice", thread="thread:alice:2").
AgentExtraction@(cid=cid, user=user_id, fact_key=fact_key, fact_value=fact_value, agent=agent_id, thread=thread_id)?
$$) as t(cid text, user_id text, fact_key text, fact_value text, agent_id text, thread_id text)
where user_id = 'user:alice';

select count(*) as alice_user_signal_count
from liquid.query_as('session:alice-app', $$
UserSignal@(user="user:alice", intent="dislike", value="sardines", source="chat").
UserSignal@(cid=cid, user=user_id, intent=intent, value=value, source=source)?
$$) as t(cid text, user_id text, intent text, value text, source text)
where user_id = 'user:alice';

select user_id, intent, value
from liquid.read_as('session:alice-app', $$
UserSignal@(cid=cid, user=user_id, intent=intent, value=value, source=source)?
$$) as t(cid text, user_id text, intent text, value text, source text)
order by 1, 2, 3;

select count(*) as bob_reads_alice_signal_count
from liquid.read_as('session:bob-app', $$
UserSignal@(cid=cid, user="user:alice", intent=intent, value=value, source=source)?
$$) as t(cid text, intent text, value text, source text);

select fact_key, fact_value, agent_id
from liquid.read_as('session:alice-app', $$
AgentExtraction@(cid=cid, user="user:alice", fact_key=fact_key, fact_value=fact_value, agent=agent_id, thread=thread_id)?
$$) as t(cid text, fact_key text, fact_value text, agent_id text, thread_id text)
order by 1, 2, 3;

select count(*) as bob_reads_alice_extraction_count
from liquid.read_as('session:bob-app', $$
AgentExtraction@(cid=cid, user="user:alice", fact_key=fact_key, fact_value=fact_value, agent=agent_id, thread=thread_id)?
$$) as t(cid text, fact_key text, fact_value text, agent_id text, thread_id text);

select subject_id, predicate_id, object_id, author_id, confidence
from liquid.read_as('session:alice-app', $$
OntologyClaim@(cid=cid, user="user:alice", subject=subject_id, predicate=predicate_id, object=object_id, author=author_id, source=source_id, confidence=confidence)?
$$) as t(cid text, subject_id text, predicate_id text, object_id text, author_id text, source_id text, confidence text)
order by 1, 2, 3, 4, 5;

select subject_id, predicate_id, object_id, author_id, confidence
from liquid.read_as('session:bob-app', $$
OntologyClaim@(cid=cid, user="user:bob", subject=subject_id, predicate=predicate_id, object=object_id, author=author_id, source=source_id, confidence=confidence)?
$$) as t(cid text, subject_id text, predicate_id text, object_id text, author_id text, source_id text, confidence text)
order by 1, 2, 3, 4, 5;

select inferred_type
from liquid.query($$
TypeOf(x, t) :- Edge(x, "onto/instance_of", t).
TypeOf(x, sup) :- TypeOf(x, t), Edge(t, "onto/subclass_of", sup).
TypeOf("concept/greek_yogurt", inferred_type)?
$$) as t(inferred_type text)
order by 1;

select subject_id, label, inferred_type
from liquid.query($$
TypeOf(x, t) :- Edge(x, "onto/instance_of", t).
TypeOf(x, sup) :- TypeOf(x, t), Edge(t, "onto/subclass_of", sup).
GraphNode(x, label, t) :- Edge(x, "onto/preferred_label", label), TypeOf(x, t).
GraphNode(x, label, t)?
$$) as t(subject_id text, label text, inferred_type text)
where subject_id in ('concept/banana', 'concept/greek_yogurt')
order by 1, 2, 3;
