Headline

A graph-based implementation using Neo4J and the Gremlin graph query DSL

Motivation

This implementation uses the Technology:Neo4j Technology:Graph database to directly map the 101companies Feature:Hierarchical company to the database model.

Illustration

Employees and departments both are modelled as nodes with names and addresses as properties. Employment, Management and Subdepartment relations are modelled as the graph's edges. Salaries are considered properties of employment and therefore annotated to the corresponding edges.

  • Employment is marked via edges with a "EMPLOYED" relation.
  • A department having a subdepartment is shown by an edge with the "HAS SUBUNIT" relation.
  • An employee being manager of a department is noted via the "MANAGES" relation.
The Gremlin language works by chaining operations on the graph that traverse, or select nodes and edges. To total all salaries in a company, the graph just has to be queried for all edges that are of the "EMPLOYED" type, take their salary property and sum it up. This is achieved with the line:

g.V.outE('EMPLOYED').collect{it.salary}.flatten().sum()

To cut the salaries of employees, Gremlin has to set the value of a property which is treated as a side effect and properly signaled by the use of the sideEffect method:

g.V.out('EMPLOYED').sideEffect{it.salary = (double)it.salary / 2.0}

Note that there has to be a cast in the division as Groovy treats the results of division as BigDecimal rather than double.

Gremlin scripts can be executed on Neo4J via its REST API. Example queries can be sent via the UNIX tool curl, either directly as payload to a request to the respective URL at the local database host:

$ curl -d "script=$MYSCRIPT" http://localhost:7474/db/data/ext/GremlinPlugin/graphdb/execute script

Please note that special characters in the script need to be properly escaped.

Usage

There are three shellscripts that send the respective commands to a local running Neo4J database on default port 7474. Please make sure that no other content is hosted on the local database! Run rebuild.sh to insert the example information into the database:

$ ./rebuild.sh

The basic features are demonstrated by running the respective shellscripts such as:

$ ./total.js

Each script will print out the results of the respective computation.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.