Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Chanakya: Building Composable Logic Rules

Neha
January 12, 2019

Chanakya: Building Composable Logic Rules

This is a generic library for building and executing a complex rule by composing simple logical filters. We will discuss how we (at Helpshift) have built a lightweight rule based engine with a common way to define the criteria (a rule) and a common way to know if some object matches a rule. The rule engine will convert a rule into a query or fetch documents that match the query. The engine will take an object and a series of rules and return all the rules that match the object.

Neha

January 12, 2019
Tweet

Other Decks in Education

Transcript

  1. Wouldn’t it be nice to have “ONE query language for

    all” Variety of datastores: • Elasticsearch • Mongo • SQL • Redis
  2. Who will get a laptop? Subjects: • Maths • Science

    • History Score is greater than 50
  3. Who will get a laptop? Subjects: • Maths • Science

    • History Score is greater than 50 and
  4. Who will get a laptop? Subjects: • Maths • Science

    • History Score is greater than 50 and Subject is one of (Maths, Science)
  5. Who will get a laptop? Subjects: • Maths • Science

    • History Score is greater than 50 and Subject is one of (Maths, Science) X
  6. Who will get a laptop? Subjects: • Maths • Science

    • History Score is greater than 50 and Subject is one of (Maths, Science) X Y
  7. Who will get a laptop? Subjects: • Maths • Science

    • History Score is greater than 50 and Subject is one of (Maths, Science) X Y and
  8. Score is greater than 50 and Subject is one of

    (Maths, Science) Mongo Query Case 1 : English -> MongoDB Query
  9. Case 1 : English -> MongoDB Query db.class.find( {score: {$gt:

    50 }}) Score is greater than 50 and Subject is one of (Maths, Science)
  10. Case 1 : English -> MongoDB Query db.class.find( {score: {$gt:

    50 }, subject: {$in: [“Maths” , “Science”]}}) Score is greater than 50 and Subject is one of (Maths, Science)
  11. Score is greater than 50 and Subject is one of

    (Maths, Science) SQL Query Case 2 : English -> SQL Query Mongo Query
  12. Case 2 : English -> SQL Query Score is greater

    than 50 SELECT * FROM class WHERE score > 50;
  13. Case 2 : English -> SQL Query Score is greater

    than 50 and Subject is one of (Maths, Science) SELECT * FROM class WHERE score > 50;
  14. Case 2 : English -> SQL Query Score is greater

    than 50 and Subject is one of (Maths, Science) SELECT * FROM class WHERE score > 50 AND subject IN ("Maths","Science");
  15. Score is greater than 50 and Subject is one of

    (Maths, Science) SQL Query Case 3 : English -> Elasticsearch Query Mongo Query Elasticsearch Query
  16. Score is greater than 50 Subject is one of (Maths,

    Science) {"filter" {"range" {"Score" {"gt" 50}}}} Case 3 : English -> Elasticsearch Query
  17. Score is greater than 50 Subject is one of (Maths,

    Science) {"filter" {"range" {"Score" {"gt" 50}}}} {"filter" {"bool" {"should" [{"term" {"Subject” "Maths"}} {"term" {"Subject" "Science"}}]}}} Case 3 : English -> Elasticsearch Query
  18. {"filter": {"bool": {"must": [{"range": {"Score": {"gt": 50}}} {"bool": {"should": [{"term":

    {"Subject": "Maths"}} {"term": {"Subject": "Science"}}]}}]}}} Score is greater than 50 and Subject is one of (Maths, Science) Case 3 : English -> Elasticsearch Query
  19. Score is greater than 50 and Subject is one of

    (Maths, Science) SQL Query Mongo Query Elasticsearch Query Problem Statement Elasticsearch SQL DB MongoDB
  20. Next Questions.. • DSL of Business logic query? • DSL

    of Datastore query logic rules? • Will these DSLs be compatible with different types of interfaces?
  21. Define Business Logic query {:prop "Score" :op "is-greater-than" :type "number"

    :val "50"} Is Score greater than 50? Subject is one of [“Maths” , “Science”]
  22. Define Business Logic query {:prop "Score" :op "is-greater-than" :type "number"

    :val "50"} {:prop "Subject" :op "is-one-of" :type "enum" :val ["Maths" "Science"]} Is Score greater than 50? Subject is one of [“Maths” , “Science”]
  23. Define Business Logic query {:prop "" :type "logical" :op "and"

    :val [{:prop "Score" :op "is-greater-than" :type "number" :val "50"} {:prop "Subject" :op "is-one-of" :type "enum" :val ["Maths" "Science"]}]} Is Score greater than 50? AND Subject is one of [“Maths” , “Science”]
  24. Define Datastore Query Logic rules - SQL SELECT * FROM

    class WHERE score > 50 AND subject IN ("Maths","Science");
  25. Define Datastore Query Logic rules - SQL {:score [> 50]}

    SELECT * FROM class WHERE score > 50 AND subject IN ("Maths","Science");
  26. Define Datastore Query Logic rules - SQL {:score [> 50]}

    {:subject [in ["Maths" "Science"]]} SELECT * FROM class WHERE score > 50 AND subject IN ("Maths","Science");
  27. Define Datastore Query Logic rules - SQL {:score [> 50]

    :subject [in ["Maths" "Science"]]} {:score [> 50]} {:subject [in ["Maths" "Science"]]} SELECT * FROM class WHERE score > 50 AND subject IN ("Maths","Science");
  28. Define Datastore Query Logic rules - SQL {:score [> 50]

    :subject [in ["Maths" "Science"]]} {:score [> 50]} {:subject [in ["Maths" "Science"]]} {:prop "" :type "logical" :op "and" :val [{:prop "score" :op "is-greater-than" :type "number" :val "50"} {:prop "subject" :op "is-one-of" :type "enum" :val ["Maths" "Science"]}]}
  29. Define Datastore Query Logic rules - Mongo db.class.find( {score: {$gt:

    50 }, subject: {$in: [“Maths” , “Science”]}})
  30. Define Datastore Query Logic rules - Mongo {:score {:$gt 50}}

    db.class.find( {score: {$gt: 50 }, subject: {$in: [“Maths” , “Science”]}})
  31. Define Datastore Query Logic rules - Mongo {:score {:$gt 50}}

    {:subject {:$in [“Maths”,”Science”]}} db.class.find( {score: {$gt: 50 }, subject: {$in: [“Maths” , “Science”]}})
  32. Define Datastore Query Logic rules - Mongo {:score {:$gt 50}

    :subject {:$in [“Maths”,”Science”]}} {:score {:$gt 50}} {:subject {:$in [“Maths”,”Science”]}} db.class.find( {score: {$gt: 50 }, subject: {$in: [“Maths” , “Science”]}})
  33. Define Datastore Query Logic rules - Mongo {:score {:$gt 50}

    :subject {:$in [“Maths”,”Science”]}} {:score {:$gt 50}} {:subject {:$in [“Maths”,”Science”]}} {:prop "" :type "logical" :op "and" :val [{:prop "score" :op "is-greater-than" :type "number" :val "50"} {:prop "subject" :op "is-one-of" :type "enum" :val ["Maths" "Science"]}]}
  34. Define Compilation Rules is enum Compilation Logic Rules number is-one-of

    is-greater-than preprocessor preprocessor preprocessor str #(map str %) string->number
  35. Define Compilation Rules is enum Compilation Logic Rules number is-one-of

    is-greater-than constraint preprocessor constraint preprocessor constraint preprocessor str string? #(map str %) #(every? string? %) string->number number?
  36. Define Execution Rules is enum Execution Logic Rules logical is-one-of

    and processor processor processor number is-greater-than processor
  37. Define Execution Rules is enum Execution Logic Rules logical is-one-of

    and processor processor processor number is-greater-than processor
  38. ? Business Logic query SQL query logic rules SQL Query

    Will these DSLs be compatible with different types of interfaces?
  39. ? Business Logic query MongoDB query logic rules SQL Query

    MongoDB Query Will these DSLs be compatible with different types of interfaces?
  40. ? Business Logic query Elasticsearch query logic rules SQL Query

    MongoDB Query Elasticsearch Query Will these DSLs be compatible with different types of interfaces?
  41. Chanakya Business Logic query DB query logic rules SQL Query

    MongoDB Query Elasticsearch Query Will these DSLs be compatible with different types of interfaces?
  42. What is Chanakya? Chanakya was born out of a desire

    to define • A common way to define the criteria (a rule) • A common way to define the query object • A common way to know if some object matches a rule The end-user can define rules • Rules are abstract
  43. Chanakya can: • convert a rule into an optimised query

    which can be used to fetch documents that match the query Why Chanakya?
  44. Chanakya can: • convert a rule into an optimised query

    which can be used to fetch documents that match the query • take an object and a series of rules and returns true/false if object matches all rules Why Chanakya? Rule 1 Rule 2 …. Rule n Object Chanakya true/false
  45. {:prop “score” :op “is-greater-than” :type “number” :val “50”} {:marks 60

    :subject “Maths”} Chanakya true {:prop “subject” :op “is” :type “enum” :val “Maths”} Has the student scored > 50 marks and opted for Maths?
  46. {:prop “score” :op “is-greater-than” :type “number” :val “50”} {:marks 60

    :subject “Maths”} Chanakya true {:prop “subject” :op “is” :type “enum” :val “Maths”} Object Has the student scored > 50 marks and opted for Maths?
  47. {:prop “score” :op “is-greater-than” :type “number” :val “50”} {:marks 60

    :subject “Maths”} Chanakya true {:prop “subject” :op “is” :type “enum” :val “Maths”} Rule 1 Has the student scored > 50 marks and opted for Maths?
  48. {:prop “score” :op “is-greater-than” :type “number” :val “50”} {:marks 60

    :subject “Maths”} Chanakya true {:prop “subject” :op “is” :type “enum” :val “Maths”} Rule 2 Has the student scored > 50 marks and opted for Maths?
  49. {:prop “score” :op “is-greater-than” :type “number” :val “50”} {:marks 60

    :subject “Maths”} Chanakya true {:prop “subject” :op “is” :type “enum” :val “Maths”} Has the student scored > 50 marks and opted for Maths? Object Set of Rules
  50. {:prop “score” :op “is-greater-than” :type “number” :val “50”} {:marks 60

    :subject “Maths”} Chanakya true {:prop “subject” :op “is” :type “enum” :val “Maths”} Has the student scored > 50 marks and opted for Maths? Object Set of Rules
  51. Chanakya can: • convert a rule into an optimised query

    which can be used to fetch documents that match the query • take an object and a series of rules and return true if object matches all rules • filter a stream of events based on a set of rules Why Chanakya? Stream of events Chanakya Filtered events
  52. Get list of students with laptop {:laptop true :name “A”}

    {:laptop false :name “B”} {:laptop true :name “C”}
  53. Get list of students with laptop {:laptop true :name “A”}

    {:laptop false :name “B”} {:laptop true :name “C”} (true? laptop)
  54. Get list of students with laptop Chanakya {:laptop true :name

    “A”} {:laptop false :name “B”} {:laptop true :name “C”} (true? laptop)
  55. Get list of students with laptop Chanakya {:laptop true :name

    “A”} {:laptop false :name “B”} {:laptop true :name “C”} (true? laptop) {:laptop true :name “A”} {:laptop true :name “C”}
  56. Requirements satisfied? ✔ Datastore agnostic ✔ Reusable ✔ Easy to

    modify business logic ✔ Separation of concern