Cloudformation condition for string parameter length - conditional-statements

In my cloudformation code (yaml file) I want to write condition that check if string parameter is longer than some value(lets say 15) and I did not find any way to do it.
Two main questions are,
How to get string parameter length?
After I have the length, how can I write condition that check if this length is longer/smaller than specific value?
I wish cloudformation has a Fn like LongerThan or something like this
Conditions:
isLongerThanParam:
'Fn::LongerThan':
- {Ref: Param}
- '15'
But they only support these functions: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html

I did not find any way to do it
There is no such functionality in CFN. You already provided the link to available conditional checks, and there are no condition to check if something is greater or lower then something else.
Some possible workaround would possibly involve development of CloudFormation macro, which would be in the form of a lambda function. The function would take your Fn::LongerThan, and performed some transformations of it based on how you want it to behave. As an outcome of the macro, the valid conditions would be returned, whether or not Fn::LongerThan is satisfied.

Related

Setting up multiple schedules for a ResourcePool

I am doing a simulation of a production line, which is built through Excel.
Now there is one, which needs free setting of worker shifts for each machine, three shifts in total.
My idea is to create three different Schedules, representing each of the three shifts, and then in the ResourcePool, use the If statement to set up the use of each shift. As shown in the picture enter image description here
But it didn't work. If it is possable, Could you please tell me if there is something wrong with the Java statement or the idea is wrong.
If possible, could you please tell me how to set multiple schedules in ResourcePool?
Additions to the question:
The model is to do the evaluation of the production system and the shift of the workers is an important evaluation parameter. What I want to do is to enter the shift of the workers directly in the Excel sheet and ResourcePool recognizes and uses the corresponding Schedule.
I tried to generate the corresponding Schedule by code, but the Schedule of Resourcepool cannot be set dynamically. So I would like to try to manually input multiple Schedules into the model and then set the corresponding Schedule in ResourcePool by If statement like the image.
Thanks in advance
The first thing you need to know is that the location where you placed the code is a static parameter, thus it is only evaluated once, when the object is created and not checked continuously.
This is indicated by the little popup when you hover over the button that change the entry field from code to value
If it was dynamic it would state Dynamic Value instead of Static Value
Secondly, inside that field you must use a ternary operator, not an if statement, so that the result of the formula is a scheduled object, else you will get a "misplaced construct(s)" error
If you changed the code to
v_Shift == "Shift1" ?
s_Shift1 : v_Shift == "Shift2" ? s_Shift2 : s_Shift3
It will work BUT:
It will only be evaluated when the object is created and not again
Rather not use == on Strings, always use .equals(), on Strings it might work, and sometimes it might not. You can do some research as to why ;-)
Solution: You will have to use a function to change the schedule of the resource pool. Call this function whenever the v_Shift variable changes

ANTLR4 - replace op boundaries error|How to use TokenStreamRewriter to transform text from two listener events on overlapping tokens in original AST?

Hello ANTLR creators/users,
Some context - I am using PlSql ANTLR4 parser to do some lightweight transpiling of some queries from oracle sql to, let's say, spark sql. I have my listener class setup which extends the base listener.
Example of an issue -
Let's say the input is something like -
SELECT to_char(to_number(substr(ATTRIBUTE_VALUE,1,4))-3)||'0101') from xyz;
Now, I'd like to replace || with CONCAT and to_char with CAST as STRING, so that the final query looks like -
SELECT CONCAT(CAST(to_number(substr(ATTRIBUTE_VALUE,1,4))-3) as STRING),'0101') from xyz;
In my listener class, I am overriding two functions from base listener to do this - concatenation and string_function. In those, I am using a tokenStreamRewriter's replace to make the necessary transformation. Since tokenStreamRewriter is evaluated lazily, I am running to issue ->
java.lang.IllegalArgumentException: replace op boundaries of
<ReplaceOp#[#38,228:234='to_char',<2193>,3:15]..[#53,276:276=')',
<2214>,3:63]:"CAST (to_number(substr(ATTRIBUTE_VALUE,1,4))-3 as STRING)">
overlap with previous <ReplaceOp#[#38,228:234='to_char',<2193>,3:15]..
[#56,279:284=''0101'',<2209>,3:66]:"CONCAT
(to_char(to_number(substr(ATTRIBUTE_VALUE,1,4))-3),'0101')">
Clearly, the issue is my two listener functions attempting to replace/transform text on overlapping boundaries.
Is there any work around for territory overlap kind of issues for ANTLR4? I'm sure folks run into such stuff all the time probably.
I'd appreciate any workarounds, even dirty ones at this point of time :)
I did realize that ANTLR4 does not allow us to modify original AST, otherwise this would have been a little bit easier to solve.
Thanks!
A look at how tokenstreamrewriter works leads to the following understanding:
first, a list of all modification operations are built
then, you invoke getText()
here, there is a reduction of modification operations. The idea for example is to merge multiple insert together in one reduction. Its role is also to avoid multiple replace on same data (but i will expand on this point later).
every token is then read, in the case there is a modification listed for the said token index, TokenStreamRewriter do the operation, otherwise it just pop the read token.
Let's have a look on how modification operations are implemented:
for insert, tokenstream rewriter basically just adds the string to be added at the current token index, and then do an index+1, effectively going to next token
for replace, tokenstream rewriter replace a range of tokens with the new string, and set the new index to the end of this range.
So, for tokenstreamrewriter, overlapping replaces are not possible, as when you replace you jump to the end of the range of tokens to be replaced. Especially, in the case you remove the checks of overlapping, then only the first replace will be operated, as afterwards, the token index is past the other replaces.
Basically, this has been done because there is no way to tell easily what tokens should be replaced while using overlapping replaces. You would need for that symbol recognition and matching.
So, what you are trying to do is the following (for each step, the part between '*' is what is modified):
*SELECT to_char(to_number(substr(ATTRIBUTE_VALUE,1,4))-3)||'0101')* from xyz;
|
V
CONCAT (*to_char(to_number(substr(ATTRIBUTE_VALUE,1,4))-3)*,'0101') from xyz;
|
V
SELECT CONCAT(CAST(to_number(substr(ATTRIBUTE_VALUE,1,4))-3) as STRING),'0101') from xyz;
to achieve your transformation, you could do so a replace of :
'to_char' -> 'CONCAT(CAST'
'||' -> ' as STRING),'
And, by using a bit of intelligence while parsing your tokens, like is there a '||' in my tokens to know if it's string, you would know what to replace.
regards
The way I solve it in multiple projects based on ANTLR is this: I translated ANTLR parse-tree to an AST written using Kolasu, an open-source library we developed at Strumenta.
Kolasu has all sort of utilities to process and mutate ASTs. For all non-trivial projects I end up doing transformations on the AST.
Kolasu

AnyLogic - Why does my condition based transition not work?

I have a question related to a transition in my statechart (see image above). I have a variable called palletInUse which is a boolean-type and changes between true and false. For one transition in my statechart I want it to change when the variable palletInUse has the value true. I have tried it with for example:
palletInUse == true;
and also tried different code like, equals and contentEquals etc. but nothing seems to work. Do you have a solution for this, seemingly simple problem?
Thanks in advance
The condition is not monitored constantly, only when something is changed in the agent. When you assign a new value to variable with common "=" Java operator, it is not caught by the AnyLogic engine. You need to call onChange() function after that. Then, the transition should be executed.
There are other ways to trigger the condition check without explicit onChange() call. You may find them in AnyLogic Help article.
BTW, you may specify just boolean variable as the condition, it is not required to compare it with true or false:
palletInUse
The condition is not evaluated if nothing is happening, for that reason you have to make something happen constantly to have your condition evaluated. A typical way of doing is as you see in the following pictures:

JMeter Random Variable Element not recognise variable from previous step

tl;dr
When I use my variable created in Regular Expression Extractor I cannot use it in Random Variable as Maximum Value
Long description:
My test structure:
I have variable my_test what is crated in Regular Expression Extractor
request: GET //echo.getpostman.com/get?test=123
regex:
Then I want it use as Maximum Value in Regular Expression Extractor
So finally I can make request:
//echo.getpostman.com/get?test=${rand}
Unfortunately I get error from Random Variable
2016/10/07 07:52:41 ERROR - jmeter.config.RandomVariableConfig: maximum(${my_test}) must be > minimum1)
Why my_test is not evaluated?
I have tried ${__javaScript(parseInt('${my_test}'))} but it looks like it is evaluated before my variable initialization
2016/10/07 08:06:01 ERROR - jmeter.config.RandomVariableConfig: maximum(NaN) must be > minimum1)
If I initialize this variable in Test Plan in User Defined Variables value from that setting will be used - not updated by regex.
I know that I can do //echo.getpostman.com/get?test=${__Random(0,${my_test})}
I'm just curious how pass my variable as value for Maximum Value in Regular Expression Extractor.
Random Variable is a Config Element and it will be executed first before any other components get executed first.
4.9 Execution order
Configuration elements
Pre-Processors
Timers
Sampler
Post-Processors (unless SampleResult is null)
Assertions (unless SampleResult is null)
Listeners (unless SampleResult is null)
If two or more Config elements present in the Test Plan, then they will be executed in the order they appear in the Test Plan.
Check the execution order and Scope here:
Refer 4.9 7 4.10 here Execution Order and Scope Rules
So, first Random Variable is evaluated first and then Sampler and then regular expression extractor.
When you used User Defined Variables, which is another Config Element, and probably you put it before Random Variable, so it evaluated the expression as you already defined the value for "my_test". But it won't override the value you captured in Regular Expression Extractor.
To solve your problem (one probable solution):
you can use different thread groups. In first thread group, you capture the value and in second thread group, you use the value.
Run Thread Groups consecutively.
Use BeanShell Assertion to capture the value by setProperty. (in first thread group)
Use value using __property() (in thread group)
https://www.blazemeter.com/blog/knit-one-pearl-two-how-use-variables-different-thread-groups
It seems that Random Variable element does not evaluate variables, maybe it worth creating an issue in JMeter Issue Tracker
As a workaround you can substitute it with __Random() function directly where required like:
${__Random(1,${my_test},)} - if you need the value right away, directly in you URL:
//echo.getpostman.com/get?test=${__Random(1,${my_test},)}
${__Random(1,${my_test},rand)} - if if you need to store the value into ${rand} variable as well
See:
Using JMeter Functions article for extended information on this and others JMeter Functions.
Function Helper Dialog - an utility helping to generate correct function syntax.
I think it's valid for me to comment on a problem I had, I set the random variable and it didn't work at all, I did everything I could to try to solve it.
In the end my problem was a space at the beginning of the variable.
One possibility is you can use a Beanshell Postprocessor to write the RegEx value to the variable name.
After that you can use it as ${variable_name}
NB: Beanshell function vars.get can be used for getting regex value and vars.put can be used for putting it into your variable.

Use String for IF statement conditions

I'm hoping someone can help answer my question, perhaps with an idea of where to go or whether what I'm trying to do is not possible with the way I want to do it.
I've been asked to write a set of rules based on the data held by our ERP form components or variables.
Unfortunately, these components and variables cannot be accessed or used outside of the ERP, so I can't use SQL to query the values and then build some kind of SQL query.
They'd like the ability to put statements like these:
C(MyComponentName) = C(MyOtherComponentName)
V(MyVariableName) > 16
(C(MyComponentName) = "") AND V(MyVariableName) <> "")
((C(MyComponentName) = "") OR C(MyOtherComponentName) = "") AND V(MyVariableName) <> "")
This should be turned into some kind of query which gets the value of MyComponentName and MyOtherComponentName and (in this case) compares them for equality.
They don't necessarily want to just compare for equality, but to be able to determine whether a component / variable value is greaterthan or lessthan etc.
Basically it's a free-form statement that gets converted into something similar to an IF statement.
I've tried this:
Sub TestCondition()
Dim Condition as string = String.Format("{0} = {1}", _
Component("MyComponent").Value, Component("MyOtherComponent").Value)
If (Condition) Then
' Do Something
Else
' Do Something Else
End If
End Sub
Obviously, this does not work and I honestly didn't think it would be so simple.
Ignoring the fact that I'd have to parse the line, extract the required operators, the values from components or variables (denoted by a C or V) - how can I do this?
I've looked at Expression Trees but these were confusing, especially as I'd never heard of them, let alone used them. (Is it possible to create an expression tree for dynamic if statements? - This link provided some detail on expression trees in C#)
I know an easier way to solve this might be to simply populate the form with a multitude of drop-down lists, so users pick what they want from lists or fill in a text box for a specific search criteria.
This wouldn't be a simple matter as the ERP doesn't allow you to dynamically create controls on its forms. You have to drag each component manually and would be next to useless as we'd potentially want at least 1 rule for every form we have (100+).
I'm either looking for someone to say you cannot do this the way you want to do it (with a suitable reason or suggestion as to how I could do it) that I can take to my manager or some hints, perhaps a link or 2 pointing me in the right direction.
If (Condition) Then
This is not possible. There is no way to treat data stored in a string as code. While the above statement is valid, it won't and can't function the way you want it to. Instead, Condition will be evaluated as what it is: a string. (Anything that doesn't boil down to 0 is treated as True; see this question.)
What you are attempting borders on allowing the user to type code dynamically to get a result. I won't say this is impossible per se in VB.Net, but it is incredibly ambitious.
Instead, I would suggest clearly defining what your application can and can't do. Enumerate the operators your code will allow and build code to support each directly. For example:
Public Function TestCondition(value1 As Object, value2 As Object, op as string) As Boolean
Select Case op
Case "="
Return value1 = value2
Case "<"
Return value1 < value2
Case ">"
Return value1 > value2
Case Else
'Error handling
End Select
End Function
Obviously you would need to tailor the above to the types of variables you will be handling and your other specific needs, but this approach should give you a workable solution.
For my particular requirements, using the NCalc library has enabled me to do most of what I was looking to do. Easy to work with and the documentation is quite extensive - lots of examples too.