Fork me on GitHub
#clara
<
2017-07-25
>
misha08:07:36

@wparker thanks, will check it out. to have a real life value, FSMs need to support arbitrary code (i/o) execution as part (or as a consequence) of the state's transitions as well, so that comes down to a design decisions in both cases. In the exercise app I am (re)writing now - html5 video/audio tags are basically i/o, as far as the rest of the buttons in UI are concerned, despite those tags being their own FSMs.

wparker21:07:24

@ I was thinking of mainly what types the rules could insert; given an arbitrary set of rules one can’t infer which other rules a given rule could trigger. It is possible to add metadata of some kind specifying this or to parse it if you restrict the RHS code you write to something you can reliably parse. This seemed possibly relevant if you were trying to translate rules to a FSM, specifically w.r.t. the allowed state transitions.

iku00088816:07:35

Is it possible to use protocol methods in the left hand side of rules?

iku00088816:07:10

I have a vague feeling it would violate clara's design but cannot articulate why πŸ™‚

ryanbrush16:07:40

Yes, the LHS supports arbitrary Clojure expressions, including protocol methods.

iku00088816:07:21

Would you be kind to give an example?

iku00088816:07:06

Having trouble wrapping my head around how to refer to the instance showing up in the LHS

iku00088816:07:20

(defprotocol IFoo
  (test-foo [this]))

(defrule is-foo
  [IFoo (test-foo ?not sure how to express?)]
  =>...)

iku00088816:07:04

@ryanbrush Thanks for the help in advance πŸ™‚

ryanbrush16:07:36

@iku000888 Ahh, I thought you meant calling protocol-based methods on the LHS. The type the rules matches needs to be an actual type, either a Java type or a Clojure type via deftype.

ryanbrush16:07:13

(The one exception is customized "types" discussed here, but this probably isn't applicable to your discussion: http://www.clara-rules.org/docs/fact_type_customization/)

ryanbrush16:07:04

One correction: I meant clojure.core/derive rather than deftype above.

iku00088816:07:55

I think I understand

iku00088816:07:52

Just thought at first glance I could reify a bunch of stuff and passing it through rules would be a usecase

iku00088816:07:51

Thanks for clarifying! Much appreciate your time.

iku00088816:07:17

(defprotocol IFoo
  (test-foo [this]))

(deftype Foo []
  IFoo
  (test-foo [this] true))

(defrule is-foo
  [Foo (test-foo ?not sure how to express?)]
  => (println "is a foo"))

iku00088816:07:52

Wait, so if it is an actual type I can invoke protocol methods?

wparker21:07:56

Heads up about something potentially tricky: If you’re on the JVM when you create a protocol, behind the scenes Clojure creates a corresponding Java interface of the same name. An interface is a type that you can write rules against, but it might not do what you expect. Only types that Clojure knows satisfy the protocol at the time of their creation will implement the interface. For example:

wparker21:07:20

` user=> (defprotocol MyProtocol (proto-method [_]))
MyProtocol
user=> (defrecord MyRecord1 [] MyProtocol (proto-method [_] "Hello World"))
user.MyRecord1
user=> (defrecord MyRecord2 [])
user.MyRecord2
user=> (extend-type MyRecord2 MyProtocol (proto-method [_] "Hi World"))
nil
user=> (proto-method (->MyRecord1))
"Hello World"
user=> (proto-method (->MyRecord2))
"Hi World"
user=> (instance? user.MyProtocol (->MyRecord1))
true
user=> (instance? user.MyProtocol (->MyRecord2))
false 
`

wparker21:07:58

In another ns you could potentially import the interface rather than the protocol depending on how you set up your ns header i.e. import vs require on it

wparker21:07:41

@iku000888

iku00088802:07:55

@wparker thanks! Did not know about that.

ryanbrush16:07:43

Yes, if Foo is a type, you should be able to invoke the test-foo protocol method on it like you show.

iku00088816:07:58

(defrule is-foo [Foo (test-foo this)] => (println "is a foo"))

iku00088816:07:23

Thanks again and sorry for the noob noise πŸ™‡

ryanbrush16:07:42

Sure thing! I'm glad it works for you.

ryanbrush16:07:21

Just be aware you might have to reload your rules if you redefine the types in a REPL, since the type will change when you do that. πŸ˜‰

iku00088817:07:11

@ryanbrush blogged about my first glance of clara-rules (for people in Japan) and thanked you there. Hope that is okay.

ryanbrush18:07:26

@iku000888 Nice! Thanks for blogging about our little project. πŸ˜‰