Resource refactorisation

#10 Merged
Repository
simpy-resource-refactoring
Branch
default
Repository
simpy
Branch
default
Author
  1. luensdorf
Reviewers
Description

This pull request is only for discussion

General motivation from the commit message:

All resources now share a common base class called BaseResource, which provides generic methods to put and get items from the resource. This works with the old Resource class too, which basically was a lock. Resource.request() is equal to putting a token in the resource while Resource.release() just removes this token again.

put() and get() accept arbitrary arguments which will be directly forwarded into the respective event instances. This allows to specify for example custom filters.

Comments (6)

  1. Neill Byrne

    @luensdorf could you show an example of how the get filter works, so far I have tried.

    class Widget(object):
        def __init__(self, weight):
            self.weight = weight
    
    def putter(env, store):
        yield env.timeout(1)
        yield store.put(Widget(weight = 10))
        yield store.put(Widget(weight = 20))
    
    def getter(env, store):
        item = yield store.get(filter=lambda obj: obj.weight > 15)
        print(env.now, item.weight)
    
    env = simpy.Environment()
    store = simpy.resources.FilterStore(env, capacity=4)
    env.start(putter(env, store))
    env.start(getter(env, store))
    simpy.simulate(env, until=5)
    

    This returns an error that the list has no weight attribute so i tried...

     item = yield store.get(filter=lambda objs: [obj.weight > 15 for obj in objs])
    

    But this returns the first item whose weight is 10?? so I'm clearly missing something here.

    Another question, is it your intent to have all items that pass the filter check returned or just the first one?

    Thanks in advance..

    1. luensdorf author

      Hi Neill,

      my FilterStore implementation was incorrect, sorry. Could you try again? Your example above should work in this updated version.

      Cheers, Ontje

  2. Stefan Scherfke

    Generally, I like this idea and implementation. There are, however, a few minor glitches and organisational/formatting issues that I’d like to discuss with you next week. I’m also not sure why the API changes for the queues were necessary (e.g., renaming Queue.push() to Queue.append()).

    1. luensdorf author

      Cool, the answer to the latter is easy. To simply use default python lists as queues. This is a performance improvement and reuses the existing Python list interface instead of redundantly creating a new one.