conditional expression in a Exhibit facet

47 views
Skip to first unread message

Rahul

unread,
Feb 28, 2009, 9:56:11 AM2/28/09
to SIMILE Widgets
I had a question about using a conditional expression in a Exhibit
facet. (I think I sent the messege to the other defunct mailing list
yesterday; sorry if you get a double post if you still follow
gen...@simile.mit.edu)

Lets say, my exhibit is related to foodstuffs. Each foodstuff has two
ingredients major_ingredient and minor_ingredient (based purely on
quantity used irrespective of the cost of each ingredient).

"label" : "hotdog",
"type" : "foodstuff",
"major_ingredient" : "beef",
"minor_ingredient" : "soy"


There's a separate list that associates each possible ingredient with
a price.


label: beef
type: ingredient
cost: 10

label:soy
type:ingredient
cost:2


Now let's say I want a numeric range facet that displays the cost of
the more expensive ingredient that goes into each foodstuff. eg. for a
hot dog it ought to display 10 (cost of beef the more expensive
ingredient, beef)

I know how to get the cost of either the "major" or "minor" ingredient
like so:

<div ex:role="facet"
ex:facetClass="Exhibit.NumericRangeFacet"
ex:expression=".major_ingredient.cost"
ex:facetLabel="Price of more expensive
component"
ex:height="10em"></div>

But can I somehow on the fly use either the "major_ingredient" or the
"minor_ingredient" cost whichever is more expensive?

--
Rahul

David Huynh

unread,
Feb 28, 2009, 5:27:54 PM2/28/09
to simile-...@googlegroups.com
Rahul,

I'd try something like this

ex:expression="max(.major_ingredient.cost, .minor_ingredient.cost)"

Let me know if that works!

David

RPN

unread,
Feb 28, 2009, 10:04:15 PM2/28/09
to SIMILE Widgets


On Feb 28, 4:27 pm, David Huynh <dfhu...@alum.mit.edu> wrote:
> I'd try something like this
>
>    ex:expression="max(.major_ingredient.cost, .minor_ingredient.cost)"
>
> Let me know if that works!
>
> David

Ah! So simple. That works perfectly David, thank you.

Does that also open the imagination to more complicated on-the-fly
mathematical expressions like:

ex:expression=".major_ingredient.cost * major_ingredient.density +
minor_ingredient.cost*minor_ingredient.density*1000"
ex:facetLabel="Total cost"

[I know my particular logic does not make much physical sense]

That would be awesome. What sort of math can be embedded in
expressions? Is there a standard list of functions and operators?

--
Rahul

David Huynh

unread,
Mar 6, 2009, 4:44:48 PM3/6/09
to simile-...@googlegroups.com
The functions are here
http://static.simile.mit.edu/exhibit/api-2.1.0/scripts/data/functions.js
and the operators are at the bottom of

http://static.simile.mit.edu/exhibit/api-2.1.0/scripts/data/expression.js

Please note, though, that the behavior of these expression might get
bizarre once each operand or argument is a set of values rather than a
single value. For example,

.foo * .bar

works well if .foo evaluates to, say, [ 2 ] and .bar to, say, [ 3 ]. The
result would be [ 6 ]. However if

.foo --> [ 2, 5 ]
.bar --> [ 7, 3 ]

the then result is (as implemented)

[ 2*7, 2*3, 5*7, 5*3 ]

Also note that the dot . operator returns a set of values with no
duplicate. In certain cases, when you want duplicates, use the .@ operator.

David

RPN

unread,
Mar 9, 2009, 1:12:38 PM3/9/09
to SIMILE Widgets


On Mar 6, 4:44 pm, David Huynh <dfhu...@alum.mit.edu> wrote:

>
> The functions are here
>    http://static.simile.mit.edu/exhibit/api-2.1.0/scripts/data/functions.js
> and the operators are at the bottom of

>
> Please note, though, that the behavior of these expression might get
> bizarre once each operand or argument is a set of values rather than a
> single value. For example,
>
>     .foo * .bar
>
> works well if .foo evaluates to, say, [ 2 ] and .bar to, say, [ 3 ]. The
> result would be [ 6 ]. However if
>
>     .foo --> [ 2, 5 ]
>     .bar --> [ 7, 3 ]
>
> the then result is (as implemented)
>
>     [ 2*7, 2*3, 5*7, 5*3 ]
>
> Also note that the dot . operator returns a set of values with no
> duplicate. In certain cases, when you want duplicates, use the .@ operator.
>


Thanks for the pointers and the detailed explanation and caveats
David. That helps a lot!

--
Rahul
Reply all
Reply to author
Forward
0 new messages