I am trying to get an MVEL expression to work but am having probelms. I am tyring to determine if date defined as a property falls between two other dates.
props['ExistingStartDate'] >= props['current_period_start_date'] && props['ExistingStartDate'] <= props['current_period_end_date']
So in this case, my ExistingStartDate = 3/6/14, current_period_start_date = 3/3/14 and current_period_end_date = 3/16/14
I am expecting this to be true. I feel like there must be something wrong with my syntax. Any help would be appreciated!
Use parentheses for each term for multiple conditions:
(props['ExistingStartDate'] >= props['current_period_start_date']) && (props['ExistingStartDate'] <= props['current_period_end_date'])
Also, the props references may not have the correct syntax depending on what type of Java object it is.
Related
I think the answer is probably 'no' as from what I can tell maths libraries don't seem to handle null, but what I'd like to have is to be handle null in these scenarios:
x=1
(y not defined)
z = sum(x, y)
with eval() ideally giving:
z=1
However, this throws an exception, as y is not defined.
In effect I'm defaulting 'y' to zero in that example, and that won't work in all scenarios.
So, what I suppose I'm asking is, is there an in-built way to define a default for a variable if it's not assigned a value?
If not, I'm assuming that this would be possible by writing a custom function. If someone could confirm this, I'd really appreciate it.
Thanks :-).
Ok - I was looking through the release notes for v4 and saw it talking about the constants 'null, undefined' (and others). So, in order to solve this I have to define a variable as undefined (probably should have known this!).
(Note: I suspect these constants are available in previous version too.)
Then combined with a 'default' custom function I can get what I wanted:
x=1
y = null
z = sum(x, def(y, 0))
z=1
where 'def' is defined and imported as below (making use of 'lodash'):
var customFunctions = {
def: function (value, defaultValue) {
return !_.isNil(value) ? value : defaultValue;
}
};
math.import(customFunctions);
BOSH!
Is it possible (and if so how) to use an objective function that has a conditional expression?
Changing the example from the docs, I would like an expression like:
def objective_function(model):
return model.x[0] if model.x[1] < const else model.x[2]
model.Obj = Objective(rule=objective_function, sense=maximize)
Can this be modelled directly like this or do I have to consider some sort of transformation (and if so how would this look like)?
Just executing the above gives an error message like:
Evaluating Pyomo variables in a Boolean context, e.g.
if expression <= 5:
is generally invalid. If you want to obtain the Boolean value of the
expression based on the current variable values, explicitly evaluate the
expression using the value() function:
if value(expression) <= 5:
or
if value(expression <= 5):
which I think is because Pyomo thinks I'd like to obtain a value, instead of an expression with the variable.
One way to formulate that is by using a logical disjunction. You can look into the Pyomo.GDP documentation for usage, but it would look like:
m.helper_var = Var()
m.obj = Objective(expr=m.helper_var)
m.lessthan = Disjunct()
m.lessthan.linker = Constraint(expr=m.helper_var == m.x[0])
m.lessthan.constr = Constraint(expr=m.x[1] < const)
m.greaterthan = Disjunct()
m.greaterthan.linker = Constraint(expr=m.helper_var == m.x[2])
m.greaterthan.constr = Constraint(expr=m.x[1] >= const)
m.lessthanorgreaterthan = Disjunction(expr=[m.lessthan, m.greaterthan])
# some kind of transformation (convex hull or big-M)
You can also formulate this using complementarity constraints.
I am very new to EPL queries.
Wrote this and it is throwing syntax error.
#Name('ExpressionTotalQuantitySoFar')
#Description('Gets the total quantity of a symbol so far')
create expression totalQuantitySoFar{ (TAX) =>
(Select sum(T.quantity) from TaxlotWindow as T where T.symbol = TAX.symbol and T.taxlotId < TAX.taxlotId)
};
create variable double totQty = 5.0 ;
#Name('ExpressionLongDebitBalanceTaxlotNoBox')
#Description('Check is if a trade side is invalid, returns rue for invalid statements')
create expression longDebitBalanceTaxlotNoBox{ (SECUR,TAX,ORD,AUE,FX) =>
totQty = totalQuantitySoFar(TAX)
case when (totQty > 0)
then cashImpactBase(SECUR,TAX,ORD,AUE,FX)*(-1)
else
0.0
end
};
It gives syntax error near case.
Any help?
Always include the syntax error text when posting. Else how is one supposed to be able to help.
My tip would be to simplify until the syntax is fine. Then add back stuff.
Most likely this strange declaration "totQty=.." is the cause as its wrong. EPL expressions are not a programming language and don't allow variable declarations like in Java or Scala. Perhaps just use a Java static method to compute instead of you need a programming language.
I'm migrating to Neo4j 1.9 and updating the cypher queries according to the deprecations migration guide, specifically:
The ! property operator in Cypher Expressions like node.property! =
"value" have been deprecated, please use has(node.property) AND
node.property = "value" instead.
The problem is that when using the HAS clause in combination with NOT I can't get the expected result. For example:
When the status property exists and is set to something other than "DONE":
AND not(n.status! = "DONE")
evaluates true (as expected)
AND not(has(n.status) AND n.status = "DONE")
evaluates false???
When the status property doesn't exist
AND not(n.status! = "DONE")
evaluates false
AND not(has(n.status) AND n.status = "DONE")
throws exception because the node doesn't exist, surely HAS should have prevented this? It's as though wrapping the check in NOT prevents the HAS check from being executed.
This can be reproduced via the live query examples on the neo4j docs website using the following queries:
MATCH n
WHERE NOT (n.name! = 'Peter')
RETURN n
This returns all (3) nodes who either have no name or whose name is not "Peter". This is the result I want to reproduce but without using the now deprecated "!" operator.
MATCH n
WHERE NOT (HAS (n.name) AND n.name = 'Peter')
RETURN n
Throws a node not found exception because one node doesn't have a name property. :/
MATCH n
WHERE (HAS (n.name) AND n.name = 'Peter')
RETURN n
Correctly returns the node whose name is "Peter".
I've tried rewriting the query in a few alternative ways but can't seem to reliably get the result I want that matches the deprecated ! operator. Maybe it's just getting late and I'm missing something obvious? :)
Any Help is appreciated!
Thanks,
Mark.
I think the second query raising an exception is a bug.
What you can do is use de Morgan's law to transform the predicate:
MATCH n
WHERE NOT (HAS (n.name)) OR (n.name <> 'Peter')
RETURN n
I have hack I need to employ under these conditions:
It's the last page of data.
It's not the first page, either.
There's not a page-size-even number of data items.
So I tried this code:
my $use_hack =
$last_page_number == $current_page_number and
$page_number != 1 and
$total_items % $items_per_page != 0;
And I keep getting this warning Useless use of numeric ne (!=) in void context about the last condition and it's evaluating true when $total_items % $items_per_page = 0.
say 'NOT EVEN' if $total_items % $items_per_page != 0; #works properly, though...
I've tried various combinations of parentheses to get it right, but nothing seems to work.
Okay, operator precedence. and has almost the lowest precedence of any operator in Perl, so Perl was evaluating the expression in a weird order. Switching to && instead got me correct results. Blarg.
The More You Know.
EDIT:
As Philip Potter pointed out below, Perl Best Practices (p.70) recommends always using &&,||, ! for boolean conditions - limiting and or and not for control flow because of their low precedence. (And it even goes so far as to say to never use and and not, only or for fallback logic.)
Thanks, everybody!
Enclose the RHS in parenthesis:
my $use_hack = (
$last_page_number == $current_page_number and
$page_number != 1 and
$total_items % $items_per_page != 0);
Assignment (=) is having higher precedence when compared to and operator. You can take a look at the Perl Operator Precedence.