Fork me on GitHub

What's the best way to ensure that a fact doesn't get inserted twice. Is there a way to ensure that duplicate facts don't get inserted into the same session?


@mjmeintjes I talked about this in a few posts above if you can see them?


I discussed “cardinality” of equal facts in clara and ways to work with it


and tradeoffs etc


Thanks for the information. I was thinking more about how to efficiently update the session - for example, I have a list of rows from the database. Before firing the rules I need to make sure that all the db rows are included in the session. However, I do not want all the rules to be inserted each time (which causes lots of duplicates), and I also don't want the session to be recreated each time. What I've done now is create a query to retrieve the already inserted rules from the session, and then I'm comparing that against the db rows, and then I only insert the new ones. I was just wondering whether that is the best solution.


@mjmeintjes I think that is a fine way to do it. I think there may be a potential enhancement to clara to do something like an “update” of facts and/or a “mode” that had the working memory act in a set-based way there weren’t duplicates. For now though, the external query idea is good if it can work for you


Yeah, being able to efficiently update the session would be great, as each time I've used Clara that's something that I've run up against. The only problem with the query approach is that queries are only updated once fire-rules has run, which means you can still get duplicates if you aren't careful to run fire-rules before inserting any new data.


@mjmeintjes Yeah, you should never query prior to fire-rules. I’d call that an “undefined” state


Clara does some “lazy” sort of batching that isn’t forced until fire-rules. It can do some good optimizations with this and could even get more “lazy” in the future I’d think.


I think it can be difficult to do “logical updates” to facts with what Clara currently offers


it relies basically entirely on the caller to query facts and figure out what inserts/retracts to make externally. That is if the rules are going to be written in a way that can smoothly work with the truth maintenance system and logical inserts (ie not insert-unconditional! and right-hand-side based retract!).


These sort of discussions have came up quite a bit here lately.


I wrote this a while back Not sure if it is really useful to you. You seem to already have a setup you’ve been using to do your de-duping anyways


Thanks for that post, I'll have a read. Love reading information about Clara, as it is such a great programming model. Always trying to find places to use it.


Well that’s good.


Yeah, I’d like to try out some ideas to deal with these sorts of things. I think some sort of rule-triggered “update” would be interesting.


As well as externally specified updates


The challenge would be to do so in a way that interacted in a well-defined way with the truth maintenance system and logically inserted/retracted facts.


Thanks, useful post, helped clarify/validate the approach I'm taking.