Order of boolean operations in Spring Data generated queries - spring

Would this Spring Data JPA method:
List<Foo> findAllByXOrYAndZ();
represent which logical case of the two:
(X || Y) && Z
X || (Y && Z)
And how can I represent the other one with Spring Data generated queries?

It represents: X || (Y && Z).
The Query that Hibernate will execute, in the case of List<Foo> findAllByXOrYAndZ();, would be:
select
*columns*
from
*table*
where
x = ?
or y = ?
and z = ?
Pay attention to the WHERE clause of the query. Knowing that AND has higher precedence over OR operation, the equivalent WHERE clause would be:
where
x = ? or ( y = ? and z = ? )
Hibernate automatically removes the parentheses because they are not needed.
I generally would avoid using such methods, because they are not very readable. You should use a Custom Query with #Query instead.

Related

How to convert logical statements into their opposites?

I have a scenario where I am checking whether to hide something depending on multiple variables values. For some these statements, if they evaluate to true, then I will skip over showing something (showing by default). However, I need to re-write my statements so that if they evaluate to true, then I will show something (hiding by default).
For Example:
If X !== 1 && Y !==1
For Example:
If X === 3 or Y === 3
If either example were true, then I would skip (hide) something.
I need to flip conditions the conditions written in the code so that the expressions are their opposites. I cannot just evaluate the original expression and then swap the end true/false result.
Would
X === 1 or Y === 1
And
X !== 3 && Y !== 3
Be the logical opposites respectively? How can I approach re-writing these expressions so that they would evaluate to their opposite result every time?
I cannot simply do !(If X !== 1 && Y !==1) in this situation. Would De Morgan's law be applicable to what I am trying to do? I'm considering just flipping all "===" to "!===" (and vice versa) and flipping all "ands/ors" so that I can get the results I want. This seems to work in cases I've tried, however I am not sure whether it would be working in "all" cases. Thank you!
if I follow what you're asking the inverse of
If X !== 1 && Y !==1
is:
if (X == 1 || Y == 1)
which is the same as:
if !(X !== 1 && Y !==1)
Here's how to think about it: In your example, both X and Y must not equal 1 to be true; therefore if either X or Y equal 1 the statement is false. Now convert that last sentence to boolean.
When inverting a logical statement, invert the arguments and the logical condition: "and" becomes an "or" and vise-versa.
If the goal is to negate the whole condition, the simplest way to do it is to just wrap the entire expression in !.
For example the following conditional clause:
If X !== 1 && Y !== 1
can be flipped by replacing it with:
If !(X !== 1 && Y !== 1)
Now, this is perfectly correct, but it may seem rather ugly to you. This is where DeMorgan's laws can be applied, to rewrite the expression in an equivalent, but possibly syntactically simpler form.
DeMorgan's laws state that:
!(x && y) === !x || !y, and that
!(x || y) === !x && !y.
So we can for example take our ugly (but correctly flipped) conditional clause above:
If !(X !== 1 && Y !== 1)
and use the first of DeMorgan's laws listed above to rewrite it (without changing the meaning at all):
If !(X !== 1) || !(Y !== 1)
and then by simple reasoning (!(a !== b) is equivalent to !(!(a === b)) is equivalent to a === b):
If X === 1 || Y === 1
and this is (also) a flipped version of the original clause If X !== 1 && Y !== 1.

Understanding precedence of assignment and logical operator in Ruby

I can't understand precedence of Ruby operators in a following example:
x = 1 && y = 2
Since && has higher precedence than =, my understanding is that similarly to + and * operators:
1 + 2 * 3 + 4
which is resolved as
1 + (2 * 3) + 4
it should be equal to:
x = (1 && y) = 2
However, all Ruby sources (including internal syntax parser Ripper) parse this as
x = (1 && (y = 2))
Why?
EDIT [08.01.2016]
Let's focus on a subexpression: 1 && y = 2
According to precedence rules, we should try to parse it as:
(1 && y) = 2
which doesn't make sense because = requires a specific LHS (variable, constant, [] array item etc). But since (1 && y) is a correct expression, how should the parser deal with it?
I've tried consulting with Ruby's parse.y, but it's so spaghetti-like that I can't understand specific rules for assignment.
Simple. Ruby only interprets it in a way that makes sense. = is assignment. In your expectation:
x = (1 && y) = 2
it does not make sense to assign something to 1 && y. You can only assign something to a variable or a constant.
And note that the purpose of the precedence rule is to disambiguate an otherwise ambiguous expression. If one way to interpret it does not make sense, then there is no need for disambiguation, and hence the precedence rule would not be in effect.
My understanding is that in the case of
x = 1 && y = 2
The logical AND is parsed first. The AND then is forced to evaluate its left side and its right side. In the evaluation the left side the first assignment occurs, and then in the evaluation of the right side the second does the same. It is for this reason that in the case of:
x = false && y = 2
"y" will never be assigned. The AND is hit, forcing the x assignment to evaluate, but never bothering to run the y assignment, as it is unnecessary for the AND to fulfill its purpose.
In other words, the parser is, again, simply to my understanding, smart enough to recognize that in running the AND the equation is split into a left and right side (equations), which are in turn evaluated according to the order of operation.

Qlikview - Count IF A = X AND B != Y OR Z

As the title says, I'm trying to get a count of data that follows the logic above. What I have so far;
=COUNT(IF(A='X', A AND IF(B <> 'Y' OR B <> 'Z', B)))
I'm aware I could do a set analysis, but A) doing so makes it so that selecting a different value of A still shows all data where A = X and B) I want to figure this out.
First, I'd encourage using set analysis. If you're going this route, however, I think that it's easier to read if you switch to using the Sum function:
Sum(If(A='X' AND (B <> 'Y' OR B <> 'Z'), 1, 0))

SICStus Prolog making product/3 rule from sum/3

First of all, this is a homework question, so please just give me a hint!
%Here is a rule that defines sum/3 that returns yes if Z is sum of X and Y
sum(X,Y,Z) :-
Z is X + Y.
%How can I make product/3
product(X,Y,Z) :- % based on sum/3 above?
Also, how can write a query on product such that it returns the answer of X * Y and not that it's merely true?
Consider that in mathematics:
x * 0 = 0
x * y = x + x * (y - 1)
That should help you write your rules.
As for a query, you can use something like this to get a result like this:
?- product(5, 3, Result).
Result = 15 ?
yes
In short, if you have an unbound variable in a query, it tries to find a value for that variable such that the predicate succeeds.

Syntax or construct to simplify if() statement?

I'm looking for a semantic or language construct that will simplify some of my if statements. If I have an if statement with an or, where I 'choose' between two values, I'd like to have that chosen variable available later on in the code.
I'll write this in pseudo-code:
if ( x or y ) {
function(z);
}
Where z is the value of x or y, whichever triggered the if to be true. Otherwise I'm writing
if ( x ) {
...
function(x);
}
if ( y ) {
...
function(y);
}
Now in that example the duplication isn't much, but in my actual situation, there are about 6 lines of code that would be identical duplication within each if block. So I write something like:
if ( x ) {
z = x;
}
if ( y ) {
z = y;
}
if ( z ) {
...
function(z);
}
But that just seems so roundabout I suspect that somewhere along the line some guru came up with a nice syntax to make a one-line if. Is there a construct in a language anywhere where I can bind the triggering value into a variable in the subsequent block? What would this structure be called?
The actual situation I'm working in is in PHP, so I realize PHP may not have this capability.
In Python (also Ruby), the expression x or y evaluates to x if it is true, otherwise y. You could write the statement as f(x or y), or z = x or y.
For this exact situation. You can use the ? operator:
z = x ? x : y;
Which reads: z is (if x is not false) x otherwise it is y.
Actually, something like
if ( $z = ( $x ? $x : $y ) ) {
function($z);
}
Works as advertised in PHP. Thanks, slebetman!

Resources