Re: Expression Handling Again

Keats Kirsch <[email protected]> Tue, 27 Sep 2005 11:32:35 -0400
Newsgroups gmane.comp.java.webmacro.user
Message-ID <[email protected]>
I'm curious about this benchmarking result.  I can't figure out where 
the savings would come from unless it is a trivial template.  AFAICT the 
only step that gets skipped is the the hash lookup which should be 
fast.  You still have to create the context, load the context, parse the 
template, and expand the template.  I suppose if the template is cached 
and the context is reused as well and the template is simple, then it 
would make sense.

There is probably some room for optimizing this even further ... a tool 
to evaluate a WM expression without any output buffer (NullFastwriter?) 
or even creating a variable.  Hmmm ...

Keats

Eric B. Ridge wrote:

> On Sep 22, 2005, at 5:27 PM, Keats Kirsch wrote:
>
>> Eric's method is nice.
>
>
> It's also really fast.  On the order of 20% faster than communicating  
> the value through the Context.  Which is important if you're doing  
> this sort of thing over and over in web-requests.
>
> eric
>
>> I do something similar (though less elegant) in the  
>> PropertiesDirective.  However for XML files there is a much simpler  
>> method that I sometimes use: just parse the XML file as a template  
>> before you parse the XML.  Works great.
>>
>> You can use doubles in WMScript, but not natively with the built-in  
>> operators.  You can use the $Math tool and the casting functions,  
>> like $toDouble.
>>
>> Keats
>>
>> Dan Miller wrote:
>>
>>
>>> That method works great to get the result. One last
>>> thing, how come webmacro expressions do not return
>>> double. Is that historical? Now that we use a grammar
>>> and parser for WM can is it possible to expand
>>> expressions?
>>> Thanks
>>>
>>> --- "Eric B. Ridge" <[email protected]> wrote:
>>>
>>>
>>>
>>>> Rather than creating a StringMacro, create a
>>>> StringTemplate, with a  mini-template of:
>>>>     #set $foo = $x + 1
>>>>
>>>> Then use a custom TemplateVisitor to pull out the
>>>> "SetValue" argument  of the #set directive:
>>>>
>>>>     static class MyTemplateVisitor extends
>>>> TemplateVisitor {
>>>>         Object value;
>>>>
>>>>         public Object getValue() {
>>>>             return value;
>>>>         }
>>>>
>>>>         public void visitDirectiveArg(String
>>>> string, Object object) {
>>>>             // the #set directive has 2 arguments: "SetTarget" (LHS)
>>>>             // and "SetValue" (RHS).  We're only
>>>> interested in  "SetValue"
>>>>             if (string.equals("SetValue")) {
>>>>                 value = object;
>>>>             }
>>>>         }
>>>>     }
>>>>
>>>> So, you're code will be something like:
>>>>
>>>> public Object evalExpression (Context context,
>>>> String expression) {
>>>>     StringTemplate tmpl =  new StringTemplate
>>>> (context.getBroker(),  "#set $foo = " + expression);
>>>>     MyTemplateVisitor visitor = new
>>>> MyTemplateVisitor();
>>>>
>>>>     template.parse();
>>>>     template.accept(visitor);
>>>>
>>>>     Object value = visitor.getValue();
>>>>     if (value instanceof Maco) {
>>>>         return ((Macro) value).evaluate(_context);
>>>>     } else {
>>>>         return value;
>>>>     }
>>>> }
>>>>
>>>> Hope this helps.
>>>>
>>>> eric
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Sep 22, 2005, at 12:22 PM, Dan Miller wrote:
>>>>
>>>>
>>>>
>>>>> I guess what I was wondering about was using the
>>>>>
>>>>>
>>>> api
>>>>
>>>>
>>>>> directly stepping in before the #set places the
>>>>>
>>>>>
>>>> result
>>>>
>>>>
>>>>> into the context. I was looking into the src for
>>>>> PropertyMethod and trying to figure out how to get
>>>>>
>>>>>
>>>> the
>>>>
>>>>
>>>>> result.
>>>>>
>>>>> I am using webmacro throughout a webproject, and
>>>>>
>>>>>
>>>> would
>>>>
>>>>
>>>>> like to use similiar webmacro expression in xml
>>>>>
>>>>>
>>>> files
>>>>
>>>>
>>>>> which are read and processed. Thanks
>>>>>
>>>>> --- Keats Kirsch <[email protected]> wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> AFAIK there are only two ways to get something
>>>>>>
>>>>>>
>>>> out
>>>>
>>>>
>>>>>> of a WM template
>>>>>> expansion -- capture the result of the expansion,
>>>>>>
>>>>>>
>>>> or
>>>>
>>>>
>>>>>> pull something from
>>>>>> the context.  Actually I suppose you could use
>>>>>>
>>>>>>
>>>> the
>>>>
>>>>
>>>>>> #bean directive to
>>>>>> create a "global" variable and then get it from
>>>>>>
>>>>>>
>>>> the
>>>>
>>>>
>>>>>> Broker.
>>>>>>
>>>>>> Maybe if you can explain your requirements we
>>>>>>
>>>>>>
>>>> could
>>>>
>>>>
>>>>>> think of a way of
>>>>>> doing what you want.
>>>>>>
>>>>>> Keats
>>>>>>
>>>>>> Dan Miller wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>> The usage of the StringMacro to use the #set
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> directive
>>>>>>
>>>>>>
>>>>>>
>>>>>>> works great. I was wondering if there is
>>>>>>>
>>>>>>>
>>>> anything
>>>>
>>>>
>>>>>>> exposed to evaluate the expression without
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> modifying
>>>>>>
>>>>>>
>>>>>>
>>>>>>> the context.
>>>>>>>
>>>>>>> This works great, but I would like to leave the
>>>>>>> context as is, not adding new temp variables to
>>>>>>>
>>>>>>>
>>>> the
>>>>
>>>>
>>>>>>> context.
>>>>>>>
>>>>>>> Context ctx = new Context();
>>>>>>> ctx.put("val1", 1);
>>>>>>> ctx.put("val2", 155);
>>>>>>> Macro macro = null;
>>>>>>> macro = new StringMacro("#set $val = $val1 +
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> $val2");
>>>>>>
>>>>>>
>>>>>>
>>>>>>> macro.evaluate( ctx);
>>>>>>> Integer val = (Integer)ctx.get("val");
>>>>>>>
>>>>>>>
>>>>>>> I am looking forward to the 2.0 release.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>
>>> === message truncated ===
>>>
>>>
>>
>>
>>
>
>




-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl