Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to query for True/ False in Scallopy #4

Open
laurendelong21 opened this issue Feb 8, 2023 · 5 comments
Open

How to query for True/ False in Scallopy #4

laurendelong21 opened this issue Feb 8, 2023 · 5 comments

Comments

@laurendelong21
Copy link

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
Copy link
Contributor

Liby99 commented Apr 3, 2023

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
Copy link

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
Copy link
Contributor

Liby99 commented Jan 16, 2024

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
Copy link

mukhtarhussainb commented Jan 17, 2024

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
Copy link
Contributor

Liby99 commented Jan 18, 2024

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")]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants