Ilog Opl Script mixing mismatch environment error - ilog

There are 3 models that I run iteratively by using proper outputs. However, at one point, I get “mixing mistach environment” error. The code is basically as below:
//model1
..
main{
generate & solve model1
call model2
model1.output_x=model2.input_x
generate & solve model2
call model3
model2.output_y=model3.input_y
generate & solve model3
}
While generating of model 3, mismatch environment error occurs. However, model 2 and model 3 are similar.
At model 3, here’s the constraint that I think causes this problem:
totalcost (dvar of model 3)-epsilon(variable of model 3)<=n1_cost (output of model2);

Be really careful. The statements
model1.output_x=model2.input_x
model2.output_y=model3.input_y
seem backwards. Don't you mean:
model2.input_x = model1.output_x
model3.input_y = model2.output_y
Having said that, the proper design pattern is to copy the output of one model into data input for the subsequent model. If you make sure that you have good model/data separation, then the problem should go away.

Related

Organising code in Specflow steps

I understand from Techtalk that coupling step definitions to features is an anti-pattern.
However I am wondering how to organise my step definitions so that I can easily see in the code what steps go together.
For example should I create a code file for each Feature and a separate file for steps that are shared? or should I have a code file for a set of features?
I tend to organize step definitions in a couple of ways, depending on how big the step definition files get.
Separate data and UI steps
I have lots of Given some thing exists in the database steps, so I usually throw these into a file call DataSteps.cs. I also have lots of Then something exists in the database steps and those go into DataAssertionSteps.cs.
All of my When I fill in some form field steps go in FormSteps.cs. Anytime I need Then something in the form is enabled|disabled or asserting that form fields have a certain value, I throw those into FormPresentationSteps.cs.
Separate Steps by Model
Sometimes my step definition files get very large, and I start moving step definitions into files related to a certain model. Say I have a Person model. I might create a PersonSteps.cs file that is split into three regions:
[Binding]
public class PersonSteps
{
#region Given
// Given a person exists with the following attributes:
#endregion
#region When
// When something something about a Person
#endregion
#region Then
// Then a person should exist with the following attributes:
#endregion
}
Basically I start out with these files:
DataSteps.cs - The Givens for setting up test data
DataAssertionSteps.cs - The Thens for asserting that data exists correctly in the database
PresentationAssertionSteps.cs - Just general stuff making sure the UI looks as itshould
FormSteps.cs - Steps for manipulating forms
FormPresentationAssertionSteps.cs - Making sure form fields have the proper values, are enabled/disabled correctly. Validation messages show up, etc.
GeneralSteps.cs - A poorily named catch-all for stuff that doesn't fit in the above files. Maybe I should rename it "AndIShallCallItSomethingManagerSteps.cs"?

Saving record fails due to uniqueness conflict with itself?

I have a procedure which receives two models, one which already exists, and another one which holds new attributes which I want to merge in the first one.
Since other parts of the program are holding the same reference to the new model, I can't just operate on the existing one. Therefor I do the following:
def merge(new_model, existing_model)
new_model.attributes = existing_model.attributes.merge(new_model.attributes)
new_model.id = existing_model.id
end
Now the new_model is being saved which gives me the uniqueness erorr (even though it's technically the same model). I also tried using the reload method, but that yields the same result.
Background:
The method above is run in a before_add callback on an association. I want to be able to call update on a model (with nested associations) without having to specify IDs of the nested models. This update is supposed to merge some associations, which is why I try to do the whole merge thing above.
You can't set the id of a model and then save the record expecting the id to be set since the id is the primary key of the database. So you are actually creating a whole new record and, thus, the uniqueness validation error. So you'll need to think of some other design to accomplish what you are wanting. It may help to know that what you are trying to do sounds similar to a deep_dup, except that ActiveRecord doesn't define this method (but Hash does).

In Rails should i generate a model to contain simple plain list?

I'm currently building a web-page with Ruby on Rails. Im creating A data model that works as follows: Project has many Milestones and milestone has many goals
Example:
Project.milestones.goals
So I generated a model for Project and a model for milestones but generating a model for list of goals seems like big and unnecessary.
Should I generate a model for goals or is there something else that would fit more for this purpose?
It all comes down to this question: Goals need to be persisted on your database?
If the answer is yes, then you must create a model that inherits from ActiveRecord::Base, and if the answer is no, you could create a model, that don't have the need for persistence methods of ActiveRecord, but acts like an Enum, having static values and stuff.

How to build ActiveRecord associations with STI

I'm having problems with AR trying to build associations of models that inherit from others. The problem is the associated models are being saved to the database before the call do the save method.
I found more information in this page http://techspry.com/ruby_and_rails/active-records-or-push-or-concat-method/
That's really weird, why would AR automatically save models appended to the association (with << method) ? One would obviously expect that the save method must called, even if the parent already exists. We can prevent this calling
#user.reviews.build(good_params)
but this would be a problem in a context where the association have an hierarchy, for example: if a Hunter has_many :animals, and Dog and Cat inherit from Animal, we can't do
#hunter.dogs.build
#hunter.cats.build
instead we are stuck with
#hunter.animals << Cat.new
#hunter.animals << Dog.new
and if the Cat/Dog class has no validations, the object will be saved automatically to the database. How can I prevent this behaviour ?
I found out that Rails 3 doesn't fully support associations with STI, and usually hacks are needed. Read more on this post http://simple10.com/rails-3-sti/. As mentioned in one of the comments, this issue is referred in rails 4 https://github.com/rails/rails/commit/89b5b31cc4f8407f648a2447665ef23f9024e8a5
Rails sux so bad handling inheritance = (( Hope Rails 4 fixes this.
Meanwhile I'm using this ugly workaround:
animal = #hunter.animals.build type: 'Dog'
then replace the built object, this step may be necessary for reflection to workout (check Lucy's answer and comments)
hunter.animals[#hunter.animals.index(animal)] = animal.becomes(Dog)
this will behave correctly in this context, since
hunter.animals[#hunter.animals.index(animal)].is_a? Dog
will return true and no database calls will be made with the assignment
Based on Gus's answer I implemented a similar solution:
# instantiate a dog object
dog = Dog.new(name: 'fido')
# get the attributes from the dog, add the class (per Gus's answer)
dog_attributes = dog.attributes.merge(type: 'Dog')
# build a new dog using the correct attributes, including the type
hunter.animals.build(dog_attributes)
Note that the original dog object is just thrown away. Depending on how many attributes you need to set it might be easier to do:
hunter.animals.build(type: 'Dog', name: 'Fido')

Rails 2.3.5: How does one add an error when it doesn't make sense to put it in a validation?

I recently was trying to add errors.add_to_base code in the middle of some model logic and was wondering why it wasn't showing up in my view that was iterating over all errors. I then ran across this e-mail which explains why: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/e045ec1dead1ff06?pli=1
The question is then, how does one add errors with add_to_base if it doesn't make sense to put them into a validate method? I have some complex logic. The model needs to talk to a has_many relationship which has its own relationships that go through a myriad of conditionals to figure out if a request makes sense. It's nothing that can be tied to a validate method easily.
The one thing that I can do is create an instance variable and push all errors as strings to it while I iterate through my complex logic. After that I can run validate against the array and push those errors to the base object. Seems kind of lame though and not the Rails way of doing things.
Is the bottom line in the Rails community that complex logic, even when the logic spans multiple models, has to be in one particular models validate method? How is it handled when the validation is entirely complex and can span multiple models?
I don't understand why it doesn't make sense to put it into validations. For sure this what you want to do should be in a model, so you have to put it in some method inside the model. Then just add:
validate :my_method
and that's all. Of course if my_method gets too complicated, then split it in some logical sub methods.
If you have many related objects then you should put all validations that belongs to that objects in that objects models and when you try to save "parent" object, "child" objects will be also validated with their validations.

Resources