Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retraction broken when using custom :fact-type-fn #116

Closed
jpthomson opened this issue Aug 7, 2015 · 4 comments
Closed

Retraction broken when using custom :fact-type-fn #116

jpthomson opened this issue Aug 7, 2015 · 4 comments
Milestone

Comments

@jpthomson
Copy link

Not sure if this is a known limitation? This test:

(clojure.test/is
 (= (do

      ;; Records

      (defrecord A [])
      (defrecord B [])

      (defrule a->b
        [A] => (insert! (->B)))

      (defquery b? []
        [?b <- B])

      (-> (mk-session [a->b b?])
          (insert (->A))
          (fire-rules)
          (retract (->A))
          (query b?)))

    (do

      ;; Maps

      (defn ->a [] {:type :a})
      (defn ->b [] {:type :b})

      (defrule a->b
        [:a] => (insert! (->b)))

      (defquery b? []
        [?b <- :b])

      (-> (mk-session [a->b b?] :fact-type-fn :type)
          (insert (->a))
          (fire-rules)
          (retract (->a))
          (query b?)))))

fails with

(not (= () ({:?b {:type :b}})))

Haven't had a chance to dig into why yet, just posting incase I'm missing something obvious or well-known!

@rbrush
Copy link
Contributor

rbrush commented Aug 7, 2015

This looks like a bug. Thanks for reporting it! I poked around the code a bit and have a pretty good idea of what's going on and will get a fix posted soon.

@rbrush
Copy link
Contributor

rbrush commented Aug 7, 2015

I believe this is fixed in the above commit. The problem was that the truth maintenance flow didn't have the custom type function provided by the user, and was always using Clojure's built-in type. This is an edge case we hadn't run into since we don't make heavy use of custom type functions, but it's definitely an important one and I'm glad we found and fixed it.

The key change is around line 281 in engine.clj. I had to add the needed function to the working memory data structure to make it visible at that point in the flow. I also added some unit tests to be sure we don't break this in the future.

If this works for your use case I can get it merged into master and push out a fix release without too much trouble.

Thanks for reporting this and for posting a simple unit test! It made finding it much easier.

-Ryan

@jpthomson
Copy link
Author

Thanks so much Ryan!

I noticed there wasn't a unit test for custom type fns and retract, so can understand how this might have slipped through the net! All makes sense and the fix definitely works for me.

I'm hoping to use Clara as the 'view model' in a reagent / datomic app - so will likely touch on a few other less well trodden aspects. Happy to help solve problems as they arise once I'm a little more familiar with the code base!

So far it's been smooth sailing, so thank you for all the work you've put in.

-Jonathan

@rbrush
Copy link
Contributor

rbrush commented Aug 10, 2015

Hey Jonathan,

Sounds great! I went ahead and merged this to master, so I think we can consider this closed. Feel free to reopen or open another issue if you disagree.

Cool use case, by the way. If you do run into other edge cases in your app please don't hesitate to report them here. My use of Clara is heavily oriented to Clojure rather than ClojureScript, but all of this only makes the system stronger.

Thanks!

-Ryan

@rbrush rbrush closed this as completed Aug 10, 2015
@rbrush rbrush added this to the 0.8.9 milestone Sep 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants