8 Once features.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Spring Semester 2013 Lecture 5
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
SCOOP on.NET Volkan Arslan Chair of Software Engineering ETH Zurich, Switzerland
Chair of Software Engineering Constants, once routines, and helper functions these slides contain advanced material and are optional.
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 Lecture 15: Exception handling.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Ranga Rodrigo. Class is central to object oriented programming.
Applied Discrete Mathematics Week 9: Relations
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
User-Written Functions
Functions.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Object-Oriented Programming: Classes and Objects
A Simple Syntax-Directed Translator
Chapter 16 UML Class Diagrams.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
COMP 170 – Introduction to Object Oriented Programming
Lecture 7: Repeating a Known Number of Times
Chapter 14 Templates C++ How to Program, 8/e
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
CS1061 C Prgramming Lecture 11: Functions
Chapter 4: Writing Classes
Secure Coding Rules for C++ Copyright © Curt Hill
Programmazione I a.a. 2017/2018.
Object-Oriented Programming: Classes and Objects
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Repeating Instructions And Advance collection
Packages and Interfaces
The Procedure Abstraction Part I: Basics
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Defining Classes I Part A.
Chapter 11: Classes, Instances, and Message-Handlers
Object Oriented Programming in java
Polymorphism Polymorphism
Classes and Objects.
CSE 143 Java Exceptions 1/18/2019.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 8 Advanced SQL.
9-10 Classes: A Deeper Look.
Once features.
Chapter 13 Conditional Repetition
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
QUICK GUIDE TO CIRCULATION IN ALMA
9-10 Classes: A Deeper Look.
Design by Contract – Exceptions
SPL – PS1 Introduction to C++.
Storage Classes.
Generics, Lambdas and Reflection
Presentation transcript:

8 Once features

Once routines If we have a call x.r and x is not Void, the next thing to do is to execute the routine body of r. Right? Wrong. The routine might be a once routine.

Once routines A routine body may start not only with do r do ... Instructions ... end But also with the keyword once r once ... Instructions ... end Where once is possibly followed by one or more “once keys” once (“THREAD”) once (“PROCESS”)

Once routines In the basic case, when you write r once ... Instructions ... end without once keys, then Instructions will be executed at most once in the entire system execution. The first time someone calls the routine, its body will be executed. Any subsequent call will not execute the routine body.

Once functions If the routine is a function r: STRING once ... Instructions ... end The first time someone calls the function, its body will be executed and it will return its result normally. Any subsequent call will not cause the execution of the routine body. It will return immediately to the caller, giving as result the value computed by the first call.

Once Uses: Smart Initialization To make sure that a library works on a properly initialized setup, write the initialization procedure as a Once and include a call to it at the beginning of every externally callable routine of the library. class LIBRARY feature {NONE} init once ... Setup library ... end feature --externally callable routines r do init … end

Once Uses: Shared Objects To let various components of a system share an object, represent it as a once function that creates the object. shared_object: SOME_REFERENCE_TYPE -- A single object useful to several clients once ... ; create Result end Clients will “call” that function and in any case but the first, such a call returns a reference to the object created the first time around.

Predefined Once Keys once (“OBJECT”) -- Once for each instance once (“THREAD”) -- Once per execution of a thread -- (default) once (“PROCSS”) -- Once per execution of a process

Once Tuning For more flexibility you can define your own once keys. once (“MY_KEY”) Lets you refresh a once key: The next call to a once routine that lists it as one of its once keys will execute its body. To refresh once keys class ANY has a feature onces which you can use for calls like: onces.refresh(”MY_KEY") onces.refresh_some([”MY_KEY","OTHER_KEY"]) onces.refresh_all onces.refresh_all_except([”MY_KEY","OTHER_KEY"]) onces.nonfresh_keys

Once Routine Semantics Definition: Freshness of a once routine call During execution, a call whose feature is a once routine r is fresh if and only if every feature call started so far satisfies any of the following conditions: It did not use r as dynamic feature It was in a different thread and r has the once key “THREAD” or no once key. Its target was not the current object and r has the once key “OBJECT” After it was started, a call was executed to one of the refreshing features of onces from ANY, including among the keys to be refreshed at least one of the once keys of r. Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Once Routine Semantics: Example A routine r is fresh if: It hasn’t been called at all It has been called on different objects, and is declared once (“OBJECT”) It’s declared once (“MY_KEY”) and there has been, since the last applicable execution of r, a call onces.refresh (“MY_KEY”) Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Once Routine Semantics: Example An applicable call makes r unfresh again, since the conditions have to apply to every call started so far. feature some_routine once (“OBJECT”) … end other_routine do unfresh afterwards Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Once Routine Semantics Latest applicable target and result of a non-fresh call The latest applicable target of a non-fresh call to a once routine r to a target object O is the last value to which it was attached in the call to r most recently started on: If r has the once key "OBJECT” :O. Otherwise, if r has the once key "THREAD” or no once key: any target in the current thread. Otherwise: any target in any thread. If r is a function, the latest applicable result of the call is the last value returned by a fresh call using as target object its latest applicable target. Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Once Routine Execution Semantics The effect of executing a once routine r on a target object O is: 1・If the call is fresh: that of a non-once call made of the same elements, as determined by Non-once Routine Execution Semantics. 2・If the call is not fresh and the last execution of r on the latest applicable target triggered an exception: to trigger again an identical exception. The remaining cases do not then apply. 3・If the call is not fresh and r is a procedure: no further effect. 4・If the call is not fresh and r is a function: to attach the local variable Result to the latest applicable result of the call. Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Once Routine Exceptions “once a once exception, always a once exception” If the first call to a once routine yields an exception, then all subsequent calls for the same applicable target re-trigger the exception. Clients that repeatedly ask for the same once routine, repeatedly get the same exception, telling them that the requested effect or value is impossible to provide. Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Recursive Once Routines if a once routine is directly or indirectly recursive, its self-calls will not execute the body (in the absence of an intervening explicit refresh) for a function, they will return the Result as computed so far. With recursion a new call usually starts before the first one has terminated, so the result of the first call would not be a meaningful notion in this case. In this case the recursive call will return whatever value the first call has obtained so far for Result (starting with the default initialization) Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Recursive Once Routines Recursive once functions are a bit bizarre, and of little apparent use, but no validity constraint disallows it. recursive_once_routine (x: INTEGER): INTEGER once Result := recursive_once_routine (x-1) + x end Call to recursive_once_routine would return the default initialization of INTEGER: 0. Case 2 indicates that メonce per threadモ is the default in the absence of an explicit once key A fresh call executes the once routine’s body.

Non-valid Once features Once features are not allowed if the result type involves Genericity item: G once … end used in a creation procedure A creation procedure must ensure the invariant, a non-fresh call to a creation procedure would not ensure the invariant anymore.

End of Lecture 8