Recursive Model Example

This example explain how to apply X-O lite XML serialization/deserialization to a complex recursive model.

The model for this example is a boolean expression like:

(a and (true or b)) or (not (a and c)))

for this example, he expressions are kept simple, they are based on few building blocks:

  • 3 operators: and, or, not.
  • 2 constants: true, false.
  • variables identified by their name.

This expression above (as any expression) can be expressed as a syntactic tree:

syntactic tree for a boolean expression

This syntactic tree naturally maps to a XML tree:

<?xml version="1.0"?>
<or xmlns="xo-lite.sf.net/examples/expression">
    <and>
        <variable name="a" />
        <or>
            <true />
            <variable name="b" />
        </or>
    </and>
    <not>
        <and>
            <variable name="a" />
            <variable name="c" />
        </and>
    </not>
</or>

In the scope of this example, the boolean expressions are composite of java objects with following behaviour:

  • Evaluation: giving a context (where value of each variable is defined) you can evaluate a boolean expression.
  • Usual string representation: from the tree of java object we can generate usual infix operator representation like: a and (true or b)

Source files

The complete source code of this example is located in the:

xo-lite-1.0/src-projects/xo-lite-examples/src/main/java/net/sf/xolite/expression/xolite

directory inside the xo-lite-1.0-all.zip or xo-lite-1.0-all.tgz distribution archive.

To build and run all the examples:

  • Extract the distribution archive in any directory.
  • ensure you have Maven latest version installed and configured on your computer.
  • issue the "mvn compile exec:java" shell command from the .../xo-lite-1.0/src-projects/xo-lite-examples directory.

Implementation Description

TODO.

Conclusion

Serializing/deserializing recursive models (the 'composite' design pattern) is done naturally with X-O lite. In addition, the inheritance between classes can easily be used to simplify the parsing (in this case, the "And", "Or" and "Not" operators doesn't have to define parsing as they inherit everything needed from the CompositeOperator class).