refactor(pumpkin-solver): PropagatorConstructor::create now returns event registrations#456
refactor(pumpkin-solver): PropagatorConstructor::create now returns event registrations#456maartenflippo wants to merge 8 commits into
PropagatorConstructor::create now returns event registrations#456Conversation
|
I have done a partial review, e.g., I did not assess the "what is does not do". Added some comments to the code, here are a few more: About naming.
About the trait EventTarget: I am not against having the trait, but I am wondering. You said that you would like to keep this as a separate trait as opposed to adding to IntegerVariable. But it seems to me that this trait will only be used with IntegerVariables, and it is required to implement it, so why not keep it as part of IntegerVariable, it does not seem to be that problematic. It also helps with naming. But fine by me in both cases. Other minor comments: I suppose we would like to have the event registration be an enum with types "EventsToRegister" and "Empty". But this might be a bit cumbersome for something used in only a few lines of code, so I am fine with the current design with the builder! Both EventTarget and Dispatcher have the same function name "register" but they do slightly different things. Not sure if this is a problem or not, but just mentioning it, since in the code it would look like calling the same function (but it is also obviously different, the name is simply the same). |
Of these two I prefer
I have developed a preference for smaller traits. They make it easier to write test dummies in the future. If we want to write unit tests for this part, we do not need to create dummy implementation for the entire
I also prefer the current builder pattern with an assertion over the enum.
I don't think they do different things. I have made changes for and responded to the other comments @EmirDe |
You are right that
I thought about this since, and now I am convincing having this as a trait is a fantastic idea, much better than adding it to
One registers itself, and the other registers another entity. Not entirely the same but similar. In any case this is probably not so important so we could leave the names and if later it becomes an issue we can change. Overall this looks good to me. The only issue is with the things this PR does not do. It seems okay with me to proceed without those points, but please see what to do with those, e.g., open issues for later? |
EmirDe
left a comment
There was a problem hiding this comment.
See my comments for potential changes, but overall seems good for merging
ImkoMarijnissen
left a comment
There was a problem hiding this comment.
Some small points I think could be addressed but other than that it looks good to me!
| self.notification_engine | ||
| .watch_all(domain_id, events, propagator_var); |
There was a problem hiding this comment.
Think this could also be called register or register_events
| struct NotificationEngineWatchers<'a> { | ||
| propagator_id: PropagatorId, | ||
| notificaton_engine: &'a mut NotificationEngine, | ||
| } |
There was a problem hiding this comment.
Docs; also the naming could be improved, there are no watcher here, it is just the notification engine
| fn create( | ||
| self, | ||
| context: PropagatorConstructorContext, | ||
| ) -> (EventsToRegister, Self::PropagatorImpl); |
There was a problem hiding this comment.
Could we make this a separate struct, I am personally not a big fan of returning tuples since it is difficult to keep track of what is related to what (especially if we want to return more from the constructor, such as consistency checkers).
| @@ -48,7 +45,10 @@ pub trait PropagatorConstructor { | |||
| fn add_inference_checkers(&self, _checkers: InferenceCheckers<'_>) {} | |||
|
|
|||
| /// Create the propagator instance from `Self`. | |||
There was a problem hiding this comment.
Docs should be updated
| use crate::propagation::LocalId; | ||
| use crate::variables::DomainId; | ||
|
|
||
| /// Anything that can subscribe to domain events. |
There was a problem hiding this comment.
An example would be nice
|
|
||
| /// Anything that can subscribe to domain events. | ||
| pub trait EventTarget { | ||
| /// Add a registration of self for the given domain events with a local id. |
There was a problem hiding this comment.
What does it mean to add this "registration"?
|
|
||
| /// Contains all the events and domains that a propagator needs to be enqueued for. | ||
| #[derive(Clone, Debug)] | ||
| pub struct EventsToRegister(Vec<(DomainId, EnumSet<DomainEvent>, LocalId)>); |
There was a problem hiding this comment.
Same comment here about structs
| let mut registration = EventsToRegister::builder(); | ||
| for (idx, var) in array.iter().enumerate() { | ||
| context.register(var.clone(), DomainEvents::BOUNDS, LocalId::from(idx as u32)); | ||
| registration = registration.add(var, DomainEvents::BOUNDS, LocalId::from(idx as u32)); |
There was a problem hiding this comment.
Could be nice to implement an Into which allows you to provide an iterator?
We want to make it more explicit that variables should be registered in the propagator constructor. To do so, the signature of
PropagatorConstructor::createis updated to not only return a propagator implementation, but also a bag of events with local IDs for which the propagator should be registered.Implemented as a result of the discussion in #449.
What this adds
EventTarget: Anything that can be registered for domain events. Implemented for all variables, and required for any implementation ofIntegerVariable. This is a separate trait instead of a method onIntegerVariableto try to move away from the monolithic interface thatIntegerVariableis becoming.EventDispatcher: Anything that anEventTargetcan be registered with. Implemented for a wrapper ofNotificationEngineas well as the newEventRegistrationtype.EventRegistration: The bag of domain events that is returned byPropagatorConstructor::createalongside the propagator. The implementation tries to be explicit about when an empty registration is expected. To do so, we introduced theEventRegistrationBuilderabstraction.What this does not do
EventTargettrait. In the future it is likely we will want to do this.EventTargetandEventRegistration.Feedback
The names of the new types added here may not be very clear. Please consider whether you have better ideas, or whether you like the names as they are now.