how to keep track of planning variable assignment in optaplanner - optaplanner

Is there a way to access planning variable's assignment during planning?
In my use case, I want to assign a planning variable with certain status only one time only during planning. After that I don't want to use that planning variable.
I know that in optaplanner, a planning variable/problem fact can not change, so i can not change its status.
Is there a way to get list of planning variable assignment during planning so that in java code or drools file, i can avoid re-assignment if it has been used once?
Thanks!

Use a hard constraint to enforce that.
Yes, you could use MoveFilters too, but that's not a good idea because sometimes you need to break hard constraints to escape local optima.

Related

Handle database look-up values in ASP.NET application

Almost all the applications I worked on involve some look-up values. For example, a lot of times a list of languages ( English, French, etc...) need to be displayed on the WebForm for user to choose.
The common data structure for such look up values include an integer Id and a string as name. Since these look-up values are used so frequently, and they are unlikely to be changed. Most of time, instead of grabbing them from database, I just define a global enum in C# like this
enum Language : int { English = 1, French = 2}
I've been handling look-up values like this for years now. I knew it may not be the best way to handle them. For example, every time a new language is added to the system, somebody needs to remember to update that enum.
I guess this is a very common scenario, just wondering how you guys handle this type of look-up values.
Thanks,
I usually put them int he database anyway, because you never know if and when they'll need to change. There have been times when - by design - I know that the list will not change, and for those, I will use enums instead. All other cases, I will use a database table.
Depending on the data requirements, I typically handle small lookups like that in a configuration file often loaded singularly to be applied as an application variable for all instances of the app. The data is looked up once when the application is started or recycled and then remains useful until it is dead.
Although, I have a lot of intermediate data of similar nature that requires regular maintenance. It is easier to put this into a database because at least then you can build a small maintenance application to handle it. If you put them in an enum, you have to recompile and redeploy.
I ran into a similar issue with resource files. Since resource files are compiled, they require another redeployment.
You may benefit from T4 Templates. I haven't used these yet because we haven't had a requirement to meet the need, but I'd like to use them eventually.
Here's a great article for generating enums from a database table (the code is hard to read on the page, there is a link to the source at the end of the article):
http://www.thecodejunkie.com/2008/11/generate-enums-from-database-using-text.html
Also, Scott Hanselman has a list of great T4 templates/articles on his blog.
T4 Templates on MSDN

In Business Rules Engines, is there a way to address a “stale” snapshot?

Specific environment I'm using in case it's needed: Drools, JAVA
Fairly new to BREs, but as I understand - it generates an initial snapshot of data.. then applies reasoning on the snapshot to enforce business rules. What happens if I perform data updates in between the snapshot generation and rule execution?
Is there any way to workaround this, other than limiting data updates altogether?
Drools does not generate a "snapshot of data". The Drools engine operates on a set of objects made available to it by passing object references to the insert method so that the become "facts".
Any changes made to these facts result in a change of what the engine is about to do until the engine indicates that all has been done by returning from the call fireAllRules. The idea is to be up-to-date during rule evaluation.
If you want to analyse a static set of facts or make sure that you are looking at consistent sets of facts you'll have withhold updates or operate on cloned objects. But that is usually not what is useful and wanted.

ILOG variable set equivalent in Drools

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,

Google App Engine, BigTable and model evolution

I am currently playing with Spring/JPA/Google App Engine, and there is an issue that I am worrying about.
The cool thing with GAE is, once my mapping is defined, I just need to insert data and everything associated to it is stored too.
However, if I happen to change my mapping, how can I do with my previous data ? Do I have to create migration scripts each time something has changed ? Is there a way to use Liquibase or something like that in this case ? Or is there another way to handle these changes on existing data ?
Thanks a lot for your help!
Rolf
It depends on the nature of those changes, but in most cases, at least some light updating will be required. One big thing to keep in mind is parent-child relationships (see this previous question for more details) and entity groups while design; since parents become part of an entity key, they are very much immutable. Same with key names.
Another thing is that app engine entities are schemaless; for example if you have some class Foo and you suddenly add a property, prop = db.BooleanProperty(default=True) to it, all existing Foo entities will not have prop set to True (though new ones will). Likewise, you will have to manage ReferencePropertys and ListProperty(db.Key) manually. App Engine does have a _set operator to help with this, but to be honest I've found it to be a bit unreliable, with _sets coming out empty when I knew for a fact that they should not be. Regardless, here's the documentation on using the _set functionality.

which is better small relation db query or constant global variable interaction?

I am building a web application where i am using multilingual support.
I am using variables for label text display, so that administrators can
change a value in one place and that change is reflected throughout the application.
Please suggest which is better/less time consuming for displaying label text?
using relational db interaction.
constant variable.
xml interaction.
How could I find/calculate the processing time of the above three?
'Less time Consuming' is easy, and completely intuitive; constants will always be faster than retrieving the information from any external source, even another in-memory source. Probably even faster than retrieving it from a variable (which is where any of the other solutions would have to end up putting the data)
But I suspect there is more to it than that. If you need to support the ability to change that data (and even if not), you may consider also using Resource Files, which would enable you to replace all such resources based on language/culture.
But you could test the speed fairly easily using the .NET 4 StopWatch class, or the system tickcount (not sure of the object offhand where that comes from) if you don't have 4.0
db interaction, in that case the rate of db-interaction would be increased, unless you apply some cache logic.
Constants, Manageability issues.
XML, parsing time+High rate of IO etc.
Create three unit test for each choice.
Load test them and compare the results.

Resources