I definitely agree with Perrin. The most effective, usable system is going to be a framework that allows you to glue together concise, targeted bits of code. You can't beat the power of Perl for scripting complicated interactions between little bits of data.

We use XML::Comma for managing web-site user profiles, mailing list systems, content archives, etc. We haven't done quite this kind of shopping cart, but it's a pretty good fit.

In Comma, you write document definitions that specify data structures, constrain the contents of the parts of those structures, and govern how data is saved in filesystem- and database-backed storage. There's a pretty extensive API for manipulating both individial documents and collections of docs.

You always hand-write the definitions -- that's the core exercise of figuring out basic data structures and behaviors. Writing defs feels like a kind of OO mating ritual: XML on one side, perl on the other, and between the two of them a Class is born.

Most of our systems are built to assume completely tools-generated docs (for various definitions of "tools"). You can write docs by hand, and for your shopping cart system you might want to. For instance, creating a new "item" in the product catalog could be the job for a decent programmer who understands something about how the system is used but doesn't have much knowledge of the internals/architecture/subtleties.

Anyway, I threw together a bit of Comma stuff showing how you might structure your widgets & gizmos example. Whether or not you want to look more at Comma, it might provide some more food for thought. There are a few example "item" and "customer" docs, followed by defs for "item", "customer" and "cart".

<item> <name>widget</name> <base_cost>250</base_cost> </product> <item> <name>gizmo</name> <base_cost>125</base_cost> <cost_hook> <apply_order>100</apply_order> <!-- buy three widgets, get a gizmo free --> <cod><![CDATA[ sub { my ( $cart, $item, $customer, $cost ) = @_; if ( scalar(@{$cart->elements('widget')}) > 2 ) { return 0; } else { die; } ]]></cod> </cost_hook> <!-- buy 10 widgets, get $30-off per --> <cost_hook> <apply_order>110</apply_order> <cod><![CDATA[ sub { my ( $cart, $item, $customer, $cost ) = @_; if ( $cart->number_of('gizmo') > 10 ) { return $cost - 30; } else { die; } } ]]></cod> </cost_hook> </item>
<item> <name>service_agreement_3yr</name> <base_cost>150_000</base_cost> <!-- bought widgets within the last thirty days, first year is free --> <cost_hook> <apply_order>100</apply_order> <cod><![CDATA[ sub { my ( $cart, $item, $customer, $cost ) = @_; my $widgets_order = $cart->customer()->last_ordered('widget'); if ( $widgets_order and $widgets_order->date() > time()-60*60*24*30 ) { return 100_000; } else { die; } } ]]></cod> </cost_hook> </item> <customer> <email>foo@bar.com</email> <cost_hook> <!-- 3 orders or more discount --> <apply_order>400</apply_order> <cod><![CDATA[ Foo::Util::get_Std_3_Orders_hook() ]]></cod></cost_hook> <cost_hook> <!-- Joe belongs to the 50%-off club --> <apply_order>300</apply_order> <cod><![CDATA[ sub { sub { my ( $cart, $item, $customer, $cost ) = @_; return $cost / 2; } } ]]></cod> </cost_hook> </customer> #---- <DocumentDefinition> <name>item</name> <element>widget</element> <element><name>base_cost</name></element> <nested_element> <name>cost_hook</name> <element>apply_order</element> <element>code</element> </nested_element> <plural>'cost_hook'</plural> <store> <name>main</name> <base>items</base> <location>Read_only_file:'extension','.item'</location> </store> </DocumentDefinition> <DocumentDefinition> <name>customer</name> <element><name>email</name></element> <nested_element> <name>cost_hook</name> <defname>item:cost_hook</defname> </nested_element> <plural>'cost_hook'</plural> <method> <name>num_orders</name> <cod><![CDATA[ sub { my $self = shift; return XML::Comma::Def->read('order') ->get_index->('completed')->count ( where_clause => 'doc_id="' . $self->doc_id() . '"' ); ]]></cod> </method> <method> <name>last_ordered</name> <cod><![CDATA[ sub { my ( $self, $item ) = @_; return XML::Comma::Def->read('order') ->get_index->('orders')->single ( where_clause => 'doc_id="' . $self->doc_id() . '"', collection_spec => "items:$item", order_by => "timestamp DESC" ); ]]></cod> </method> <store> <name>main</name> <base>customers</base> <location>Sequential_dir</location> <index_on_store>main</index_on_store> </store> <index> <name>main</name> <field>email</field> </index> </DocumentDefinition> <DocumentDefinition> <name>order</name> <element><name>customer_id</name></element> <element><name>timestamp</name></element> <element><name>total_paid</name></element> <nested_element> <name>item</name> <element>item_name</element> <element>item_quantity</element> <element>price_paid</element> </nested_element> <plural>'item'</plural> <method> <name>number_of</name> <cod><![CDATA[ sub { my ( $self, $item ) = @_; my $total_quantity; map { $total_quantity += $_->item_quantity() if $_->item_name() eq $item; } $self->elements('item') ]]></cod> </method> <method> <name>make_totals</name> <cod><![[CDATA[ sub { my $self = shift; my $total = 0; my $customer = XML::Comma::Doc ->read('customer|main|'.$self->customer_id()) || die "oops, couldn't get customer doc"; foreach my $i ( $self->elements('item') ) { my $this_total = $i->base_cost(); foreach $hook ( sort { $a->apply_order() <=> $b->apply_order() } ( $customer->elements('cost_hook'), $i->elements('cost_hook') ) ) { my $cost; eval { $this_total = $hook->code()->( $cart, $ ) }; return $cost unless $@; } my $extended = $this_total * $i->item_quantity(); $i->price_paid ( $extended ); $total += $extended; } $this->total_paid ( $total ); return $total; } ]]></cod> <store> <name>carts</name> <base>carts</base> <location>Derived_file: 'derive_from','doc_id', 'extension','.order'</location> </store> <store> <name>orders</name> <base>orders</base> <location>Sequential_file</location> <index_on_store>orders</index_on_store> </store> <index> <name>orders</name> <field>customer_id</field> <field>timestamp</field> <field>total_paid</field> <collection> <name>items</name> <cod><![CDATA[ sub { return map { $_->item_name() } $_[0]->item() }; ]]></cod> </collection> </index> </DocumentDefinition>

(Comma uses the code keyword pretty extensively, which conflicts with the monks-tag of the same name. I shortened Comma's to "cod".)


In reply to Re: Re: Seeking configuration language for rules based system. by khkramer
in thread Seeking configuration language for rules based system. by ehdonhon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.