F3 Reflection and Query operators

how-to
Dec 12, 20062 mins

A friend of mine who works at MIT libraries recently showed me a neat little AJAX data visualization program they developed called Exhibit.

This seemed like a good test case for F3 reflection and for its query operators, so I decided to try to see how hard it would be to do the same kind of thing in F3.

Below is a (partial) F3 equivalent of this Exhibit example.

The above demo also displays the F3 source code.

This is actually a plain Swing program (the F3 Canvas isn’t used), but demonstrates the use of HTML templating with F3.

All of the filtering and sorting is actually done in 3 lines of code (take a look at DB.f3 and AttributeValueAssertion.f3).

Some things to note:

  • Attribute is an F3 reflection class analagous to java.lang.reflect.Field
  • Reflective access to the value of an attribute uses the

    []

    operator with an operand of type

    Attribute

    , e.g.

       var bar = foo.class.Attributes[Name == 'bar'];
       foo[bar] = 2;
       // The above is analogous to the following Java code
       // Field bar = foo.getClass().getField("bar");
       // bar.set(foo, 2);
    

    is the reflective equivalent of

       foo.bar = 2;
    
  • The F3

    order by

    operator is a binary operator. The left-hand side is any list of objects. The right-hand side is an expression that must return a list of comparable objects. The right-hand expression is evaluated in the context of each element of the left hand side. The current element can be accessed with the

    .

    (dot) operator (as in XPath).

    For example,

       [1, 2, 3] order by -.
    

    yields

       [3, 2, 1]