Fork me on GitHub
#clara
<
2016-07-18
>
wparker14:07:20

Regarding using the fact-type-fn, the automatic field lookup is determined by the type; see https://github.com/rbrush/clara-rules/blob/0.11.1/src/main/clojure/clara/rules/compiler.clj#L152 So right now the auto field access will just be by the record or bean properties. I could conceivably see this is an extension point if there were use-cases for it. However, I don’t see any reason why you can’t use hash bindings for other types. So, for example, something like

wparker14:07:42

[FactClass (= ?fieldA (.someNonBeanAccessorMethod this))]

mattdarling15:07:09

I'm actually interested in achieving something similar, is there any kind of field access aside from "auto field access"?

wparker17:07:13

@mattdarling: The fact itself is bound to “this” in the constraints on the RHS (right-hand side) so you can do arbitrary things with it there. So

wparker17:07:19

(defrule some-rule

wparker17:07:39

[FactClass (= ?fieldA (some-arbitrary-operation this))]

wparker17:07:00

=> ;; whatever RHS

wparker17:07:11

Note: I meant constraints on the LHS above, not RHS 😛

wparker17:07:16

so left-hand-side

wparker17:07:53

So the lack of automatic field access shouldn’t prevent you from doing anything functionality, it is purely a bit of syntax sugar

wparker17:07:58

*functionally

wparker17:07:54

That said, no, I don’t believe anything other than records or beans is supported for that syntax sugar now. I don’t think it would be terribly difficult to change the compiler to make that an extension point, perhaps making clara.rules.compiler/get-fields a multifunction (http://clojuredocs.org/clojure.core/defmulti) if someone was interested in doing so and Ryan agreed (I haven’t thought deeply on the issue though, it is possible there would be a complication I’m not seeing immediately).

mattdarling18:07:41

Got it working as I wanted with "this", thank you! I thought that was just example syntax, not an existing feature

wparker18:07:26

Ah no, the “this” binding and field bindings for Clojure records and JavaBeans exist already and have for some time; they aren’t an experimental feature or anything like that

mattdarling18:07:09

I had used the bindings for Clojure records initially, but wanted to introduce a bit of polymorphism, so I switched to maps with a key to indicate their type + made that the fact-type-fn