Fork me on GitHub
#clara
<
2022-09-28
>
Joel18:09:50

How can slow-running queries be inserted on the RHS? I’d like to kick off a thread, and insert the data when it is available, while allowing rule evaluation to continue, but waiting until thread returns before completing evaluation.

ethanc20:09:04

I’m not sure i quite follow the question here. Are you saying that you have a state-based transaction in the RHS of a rule and you wish the session to continue executing while that RHS “retrieves” data?

Joel01:09:40

multiple RHS that have longer-running fetches, I’d like the rules to trigger the ones it can and somehow wait for them/insert before finishing the session/firing.

Joel01:09:32

maybe a low salience rule can wait/insert retrieved data?

ethanc01:09:12

Generally speaking, "Fetching" implies that data is being retrieved from external locations. External data generally implies stateful execution. Clara has trouble dealing with stateful RHS/LHS executions. As some/most of the core Truth Maintenance systems expect the LHS/RHS to be stateless. The bit that jumps out immediately would be logical retractions, expecting RHS to insert the same facts given the same inputs. If your RHSs are not stateless you run the risks of breaking the truth of the rulebase.

Joel01:09:24

You are saying that for instance if the DB is queried twice then it might read two different values?

Joel01:09:44

In that case I can just cache data for the session.

ethanc01:09:51

yes, if you RHS retrieved data for one execution of the RHS, given the same input clara expects that it will always return the same value.

Joel01:09:50

I suppose either a low-salience rule that waits on outstanding threads would work? Or, maybe just multiple firings on the same session until there are no more active threads?

ethanc01:09:47

As a side note, clara makes no guarantees on how many times the RHS/LHS might be executed, only that in the end the session should be truthful. Your RHS might beat up your database.

Joel01:09:04

so then the cache is more essential.

Joel01:09:47

or couldn’t the LHS, just have in it “have i not fetched x”?

ethanc01:09:10

Going multi threaded is a little troublesome as introducing parallelism in the stored memory of clara might have undefined consequences. In the scenario where two interdependent facts are traversing the same join node... you'd have to have some global lock on node to avoid the "Facts passing in the night" without acknowledging each others presence.

ethanc01:09:47

Most patterns that i have encountered generally leaves the retrieval of data outside the scope of a session.

Joel01:09:21

so then if i kick off threads in RHS, wait till after firing, check the threads/insert, then fire again/repeat would still fit that model of “retrieval of data” outside the scope of the session it sounds like. I could even just insert a fact that data needs to be retrieved and do all threading i suppose outside the session.

ethanc01:09:49

more in the sense that all external data is added to the session from calls like insert on the session directly

ethanc02:09:06

i suppose that in the fire-rules loop... you could have a "future-like" construct that would re-enqueue the pending updates in the session.... but that would likely only be at one activation grouping... likely spinning through all updates in a salience group then waiting on the futures within that salience group....

Joel02:09:00

Yeah, I mean I was thinking it would be nice to get away from “spoon-feeding” facts to the rules and let the rules determine what facts are needed.

Joel02:09:42

Jess has need-* rules that can be written that determine when facts are needed. I don’t know that it has thread support, but generally sounds useful.

ethanc02:09:55

the need-* supports backward chaining of rules, where one rule effectively goes in search of what it needs. Clara was written based on the Rete algorithm, focused on forward chaining. The spoon feeding in this case is a side effect of that forward chaining.