Building ODataURI Parser with Scala Parser Combinators

Objective

Open Data Protocol (ODATA) facilitates end-users to access the data-model via REST-based data services by utilizing Uniform Resource Identifiers (URIs). In this post, we present the result of our recent experiment to build an abstraction on ODATA URIs to generate AST (Abstract Syntax Tree).

Note that this experiment is in its initial stage; hence, the implementation does not support complete feature-set of ODATA URI specification outlined at odata.org. It states a set of recommendations to construct these URIs to effectively identify data and metadata exposed by ODATA services.

To give an example of ODATA URI, consider following URI:

http://odata.org/service.svc/Products?$filter=Price ge 10

It in essence refers to a service request to return all the Product entities that satisfies the following predicate: Price greater than or equal to 10.

Motivation

Primary motivation of building such abstraction is to promote separation-of-concern and consequently, to allow the underlying layers of ODATA service implementation to process query expression tree and yield the result-set in a more efficient manner.

Approach

To implement this parser, we use Parser Combinators, which is in essence a higher-order function that accepts a set of parsers as input and composes them, applies transformations and generates more complex parser. By employing theoretical foundations of function composition, it allows constructing complex parser in an incremental manner.

Scala facilitates such libraries in its standard distribution (see scala.util.parsing). In this implementation, we in particular, use JavaTokenParsers along with PackratParser.

class ODataUriParser extends JavaTokenParsers with PackratParsers {
//...
}

Implementation

ODATA URI contains three fundamental parts, namely Service Root URI, Resource Path and Query Options as below and as per the documentations at [1].

odata-uri-grammar

If we consider the ODATA URI mentioned previously, following illustrates the stated three parts of ODATA request:

odata-uri-parts

Hence, we can construct this parser by building combinators for the three sub-parts in a bottom-up manner and then compose them to construct the complete parser as listed below.

To gets started with an example, lets consider following URI:

http://odata.io/odata.svc/Schema(231)/Customer?$top=2&$filter=concat(City, Country) eq 'Berlin, Germany'

and we are expecting an expression tree based on a pre-defined model as follows:

Building a parser combinator for Service Root and Resource Path are considerably simpler compared to that of Query Options (the third part). Let’s build them first.

We are using this convention (see ODATA specification) that a ODATA service root should always be ended by .svc. The following snippet can parse for instance http://odata.io/odata.svc to URL("http://odata.io/odata.svc").

Next we are defining a resource path which can parse for instance Schema(231) to ResourcePath("Schema",Number("231"),ResourcePath("Customer",EmptyExp(),EmptyExp())) expressions. A compound resource path can be augmented with multiple resources.

After that we have reached to the crux of the problem: to build a parser that can handle the query operators defined in the OData specification. To solve it, we apply bottom up approach in conjunction with top-down realization.

First we define a basic parser that can parse arithmetic expressions as follows.

Then we incrementally augment support for handling relational operators, and thus can handle logical and, or and similar operation.

The above two code listings form the basis to provide support for the query operations such as $filter and $select. See below.

Thus, it allows to parse the URI to expression tree as shown below.

Or, as follows:

Conclusion

The complete source of this project is available at github repository. Please feel free to browse and if there is any question, please post.

See More:

  1. OData URI Specification
  2. External DSLs made easy with Scala Parser Combinators
  3. DSLs in Action

Coalesce data functionally

In a recent project I had to coalesce quite significant amount of data in the following way. To simplify it for this post, consider that we have the following two lists.

val x = List(“a”, “b”, “c”, “a”)

val y = List(1, 2, 6, 9)

We are about to write a function which would return the following list as the result.

val result = List((a,10), (b,2), (c,6))

Basically it would coalesce value with the same category. See for instance “b” in the above example.

Language that came up with repl inherently provides very nice way to try out different expression and to get to the expected outcome. In this context, as we are using scala, we can use repl-driven development quite conveniently as illustrated below.

  • Define the Lists:
scala> val x = List("a", "b" , "c", "a")
x: List[String] = List(a, b, c, a)

scala> val y = List(1,2,6,9)
y: List[Int] = List(1, 2, 6, 9)
  • Zip them.
scala> val z = x zip y
z: List[(String, Int)] = List((a,1), (b,2), (c,6), (a,9))
scala>
  • Group them based on the values of x.
scala> val grps = z groupBy (_._1)
grps: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(b -> List((b,2)), a -> List((a,1), (a,9)), c -> List((c,6)))

scala>
  • Map the values of res8 and reduce them to compute the sum.
scala> val res = grps.values.map {_.reduce((i,j) => (i._1, (i._2+j._2)))}

res: Iterable[(String, Int)] = List((b,2), (a,10), (c,6))
scala>
  • Sort res based on the 1st value of the tuple.
scala> res.toList.sorted
res23: List[(String, Int)] = List((a,10), (b,2), (c,6))

scala>

Thus, the function can be simply written as follows:

Thus we get the expected result.

Akka: Links, News And Resources (7)

Akka: Links, News And Resources (6)

Originally posted on Angel \"Java\" Lopez on Blog:

Previous Post
Next Post

You know: I’m interested in actor models in general, and Akka implementation in particular, having distributed applications. I started two projects implementing Akka ideas, in Node.js and in C#:

https://github.com/ajlopez/SimpleActors
https://github.com/ajlopez/Aktores

Now, more links of my collection of links:

mrb: Distributed Systems Archaeology: Ricon West, 2013.10.30
http://michaelrbernste.in/2013/11/22/distributed-systems-archaeology.html

Using Akka in Scala IDE – Stack Overflow
http://stackoverflow.com/questions/13585927/using-akka-in-scala-ide

Pacific Northwest Scala 2013 Akka in Production: Our Story by Evan Chan – YouTube
http://www.youtube.com/watch?v=c1heorOM2LE

Akka vs Storm | Blog of Adam Warski | Planet JBoss Community
http://planet.jboss.org/post/akka_vs_storm

Akka in Production: Our Story
http://www.slideshare.net/EvanChan2/akka-inproductionpnw-scala2013

typesafe.com
http://typesafe.com/blog/typesafe-gets-sprayed

Typesafe Reactive Platform Acquires New High-Performance HTTP Foundation
http://www.marketwired.com/press-release/Typesafe-Reactive-Platform-Acquires-New-High-Performance-HTTP-Foundation-1841738.htm

Dev Time: Cope with Failure – Actor Supervision in Akka
http://blog.florian-hopf.de/2013/10/cope-with-failure-actor-supervision-in.html

Akka Work Pulling Pattern to prevent mailbox overflow, throttle and distribute work » Michael on development
http://www.michaelpollmeier.com/akka-work-pulling-pattern/

Going Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
http://www.slideshare.net/jboner/going-reactive-eventdriven-scalable-resilient-systems

Let it crash • Where Akka Came From

View original 18 more words

JAVA 8: Building a TriFunction functional interface

Java 8 facilitates several functional interface in java.util.function package. If we observe carefully, there are primarily four kinds of interfaces provided in this package, which are:

  • Supplier
  • Consumer
  • Function
  • Predicate

with the following signatures:

    Supplier: () -> T
    Consumer: T -> () 
    Predicate: T -> boolean 
    Function: T -> R

In this post, alike java.util.function package, we define a new functional interface, called TriFrunction that accepts three arguments as parameters and returns the result after computation.

    @FunctionalInterface
    public interface TriFunction {

        /**
         * Applies this function to the given arguments.
         *
         * @param t the first function argument
         * @param u the second function argument
         * @param s the third function argument
         * @return the function result
         */
        R apply(T t, U u, S s);
    }

We can use this functional interface to compute the volume of a rectangular prism, by using following lambda expression.

TriFunction volume = (x,y,z) -> x*y*z

To use this lambda expression in the volume computation, we can simply use following statement

volume.apply(2.4, 5.3, 10.4)

In this post, we have shown how to create a custom functional interface and use it to write concise lambda expressions using Java 8. It seems quite straight-forward. If you have any question/remark, please leave a comment below.