Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Limbo Overview

Limbo Overview

Ratings: (0)|Views: 1,643|Likes:
Published by Wilfred Springer
Limbo is the expression language used by Preon. It features early binding and the ability to generate a human-readable description of the expression.
Limbo is the expression language used by Preon. It features early binding and the ability to generate a human-readable description of the expression.

More info:

Published by: Wilfred Springer on Sep 12, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/04/2014

pdf

text

original

 
1
Limbo Overview
Wilfred Springer
1. Introduction
Limbo is the code name of an expression language used in Preon
1
. It allows you to evaluate expressions on acertain context. The expression language is fairly simple; it supports basic logical and arithmetic operators, and itsupports attribute references and item references.
Example 1. Simple Limbo Examples
3 * 43 * person.age2 ^ group.persons[3].ageperson.age >= 12
... and that's probably where the correspondence with the JSP Expression Language and other expressionlanguages ends. Because there is a
big
difference with those languages. And that difference is not based onnotation (syntax and grammar), but on the way you apply Limbo.Here are some of the differences of Limbo:Limbo supports
early binding
. That is, it will validate the correctness of the expression before it actually validatesit. So, when you build the expression, you know if it is an expression that can be evaluated at runtime within acertain context.Limbo allows you to bind to
any 
context, not just state exposed through bean properties or private fields.The Limbo API allows you to export the expression as a natural language based human readable snippet of text.Limbo originates from a project to capture dependencies in different fields in a binary media format in anunambiguous way. Using Limbo, that project is capable of generating a human readable description of theencoding format. Expressions like the ones mentioned in Example 1, “Simple Limbo Examples” could be renderedinto this:
3 times 43 times the age of a person2 to the power of the age of the third person in the groupthe age of the person is greater than or equal to 2 
This article is a tutorial on Limbo. It will explain the language itself, but it will also explain how you weave it intoyour own project.
1
Limbo used to be a separate project, however when moving to Preon 2.0, knowing that Limbo is only used inside Preon, it did not seem tomake an aweful lot of sense to keep it a separate project, especially with some refactoring that had to be done.
 
Limbo Overview2
2. The Language
The first thing you need to know about Limbo is that it is an
expression language
, intended to execute on acertain context. The context might be a fairly complicated data structure, or it may not. Two things are for sure:Limbo only allows you to refer to things by name or their index, and Limbo does
not 
allow you to define complexstructures in the language itself.Let's start with the most commons example: let's look at an simple example object-based data structure. InFigure 1, “Simple data model”, Persons have a name and an age, a father and mother, and optionally somechildren of their own.
Figure 1. Simple data model
Based on this, here are some example Limbo expressions, given that the context is a person.
age // the age of the current personage >= 35 // the age is greater than or equal to 35father.age // the age of the fatherfather.age + mother.age >= 70 // the sum of the age of the father and// the age of the mother is greater than//or equal to 70children[0].age < 7 
There are a couple of lessons to learn from the example above. First of all, valid Limbo expressions always evaluateto boolean values or integer values. You wonder why? Well, simply because we did not need anything else. Thewhole purpose of Limbo is to express arithmetical and logical relationships between things. Producing text simplyhas never been a requirement.The next thing to notice is that the expressions do not look all that different than Java expressions
2
. It supportsarithmetic operators ('+', '-', '/', '*', '^'), comparison operators ('>', '<', '>=', '<=', '==') and logical operators ('&&','||', '!').The third thing to notice is that you can refer to attributes as well as items. (In that sense, Limbo is comparable toPython.) In this example, that works out quite well. Objects also have attributes and some types of objects mighthave items. However, it is important to remember that Limbo does not bind to objects only. Limbo is able to bindto
any 
model exposed as 'things' with attributes and items.Integer literals can be expressed as decimals (1254), hexadecimals (0xFF, 0xff, etc.) or as binary numbers(0b10101011, 0b1001, etc.). Limbo ignores all whitespace.
2
Notice that I say it does not
look 
all that different. It
actually 
more different than you might expect. More on that somewhere else.
 
Limbo Overview3
3. The API3.1. Getting Started
Let's start with a simple example first:
Example 2. Simple expression
Expression<Integer, Person>doubleAge = Expressions.from(Person.class).toInteger("age * 2");Person wilfred = new Person();wilfred.name = "Wilfred";wilfred.age = 35;assert 70 == doubleAge.eval(wilfred);
In the first line, we build the
Expression
instance. Since the expression is based on a
Person
object, the
from(...)
method takes
Person
class reference. After that, we specify that we expect an integer result, andpass the expression at the same time. (The builder methods actually have a couple of other options, but we willleave that out for now.)Once the
Expression
has been built, evaluating is simply calling
eval(...)
on the expression, passing in the
Person
instance. And - like you could have expected - the result is 70. Note there is no cast in order to compare to70, courtesy of the use of generics and auto unboxing.
3.2. The ReferenceContext
I said before that Limbo is capable of binding to
anything
capable of representing itself as 'things' with namedattributes and numbered items. Let me refine that: it is capable of binding to anything for which you canimplement a
ReferenceContext
. So, when in Example 2, “Simple expression” you passed in a Person class,under the hood, Limbo wrapped that inside a ReferenceContext.Now, if you ever used an expression language like the JSP EL, then you are probably aware of a similar mechanismin that expression language. JSP EL has a
VariableResolver
. Your EL expression can be evaluated againstanything, as long as there is a
VariableResolver
capable of resolving the named things.One of the differences between Limbo's ReferenceContext and JSP EL's VaribleResolver is that theReferenceContext is parameterized with type of context passed in at evaluation time. Typically, with JSP EL, youwill evaluate your expression against a context of of type
java.lang.Object
. The Java compiler will not be ableto verify if the subtype of 
java.lang.Object
you pass in is actually something against which you can evaluatethe expression.If you are creating an expression in Limbo, you will always need to construct that expression parameterized
ReferenceContext
, in which the type parameter is the type of object on which you can apply the expression.So if you have an expression you want to evaluate against an instance of 
Person
, you need to construct the
Expression
based on a
ReferenceContext<Person>
.

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->