scallop-lang / scallop

Framework and Language for Neurosymbolic Programming.
https://www.scallop-lang.org
MIT License
201 stars 8 forks source link

How to query for True/ False in Scallopy #4

Open laurendelong21 opened 1 year ago

laurendelong21 commented 1 year ago

Hello,

I was wondering if it's possible to make a query with a KB in scallopy in which you simply get a True/False or Yes/No answer. For example, within the tutorial, what if I want to check whether a path exists between two nodes without having to list out all of the paths?

Such as path(0,4)

Thank you in advance.

Liby99 commented 1 year ago

Yes! Currently, this is the dumbest but most direct way

rel exists_path(true) = path(0, 4)
rel exists_path(false) = not path(0, 4)

I'm working on a patch in which the following syntax will be supported:

rel exists_path(b) = b := exists(path(0, 4))

Hope this answers your question!

mukhtarhussainb commented 9 months ago

I colud not find any example code making a query using "Scallopy" Python code. All query codes use Scallop syntax. For example given bellow scallopy code

ctx.add_relation("color",(int, str))
ctx.add_facts("color",[(0, "blue"), (1, "green"), (2, "blue")])

How do I query the context to get the "blue" ?

Liby99 commented 9 months ago

You will be able to query an output relation. For instance, what you can do is:

ctx.add_rule("color_of_obj_0(c) = color(0, c)")  # adding a query
ctx.run(). # execute the query
result_facts = list(ctx.relation("color_of_obj_0")) # get the results stored inside of `color_of_obj_0` relation; will likely be [("blue",)]
result = result_facts[0][0] # "blue"
mukhtarhussainb commented 9 months ago

Thank you for the quick reply, and I'm sorry I did not clarify it. The query should find all the tuples having "blue". The expected result should be: [(0, "blue"), (2, "blue")]

Liby99 commented 9 months ago

In that case, you can write the following

ctx.add_rule("blue_objs(o, c) = color(o, c) and c == \"blue\"")
ctx.run()
result_facts = list(ctx.relation("blue_objs")) # [(0, "blue"), (2, "blue")]