Fork me on GitHub
#clara
<
2018-01-14
>
dave.dixon00:01:06

I have it working with a callback and unconditional insert. I don't think there's a way to do a logical insert in the context of the callback. The session has already moved forward, could have been modified further by the time the callback is called, etc.

alex-dixon14:01:04

I haven’t played around with it much. Typically I do the same as you I think….wait to handle it on the next fire-rules iteration, but that can be problematic in certain cases. So I’m kind of curious about being able to park/block within the RHS but I haven’t thought about it much. It might be convenient for Javascript but in a parallel or multiple thread environment…I dunno. I forget the case I ran into where I really did want to stop the world / rule activations until I could get a result, but I feel like that’s more an error in my thinking than a feature I need to figure out how to support. Don’t know though

dave.dixon00:01:26

Part of what I was finding was that beyond the simple request/response case there was a proliferation of auxiliary fact types to deal with requests from the UI. For example, if the user toggled the "Favorite" button on an article, I would insert a FavoritedArticle fact, which in turn triggered Request to be inserted, along with a rule to handle the response to that Request. I also had to add clean up rules to get rid of these FavoritedArticle facts when the page changed, as well as properly manage upsertion vs. insertion, etc. It was all starting to feel a bit fiddly. But I think you could actually do this with the same Request/`Response` pattern, just replacing making an HTTP request with rendering the UI which will generate the Response.

alex-dixon14:01:50

So for precept I implemented something like the following. Below is somewhat similar to Redux in Javascript which also reifies this and request/response/fail for api calls for front-end state management.

(defrecord Action [type payload])

(defrule actions-only-survive-one-call-to-fire-rules
{:salience -100}
 [?fact <- Action]
=>
(retract! Action)

(defrule on-article-favorited
{:salience 100}
[Action (= ?type :article-favorited) (= ?payload payload)]
=>
;; Still have the async dance but Action facts are automatically cleaned up at the end
(do)

dave.dixon15:01:35

Right. I'm trying to do this without salience, just to see where it takes me in terms of design, possible changes to clara, etc. One thing that has fallen out is that I think the fact-based immutable history approach of precept is probably the way to go, as opposed to using whole entities as facts and trying to update "in place".

alex-dixon17:01:25

Would you consider agenda groups? I’ve very little experience with systems that use them but we try to simulate them partially in precept by using Clara’s :activation-groups and :activation-group-sort-fn http://www.clara-rules.org/docs/conflictsalience/

alex-dixon17:01:51

So we have :group :cleanup I think, which is given least precedence in the sort fn

alex-dixon17:01:37

I still run into wanting update in place but I’m on the fence about it because immutability is easier for me to understand. Case that comes up a lot is a default value:

(rule when-no-user-selected-article
 [:not SelectedArticleId]
  [?most-recent <- (acc/min :time :returns-fact true) :from [Article]]
=>
(insert! (->SelectedArticleId (:id ?most-recent)))

alex-dixon17:01:44

So that loops

alex-dixon17:01:39

There’s a :no-loop prop that’s relatively undocumented. Haven’t played with it but seems like it would solve this case. A (modify!) wouldn’t I guess because the SelectedArticleId doesn’t exist. If it did as something like (SelectedArticleId :unset) then you could match on (= id :unset) and (modify! ?selected-article :id (:id ?most-recent))

dave.dixon20:01:26

I need to think about it. But I really am trying, as an exercise, to see what happens if you constrain yourself solely to the logical consistency implied by clara. I think I'm converging on something, but I've thought that before only to trip over an ugly fact that ruined my beautiful theory.

dave.dixon20:01:08

BTW, the "real world" example app (conduit) is proving to be a great way to exercise this stuff. You might find it useful for precept development. https://github.com/jacekschae/conduit

alex-dixon20:01:05

Yeah was thinking same! Would be nice to see it and Clara on their official list as well