|
|
|
wkbutler
|
Hello -
I am a bit confused on the built-in capability of FM to parse a localized numeric expression, and before I write a custom routine for this, maybe there is wisdom I lack. I am trying to process a mathematical expression on the contents of a map of string data: --------------- | a | 1,111 | | b | 2,222 | --------------- so a simple expression like ${a?number + b?number} would work if the numbers were not localized, right? That I have had success with. The original syntax #{a} + #{b} is deprecated according to this page in the docs http://freemarker.sourceforge.net/docs/ref_depr_numerical_interpolation.html although trying this I get the error message: a is not a number, it is freemarker.template.SimpleScalar Using the number built-in ?c and the string built-in ?string.computer fail with similar messages, such as "Expected hash" etc. It seems I do not understand what data type I am dealing with. I would like to have have the 2 numbers added together and output with the default localized syntax. Am I looking past something? Thanks for ideas - |
|
Daniel Dekany
|
Tuesday, September 15, 2009, 4:34:22 AM, wkbutler wrote:
> > Hello - > I am a bit confused on the built-in capability of FM to parse a localized > numeric expression, It's none. FM can convert simple "computer-audience number" strings back to numbers, because sometimes, for purely technical reasons, you get numbers "serialized" to text format. Parsing human-audience numbers is out of the scope of FM, and in theory you should deal with that in the C of MVC, not in the V. Means, although you can solve that in the V with a TempateMethdoModelEx (or with a #function), you might shouldn't do that. > and before I write a custom routine for this, maybe > there is wisdom I lack. > > I am trying to process a mathematical expression on the contents of a map of > string data: > > --------------- > | a | 1,111 | > | b | 2,222 | > --------------- > > so a simple expression like > ${a?number + b?number} > > would work if the numbers were not localized, right? At least with this concrete example, yes. > That I have had success with. > > The original syntax > #{a} + #{b} > > is deprecated according to this page in the docs > > http://freemarker.sourceforge.net/docs/ref_depr_numerical_interpolation.html It is. Don't use it. > although trying this I get the error message: > > a is not a number, it is freemarker.template.SimpleScalar Unlike ?number, #{...} doesn't even try to convert strings to numbers. > Using the number built-in ?c and the string built-in > ?string.computer fail with similar messages, These convert a number (or date, etc.) to a string, not the other way around. > such as "Expected hash" etc. It seems I do not understand what data > type I am dealing with. notANumber?string is not a hash (aNumber?string is), but the dot (.) operator will expect a hash on its left side, hence it throws the above error. Yeah, a but tricky... > I would like to have have the 2 numbers added together and output with the > default localized syntax. Am I looking past something? No, except maybe that, whatever view technology are you using (FreeMarker or not) it's not something that you should do in a the view layer of your application. At least usually it's not... > Thanks for ideas -- Best regards, Daniel Dekany ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ FreeMarker-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/freemarker-user |
||||||||||||||||
|
wkbutler
|
Thank you Daniel. Makes sense, so I shall pursue another route.
To speak theoretically then... I did consider putting this logic into the 'C' part of MVC, but that breaks my use case. This application allows the user to define macros (happily implemented with FM) against a pool of data, and to define macros using the output of other macros (2nd-order macros). Thus by definition, I have FM scripts that reference the output of other FM scripts, which may possibly be mathematical expressions against formatted numeric values. A followup question... In the past I have added custom processing methods by inserting a class reference into the data Map given to the Template.process(..) method, i.e. data.put("util", new DataUtil()); however my data map these days is a Map<String,String> so this will not work. Do I need to revert my dataset to a Map<String, Object>, or is there another way to inject custom utilities? Thanks again -
|
||||||||||||||||
|
Daniel Dekany
|
Tuesday, September 15, 2009, 3:46:17 PM, wkbutler wrote:
> Thank you Daniel. Makes sense, so I shall pursue another route. > > To speak theoretically then... I did consider putting this logic into the > 'C' part of MVC, but that breaks my use case. This application allows the > user to define macros (happily implemented with FM) against a pool of data, > and to define macros using the output of other macros (2nd-order macros). > Thus by definition, I have FM scripts that reference the output of other FM > scripts, which may possibly be mathematical expressions against formatted > numeric values. I don't know... it's not enough concrete for me to form an opinion. The part that is a bit suspicious for me, that if the numerical output generated by the 1st order macros meant to be treated as numbers by the 2nd order macros, then why are they in human-audience format? I mean, FM can output in computer-audience numerical format as well. > A followup question... > > In the past I have added custom processing methods by inserting a class > reference into the data Map given to the Template.process(..) method, i.e. > > data.put("util", new DataUtil()); > > however my data map these days is a Map<String,String> so this will not > work. > Do I need to revert my dataset to a Map<String, Object>, or is there another > way to inject custom utilities? There is a way (as far as using a global variable instead of data-model variable is fine for you). Use something like this instead of the myTemplate.process(root, out) call: Environment env = myTemplate.createProcessingEnvironment(root, out); env.setGlobalVariable("util", env.getObjectWrapper().wrap(new DataUtil())); env.process(); > Thanks again - > > > > Daniel Dekany wrote: >> >> Tuesday, September 15, 2009, 4:34:22 AM, wkbutler wrote: >> >>> >>> Hello - >>> I am a bit confused on the built-in capability of FM to parse a localized >>> numeric expression, >> >> It's none. FM can convert simple "computer-audience number" strings >> back to numbers, because sometimes, for purely technical reasons, you >> get numbers "serialized" to text format. Parsing human-audience >> numbers is out of the scope of FM, and in theory you should deal with >> that in the C of MVC, not in the V. Means, although you can solve that >> in the V with a TempateMethdoModelEx (or with a #function), you might >> shouldn't do that. >> >>> and before I write a custom routine for this, maybe >>> there is wisdom I lack. >>> >>> I am trying to process a mathematical expression on the contents of a map >>> of >>> string data: >>> >>> --------------- >>> | a | 1,111 | >>> | b | 2,222 | >>> --------------- >>> >>> so a simple expression like >>> ${a?number + b?number} >>> >>> would work if the numbers were not localized, right? >> >> At least with this concrete example, yes. >> >>> That I have had success with. >>> >>> The original syntax >>> #{a} + #{b} >>> >>> is deprecated according to this page in the docs >>> >>> http://freemarker.sourceforge.net/docs/ref_depr_numerical_interpolation.html >> >> It is. Don't use it. >> >>> although trying this I get the error message: >>> >>> a is not a number, it is freemarker.template.SimpleScalar >> >> Unlike ?number, #{...} doesn't even try to convert strings to numbers. >> >>> Using the number built-in ?c and the string built-in >>> ?string.computer fail with similar messages, >> >> These convert a number (or date, etc.) to a string, not the other way >> around. >> >>> such as "Expected hash" etc. It seems I do not understand what data >>> type I am dealing with. >> >> notANumber?string is not a hash (aNumber?string is), but the dot (.) >> operator will expect a hash on its left side, hence it throws the >> above error. Yeah, a but tricky... >> >>> I would like to have have the 2 numbers added together and output with >>> the >>> default localized syntax. Am I looking past something? >> >> No, except maybe that, whatever view technology are you using >> (FreeMarker or not) it's not something that you should do in a the >> view layer of your application. At least usually it's not... >> >>> Thanks for ideas >> >> -- >> Best regards, >> Daniel Dekany >> >> >> ------------------------------------------------------------------------------ >> Come build with us! The BlackBerry® Developer Conference in SF, CA >> is the only developer event you need to attend this year. Jumpstart your >> developing skills, take BlackBerry mobile applications to market and stay >> ahead of the curve. Join us from November 9-12, 2009. Register >> now! >> http://p.sf.net/sfu/devconf >> _______________________________________________ >> FreeMarker-user mailing list >> [hidden email] >> https://lists.sourceforge.net/lists/listinfo/freemarker-user >> >> > -- Best regards, Daniel Dekany ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ FreeMarker-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/freemarker-user |
||||||||||||||||
|
wkbutler
|
Good question - consider the case of subtotals and grand totals. Grand totals operate on subtotals, subtotals operate on 'raw' data. Both are products for the user. You allude to another possible solution though, which is to store both formats; I would rather avoid that, but it may be the easiest approach. Perfect - I will try that. Thank you - |
||||||||||||||||
|
Daniel Dekany
|
Tuesday, September 15, 2009, 11:45:54 PM, wkbutler wrote:
> Daniel Dekany wrote: >> >> Tuesday, September 15, 2009, 3:46:17 PM, wkbutler wrote: >> I don't know... it's not enough concrete for me to form an opinion. >> The part that is a bit suspicious for me, that if the numerical output >> generated by the 1st order macros meant to be treated as numbers by >> the 2nd order macros, then why are they in human-audience format? I >> mean, FM can output in computer-audience numerical format as well. >> > > Good question - consider the case of subtotals and grand totals. Grand > totals operate on subtotals, subtotals operate on 'raw' data. Both are > products for the user. You allude to another possible solution though, > which is to store both formats; I would rather avoid that, but it may be the > easiest approach. In principle you don't generate the grand totals from the formatted output of the subtotals. You just generate them from the presentation-independent data that represents the subtotals. Extracting data from formatted output is quite difficult in general (since that output is optimized for human brains, not for computer programs), and thus avoided whenever possible. Of course, building presentation-independent data structures is not something FM is often used for, since it generates text, not Java objects and like. But, there are some exceptions, when FM generates XML or JSON, which are not about presentation but describing structured "data", and still are text. > Daniel Dekany wrote: >> >>> Do I need to revert my dataset to a Map<String, Object>, or is there >>> another >>> way to inject custom utilities? >> >> There is a way (as far as using a global variable instead of >> data-model variable is fine for you). Use something like this instead >> of the myTemplate.process(root, out) call: >> >> Environment env = myTemplate.createProcessingEnvironment(root, out); >> env.setGlobalVariable("util", >> env.getObjectWrapper().wrap(new DataUtil())); >> env.process(); >> > > Perfect - I will try that. Thank you - -- Best regards, Daniel Dekany ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ FreeMarker-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/freemarker-user |
||||||||||||||||
|
wkbutler
|
Yep I'd agree completely, ordinarily. But the way this system is designed, the raw data is actually unknown to the system. The user is in control of the raw data content and all macros. Hence only the user knows which data points are numeric (string representation of numerics of course), and how he wants to use the data. I guess that means the user should be responsible for setting up two versions of each numeric macro. I may fall back to that solution, it probably is the safest for the reasons you mention. But for maintenance concerns on near-duplicate macros, I press on... The raw data is presentation-independent. So solution #1 would be be to make the 2nd order macros reference only the raw data. But then they wouldn't be 2nd order macros; and would lead to other technical, usability and maintenance issues. Another option is for the processor to attempt to identify numeric results of 1st order macros, and store a raw format for 2nd order macros to reference. I don't like this solution much for a couple reasons. That more or less leaves the solution that I'm going for at the moment - putting a tool in the hands of the user. Any 2nd order mathematical macro would need to scrub numeric args (which are macro results) before using them. And here I'm glad for FM's ability to inject code references. At the moment anyway, the server will only support two i18n formats, so the parser does not have to cover all possible cases. |
||||||||||||||||
|
Denis Bredelet
|
>>> Daniel Dekany wrote: >>>> >>>> Tuesday, September 15, 2009, 3:46:17 PM, wkbutler wrote: >>>> I don't know... it's not enough concrete for me to form an opinion. >>>> The part that is a bit suspicious for me, that if the numerical output >>>> generated by the 1st order macros meant to be treated as numbers by >>>> the 2nd order macros, then why are they in human-audience format? I >>>> mean, FM can output in computer-audience numerical format as well. >>>> >>> >>> Good question - consider the case of subtotals and grand totals. Grand >>> totals operate on subtotals, subtotals operate on 'raw' data. Both are >>> products for the user. You allude to another possible solution though, >>> which is to store both formats; I would rather avoid that, but it may be >>> the >>> easiest approach. >> >> In principle you don't generate the grand totals from the formatted >> output of the subtotals. You just generate them from the >> presentation-independent data that represents the subtotals. >> Extracting data from formatted output is quite difficult in general >> (since that output is optimized for human brains, not for computer >> programs), and thus avoided whenever possible. >> >> Of course, building presentation-independent data structures is not >> something FM is often used for, since it generates text, not Java >> objects and like. But, there are some exceptions, when FM generates >> XML or JSON, which are not about presentation but describing >> structured "data", and still are text. >> > >Yep I'd agree completely, ordinarily. But the way this system is designed, >the raw data is actually unknown to the system. The user is in control of >the raw data content and all macros. Hence only the user knows which data >points are numeric (string representation of numerics of course), and how he >wants to use the data. > >I guess that means the user should be responsible for setting up two >versions of each numeric macro. I may fall back to that solution, it >probably is the safest for the reasons you mention. But for maintenance >concerns on near-duplicate macros, I press on... In fact since you are using macros for data processing all your macros should produce computer-audience numbers. Only when a number reaches the view it should be formatted for human audience. -- Denis. ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ FreeMarker-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/freemarker-user |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |