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

java: Parse Expressions and Operators returned from the polar core. #1011

Merged
merged 15 commits into from Jul 16, 2021
Merged

java: Parse Expressions and Operators returned from the polar core. #1011

merged 15 commits into from Jul 16, 2021

Conversation

MFAshby
Copy link
Contributor

@MFAshby MFAshby commented Jul 12, 2021

Remove test for throwing assertion when Expression is returned.

Ports tests for partial evaluation from the python code to java.

#1010

PR checklist:

  • Added changelog entry.
    -> I'm not sure of the format yet, so I haven't done this.

@github-actions
Copy link

github-actions bot commented Jul 12, 2021

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@MFAshby
Copy link
Contributor Author

MFAshby commented Jul 12, 2021

I have read the CLA Document and I hereby sign the CLA

Remove test for throwing assertion when Expression is returned.

Add test for Expression successfully returned and parsed from polar core.

#1010
Ported the remaining relevant test testPartialConstraint from python to java.

#1010
@MFAshby MFAshby marked this pull request as ready for review July 12, 2021 21:51
@MFAshby
Copy link
Contributor Author

MFAshby commented Jul 12, 2021

I've ported over the relevant tests from the python module, I welcome suggestions for more testing though.

Previous commits to add support for Operators mean that this is now supported in the java library.

Fixes: #902
Copy link
Member

@gj gj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! To my currently very tired eyes, this looks like a great start. I'm out tomorrow, but I'll make sure that this either (A) gets looked at more closely by someone else tomorrow or (B) is first thing on my plate Thursday morning.

@dhatch
Copy link
Contributor

dhatch commented Jul 14, 2021

@MFAshby added an acceptExpression flag on the Host class. We use this to protect users that are not expecting to receive a partial result from receiving expressions in the query output. All the python adapters set this flag to true when running queries, but by default it is False. The equivalent functionality in Python is here: https://github.com/osohq/oso/blob/main/languages/python/oso/polar/host.py#L237

Copy link
Member

@samscott89 samscott89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments/suggestions.

public Query query(Predicate query, Map<String, Object> bindings, boolean acceptExpression)
throws Exceptions.OsoException {
Host new_host = host.clone();
new_host.setAcceptExpression(acceptExpression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we make setAcceptExpression a method on the Query object that calls this? And then we don't need the multiple APIs for it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately Query gets a result when it is constructed from the core, so this parameter needs to be set on Host before query construction.

This also (roughly) matches the API in other languages, but I agree it's not ideal.


Predicate rule = new Predicate("f", List.of(x));
List<HashMap<String, Object>> results =
p.query(rule, Map.of("x", new TypeConstraint(x, "User")), true).results();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this working if ToPolar isn't implemented for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that it's not working, this might be the reason for 2 results instead of 1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working now, I added the implementation to the PR. I noticed this test was different though as ported. The python test expects 1 result.

Are there any other tests you changed while porting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to my knowledge. I erroneously thought this should return 2 results for some reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I think it's just because the python test looks for two arguments in the And expression. I got a bit confused myself reading it.

I checked the other tests & they look good to me!

Predicate rule = new Predicate("f", List.of(x));
List<HashMap<String, Object>> results =
p.query(rule, Map.of("x", new TypeConstraint(x, "User")), true).results();
assertEquals(2, results.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only have one result from the rule matching User, not the rule matching Post.

Copy link
Contributor Author

@MFAshby MFAshby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dhatch ! I didn't know the motivation behind that argument, but it makes sense now.


Predicate rule = new Predicate("f", List.of(x));
List<HashMap<String, Object>> results =
p.query(rule, Map.of("x", new TypeConstraint(x, "User")), true).results();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that it's not working, this might be the reason for 2 results instead of 1

@dhatch
Copy link
Contributor

dhatch commented Jul 14, 2021

It's possible that it's not working, this might be the reason for 2 results instead of 1

The same test in Python is also returning two results, but I would expect it to return one. I'm going to do a little investigation on that. I'll update you tomorrow @MFAshby!

@@ -191,6 +226,29 @@ public JSONObject toPolarTerm(Object value) throws Exceptions.OsoException {
"Call", new JSONObject(Map.of("name", pred.name, "args", javaListToPolar(pred.args))));
} else if (value != null && value instanceof Variable) {
jVal.put("Variable", value);
} else if (value != null && value instanceof Expression) {
Expression expression = (Expression) value;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I completely missed transforming in this direction.

Copy link
Member

@samscott89 samscott89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MFAshby
Copy link
Contributor Author

MFAshby commented Jul 16, 2021

Does the changelog need updating here or in another PR?

@gj gj merged commit 41aeaed into osohq:main Jul 16, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants