Git Product home page Git Product logo

thepill's Introduction

thepill

Decision Trees with Neo4J

Features | Example | Procedure Installation | Extension Installation | Build | Notes

Features

Model decision trees using Neo4j and Javascript based decision evaluation.

Example

CREATE (tree:Tree { name: 'neo' })
CREATE (red:Leaf { value: 'knowledge' })
CREATE (blue:Leaf { value: 'ignorance' })
CREATE (pill: Decision { name: 'Red Pill Or Blue Pill', question: 'Red Pill Or Blue Pill', 
  choice: 'result = {relationship: \"RED\"}; if(chosenColor === \"blue\") result = {relationship: \"BLUE\"};' })
CREATE (tree)-[:HAS]->(pill)
CREATE (pill)-[:RED]->(red)
CREATE (pill)-[:BLUE]->(blue)

Traverse the decision tree and make a decision:

CALL com.stacktoheap.thepill.make_decision('neo', {chosenColor: "blue"}) yield path return path

Example explained

Create the root of the tree:

CREATE (tree:Tree { name: 'neo' })

Create the final decision for the tree (the leaves):

CREATE (red:Leaf { value: 'knowledge' })
CREATE (blue:Leaf { value: 'ignorance' })

Create the decision node with result evaluation using JS code:

CREATE (pill: Decision { name: 'Red Pill Or Blue Pill', question: 'Red Pill Or Blue Pill', 
  choice: 'result = {relationship: \"RED\"}; if(chosenColor === \"blue\") result = {relationship: \"BLUE\"};' })

result is the variable that is used to make the decision. It is to be set to an object with two properties:

  • relationship - The relationship type for the chosen decision
  • properties - Map of properties that choose the next node (useful if there are multiple relationships of the same type)

The variables available in the choice script are facts supplied to the tree at evaluation time (see below)

Create the relationships:

CREATE (tree)-[:HAS]->(pill)
CREATE (pill)-[:RED]->(red)
CREATE (pill)-[:BLUE]->(blue)

Traverse the tree and make decision:

CALL com.stacktoheap.thepill.make_decision('neo', {chosenColor: "blue"}) yield path return path

Procedure Installation

  1. Download the latest jar from https://github.com/manojlds/thepill/releases

  2. Copy the jar to the plugins folder of Neo4J instance - <neo4j-home>/plugins

  3. To use property based leaves, set the following configuration in <neo4j-home>/conf/neo4j.conf:

    thepill.property_based_leaves=true
    thepill.thepill.leaves_property=is_leaf
    

    With property based leaves enabled, existing nodes can be reused as leaves in the decision tree by adding the property specified by the thepill.thepill.leaves_property configuration.

  4. (Re)Start the server

  5. Apply the schema for thepill:

    CALL com.stacktoheap.thepill.schema.generate

Extension Installation

The optional unmanaged server extension can be installed as follows:

  1. Download the latest jar from https://github.com/manojlds/thepill/releases

  2. Copy the jar to the plugins folder of Neo4J instance - <neo4j-home>/plugins

  3. Add the following configuration in <neo4j-home>/conf/neo4j.conf:

    dbms.unmanaged_extension_classes=com.stacktoheap.thepill=/thepill
    
    
  4. Restart the server

  5. The <server>/thepill/make_decision endpoint is available for use. Example:

    curl -H 'Content-Type: application/json' -X POST  http://localhost:7474/thepill/make_decision/neo -d "{\"chosenColor\": \"blue\"}"

Build

Compile and run tests with ./gradlew build

Create fat jar with ./gradlew shadowJar

Notes

Based on - https://github.com/maxdemarzi/decision_trees_with_rules

Differs in the following ways:

  1. Uses Javascript for script evaluation rather than Java.
  2. Ability to choose relationships based on properties.
  3. No insecure APIs usage.
  4. Updated for latest Neo4j and written in Kotlin.

thepill's People

Contributors

manojlds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

dapper-magician

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.