def execute_transaction(self, transaction):
        log('Execute transaction %s at otp queue %s' %(transaction, self.queue))

        book = transaction.book_to_transmit

        transaction.book_destination.put_book(book)
        book.owner.give_own_book(book)
Beispiel #2
0
    def __init__(self, address, point_capacity=None):
        self.address = address
        self.stored_books = []
        self.capacity = point_capacity or SimulationExchangePoint.default_capacity
        self.successor = None

        log('Instantiated Exchange Point with address "%s" and capacity %i' % (address, self.capacity))
    def execute_transaction(self, transaction):
        log('Execute transaction %s at otp queue %s' %
            (transaction, self.queue))

        book = transaction.book_to_transmit

        transaction.book_destination.put_book(book)
        book.owner.give_own_book(book)
Beispiel #4
0
    def put_book(self, book):

        # Pattern: Chain of Responsibility

        if self.point_is_not_full():
            self.proxied_point.put_book(book)
        else:
            successor_point = self.proxied_point.successor

            log('Trying exchange point successor %s' % successor_point)

            if successor_point:
                self.proxied_point = successor_point
                self.put_book(book)
            else:
                raise Exception("Can't add book %s, whole chain of exchange points is full." % book)
Beispiel #5
0
    def test_resources(self):

        book_implementor = BookResourceImplementor()
        book = BookResource(implementor=book_implementor, title='Книга про UTF8', owner='Python3 User')

        book_description = book.get_resource_description()
        log(book_description)

        self.assertTrue(len(book_description)>0)
        self.assertTrue(book.implementor == book_implementor)


        computer_implementor = ComputerResourceImplementor()
        computer = ComputerResource(model='BRDG3', vendor='Pattern Computers', implementor=computer_implementor)

        computer_description = computer.get_resource_description()
        log(computer_description)

        self.assertTrue(len(computer_description)>0)
        self.assertTrue(computer.implementor == computer_implementor)
Beispiel #6
0
    def generate_items(self):

        log('Items generation...')

        users_observer = UserObserver()

        # generate random users
        for _ in range(self.users_count):
            new_simulation_user = simulation_item_factory.create_simulation_item('user')
            new_simulation_user.attach(users_observer)  # common observer for all users
            self.all_users.append(new_simulation_user)
            user_books_amount = random.randint(1, self.max_books_per_user)

            # for users generate random own books
            for _ in range(0, user_books_amount):
                new_book = simulation_item_factory.create_simulation_item('book')
                new_simulation_user.own_books.append(new_book)
                new_book.owner = new_simulation_user
                self.all_books.append(new_book)

        self.generate_exchange_points()
Beispiel #7
0
    def __init__(self, users_count=None, max_books_per_user=None, exchange_points_count=None):
        # instantiating without args, returns new class instance with the same, shared, namespace
        # this is one of several possible implementations of Singleton pattern in Python
        super().__init__()

        if users_count and max_books_per_user and exchange_points_count:

            log('Simulation singleton instantiated')

            if max_books_per_user < 1:
                raise ValueError('max_books_per_user minimal value is 1, given %i' % max_books_per_user)

            self.all_books = []
            self.all_users = []
            self.all_exchange_points = []
            self.users_count = users_count
            self.max_books_per_user = max_books_per_user  # each user has limited random books amount
            self.exchange_points_count = exchange_points_count
            self.exchange_point_capacity = None
            self.q_otp = queue.Queue()  # otp - from owners to exchange points
            self.persistence_strategy = None
            self.state = SimulationState.ReadyToRunState()
Beispiel #8
0
    def test_resources(self):

        book_implementor = BookResourceImplementor()
        book = BookResource(implementor=book_implementor,
                            title='Книга про UTF8',
                            owner='Python3 User')

        book_description = book.get_resource_description()
        log(book_description)

        self.assertTrue(len(book_description) > 0)
        self.assertTrue(book.implementor == book_implementor)

        computer_implementor = ComputerResourceImplementor()
        computer = ComputerResource(model='BRDG3',
                                    vendor='Pattern Computers',
                                    implementor=computer_implementor)

        computer_description = computer.get_resource_description()
        log(computer_description)

        self.assertTrue(len(computer_description) > 0)
        self.assertTrue(computer.implementor == computer_implementor)
Beispiel #9
0
    def test_composite(self):

        point1 = simulation_item_factory.create_simulation_exchange_point()

        points_of_country = ExchangePointsHierarchyComposite()

        all_points_of_city_A = ExchangePointsHierarchyComposite()
        all_points_of_city_B = ExchangePointsHierarchyComposite()

        points_of_country.add_child(all_points_of_city_A)
        points_of_country.add_child(all_points_of_city_B)

        # Some books at different points in city A
        for _ in range(0, 3):
            new_exchange_point = simulation_item_factory.create_simulation_exchange_point(
            )
            new_book = simulation_item_factory.create_simulation_book()
            new_exchange_point.put_book(new_book)
            all_points_of_city_A.add_child(
                ExchangePointsHierarchyLeaf(exchange_point=new_exchange_point))

        # Some books at different points in city B
        one_leaf_point_in_city_B = None
        for _ in range(0, 2):
            new_exchange_point = simulation_item_factory.create_simulation_exchange_point(
            )
            new_book = simulation_item_factory.create_simulation_book()
            new_exchange_point.put_book(new_book)

            one_leaf_point_in_city_B = ExchangePointsHierarchyLeaf(
                exchange_point=new_exchange_point)

            all_points_of_city_B.add_child(one_leaf_point_in_city_B)

        # Pattern: Composite
        # Different components of hierarchy using same interface and able to be accessed with the same method

        log(points_of_country.get_subordinated_books_count())
        log(all_points_of_city_A.get_subordinated_books_count())
        log(all_points_of_city_B.get_subordinated_books_count())
        log(one_leaf_point_in_city_B.get_subordinated_books_count())
Beispiel #10
0
    def test_composite(self):

        point1 = simulation_item_factory.create_simulation_exchange_point()

        points_of_country = ExchangePointsHierarchyComposite()

        all_points_of_city_A = ExchangePointsHierarchyComposite()
        all_points_of_city_B = ExchangePointsHierarchyComposite()

        points_of_country.add_child(all_points_of_city_A)
        points_of_country.add_child(all_points_of_city_B)

        # Some books at different points in city A
        for _ in range(0, 3):
            new_exchange_point = simulation_item_factory.create_simulation_exchange_point()
            new_book = simulation_item_factory.create_simulation_book()
            new_exchange_point.put_book(new_book)
            all_points_of_city_A.add_child(ExchangePointsHierarchyLeaf(exchange_point=new_exchange_point))


        # Some books at different points in city B
        one_leaf_point_in_city_B = None
        for _ in range(0, 2):
            new_exchange_point = simulation_item_factory.create_simulation_exchange_point()
            new_book = simulation_item_factory.create_simulation_book()
            new_exchange_point.put_book(new_book)

            one_leaf_point_in_city_B = ExchangePointsHierarchyLeaf(exchange_point=new_exchange_point)

            all_points_of_city_B.add_child(one_leaf_point_in_city_B)

        # Pattern: Composite
        # Different components of hierarchy using same interface and able to be accessed with the same method

        log(points_of_country.get_subordinated_books_count())
        log(all_points_of_city_A.get_subordinated_books_count())
        log(all_points_of_city_B.get_subordinated_books_count())
        log(one_leaf_point_in_city_B.get_subordinated_books_count())
Beispiel #11
0
 def receive_message_from_colleague(self, message, sender):
     log('Received message "%s" from colleague %s' % (message, sender))
Beispiel #12
0
 def clone(self):
     cloned = StoredUser(username=self.item_to_clone.name)
     log('New StoredUser instance derived from simulation prototype %s',
         cloned)
     return cloned
Beispiel #13
0
 def clone(self):
     cloned = StoredBook(title=self.item_to_clone.title)
     log('New StoredBook instance derived from simulation prototype %s',
         cloned)
     return cloned
Beispiel #14
0
 def __init__(self, title, owner=None):
     log('Instantiated Book with title "%s"' % title)
     self._title = title
     self.owner = owner
Beispiel #15
0
 def receive_message_from_colleague(self, message, sender):
     log('Received message "%s" from colleague %s' % (message, sender))
Beispiel #16
0
 def receive_message_from_mediator(self, message):
     log('Received message "%s" from mediator' % message)
Beispiel #17
0
 def __init__(self, name):
     log('Instantiated User with name "%s"' % name)
     self.name = name
     self._own_books = []
     self.rating = 1
Beispiel #18
0
 def receive_message_from_mediator(self, message):
     log('Received message "%s" from mediator' % message)
 def clone(self):
     cloned = StoredUser(username=self.item_to_clone.name)
     log('New StoredUser instance derived from simulation prototype %s', cloned)
     return cloned
 def run_simulation(self):
     self.simulation.run()
     log(self.simulation)
 def clone(self):
     cloned = StoredBook(title=self.item_to_clone.title)
     log('New StoredBook instance derived from simulation prototype %s', cloned)
     return cloned
 def run_simulation(self):
     self.simulation.run()
     log(self.simulation)