Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.

Slides:



Advertisements
Similar presentations
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Advertisements

Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Event-Driven programming, agents & tuples.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Object-Oriented PHP (1)
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lesson 16 Last update: 25 May 2004 An O-O design example.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 8: More on inheritance.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
Chair of Software Engineering ATOT - Lecture 24, 25 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Event-Driven programming, agents & tuples.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Event-driven programming.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Tuples and Agents Exercise Session 1 and 2 December 2008.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 14: Presentation of EiffelStudio.
Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 7: Patterns, Observer, MVC.
11/1/06 1 Hofstra University, CSC005 Chapter 8 (Part 3) High Level Programming Languages.
Chair of Software Engineering OOSC - Summer Semester Object Oriented Software Construction Prof. Dr. Bertrand Meyer Last update: 7 May 2005 Lecture.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 15: Event-driven programming.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
C++ fundamentals.
Chair of Software Engineering JMLC 2003, , Klagenfurt Austria Event Library: an object-oriented library for event-driven design Volkan Arslan,
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 8: More About OOP and GUIs.
An Object-Oriented Approach to Programming Logic and Design
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CSC 205 Programming II Lecture 5 AWT - I.
Chapter Eleven Handling Events.
C# Event Processing Model
Hands-on Introduction to Visual Basic .NET
COSC3311 – Software Design Tuples and Agents 12/9/2018 1:48 AM
Delegates & Events 1.
Tonga Institute of Higher Education
Presentation transcript:

Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples

Chair of Software Engineering OOSC - Summer Semester Agents: the basic idea Encapsulating routines in objects agent r agent r (x, ?, y) Mechanism will first be illustrated through event-driven programming

Chair of Software Engineering OOSC - Summer Semester Handling traditional input Program drives input: from read_next_character until last_character = Enter loop i := i + 1 Result.put (last_character, i) read_next_character end

Chair of Software Engineering OOSC - Summer Semester Handling input with modern GUIs User drives program: “When a user presses this button, execute that action from my program”

Chair of Software Engineering OOSC - Summer Semester Event-driven programming Routine Routine Routine Routine Routine Routine Routine Publishers Subscribers

Chair of Software Engineering OOSC - Summer Semester A solution: Observer Pattern PUBLISHER * LIBCLASS attach detach SUBSCRIBER * APPCLASS update* update+ Deferred (abstract) Effective (implemented) * + Inherits from Client (uses)

Chair of Software Engineering OOSC - Summer Semester Observer pattern Publisher keeps a list of observers: subscribed : LINKED_LIST [OBSERVER] To register itself, an observer may execute subscribe (some_publisher) where subscribe is defined in OBSERVER: subscribe (p: PUBLISHER) is -- Make current object observe p. require publisher_exists: p /= Void do p.attach (Current) end

Chair of Software Engineering OOSC - Summer Semester Attaching an observer In class PUBLISHER: attach (s: SUBSCRIBER) is -- Register s as subscriber to current publisher. require subscriber_exists: p /= Void do subscribed.extend (s) end Note that invariant of PUBLISHER includes the clause subscribed /= Void (List subscribed is created by creation procedures of PUBLISHER)

Chair of Software Engineering OOSC - Summer Semester trigger is -- Ask all observers to -- react to current event. do from subscribed.start until subscribed.after loop subscribed.item. subscribed.forth end end Each descendant of OBSERVER defines its own version of update Triggering an event update PUBLISHER * LIBCLASS attach detach SUBSCRIBER * APPCLASS update* update+

Chair of Software Engineering OOSC - Summer Semester Observer pattern  Publishers know about subscribers  Subscriber may subscribe to at most one publisher  May subscribe at most one operation  Not reusable — must be coded anew for each application

Chair of Software Engineering OOSC - Summer Semester Another approach: action-event table Set of triples [Event, Context, Action] Event: any occurrence we track Example: a mouse click Context: object for which the event is interesting Example: a particular button Action: what we want to do when the event occurs in the context Example: save the file Action-event table may be implemented as e.g. a hash table.

Chair of Software Engineering OOSC - Summer Semester The EiffelVision style my_button.click.action_list.extend (agent my_procedure)

Chair of Software Engineering OOSC - Summer Semester Mechanisms in other languages C and C++: “function pointers” C#: delegates (more limited form of agents)

Chair of Software Engineering OOSC - Summer Semester With.NET delegates: publisher (1) P1. Introduce new class ClickArgs inheriting from EventArgs, repeating arguments types of myProcedure: public class Clickargs {... int x, y; … } P2. Introduce new type ClickDelegate (delegate type) based on that class public void delegate ClickDelegate (Object sender, e) P3. Declare new type Click (event type) based on the type ClickDelegate: public event ClickDelegate Click

Chair of Software Engineering OOSC - Summer Semester With.NET delegates: publisher (2) P4. Write new procedure OnClick to wrap handling: protected void OnClick (int x, int y) {if (Click != null) {Click (this, x, y)}} P5. For every event occurrence, create new object (instance of ClickArgs), passing arguments to constructor: ClickArgs myClickargs = new Clickargs (h, v) P6. For every event occurrence, trigger event: OnClick (myclickargs)

Chair of Software Engineering OOSC - Summer Semester With.NET delegates: subscriber D1. Declare a delegate myDelegate of type ClickDelegate. (Usually combined with following step.) D2. Instantiate it with myProcedure as argument: ClickDelegate = new ClickDelegate (myProcedure) D3. Add it to the delegate list for the event: YES_button.Click += myDelegate

Chair of Software Engineering OOSC - Summer Semester Event: each event type will be an object Example: mouse clicks Context: an object, usually representing element of user interface Example: a particular button Action: an agent representing a routine Example: routine to save the file Using the Eiffel approach

Chair of Software Engineering OOSC - Summer Semester The EiffelVision style YES_button.click.action_list.extend (agent my_procedure)

Chair of Software Engineering OOSC - Summer Semester Event Library style The basic class is EVENT_TYPE On the publisher side, e.g. GUI library:  (Once) declare event type: click: EVENT_TYPE [TUPLE [INTEGER, INTEGER]]  (Once) create event type object: create click  To trigger one occurrence of the event: click.publish ([x_coordinate, y_coordinate]) On the subscriber side, e.g. an application: click.subscribe (agent my_procedure)

Chair of Software Engineering OOSC - Summer Semester click.subscribe (agent my_procedure) click.subscribe (agent ) click.subscribe (agent other_object. other_procedure ) Subscriber variants my_button. your_procedure (a, ?, ?, b)

Chair of Software Engineering OOSC - Summer Semester Another example of using agents my_integrator.integral (, a, b) my_integrator.integral (agent your_function (, u, v), a, b) agent my_function ? a b  my_function ( x ) dx a b  your_function ( x, u, v ) dx

Chair of Software Engineering OOSC - Summer Semester Applications of agents  Undo-redo  Iteration  High-level contracts  Numerical programming  Introspection (finding out properties of the program itself)

Chair of Software Engineering OOSC - Summer Semester Using an iterator all_positive := my_integer_list.for_all ( agent is_positive (?))

Chair of Software Engineering OOSC - Summer Semester Iterators In class LINEAR [G], ancestor to all classes for lists, sequences etc., you will find: for_all there_exists do_all do_if do_while do_until

Chair of Software Engineering OOSC - Summer Semester Calling the associated routine Given an agent, you may call the associated routine through the feature “call” : a.call ([horizontal_position, vertical_position]) If a is associated with a function, a.item ([...,...]) gives the result of applying the function. A tuple

Chair of Software Engineering OOSC - Summer Semester The integration function integral ( : FUNCTION [ANY, TUPLE [REAL], REAL]; low, high: REAL): REAL is -- Integral of f over the interval [low, high] local x: REAL; i: INTEGER do from x := low until x > high loop Result := Result + step  i := i + 1 x := a + i  step end f.item ([x]) f a b

Chair of Software Engineering OOSC - Summer Semester Behind agents: Tuples Tuple types (for any types A, B, C,... ): TUPLE TUPLE [A] TUPLE [A, B] TUPLE [A, B, C]... A tuple of type TUPLE [A, B, C] is a sequence of at least three values, first of type A, second of type B, third of type C Tuple values: e.g. [a1, b1, c1]

Chair of Software Engineering OOSC - Summer Semester Tuple type inheritance TUPLE [A, B] TUPLE [A] TUPLE...

Chair of Software Engineering OOSC - Summer Semester Accessing and modifying tuple elements To obtain i-th element of a tuplet, use t.item (i) May need assignment attempt: x ?= t.item (i) To change i-th element, use t.put (x, i)

Chair of Software Engineering OOSC - Summer Semester Agents and their arguments An agent can have both “closed” and “open” arguments Closed arguments set at time of agent definition; open arguments set at time of each call. To keep an argument open, just replace it by a question mark: u := agent a0.f (a1, a2, a3) -- All closed (as before) w := agent a0.f (a1, a2, ?) x := agent a0.f (a1, ?, a3) y := agent a0.f (a1, ?, ?) z := agent a0.f (?, ?, ?)

Chair of Software Engineering OOSC - Summer Semester f (x1: T1; x2: T2; x3: T3) a0: C; a1: T1; a2: T2; a3: T3 u := agent a0.f (a1, a2, a3) v := agent a0.f (a1, a2, ?) w := agent a0.f (a1, ?, a3) x := agent a0.f (a1, ?, ?) y := agent a0.f (?, ?, ?) Calling an agent with arguments u.call ([]) v.call ([a3]) w.call ([a2]) x.call ([a2, a3]) y.call ([a1, a2, a3])

Chair of Software Engineering OOSC - Summer Semester EiffelBase classes representing agents ROUTINE  PROCEDURE FUNCTION call   deferred effective   item

Chair of Software Engineering OOSC - Summer Semester Agent types ROUTINE [BASE, ARGS −> TUPLE] PROCEDURE [BASE, ARGS −> TUPLE] FUNCTION [BASE, ARGS −> TUPLE, RESTYPE]

Chair of Software Engineering OOSC - Summer Semester Declaring an agent p: PROCEDURE [ANY, TUPLE] -- Agent representing a procedure, -- no open arguments q: PROCEDURE [ANY, TUPLE [X, Y, Z]] -- Agent representing a procedure, -- 3 open arguments f: FUNCTION [ANY, TUPLE [X, Y, Z], RES] -- Agent representing a procedure, -- 3 open arguments, result of type RES

Chair of Software Engineering OOSC - Summer Semester f (x1: T1; x2: T2; x3: T3), declared in class B a0: C; a1: T1; a2: T2; a3: T3 u := agent a0.f (a1, a2, a3) v := agent a0.f (a1, a2, ?) w := agent a0.f (a1, ?, a3) x := agent a0.f (a1, ?, ?) y := agent a0.f (?, ?, ?) Calling an agent with arguments u.call ([]) v.call ([a3]) w.call ([a2]) x.call ([a2, a3]) y.call ([a1, a2, a3]) PROCEDURE [B, TUPLE [T1, T2, T3]] PROCEDURE [B, TUPLE [T2, T3]] PROCEDURE [B, TUPLE [T2]] PROCEDURE [B, TUPLE [T3]] PROCEDURE [B, TUPLE [ ]]

Chair of Software Engineering OOSC - Summer Semester Making the target open agent {TARGET_TYPE}. f (...) Open or closed arguments

Chair of Software Engineering OOSC - Summer Semester Iterating on the target or the arguments Procedures in class COMPANY:  downgrade -- No argument  record_value (val: REAL; d: DATE)-- Two arguments Then with companies: LIST [COMPANY] values: LIST [REAL] some_company: COMPANY you may use both: companies. do_all (agent {COMPANY}. downgrade} values. do_all (agent my_company.record_value (?,Today))

Chair of Software Engineering OOSC - Summer Semester End of lecture 21