r/Clojure • u/SimonGray • Feb 10 '21
O'Doyle Rules - a Clojure rules engine for the best of us
https://youtu.be/XONRaJJAhpA3
u/olymk2 Feb 11 '21
This sounds a bit like fulcro, I was watching this video https://www.youtube.com/watch?v=r1bMQxTr2QA and seems he is showing a very similar pattern.
3
u/scarredwaits Feb 14 '21
This looks very appealling, but I have a question about it's applicability to UIs. If you are in a context where you have to fully handle the lifecycle of UI components (create, update, remove), as opposed to React or the direct drawing mode of a games engine, how do you detect that a fact was just inserted instead of an existing fact being updated? Also is there a way to detect retractions?
3
u/oakes Feb 14 '21
I think it'd be best to represent the lifecycle event as a separate fact. So you could insert
[component-id ::event :create]and have a rule that receives that in its :what block. There probably is a way to distinguish between new vs updated facts but representing this with its own fact seems more explicit and flexible (you can represent more than two states).You can use :then-finally to react to retractions but you won't have access to the retracted fact. It's mainly meant for running queries to create derived facts, so not really the use case you seem to have.
2
u/scarredwaits Feb 14 '21
Thanks! I need some time to wrap my head around this, it could work. I'm thinking of using o'doyle rules with a Swing app I'm developing, and there is no React in Swing and I'm not in the mood to invent it. So all my
:thenblocks would have to be effectful creation/modification/removal of Swing components depending on what's happening with my facts. Does that sound like it would work?3
u/oakes Feb 15 '21
That sounds neat. Yeah i think it should be possible to make rules that do all the swing mutations. But if you want it to be really general and work with all the different kinds of components in the framework, you will probably end up inventing react in swing :P Something that is really targeted for just your needs seems doable though.
Just keep in mind that those mutations have to happen on swing's render thread. So you either need to wrap them in
SwingUtilities/invokeLateror make sure you only modify the o'doyle session on that thread.
2
u/Bob_la_tige Feb 14 '21
Let's say I have a rule that trigger when :what [player ::attack opponent].
To compute the result I need more data like the health and damage, but I don't think I want them to be described in my :what section because then the rule will trigger when the damage or health changes, so I need to fetch the data with a query on the *session inside of the :then statement ?
4
u/oakes Feb 14 '21
You can still put them in the :what block; just put
{:then false}in the tuples if you don't want their updates to trigger the :then block. So it would look like this::what [player-id ::attack opponent-id] [player-id ::damage damage {:then false}] [opponent-id ::health health {:then false}]2
u/Bob_la_tige Feb 14 '21
I tried that but when the fact ::attack arrive before ::damage is available then it will not trigger, but then when later on ::damage is ready the rule trigger even with {:then false}
3
u/oakes Feb 15 '21
Yeah that's by design. Rules will always wait until they have a complete match, and once they do, they will fire as long as one of the facts did not have
{:then false}, even if it wasn't the last fact received.2
u/fjolne Feb 14 '21
You still add them to :what block with an option map as the 4th argument of the EAV tuple: [player ::health health {:then false}]
1
Feb 27 '21
I am starting to see state machine libs pop up in javascript land like xstate, and robot, can someone maybe ELI5 where the differences come from state machien and rules engine like odoyle? thank you!
0
Feb 10 '21
[deleted]
8
u/joinr Feb 11 '21
How would you get solid test coverage of an application that’s written entirely in a rules engine?
Test the rules in isolation to ensure they produce the expected changes to the facts. The nice thing about this is the relative compartmentation and functional approach. You have a persistent fact store that can be fed to the rules. Test it at the repl and save the result. You now have a reproducible test going forward. Alternately, establish expected outcomes and query the resulting fact store after the rules fire. If the outcomes don't match the query results, test fails.
Then do the equivalent of integration tests with all the rules working in concert. I'm curious if you could derive data dependencies automatically and figure out fact paths to trigger exercise all the rules. Seems plausible.
0
Feb 14 '21
[deleted]
4
u/oakes Feb 14 '21
Yep they're all under the umbrella term of logic programming but prolog and core.logic are backward-chaining, whereas clara and o'doyle are forward-chaining.
9
u/SimonGray Feb 10 '21 edited Feb 10 '21
I enjoyed this rundown as I didn't really know what a rules engine is. I get this impression that quite a few Clojure libraries are converging on this triple unification idea.
Since this is also a CLJS lib I wonder whether anyone tried using it for making a SPA? Seems like a simple way to integrate data from Datomic or another Clojure Datalog db into the logic of a web app. I wonder what the performance is like, though. The fans were spinning a lot in the video, but that might have been the screen capture process itself doing that.