This is an implementation in the 101companies software corpus.
Provide an example of a graph-based implementation of 101companies using Neo4J and the Gremlin graph query DSL.
This implementation uses the Technology:Neo4J Technology:Graph database to directly map the 101companies 101feature: Tree structure to the database model.
- Technology:Neo4J
- Technology:Gremlin
- Language:Groovy
- 101feature:Tree structure
- 101feature:Type-driven query
- 101feature:Type-driven transformation
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.
<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.
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.