Skip to content

DerDackel/gremlin-neo4j101companies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is an implementation in the 101companies software corpus.

Table of Contents

Intent

Provide an example of a graph-based implementation of 101companies using Neo4J and the Gremlin graph query DSL.

Motivation

This implementation uses the Technology:Neo4J Technology:Graph database to directly map the 101companies 101feature: Tree structure to the database model.

Technologies

  • Technology:Neo4J
  • Technology:Gremlin

Languages

  • Language:Groovy

Features

  • 101feature:Tree structure
  • 101feature:Type-driven query
  • 101feature:Type-driven transformation

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:

<syntaxhighlight lang="groovy"> g.V.outE('EMPLOYED').collect{it.salary}.flatten().sum() </syntaxhighlight>

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:

<syntaxhighlight lang="groovy"> g.V.out('EMPLOYED').sideEffect{it.salary = (double)it.salary / 2.0} </syntaxhighlight>

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 Technology: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:

<syntaxhighlight lang="bash"> $ curl -d "script=$MYSCRIPT" http://localhost:7474/db/data/ext/GremlinPlugin/graphdb/execute_script </syntaxhighlight>

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:

<syntaxhighlight lang="bash"> $ ./rebuild.sh </syntaxhighlight>

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

<syntaxhighlight lang="bash"> $ ./total.js </syntaxhighlight>

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

Contributors

About

A Gremlin-based 101companies implementation using Neo4J as a backend

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages