clara

http://www.clara-rules.org/
cfleming 2018-01-04T02:26:05.000260Z

I have some questions about the fact-expr grammar over here: https://github.com/cursive-ide/clara-specs/issues/4

cfleming 2018-01-04T02:28:09.000130Z

Actually, looking at the schema I see that FactCondition :type is s/Any, bummer.

2018-01-04T12:54:47.000047Z

@cfleming Yeah, I can see where that is difficult and yes the “fact type” being anything makes things complicated

2018-01-04T12:55:46.000387Z

I’m pretty sure you can break Clara parsing if you try to use a fact type that seems too much like something else.

2018-01-04T12:56:14.000476Z

One thing that is interesting and probably should be part of the spec (I don’t believe it is now), is that variable bindings should always start with “?”

2018-01-04T12:56:34.000399Z

So in the case of the fact binding, it is the ::variable-binding from that PR

2018-01-04T12:57:14.000229Z

And perhaps we should just list a few things that a fact type should not be, like it shouldn’t be a symbol that starts with a “?”

2018-01-04T12:57:44.000210Z

I think it may not be ok for it to be a vector either. I seem to recall a bad parse example from long back where a fact type as a vector would mess up things.

tcarls 2018-01-04T13:25:54.000015Z

Are there suggested patterns for rules that can require potentially-expensive lookups? If I have a rule that depends on content that could be retrieved from an API call / database hit / etc., is there a way to trigger that retrieval (and insert any facts it results in) only when the rule's other conditions are satisfied?

2018-01-04T13:32:02.000324Z

@tcarls that’s funny you bring that up since this topic came up fairly recently here

2018-01-04T13:32:28.000133Z

In short: I think in the future Clara could do better to avoid doing work when required previous conditions aren’t yet satisfied.

2018-01-04T13:32:33.000254Z

However, there is a workaround pattern:

2018-01-04T13:34:40.000374Z

;; Say you have this:
(defrule original
  [A (= ?id id)]
  [B (= ?id id) (do-expensive-thing x)]
  =>
  (insert (->C)))


It can be transformed to this:
(defrule part-1
  [A (= ?id id)]
  [?b <- B (= ?id id)]
  =>
  (insert (->MatchedB ?b)))

(defrule part-2
  [MatchedB (do-expensive-thing x)]
  =>
  (insert (->C)))

2018-01-04T13:38:24.000208Z

If you are in a situation where there may be some thrash of insert/retracts of A and B (from example) due to the truth maintenance of the network, then you can also consider adding either :salience or setup “activation groups” for rules like part-2. However, I’d only do that if there is actually a situation where it could be an issue.

tcarls 2018-01-04T13:50:08.000470Z

Thank you -- that's useful advice.

👍 1
2018-01-04T13:55:11.000010Z

Some details on salience and activations groups is written here: http://www.clara-rules.org/docs/conflictsalience/ If you end up needing it. However, like I said, avoid it if unnecessary. It is additional complexity to deal with.

sparkofreason 2018-01-04T22:22:29.000192Z

Is there a way to establish a conditional relationship between facts outside of a rule RHS? The case I'm thinking of is something like an article app, where articles have comments. Selecting an article makes it "active", and also triggers a request to fetch comments from the server. When the async request completes, comments will be inserted. It would be slick if retracting the active article fact also automagically retracted the inserted comments, but since the response handler for the comments request executes outside of a rule context, I don't see an obvious way to enforce that logical connection.

zylox 2018-01-04T22:48:23.000441Z

You could write something fairly easily to "deactivate" them (by joining the comment fact to the article and inserting an ActiveComment or something) but that wouldnt retract the base comment fact which could lead to build up over time. I don't know of any way to do what you are talking about without an external handler atm

zylox 2018-01-04T22:50:38.000106Z

they would have to come in on the same data object into the session which doesnt match the timeline you describe