sparql query if statement with retrieval and variable - if-statement

My main need is to use something like switch cases in other programming languages.
I have a ?x g1:state (true OR false OR UNDEF) The latest is a variable incoming from a REST API and I want to perform different things based on that value
What I want to achieve is: if ?x g1:state true, bring me all the true triples. If it is false, bring me all the false triples that correspond. If the latest is undefined, bring me all the triples with whatever values. Something like this.
Bind ( if ('+test+'=true, ?x g1:state true, Bind ( if ('+test+'=false, ?x g1:state false, ?x g1:state ?y1) as ?y) as ?xf)

Ι bypassed this obstacle with a different approach, outside the sparql query
var booleanvar = req.params.booleanvar
if (booleanvar==='true' || booleanvar==='false'){
} else {
booleanvar= '?'+booleanvar
}
and then I just added a simple triple statement into the query
?x g1:state '+booleanvar+'

Related

Print one result after combining multiple expression results using jq

I want to have the output of either true or false.
I have this syntax
.[] | (if (select(.displayName=="display")) then "yes" else "no" end)
This is the json source
[{"displayName":"display","message":"bla bla"}, {"displayName":"test","message":"bla bla"}]
I only need to query against the array and if the value im looking for exists in one of them I need the output to be "yes", and if it doesnt a "no". So a single output value is what I am looking for.
This evaluates to "yes", however, if the value display is not present in the json, it does not output "no", its just empty. Can anyone tell me why?
Here is a snippet to try: https://jqplay.org/s/WKcZh91hk8L
The idea is right, but you shouldn't be using select for this. The way select works is by evaluating the boolean expression provided inside (..) and returning the object, if the expression evaluates to true and skip the object from being considered/printed on false.
On your code, for the object that evaluates to false, select is evaluated as select(false) which as explained above returns nothing - this is not evaluated to boolean false condition. i.e. the return value of select is not a boolean value.
To use explicit boolean conditions, drop the select from expression and to summarise the result to a single value do something like below
if ( [ .[] | .displayName == "display" ] | any ) then "yes" else "no" end
Demo on jqplay
The way this works, is any can evaluate an array of boolean values and produces true as output if any of the elements of the array are true. We collect the match condition results to the array and apply any on it. i.e. [true, false] evaluates to true, thereby printing yes only once
A concise solution is possible with select:
select( any(.displayName=="display") ) | "yes" // "no"

If Statements in Lua

In all other programming languages I've encountered, if statements always require a boolean to work, however in Lua the following code does not contain any errors. What is being checked in this if statement if both true and false statements can't be made? How can you only check "if (variable) then"? I am new to programming and currently working with Roblox Studio, any help would be very appreciated.
function onTouched(Obj)
local h = Obj.Parent:FindFirstChild("Humanoid")
if h then
h.Health = 0
end
end
script.Parent.Touched:Connect(onTouched)
Most languages have their own rules for how values are interpreted in if statements.
In Lua, false and nil are treated as false. All other values are treated as true.
if h == nil (null)
So if it couldn't find a humanoid in the object that touched the script's parent, it will be false (null), otherwise true (not null).
So
if [ObjectName] then
equals to if [ObjectName] != null then
*Only valid for objects (non primitive values)
It's like that in script languages.
if h then end
is basically equivalent to
if h ~= nil and h ~= false then end
In Lua all values that are not nil or false are considered to be logically true.
if h then end is usually used to check wether h is not nil. So if code depends on wether h has been defined you put it in a condition like that.
In your example h is being index. indexing nil values is not allowed. So befor you index it you should make sure it isn't nil to avoid errors.
Checking return values of functions is good practice.

(XQuery/Conditions) Is it possible to declare variables in an if-statement?

I do not find an example for my problem so here is my question.
I get an error that else is an unexpected token in the following example:
let $var1 := 'true'
if ($var1 = 'true') then
let $var2 := 1
let $var3 := $var1 + 1
else ()
As you see I want to declare variables if the if-statement is true. Is this possible in XQuery? I just saw examples where the value of just one variable can depends on a condition. The following does more or less the same I want to realize with the code at the beginning.. and it works but it is a little bit confusing in my opinion and actually I don't want the variables to be created if the condition is not true. Furthermore you have to think around the corner when you realize it like that especially when there are more than just 2 variables that depends on each other.
let $var1 := 'true'
let $var2 := if ($var1 = 'true') then (1) else (0)
let $var3 := if ($var2 = 1) then ($var2 + 1) else (0)
So my question is. Is there a prettier solution to realize that than my solution?
You could add a return clause to put a full flwor expression inside the condition, e.g. something like this:
let $var := 'true'
if ($var = 'true') then
let $var2 := 1
let $var3 := $var1 + 1
return 0
else ()
But it would be pointless: the binding of $var2 and $var3 would not extend outside of the scope of the then clause.
XQuery is a declarative and functional language, which means that variables do not get assigned, but only bound within a certain scope. This is something that should be thought about in term of space, not time, as there is no elapse of time in an XQuery program, like a ticket allows you to visit a museum but not another.
Let clauses are part of FLWOR (acronym for for-let-where-orderby-return) expressions. A variable bound in a let clause can be used in subsequent clauses, up to and including the return clause. As mholstege explains, beyond the return clause, which is required, the variable is not visible any more, like nobody would accept your ticket outside the museum.
Since expressions nest in a "well-parenthesized" way according to the XQuery grammar, any attempt to start a let clause inside an if-then-else expression requires that a return clause be present before the then (or else) expression ends. This means that a variable bound this way will never be visible after this if-then-else expression.
In general, when I program in XQuery (as opposed to, say, Java), I try to remind myself continuously that I have to write down what I want, and resist the temptation to describe how I want it computed.
Having said that, XQuery does have scripting extensions that introduce variable assignments as you describe, but this did not get standardized so far -- also, such a scripting extension should only be used when side effects to the outside world happen, meaning that one needs a notion of time and succeeding snapshots.
You could avoid using if/else altogether by defining sequences for your possible values, and a predicate that calculates the position() to select the desired value from the sequence:
The following uses number() to evaluate the numeric value of a boolean (0 for false, 1 for true) and selects either the first or the second item in the sequence of values:
let $var1 := 'true'
let $var2 := (0, 1)[number($var1 = 'true') + 1]
let $var3 := (0, $var2 + 1)[number($var2 eq 1) + 1]
return ($var1, $var2, $var3)

How to add facts through rules in Jess?

I tried reading tutorials about Jess, but I can't find anything very helpful. I want to build a program which finds out which instrument I'm talking about.
So, if an instrument has strings, we know that the instrument is either in the strings or percussion (i.e. piano) category. How would I write a rule that saves a fact saying the category is either percussion or strings based on this criteria?
I considered bind, but doesn't bind mean that I would have to have a separate variable for each potential category? Or, should I use an assert?
This demonstrates how to insert a fact from within a rule to store a set of possible categories.
(deftemplate Instrument (slot strings))
(deftemplate Classification (multislot category))
(defrule cat-by-strings
?i <- (Instrument (strings ?s&:(> ?s 0)))
=>
(assert (Classification (category STRING PERCUSSION)))
)
(assert (Instrument (strings 18)))
(run)
(facts)
Output:
f-0 (MAIN::initial-fact)
f-1 (MAIN::Instrument (strings 18))
f-2 (MAIN::Classification (category STRING PERCUSSION))
For a total of 3 facts in module MAIN.
Using bound variables is useless as they are limited to the context of a rule.

Cond If And Or in Racket

Can someone please explain these functions for me in racket. I`m totally lost. Please help me with some examples. Thanks! I just cannot figure these functions out for the life of me.
First, the If:
(if (positive? 1) 1 -1)
Racket first evaluates if 1 is positive (which is the first expresion (positive? 1)). If it is, returns 1, else, return -1. This is equivalent to c-like languages doing:
if ( positive?(1))
return 1
else
return -1
The Cond is basically a if that has multiple options. The equivalent in C-like languages would be else-if
(cond [(first-condition) (what-to-do)]
[(second-condition) (what-to-do)]
[(third-condition) (you-get-the-idea)])
And and Or are just the logical operators, equivalent to && and || in C-like languages
(and true true) => true
(and true false) => false
(or true true) => true
(or true false) => true
(or false false) => false
If is the turnary operator. It has three arguments, unless the first argument has a value of #f it will return the value of the second argument, otherwise the value of the third argument.
OR accepts one or more arguments, evaluates them one at a time left to right, and returns the first value if finds that is not #f, and returns #f if no argument satisfies that condition.
AND accepts one or more arguments evaluate them one at a time left to right, if it finds one that is #f it returns #f, otherwise it returns the value of the last argument that it evaluates.
COND accepts one or more arguments, each with one or more sub arguments (2 is the usual number though). It evaluates each argument left to right one at a time by evaluating the first subargument. If it is not #f then it evaluates each of the rest of the sub-arguments (if any) in order and returns the value of the last argument. Otherwise it moves onto the next argument and evaluates it the same way. else is a special sytax within these sub-arguments that is always treated as if it is not #f.
And here argument is understood to mean any valid s-expression.
NB: If you are familiar with non-lisp languages this is the answer for you. I'm not trying to explain them in other way than other code. Other answers do however so this is just a supplement.
None of those are functions, but special forms.
(if predicate
consequent
alternative)
is pretty much like Algol-dialects if
if( predicate )
{
consequent
}
else
{
alternative
}
Note that predicate, consequent and alternative can be anything from a complex expression to a simple value.
cond works more like an if, elseif..., else:
(cond (predicate1 consequent1)
(predicaten consequentn)
(else alternative))
and works like && in algol dialects. Thus false && print("Do it!") doesn't print anything since it short circuits.
(and #f (display "Do it!")) ; ==> #f (and does not display anything since first term was false)
(and 1 2 3) ; ==> 3 (3 is the last true value. In Scheme everything except #f is true.)
or works like || in algol dialects. thus true || print("Do it!") doesn't print since first term was true.
(or 'mama (display "Do it")) ; ==> mama (first true value, does not print "do it")