I am trying to migrate an existing ILOG JRules based application to Drools based project. In ILOG we have local variables (and variable set) which can be used to calculate and store intermediate values within rules (formulas).
Is there an equivalent of local variables in Drools? I know we have Globals in Drools but the documentation suggest to use Globals only as read only constants and to pass data between Java calling class to rules:
It is strongly discouraged to set or change a global value from inside
your rules. We recommend to you always set the value from your
application using the working memory interface.
One approach would be to write a custom java class Variable and declare all variables within it and use it in drools rules. But that would need a java code change everytime we want to add a new variable.
Is there any feasible method of defining and using variables in Drools which can be defined and altered within rules?
I don't see anything wrong in using globals to keep some kind of calculation or state between your rules. I think what Drools' documentation is trying to say (in a very complicated and unclear way) is that you should never use globals in the RHS of the rules because Drools doesn't know when the value of a global changes. If you do use globals in your constraints, then make sure you never change their values. Having saud that, I think that using globals in the RHS of your rules is harmless.
If you are not comfortable with this idea, then you could also insert your "Calculation" object as a fact in your session. Rules can match against this fact, bind a variable to it and then interact with it in the RHS.
Hope it helps,
Related
I am working with a legacy scala codebase, and as is always the case modifying the code is quite difficult without touching different parts.
One of my new requirement in to make several decisions based on some input parameters. Problem is that these decisions are to be made at various points along the execution. So either I encapsulate all those parameters in a case class instance and pass it along. But it means I would have to modify multiple methods signatures, and I want to avoid this approach as much as possible.
Another approach can be to create a global object containing all those input parameters and accessible from different points in the execution. Is it a good approach in Scala?
No, using global mutable variables to pass “hidden” parameters is not a good idea, not in Scala and not in any other programming language. It makes the code hard to understand and modify, because a function's behaviour will now depend on which functions were invoked earlier. And it's extremely fragile, because you might forget setting one of those global parameters before invoking the function, which means that it will use whatever value was stored there before. This is the kind of thing that can appear to work for years, and then break when you modify a completely unrelated part of the program.
I can't stress this enough: do not use global mutable variables, period. The solution is to man up and change those method signatures. Depending on the details, dependency injection may or may not help in your particular case.
I am looking to write a DSL in Rosette with the goal of synthesizing a function. The DSL is based off a very small subset of Haskell, and is strongly typed. I therefore want to make sure that anything that Rosette generates is well-typed.
One way I could consider going about this is writing a "is-well-typed-code" function (that's something I've already done in Julia, which I'm porting from), and then assert that it must hold true of the synthesized function. However, I do not see with define-synthax and synthesize how to view the code both as a syntactic object (i.e., an S-expr that can have any function applied to it) and as a semantic function when synthesizing.
I could also imagine using a cond inside the define-synthax module over all possible types of the arguments (this is a finite set for this small DSL), and defining the possible forms separately for each type. However, including conds inside define-synthax does not seem to be possible.
Any ideas?
Thanks!
I am creating my own index method in PostgreSQL based on GiST as extension. I want one of my functions (check out Examples section) in my operator to behave differently based on a value defined by the user- I want to avoid the user having to drop and create operators again.
So, I was looking at introducing a GUC variable. However, I am not so sure how to implement this appropriately. Right now in my implementation, the operator is being created with the value of GUC and is not behaving differently once GUC value changes at runtime.
I am not sure if I have to somehow change the signature of the function to use GUC or if I must introduce some pointer to the GUC. I cannot find any helpful material and I do not fully understand the internals to achieve this goal.
What best practices must I use for introducing an operator which changes behaviour at runtime? Unfortunately, I cannot find much information on writing internal functions/ operators/GUC in this context and so it would be great to hear any kind of feedback at all.
A “GUC” (Grand Unified Configuration) is simply a global C variable in your program code, so changing its value will take effect as soon as your code reads the variable.
Your function will be in a shared object that is loaded into PostgreSQL, and you can use the _PG_init() function that is called at load time to register a new GUC with the DefineCustomXXXVariable() functions.
Your GUC can then be set like any other GUC.
I recommend that you look at contrib modules like auto_explain to see how they do it.
In my iPhone development, I've always used global variables for lots of stuff. The style guide in my new job says we should use context parameters instead. So I need to figure out what that means and how to do that.
Can anyone explain in more detail what this means -- or point me to some code that works this way?
Thanks
It sounds like there may be a clash in nomenclature. From this definition of Context Parameters, they seem to be concerned storing global state for the duration of a session. Perhaps, you could use a 'contextParameters' NSDictionary within NSUserDefaults to store your globals. To the extent that your globals might need to be exported in their entirety (for debugging, for state saving) this might be useful in the long run.
The style guide might just be generically saying to keep your variables scoped based on the context of their usage. For example if you have a variable that you need for the lifetime of a class instance then make it a member variable of that class. If it is something that you need for the lifetime of the app then put it in an application wide object (but not a global variable).
If you use a global object (which could mostly be a big C struct containing all your former global variables) instead of individual naked global variables, you might be able to copy the object, serialize it to save it or create a unified core dump, eventually add setters/listeners, etc.
If you break the global object up, based on the shared scope or the required context of groupings of instance/struct variables, then the fractional objects might end up being good candidates for the M portion of an MVC repartitioning of your code for better reuse, extensibility, etc.
I think I understand why there is a danger in allowing closures in a language using dynamic scope. That is, it seems you will be able to close the variable OK, but when trying to read it you will only get the value at the top of global stack. This might be dangerous if other functions use same name in the interim.
Have I missed some other subtlety?
I realize I'm years late answering this, but I just ran across this question while doing a web search and I wanted to correct some misinformation that is posted here.
"Closure" just means a callable object that contains both code and an environment that provides bindings for free variables within that code. That environment is usually a lexical environment, but there is no technical reason why it can't be a dynamic environment.
The trick is to close the code over the environment and not the particular values. This is what Lisp 1.5 did, and also what MACLisp did for "downward funargs."
You can see how Lisp 1.5 did this by reading the Lisp 1.5 manual at http://www.softwarepreservation.org/projects/LISP/book
Pay particular attention in Appendix B to how eval handles FUNCTION and how apply handles FUNARG.
You can get the basic flavor of programming using dynamic closures from http://c2.com/cgi/wiki?DynamicClosure
You can get an in depth introduction to the implementation issues from ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-199.pdf
Modern dynamically scoped languages generally use shallow binding, where the current value of each variable is kept in one global location, and function calls save old values away on the stack. One way of implementing dynamic closures with shallow binding is described at http://www.pipeline.com/~hbaker1/ShallowBinding.html
Yes, that's the basic problem. The term "closure" is short for "lexical closure", though, which by definition captures its lexical scope. I'd call the things in a dynamically scoped language something else, like LAMBDA. Lambdas are perfectly safe in a dynamically scoped language as long as you don't try to return them.
(For an interesting thought experiment, compare the problem of returning a dynamically scoped lambda in Emacs Lisp to the problem of returning a reference to a stack-allocated variable in C, and how both are impossible in Scheme.)
A long time ago, back when languages with dynamic scope were much less rare than today, this was known as the funargs problem. The problem you mention is the upward funargs problem.