SlideShare a Scribd company logo
Decision Trees in Neo4j
Build dynamic expert systems and rules engines.
github.com/maxdemarzi
About 200 public repositories
Max De Marzi
Neo4j Field Engineer
About
Me !
01
02
03
04
maxdemarzi.com
@maxdemarzi
Let’s start with a Sign
Rule for Getting In
• Anyone over 21 years old gets in.
• On some nights, women 18 years or older get in.
• On some nights, men 18 years or older get in.
• The last two change dynamically.
Represent in a Graph
Rule Node
• expression (String):
• (age >= 18) && gender.equals(“female”)
• parameter_names(String):
• age, gender
• parameter_types(String):
• Int, String
There was a time…
BEFORE
When the Traversal API ruled supreme
https://maxdemarzi.com/2015/09/04/flight-search-with-the-neo4j-traversal-api/
Learn more… or read the docs
The Stored Procedure
@Procedure(name = "com.maxdemarzi.traverse.decision_tree", mode = Mode.READ)
@Description("CALL com.maxdemarzi.traverse.decision_tree(tree, facts)")
public Stream<PathResult> traverseDecisionTree(
@Name("tree") String id,
@Name("facts") Map<String, String> facts){
// Which Decision Tree are we interested in?
Node tree = db.findNode(Labels.Tree, "id", id);
if ( tree != null) {
// Find the paths by traversing this graph and the facts given
return decisionPath(tree, facts);
}
return null;
}
Tree Facts
private Stream<PathResult> decisionPath(Node tree, 

Map<String, String> facts) {
TraversalDescription myTraversal =
db.traversalDescription()
.depthFirst()
.expand(new DecisionTreeExpander(facts))
.evaluator(decisionTreeEvaluator);
return myTraversal
.traverse(tree)
.stream()
.map(PathResult::new);
}
Build a Traversal Description
public class DecisionTreeEvaluator implements PathEvaluator {
@Override
public Evaluation evaluate(Path path, BranchState branchState) {
// If we get to an Answer stop traversing, we found a valid path.
if (path.endNode().hasLabel(Labels.Answer)) {
return Evaluation.INCLUDE_AND_PRUNE;
} else {
// If not, continue down this path 

// to see if there is anything else to find.
return Evaluation.EXCLUDE_AND_CONTINUE;
}
}
Build a Path Evaluator
public class DecisionTreeExpander implements PathExpander {
private Map<String, String> facts;
private ExpressionEvaluator ee = new ExpressionEvaluator();
public DecisionTreeExpander(Map<String, String> facts) {
this.facts = facts;
ee.setExpressionType(boolean.class);
}
Build a Path Expander
@Override
public Iterable<Relationship> expand(Path path, BranchState branchState) {
// If we get to an Answer stop traversing, we found a valid path.
if (path.endNode().hasLabel(Labels.Answer)) {
return Collections.emptyList();
}
// If we have Rules to evaluate, go do that.
if (path.endNode()
.hasRelationship(Direction.OUTGOING, RelationshipTypes.HAS)) {
return path.endNode()
.getRelationships(Direction.OUTGOING, RelationshipTypes.HAS);
}
The expand method
if (path.endNode().hasLabel(Labels.Rule)) {
try {
if (isTrue(path.endNode())) {
return path.endNode()
.getRelationships(Direction.OUTGOING,
RelationshipTypes.IS_TRUE);
} else {
return path.endNode()
.getRelationships(Direction.OUTGOING,
RelationshipTypes.IS_FALSE);
}
} catch (Exception e) {
// Could not continue this way!
return Collections.emptyList();
}
}
The expand method (cont.)
boolean isTrue(Node rule) throws Exception {
Map<String, Object> ruleProperties = rule.getAllProperties();
String[] parameterNames =
Magic.explode((String) ruleProperties
.get("parameter_names"));
Class<?>[] parameterTypes =
Magic.stringToTypes((String) ruleProperties
.get("parameter_types"));
The isTrue method
Object[] arguments = new Object[parameterNames.length];
for (int j = 0; j < parameterNames.length; ++j) {
arguments[j] = Magic.createObject(parameterTypes[j],
facts.get(parameterNames[j]));
}
ee.setParameters(parameterNames, parameterTypes);
ee.cook((String)ruleProperties.get("expression"));
return (boolean) ee.evaluate(arguments);
The isTrue method (cont.)
Running it
CREATE (tree:Tree { id: 'bar entrance' })
CREATE (over21_rule:Rule { parameter_names: 'age',
parameter_types:'int',
expression:'age >= 21' })
CREATE (gender_rule:Rule { parameter_names: 'age,gender',
parameter_types:'int,String',
expression:'(age >= 18) && gender.equals("female")' })
CREATE (answer_yes:Answer { id: 'yes'})
CREATE (answer_no:Answer { id: 'no'})
CREATE (tree)-[:HAS]->(over21_rule)
CREATE (over21_rule)-[:IS_TRUE]->(answer_yes)
CREATE (over21_rule)-[:IS_FALSE]->(gender_rule)
CREATE (gender_rule)-[:IS_TRUE]->(answer_yes)
CREATE (gender_rule)-[:IS_FALSE]->(answer_no)
Build the Tree
CALL com.maxdemarzi.traverse.decision_tree(

‘bar entrance', {gender:'male', age:'20'})
YIELD path
RETURN path;
Check the Facts
Male, 20 years old?
It’s alright…
CALL com.maxdemarzi.traverse.decision_tree(

‘bar entrance', {gender:'female', age:’19'})
YIELD path
RETURN path;
Female, 19 years old?
Check the Facts
OMG…
Learn More…
https://maxdemarzi.com/2018/01/14/dynamic-rule-based-decision-trees-in-neo4j/
Details:
What about multiple Options?
https://maxdemarzi.com/2018/01/26/dynamic-rule-based-decision-trees-in-neo4j-part-2/
There is a Part 2
Decision Streams
• Paper: 

https://arxiv.org/pdf/1704.07657.pdf
• Authors:

Dmitry Ignatov and Andrey Ignatov
• Implementation: 

https://github.com/aiff22/Decision-Stream
Thank you!

More Related Content

What's hot

Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
Raji Ghawi
 
dotSwift 2016 : Beyond Crusty - Real-World Protocols
dotSwift 2016 : Beyond Crusty - Real-World ProtocolsdotSwift 2016 : Beyond Crusty - Real-World Protocols
dotSwift 2016 : Beyond Crusty - Real-World Protocols
Rob Napier
 
Java Hello World Program
Java Hello World ProgramJava Hello World Program
Java Hello World Program
JavaWithUs
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
Gagan Agrawal
 

What's hot (6)

Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
dotSwift 2016 : Beyond Crusty - Real-World Protocols
dotSwift 2016 : Beyond Crusty - Real-World ProtocolsdotSwift 2016 : Beyond Crusty - Real-World Protocols
dotSwift 2016 : Beyond Crusty - Real-World Protocols
 
Java Hello World Program
Java Hello World ProgramJava Hello World Program
Java Hello World Program
 
webScrapingFunctions
webScrapingFunctionswebScrapingFunctions
webScrapingFunctions
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
 

Similar to Decision Trees in Neo4j

MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
Howard Greenberg
 
More Stored Procedures and MUMPS for DivConq
More Stored Procedures and  MUMPS for DivConqMore Stored Procedures and  MUMPS for DivConq
More Stored Procedures and MUMPS for DivConq
eTimeline, LLC
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
Mite Mitreski
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...DEEPANSHU GUPTA
 
Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections
Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections
Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections
Teamstudio
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013yohanbeschi
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut
 
SAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersSAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginners
Hicham QAISSI
 
RESTful Web Services with Jersey
RESTful Web Services with JerseyRESTful Web Services with Jersey
RESTful Web Services with Jersey
Scott Leberknight
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
Jesper Kamstrup Linnet
 
Querydsl fin jug - june 2012
Querydsl   fin jug - june 2012Querydsl   fin jug - june 2012
Querydsl fin jug - june 2012
Timo Westkämper
 
Simple API for XML
Simple API for XMLSimple API for XML
Simple API for XML
guest2556de
 
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Ruby Meditation
 
C++ 11 usage experience
C++ 11 usage experienceC++ 11 usage experience
C++ 11 usage experience
GlobalLogic Ukraine
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
Sandeep Kr. Singh
 
Java 8 revealed
Java 8 revealedJava 8 revealed
Java 8 revealed
Sazzadur Rahaman
 

Similar to Decision Trees in Neo4j (20)

MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
 
More Stored Procedures and MUMPS for DivConq
More Stored Procedures and  MUMPS for DivConqMore Stored Procedures and  MUMPS for DivConq
More Stored Procedures and MUMPS for DivConq
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
 
Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections
Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections
Take a Trip Into the Forest: A Java Primer on Maps, Trees, and Collections
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
SAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersSAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginners
 
RESTful Web Services with Jersey
RESTful Web Services with JerseyRESTful Web Services with Jersey
RESTful Web Services with Jersey
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
Querydsl fin jug - june 2012
Querydsl   fin jug - june 2012Querydsl   fin jug - june 2012
Querydsl fin jug - june 2012
 
Simple API for XML
Simple API for XMLSimple API for XML
Simple API for XML
 
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
 
C++ 11 usage experience
C++ 11 usage experienceC++ 11 usage experience
C++ 11 usage experience
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Java 8 revealed
Java 8 revealedJava 8 revealed
Java 8 revealed
 

More from Max De Marzi

DataDay 2023 Presentation
DataDay 2023 PresentationDataDay 2023 Presentation
DataDay 2023 Presentation
Max De Marzi
 
DataDay 2023 Presentation - Notes
DataDay 2023 Presentation - NotesDataDay 2023 Presentation - Notes
DataDay 2023 Presentation - Notes
Max De Marzi
 
Developer Intro Deck-PowerPoint - Download for Speaker Notes
Developer Intro Deck-PowerPoint - Download for Speaker NotesDeveloper Intro Deck-PowerPoint - Download for Speaker Notes
Developer Intro Deck-PowerPoint - Download for Speaker Notes
Max De Marzi
 
Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph Databases
Max De Marzi
 
Neo4j Training Cypher
Neo4j Training CypherNeo4j Training Cypher
Neo4j Training Cypher
Max De Marzi
 
Neo4j Training Modeling
Neo4j Training ModelingNeo4j Training Modeling
Neo4j Training Modeling
Max De Marzi
 
Neo4j Training Introduction
Neo4j Training IntroductionNeo4j Training Introduction
Neo4j Training Introduction
Max De Marzi
 
Detenga el fraude complejo con Neo4j
Detenga el fraude complejo con Neo4jDetenga el fraude complejo con Neo4j
Detenga el fraude complejo con Neo4j
Max De Marzi
 
Data Modeling Tricks for Neo4j
Data Modeling Tricks for Neo4jData Modeling Tricks for Neo4j
Data Modeling Tricks for Neo4j
Max De Marzi
 
Fraud Detection and Neo4j
Fraud Detection and Neo4j Fraud Detection and Neo4j
Fraud Detection and Neo4j
Max De Marzi
 
Detecion de Fraude con Neo4j
Detecion de Fraude con Neo4jDetecion de Fraude con Neo4j
Detecion de Fraude con Neo4j
Max De Marzi
 
Neo4j Data Science Presentation
Neo4j Data Science PresentationNeo4j Data Science Presentation
Neo4j Data Science Presentation
Max De Marzi
 
Neo4j Stored Procedure Training Part 2
Neo4j Stored Procedure Training Part 2Neo4j Stored Procedure Training Part 2
Neo4j Stored Procedure Training Part 2
Max De Marzi
 
Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1
Max De Marzi
 
Neo4j y Fraude Spanish
Neo4j y Fraude SpanishNeo4j y Fraude Spanish
Neo4j y Fraude Spanish
Max De Marzi
 
Data modeling with neo4j tutorial
Data modeling with neo4j tutorialData modeling with neo4j tutorial
Data modeling with neo4j tutorial
Max De Marzi
 
Neo4j Fundamentals
Neo4j FundamentalsNeo4j Fundamentals
Neo4j Fundamentals
Max De Marzi
 
Neo4j Presentation
Neo4j PresentationNeo4j Presentation
Neo4j Presentation
Max De Marzi
 
Fraud Detection Class Slides
Fraud Detection Class SlidesFraud Detection Class Slides
Fraud Detection Class Slides
Max De Marzi
 
Neo4j in Depth
Neo4j in DepthNeo4j in Depth
Neo4j in Depth
Max De Marzi
 

More from Max De Marzi (20)

DataDay 2023 Presentation
DataDay 2023 PresentationDataDay 2023 Presentation
DataDay 2023 Presentation
 
DataDay 2023 Presentation - Notes
DataDay 2023 Presentation - NotesDataDay 2023 Presentation - Notes
DataDay 2023 Presentation - Notes
 
Developer Intro Deck-PowerPoint - Download for Speaker Notes
Developer Intro Deck-PowerPoint - Download for Speaker NotesDeveloper Intro Deck-PowerPoint - Download for Speaker Notes
Developer Intro Deck-PowerPoint - Download for Speaker Notes
 
Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph Databases
 
Neo4j Training Cypher
Neo4j Training CypherNeo4j Training Cypher
Neo4j Training Cypher
 
Neo4j Training Modeling
Neo4j Training ModelingNeo4j Training Modeling
Neo4j Training Modeling
 
Neo4j Training Introduction
Neo4j Training IntroductionNeo4j Training Introduction
Neo4j Training Introduction
 
Detenga el fraude complejo con Neo4j
Detenga el fraude complejo con Neo4jDetenga el fraude complejo con Neo4j
Detenga el fraude complejo con Neo4j
 
Data Modeling Tricks for Neo4j
Data Modeling Tricks for Neo4jData Modeling Tricks for Neo4j
Data Modeling Tricks for Neo4j
 
Fraud Detection and Neo4j
Fraud Detection and Neo4j Fraud Detection and Neo4j
Fraud Detection and Neo4j
 
Detecion de Fraude con Neo4j
Detecion de Fraude con Neo4jDetecion de Fraude con Neo4j
Detecion de Fraude con Neo4j
 
Neo4j Data Science Presentation
Neo4j Data Science PresentationNeo4j Data Science Presentation
Neo4j Data Science Presentation
 
Neo4j Stored Procedure Training Part 2
Neo4j Stored Procedure Training Part 2Neo4j Stored Procedure Training Part 2
Neo4j Stored Procedure Training Part 2
 
Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1
 
Neo4j y Fraude Spanish
Neo4j y Fraude SpanishNeo4j y Fraude Spanish
Neo4j y Fraude Spanish
 
Data modeling with neo4j tutorial
Data modeling with neo4j tutorialData modeling with neo4j tutorial
Data modeling with neo4j tutorial
 
Neo4j Fundamentals
Neo4j FundamentalsNeo4j Fundamentals
Neo4j Fundamentals
 
Neo4j Presentation
Neo4j PresentationNeo4j Presentation
Neo4j Presentation
 
Fraud Detection Class Slides
Fraud Detection Class SlidesFraud Detection Class Slides
Fraud Detection Class Slides
 
Neo4j in Depth
Neo4j in DepthNeo4j in Depth
Neo4j in Depth
 

Recently uploaded

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 

Recently uploaded (20)

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 

Decision Trees in Neo4j

  • 1. Decision Trees in Neo4j Build dynamic expert systems and rules engines.
  • 2. github.com/maxdemarzi About 200 public repositories Max De Marzi Neo4j Field Engineer About Me ! 01 02 03 04 maxdemarzi.com @maxdemarzi
  • 4.
  • 5.
  • 6. Rule for Getting In • Anyone over 21 years old gets in. • On some nights, women 18 years or older get in. • On some nights, men 18 years or older get in. • The last two change dynamically.
  • 8.
  • 9. Rule Node • expression (String): • (age >= 18) && gender.equals(“female”) • parameter_names(String): • age, gender • parameter_types(String): • Int, String
  • 10. There was a time… BEFORE
  • 11.
  • 12. When the Traversal API ruled supreme
  • 15. @Procedure(name = "com.maxdemarzi.traverse.decision_tree", mode = Mode.READ) @Description("CALL com.maxdemarzi.traverse.decision_tree(tree, facts)") public Stream<PathResult> traverseDecisionTree( @Name("tree") String id, @Name("facts") Map<String, String> facts){ // Which Decision Tree are we interested in? Node tree = db.findNode(Labels.Tree, "id", id); if ( tree != null) { // Find the paths by traversing this graph and the facts given return decisionPath(tree, facts); } return null; } Tree Facts
  • 16. private Stream<PathResult> decisionPath(Node tree, 
 Map<String, String> facts) { TraversalDescription myTraversal = db.traversalDescription() .depthFirst() .expand(new DecisionTreeExpander(facts)) .evaluator(decisionTreeEvaluator); return myTraversal .traverse(tree) .stream() .map(PathResult::new); } Build a Traversal Description
  • 17.
  • 18. public class DecisionTreeEvaluator implements PathEvaluator { @Override public Evaluation evaluate(Path path, BranchState branchState) { // If we get to an Answer stop traversing, we found a valid path. if (path.endNode().hasLabel(Labels.Answer)) { return Evaluation.INCLUDE_AND_PRUNE; } else { // If not, continue down this path 
 // to see if there is anything else to find. return Evaluation.EXCLUDE_AND_CONTINUE; } } Build a Path Evaluator
  • 19.
  • 20. public class DecisionTreeExpander implements PathExpander { private Map<String, String> facts; private ExpressionEvaluator ee = new ExpressionEvaluator(); public DecisionTreeExpander(Map<String, String> facts) { this.facts = facts; ee.setExpressionType(boolean.class); } Build a Path Expander
  • 21. @Override public Iterable<Relationship> expand(Path path, BranchState branchState) { // If we get to an Answer stop traversing, we found a valid path. if (path.endNode().hasLabel(Labels.Answer)) { return Collections.emptyList(); } // If we have Rules to evaluate, go do that. if (path.endNode() .hasRelationship(Direction.OUTGOING, RelationshipTypes.HAS)) { return path.endNode() .getRelationships(Direction.OUTGOING, RelationshipTypes.HAS); } The expand method
  • 22. if (path.endNode().hasLabel(Labels.Rule)) { try { if (isTrue(path.endNode())) { return path.endNode() .getRelationships(Direction.OUTGOING, RelationshipTypes.IS_TRUE); } else { return path.endNode() .getRelationships(Direction.OUTGOING, RelationshipTypes.IS_FALSE); } } catch (Exception e) { // Could not continue this way! return Collections.emptyList(); } } The expand method (cont.)
  • 23.
  • 24. boolean isTrue(Node rule) throws Exception { Map<String, Object> ruleProperties = rule.getAllProperties(); String[] parameterNames = Magic.explode((String) ruleProperties .get("parameter_names")); Class<?>[] parameterTypes = Magic.stringToTypes((String) ruleProperties .get("parameter_types")); The isTrue method
  • 25. Object[] arguments = new Object[parameterNames.length]; for (int j = 0; j < parameterNames.length; ++j) { arguments[j] = Magic.createObject(parameterTypes[j], facts.get(parameterNames[j])); } ee.setParameters(parameterNames, parameterTypes); ee.cook((String)ruleProperties.get("expression")); return (boolean) ee.evaluate(arguments); The isTrue method (cont.)
  • 27.
  • 28.
  • 29. CREATE (tree:Tree { id: 'bar entrance' }) CREATE (over21_rule:Rule { parameter_names: 'age', parameter_types:'int', expression:'age >= 21' }) CREATE (gender_rule:Rule { parameter_names: 'age,gender', parameter_types:'int,String', expression:'(age >= 18) && gender.equals("female")' }) CREATE (answer_yes:Answer { id: 'yes'}) CREATE (answer_no:Answer { id: 'no'}) CREATE (tree)-[:HAS]->(over21_rule) CREATE (over21_rule)-[:IS_TRUE]->(answer_yes) CREATE (over21_rule)-[:IS_FALSE]->(gender_rule) CREATE (gender_rule)-[:IS_TRUE]->(answer_yes) CREATE (gender_rule)-[:IS_FALSE]->(answer_no) Build the Tree
  • 30.
  • 31. CALL com.maxdemarzi.traverse.decision_tree(
 ‘bar entrance', {gender:'male', age:'20'}) YIELD path RETURN path; Check the Facts Male, 20 years old?
  • 32.
  • 34. CALL com.maxdemarzi.traverse.decision_tree(
 ‘bar entrance', {gender:'female', age:’19'}) YIELD path RETURN path; Female, 19 years old? Check the Facts
  • 35.
  • 37.
  • 42.
  • 43. Decision Streams • Paper: 
 https://arxiv.org/pdf/1704.07657.pdf • Authors:
 Dmitry Ignatov and Andrey Ignatov • Implementation: 
 https://github.com/aiff22/Decision-Stream
  • 44.