Jump to content

Help with the logic of combining "not matching" selectors


cjx2240
 Share

Recommended Posts

Hi, I have quite an unusual method of selecting pages. I need to search against 3 pairs of "min" and "max" fields, and return anything that overlaps any of the ranges. The only way I can see that this is possible, is to exclude results where both fields fall outside of the ranges. This is quite hard to wrap your head around (I know, I've had a hard enough time myself) but I feel like I have the logic right.

The integers used in this example are my search figures.

$pages->get("parent=1042, (!comp0_min_high>200, !comp0_max_override<100), (!comp1_min_high>50, !comp1_max_override<10), (!comp2_min_high>50, !comp2_max_override<10)");

So what should happen, as far as I understand is

(not A AND not B) AND (not C AND not D) AND (not E AND not F)

However, using Tracy Debugger to dig into the query, it seems to use "OR" between each of those subqueries, instead of "AND". Is this my fault? I thought separating selectors by a comma was essentially "AND".

Link to comment
Share on other sites

The round braces in selectors are named OR-groups for a reason, so you'll have to adapt your selector. You may want to remove the negation and switch ">" to "<=" and "<" to ">=" for easier readability.

Since I don't really know how

1 hour ago, cjx2240 said:

return anything that overlaps any of the ranges

maps to your data, it's difficult to tell you more. The docs I linked also explain to use named OR-groups where at least one selector expression of each named group has to match. To apply that to your example, this would e.g. read:

$pages->get("parent=1042, first=(!comp0_min_high>200, !comp0_max_override<100), second=(!comp1_min_high>50, !comp1_max_override<10), third=(!comp2_min_high>50, !comp2_max_override<10)");

However, if your logic is right, you can simply drop the parentheses since boolean AND expressions are commutative. (a AND b) AND c is the same as a AND b AND c or c AND b AND a.

  • Thanks 1
Link to comment
Share on other sites

21 minutes ago, BitPoet said:

boolean AND expressions are commutative. (a AND b) AND c is the same as a AND b AND c or c AND b AND a.

This could lead to different results when short-circuit evaluation is used. (But I don't know if this can occur with the Boolean expressions in PW selectors.)

  • Like 1
Link to comment
Share on other sites

47 minutes ago, ottogal said:

This could lead to different results when short-circuit evaluation is used. (But I don't know if this can occur with the Boolean expressions in PW selectors.)

Can you elaborate on that? I only know short circuit evaluation from programming languages / algorithms, and there it is a conscious efficiency or safety measure where you use "AND" in conditions to avoid unnecessary calls or expressions in later conditions, like preventing division by zero dumps by making sure the divisor is not zero in "if($count > 0 && $price / $count > $limit)". I can't imagine a scenario where a PW selector expression does not follow the basic laws of boolean algebra though. There should never be side effects caused by the order of selectors in a PW selector expression.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...