Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to implement such a Domain-Specific-Language (DSL)?

Reply
Thread Tools

How to implement such a Domain-Specific-Language (DSL)?

 
 
Zd Yu
Guest
Posts: n/a
 
      12-20-2010
I need to use Ruby to process performance data and calculate performance
metrics. What I have is a Hash that contains event_name => event_value
mappings. What I want to do is calculating metrics in a elegant style.

for example:

events = Hash.new

# reading the raw data.
# code omitted here.

# now I get the basic events, like
# events["event_A"] = 123.456
# events["event_B"] = 456.789

# the normal way looks like:
events["metric_A"] = events["event_A"] * 64 / events["event_B"]

# but what I am wondering if I can do it in an elegant way, like:
metric_A = event_A * 64 / event_B

Can anybody give some hints?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
zuerrong
Guest
Posts: n/a
 
      12-20-2010
2010/12/20 Zd Yu <(E-Mail Removed)>:

>
> # now I get the basic events, like
> # events["event_A"] = 123.456
> # events["event_B"] = 456.789
>


You may don't need the hash at all.
Just do:

event_A = 123.456
event_B = 456.789

Then you will get what you wanted:

> metric_A = event_A * 64 / event_B
>


Regards.

 
Reply With Quote
 
 
 
 
Zd Yu
Guest
Posts: n/a
 
      12-20-2010
zuerrong wrote in post #969511:
> You may don't need the hash at all.


you did not get my point. I want to make it flexible. I just write the
tool, and the users of the tool can define the metric by themselves.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      12-20-2010
On Mon, Dec 20, 2010 at 8:22 AM, Zd Yu <(E-Mail Removed)> wrote:
> I need to use Ruby to process performance data and calculate performance
> metrics. What I have is a Hash that contains event_name => event_value
> mappings. What I want to do is calculating metrics in a elegant style.
>
> for example:
>
> events = Hash.new
>
> # reading the raw data.
> # code omitted here.
>
> # now I get the basic events, like
> # events["event_A"] = 123.456
> # events["event_B"] = 456.789
>
> # the normal way looks like:
> events["metric_A"] = events["event_A"] * 64 / events["event_B"]
>
> # but what I am wondering if I can do it in an elegant way, like:
> metric_A = event_A * 64 / event_B
>
> Can anybody give some hints?


If the only requirement is to have event_A evaluate to
events["event_A"] you can do that easily with method_missing. This can
give you some ideas:

class EventData
def initialize events
@events = events
end

def method_missing meth, *args, &blk
super unless /\Aevent_/ =~ meth.to_s
@events.send(:[], meth.to_s)
end
end

data = EventData.new("event_A" => 123.456, "event_B" => 456.789)
metric_A = data.instance_eval { event_A * 64 / event_B }

# => 17.2972291364284

You can have a file with only the block for instance_eval for your
users to type, with just an extra little bit of work.

Jesus.

 
Reply With Quote
 
Zd Yu
Guest
Posts: n/a
 
      12-20-2010
"Jes=C3=BAs Gabriel y Gal=C3=A1n" <(E-Mail Removed)> wrote in pos=
t =

#969520:
> you can do that easily with method_missing


Thanks. it looks like a promising solution. but I have two more =

requirements:

1. what if the event name includes a dot '.'?
2. I also want the left value of the equation (metric_A in the example) =

to be added into the Hash, because it is possible that a new metric =

needs to be calculated based on the existing metric.

-- =

Posted via http://www.ruby-forum.com/.=

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      12-20-2010

On Dec 20, 2010, at 01:18 , Zd Yu wrote:

> "Jes=FAs Gabriel y Gal=E1n" <(E-Mail Removed)> wrote in post=20=


> #969520:
>> you can do that easily with method_missing

>=20
> Thanks. it looks like a promising solution. but I have two more=20
> requirements:
>=20
> 1. what if the event name includes a dot '.'?


You have two event names and you need to clarify: the key in the hash, =
or the "variable" used in the DSL?

If the former, you probably want to munge the names as they come in.

If the latter, you probably want to munge what you wind up instance =
eval'ing.

If neither, you're probably doing it as a scoping mechanism of some sort =
and you probably need to build a hierarchy in @events and change method =
missing to do a proper hierarchical lookup (probably recursively, =
passing down to subsequent sub-EventData's).

> 2. I also want the left value of the equation (metric_A in the =

example)=20
> to be added into the Hash, because it is possible that a new metric=20
> needs to be calculated based on the existing metric.


Then chuck the hash entirely and use local variables. This will probably =
collide with the above suggestion for namespacing... so munging the =
input script is a better route.=

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      12-20-2010
"Jes=C3=BAs Gabriel y Gal=C3=A1n" <(E-Mail Removed)> wrote in pos=
t =

#969520:
> On Mon, Dec 20, 2010 at 8:22 AM, Zd Yu <(E-Mail Removed)> wrote:
>>
>> Can anybody give some hints?

> If the only requirement is to have event_A evaluate to
> events["event_A"] you can do that easily with method_missing.


Or look at OpenStruct in the standard library (ostruct.rb), combined =

with instance_eval

-- =

Posted via http://www.ruby-forum.com/.=

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a good way to implement file updating? such as lock, update,reload... Evan Python 2 07-07-2008 01:40 PM
How to implement such a class? sleepinglord Javascript 3 04-01-2006 09:45 AM
Directly linking to files such as MSWord willy_gates Firefox 0 05-14-2004 11:12 AM



Advertisments