Coder Social home page Coder Social logo

Comments (7)

rbrush avatar rbrush commented on July 23, 2024

There shouldn't be an issue using facts between different namespaces. The
most common cause of surprising behavior is if the record types used as
facts are reloaded dynamically. Clara will match if the types are equal,
but if there is a rule or session based on a record type that has since
been reloaded (and an instance of the newly reloaded type is inserted into
memory), the rule won't match. Clojure protocols have a similar issue...if
I reload a protocol definition, extensions of that protocol definition
won't match unless they are reloaded as well.

So you might just try restarting the JVM to see if it works as expected.
(In this particular case, I copied your code as is and it yielded the
expected result, so I'm guessing you're running into that issue.)

I'd like to offer a way around this pitfall but there isn't a great
solution. Reloading the rule definition and clearing the session cache
(clara.rules.compiler/clear-session-cache!) would usually do the trick, but
this is ugly. I often try to keep my record and protocol definitions in
their own namespaces that aren't often reloaded to avoid running into this
problem.

On Wed, Feb 18, 2015 at 9:30 AM, deaddowney [email protected]
wrote:

I'm trying to test the examples in my REPL. I'm using the rules as defined
in the examples but they don't seem to work as I get no output. I'm
wondering if this is some sort of namespace issue.

Here is the code I'm running:

(ns clara.examples
(require [clara.examples.shopping :as shopping]
[clara.examples.validation :as validation]
[clara.examples.sensors :as sensors]
[clara.examples.durability :as durability]
[clara.examples.java.shopping :as jshopping]
[clara.rules :as r]))

(defn -main
(shopping/run-examples)
(println)
(println "JavaBean Shopping examples from Clojure:")
(jshopping/run-examples)
(println)
(println "JavaBean Shopping examples from Java:")
(clara.examples.java.ExampleMain/main (into-array String []))
(println)
(println "Validation examples:")
(validation/run-examples)
(println)
(println "Sensor examples:")
(sensors/run-examples)
(println "Durability examples:")
(durability/run-examples))

(defn run-examples
"Function to run the above example."

;; prints "10 % :vip discount"
(-> (r/mk-session 'clara.examples.shopping) ; Load the rules.
(r/insert (shopping/->Customer :vip)
(shopping/->Order 2013 :march 20)
(shopping/->Purchase 20 :gizmo)
(shopping/->Purchase 120 :widget)) ; Insert some facts.
(r/fire-rules)
(shopping/print-discounts!))

(println "Summer special and widget promotion example:")
;; prints: "20 % :summer-special discount"
;; "Free :lunch for promotion :free-lunch-for-gizmo"
;; "Free :widget for promotion :free-widget-month"
(-> (r/mk-session 'clara.examples.shopping) ; Load the rules.
(r/insert (shopping/->Customer :vip)
(shopping/->Order 2013 :august 20)
(shopping/->Purchase 20 :gizmo)
(shopping/->Purchase 120 :widget)
(shopping/->Purchase 90 :widget)) ; Insert some facts.
(r/fire-rules)
(shopping/print-discounts!)
(shopping/print-promotions!))

nil)

If I call (-main), I get the correct output. If I call (run-examples), it
get:
VIP shopping example:
Summer special and widget promotion example:

Is there some issue with inserting facts from a different namespace?


Reply to this email directly or view it on GitHub
#3.

from clara-examples.

deaddowney avatar deaddowney commented on July 23, 2024

Hi Ryan,

I've created a git repo that reproduces the issue.

https://github.com/deaddowney/clara-test

I'm using maven to build my project and when I first compile from clean, I get a compilation error. When I compile again, it works and my rules fire. If I then run my main class, the rules do not fire.

from clara-examples.

rbrush avatar rbrush commented on July 23, 2024

Hmm, this smells like an AOT compilation issue, since it seems to work
interactively. There are some pretty alarming issues with AOT documented at
the link below, and Clara could easily be tripping over some type and
recored related items. Therefore I've avoided using AOT for all my Clojure
development, including Clara, so running into something isn't shocking.
(Honestly I'm hoping some of these get fixed in time for 1.7 to make AOT
more usable.)

I did notice the initial compilation failure is probably occurring because
the last line of the shopping.clj file is trying to run the code at compile
time, which doesn't seem to play nice with AOT. But even past that things
don't seem to work right, probably for type comparison reasons.

Any chance you can avoid using AOT? I've done Maven builds for projects
where I created a minimal "main" class in Java, which just invoked
non-AOT'd Clojure code. There's a bit more cost for the initial compilation
at runtime but you avoid a lot of other issues.

I would like to get Clara to play nice with AOT, but was hoping to see some
more of these issues land before spending too much time debugging into it.

http://dev.clojure.org/display/design/AOT+Problem+Overview

On Wed, Feb 18, 2015 at 3:02 PM, deaddowney [email protected]
wrote:

Hi Ryan,

I've created a git repo that reproduces the issue.

https://github.com/deaddowney/clara-test

I'm using maven to build my project and when I first compile from clean, I
get a compilation error. When I compile again, it works and my rules fire.
If I then run my main class, the rules do not fire.


Reply to this email directly or view it on GitHub
#3 (comment).

from clara-examples.

deaddowney avatar deaddowney commented on July 23, 2024

Ryan,

I would avoid running AOT, but I am not even able to run this interactively from my REPL (Cursive). It is my experience that records introduce all kinds of bugs compared to maps. I see that clara does support the use of maps through the :fact-type-fn. I’ll see if I can replace my records with maps and get things to work.

Do you think Clojure-dev would be interested in looking at my test project as a test case for some of the AOT bugs?
Thanks,
Adam

On Feb 18, 2015, at 4:34 PM, Ryan Brush [email protected] wrote:

Hmm, this smells like an AOT compilation issue, since it seems to work
interactively. There are some pretty alarming issues with AOT documented at
the link below, and Clara could easily be tripping over some type and
recored related items. Therefore I've avoided using AOT for all my Clojure
development, including Clara, so running into something isn't shocking.
(Honestly I'm hoping some of these get fixed in time for 1.7 to make AOT
more usable.)

I did notice the initial compilation failure is probably occurring because
the last line of the shopping.clj file is trying to run the code at compile
time, which doesn't seem to play nice with AOT. But even past that things
don't seem to work right, probably for type comparison reasons.

Any chance you can avoid using AOT? I've done Maven builds for projects
where I created a minimal "main" class in Java, which just invoked
non-AOT'd Clojure code. There's a bit more cost for the initial compilation
at runtime but you avoid a lot of other issues.

I would like to get Clara to play nice with AOT, but was hoping to see some
more of these issues land before spending too much time debugging into it.

http://dev.clojure.org/display/design/AOT+Problem+Overview

On Wed, Feb 18, 2015 at 3:02 PM, deaddowney [email protected]
wrote:

Hi Ryan,

I've created a git repo that reproduces the issue.

https://github.com/deaddowney/clara-test

I'm using maven to build my project and when I first compile from clean, I
get a compilation error. When I compile again, it works and my rules fire.
If I then run my main class, the rules do not fire.


Reply to this email directly or view it on GitHub
#3 (comment).


Reply to this email directly or view it on GitHub #3 (comment).

from clara-examples.

rbrush avatar rbrush commented on July 23, 2024

Hey Adam,

I was able to get that clara-test project running in Cursive by commenting out the compilation execution in the POM, running mvn clean, and making sure Cursive had src/main/clojure set up as a root source folder (for some reason it didn't when I imported the project). Once there, I ran the local REPL in Cursive and it worked well.

I think Cursive was running into AOT problems here, picking up class files that Maven might have left in the target folder. I'm mostly an emacs user, but jump into Cursive on occasion (and am really starting to like it a lot!)

Your idea of using :fact-type-fn against simple maps would probably work around these issues outright, so that's a good call if that works for you. I tend to use records reasons specific to the project in which I use Clara the most, but simple maps might work better for others.

Anyway, here's the part of the POM I commented out:

<!--      <execution>
        <id>compile</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>  -->

I also played with editing rules in the code and was able to reload them. Here's my REPL session, with the exclamation marks later on from an edit on the fly:

Starting nREPL server...
Connecting to local nREPL server...
Clojure 1.7.0-alpha4
nREPL server started on port 62900 on host 127.0.0.1
(require 'clara.examples.shopping)
=> nil
(clara.examples.shopping/run-examples)
VIP shopping example:
10 % :vip discount
Summer special and widget promotion example:
20 % :summer-special discount
Free :lunch for promotion :free-lunch-with-gizmo
Free :widget for promotion :free-widget-month
=> nil
(clara.examples.shopping/run-examples)
VIP shopping example:
10 % :vip discount
Summer special and widget promotion example:
20 % :summer-special discount
Free :lunch for promotion :free-lunch-with-gizmo
Free :widget for promotion :free-widget-month
=> nil
(require 'clara.examples.shopping :reload)
=> nil
(clara.rules.compiler/clear-session-cache!)
=> {}
(clara.examples.shopping/run-examples)
VIP shopping example:
10 % :vip discount!!!
Summer special and widget promotion example:
20 % :summer-special discount!!!
Free :lunch for promotion :free-lunch-with-gizmo
Free :widget for promotion :free-widget-month
=> nil

from clara-examples.

rbrush avatar rbrush commented on July 23, 2024

I'm going to dig a bit more into the AOT woes, maybe there is something we can do to make Clara work better in that setting.

As a side note, if you have any questions or issues with Clara, the Google Group is probably a good forum: https://groups.google.com/forum/?hl=en#!forum/clara-rules

from clara-examples.

deaddowney avatar deaddowney commented on July 23, 2024

I updated the version of Clojure to 1.7.0-alpha5 from alpha1, leaving the rest as is and now the exec:java task works. Thanks for pointing me to the AOT issues. It looks like they are actively dealing with them in 1.7, which is good news.

from clara-examples.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.