Is there an alternative to round a number in Apache Velocity without using MathTools? - velocity

I need to round a quantity in Apache Velocity, but it is supposed I'm not allow to change pom.xml at work, so, is there an alternative to skip MathTools?
I tried this example:
#set($String = "abc")
$String.format("%.2f", $val)
but it's not working,
Any help will be thankful.

Didn't you forget an hash sign before set? The following is working for me:
#set($val=0.1294)
#set($String = "abc")
$String.format("%.2f", $val)
Output:
0.13

Related

Many inputs to one output, access wildcards in input files

Apologies if this is a straightforward question, I couldn't find anything in the docs.
currently my workflow looks something like this. I'm taking a number of input files created as part of this workflow, and summarizing them.
Is there a way to avoid this manual regex step to parse the wildcards in the filenames?
I thought about an "expand" of cross_ids and config["chromosomes"], but unsure to guarantee conistent order.
rule report:
output:
table="output/mendel_errors.txt"
input:
files=expand("output/{chrom}/{cross}.in", chrom=config["chromosomes"], cross=cross_ids)
params:
req="h_vmem=4G",
run:
df = pd.DataFrame(index=range(len(input.files), columns=["stat", "chrom", "cross"])
for i, fn in enumerate(input.files):
# open fn / make calculations etc // stat =
# manual regex of filename to get chrom cross // chrom, cross =
df.loc[i] = stat, chrom, choss
This seems a bit awkward when this information must be in the environment somewhere.
(via Johannes Köster on the google group)
To answer your question:
Expand uses functools.product from the standard library. Hence, you could write
from functools import product
product(config["chromosomes"], cross_ids)

Unable to query using 4 conditions with WHERE clause

I am trying to query a database to obtain rows that matches 4 conditions.
The code I'm using is the following:
$result = db_query("SELECT * FROM transportesgeneral WHERE CiudadOrigen LIKE '$origen%' AND DepartamentoOrigen LIKE '$origendep' AND DepartamentoDestino LIKE '$destinodep' AND CiudadDestino LIKE '$destino%'");
But it is not working; Nevertheless, when I try it using only 3 conditions; ie:
$result = db_query("SELECT * FROM transportesgeneral WHERE CiudadOrigen LIKE '$origen%' AND DepartamentoOrigen LIKE '$origendep' AND DepartamentoDestino LIKE '$destinodep'");
It does work. Any idea what I'm doing wrong? Or is it not possible at all?
Thank you so much for your clarification smozgur.
Apparently this was the problem:
I was trying to query the database by using the word that contained a tittle "Petén" so I changed the database info and replaced that word to the same one without the tittle "Peten" and it worked.
Now, im not sure why it does not accept the tittle but that was the problem.
If you have any ideas on how I can use the tittle, I would appreciate that very much.

Mule ESB: How to do Condition checking in Datamapper using Xpath

i'm facing issue in xpath-I need do a check two attribute values, if the condition satisfies need to do hard code my own value. Below is my xml.
I need to check the condition like inside subroot- if ItemType=Table1 and ItemCondition=Chair1 then i have to give a hard coded value 'Proceed'( this hard coded value i will map to target side of datamapper).
<Root>
<SubRoot>
<ItemType>Table1</ItemType>
<ItemCondition>Chair1</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
<SubRoot>
<ItemType>Table2</ItemType>
<ItemCondition>chair2</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
....Will have multiple subroot
</Root>
I have tried to define rules as below, but it is throwing error
Type: String
Context:/Root
Xpath: substring("Proceed", 1 div boolean(/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))
But it is throwing error like
net.sf.saxon.trans.XPathException: Arithmetic operator is not defined for arguments of types (xs:integer, xs:boolean)
Is there any other shortcut way to perform this.Could you please help me, i have given lot more effort. Not able to resolve it. Thanks in advance.
I am not sure where you are applying this but the XPath expression you are looking for is:
fn:contains(/Root/SubRoot[2]/ItemCondition, "chair") and fn:contains(/Root/SubRoot[2]/ItemType, "Table")
So here is an example returning "Proceed" or "Stop" as appropriate:
if (fn:contains(/Root/SubRoot[1]/ItemCondition, "Chair") and fn:contains(/Root/SubRoot[2]/ItemType, "Table")) then 'Proceed' else 'Stop'
To implement the above condition , i was initially tired to do in xpath, gave me lot of error. I have implemented by simple if else condition in script part of data mapper
if ( (input.ItemType == 'Table') and (input.ItemCondition == 'chair')) {
output.Item = 'Proceed'}
else {
output.Item = 'Stop '};
Make sure about your precedence. Example, Here in the xml structure( or converted POJO) ItemType has to be checked first then followed with ItemCondition.
&& not seems to be working for me, change to 'and' operator
If you were first time trying to implement the logic. It may help you.

Alfresco boost results from site

I'm trying to boost results from a particular Alfresco site compared to others.
I've written the following query but it's not working properly :
(((TAG:term or cm:name:term OR cm:title:term )^8 OR (cm:description:term )^6) AND PATH:'/app:company_home/st:sites/cm:Pub/cm:documentLibrary//*')^8
OR
(((TAG:term or cm:name:term OR cm:title:term )^4 OR (cm:description:term )^2) AND -PATH:'/app:company_home/st:sites/cm:Pub/cm:documentLibrary//*')
The result is overall correct but the results from Pub aren't the first ones in the list.
Is there a way to achieve that ?
^8 on the first part query part was just not enough. Putting a huge value like 100 makes it work perfectly !

Apache Velocity: hashtable?

The Velocity user guide mentions a "Hashtable". However there's no mentioning how to create one in this language.
So if you could show how to do this
-- so that I could write smth. like
#foreach( $key in $foo.keySet() )
<li>Key: $key -> Value: $foo.get($key)</li>
#end
-- I'd greatly appreciate your help.
Thanks in advance!
// PS: my original problem is : Mechanical Turk / Cmd line tools / Qualification / #set and #foreach in xml
So please understand that I am not interested in learning Velocity -- I only need one quick hack if possible. Thanks.
In Velocity you would use the #set directive to create a map. To relate it to your example you might do something like:
#set($foo = {
"NEWS": "http://news.bbc.com",
"SEARCH": "http://google.com"
})
Then your foreach example above will do exactly what you need.