1 / 20

Asgnt3and4: Hominid Example with BDE

Asgnt3and4: Hominid Example with BDE. FOOD Ch. 1 illlustrates a simple software control task, which is to control a robot or ‘hominid’ so it moves along a hallway or ‘tunnel’ of constant width that may contain left and right turns but never bifurcates.

tress
Download Presentation

Asgnt3and4: Hominid Example with BDE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Asgnt3and4: Hominid Example with BDE FOOD Ch. 1 illlustrates a simple software control task, which is to control a robot or ‘hominid’ so it moves along a hallway or ‘tunnel’ of constant width that may contain left and right turns but never bifurcates. Fig. 1.1 of FOOD shows one such path, which is a zig-gzag sequence of square cells, so the hallway is exactly one cell wide. We will use another cyclic path here. The software solution (p. 8 and 56) uses two classes (the hominid and the grid). The particular algorithm used for advancing the hominid insures that it will not reverse direction, and not attempt to advance into a wall. [Ref: Page-Jones FOOD Ch. 1, pp 1-8, 1.11(13), 1.12 p.56] 02s522Asgnt3and4r2.ppt

  2. Specifying the Hominid’s State In FOOD Ch. 1, the hominid’s start position is specified but not its orientation. Its current location is (hom.x, hom.y) where x = row index and y = column index of a W x H cell array (x in [0..W], y in [0..H]. (Origin (0,0) is top left corner cell.) Location is only one part of the hominid’s state. I’ve added an orientation component hom.d to the state. Encode (#define or enum) d as a 4-valued directional sub-state: {E=0, N=1, W=2, S=3}. The total hominid state is now (x,y,d). This state is a 3-tuple <x, y, d> in <[0..4, 0..4, 0..3>. [Ref: Page-Jones FOOD Ch. 1, pp 1-8, 1.11(13), 1.12 p.56] 02s522Asgnt3and4r2.ppt

  3. The Path and the Grid Class Note that the hominid state (x,y,d) associates the hominid object with a particular component of another object of class (rectangular) Grid. Within the grid we have to represent the path, which is a particular configuration of open or blocked cells. We avoid a path generator by predefining path. FOOD defines class Grid to represent this path as a linearly connnected sequence of cells in a complete matrix. Our controller can represent the entire grid as a matrix, but our display view keeps only cells on the specified path as a sparse matrix of adjacent nodes. [Ref: Page-Jones FOOD Ch. 1, pp 1-8, 1.11(13), 1.12 p.56] 02s522Asgnt3and4r2.ppt

  4. Sample Path for Test Case This task requires hand-tracing the algorithm in FOOD Ch. 1. As a test case, assume the following path which is square, with final cell equal to the starting one. x 0,0 1,0 2,0 3,0 4,0 0,1 4,1 0,2 4,2 y 0,3 4,3 0,4 1,4 2,4 3,4 4,4 02s522Asgnt3and4r2.ppt

  5. Behavioral Model The next step is to learn how the hominid is controlled by calling its primitive method interfaces. FOOD says the hominid occupies one cell and supports four primitive action methods: turnLeft, turnRight, advanceOneCell, and a Boolean query: isFacingWall. It also has an initialization method insert (x,y,d). (FOOD uses only x,y, but orientation d also matters if the start position is not at one end of the tunnel.) The control program calls these methods to move the hominid appropriately without causing damage by bumping into a wall. 02s522Asgnt3and4r2.ppt

  6. Hominid Performance x 0,0 1,0 2,0 3,0 4,0 0,1 4,1 START 0,2 4,2 y 0,3 4,3 0,4 1,4 2,4 3,4 4,4 Asgnt 3a: Suppose each turnLeft or turnRight or advance operation takes one step or unit of time. For the FOOD algorithm, how many steps are required to traverse the entire cyclic path, from the top center location <0,2> back to <0,2>, as a function of each possible starting direction d? (Give four answers, one for each intial value of d.) 02s522Asgnt3and4r2.ppt

  7. Life-Cycle State On the next page is a state transition sub-diagram for the hominid state’s direction component. There is one transition for each of its turn or advance action methods. Note that advance affects the <x,y> location differently in each of the four direction states. The x and y components of state have their own state diagrams, but they correspond to modulo-5 up/down counter shown below. Only one counter can change at a time; neither x nor y changes in the same step d does. 0 1 2 4 3 02s522Asgnt3and4r2.ppt

  8. State Model for Hominid Direction Commands: TR = turnRight TL = turnLeft AD = advance AD/y++ D = N = 1 TR TR TL TL D = W = 3 D = E = 0 TL TL TR TR AD/x-- D = S = 2 AD/x++ AD/y-- 02s522Asgnt3and4r2.ppt

  9. Hominid’s State Sequence Although this problem is algorithm-directed, not event-driven, the path and direction followed by the hominid is its life cycle state history. More complex interactions would warrant a UML ‘sequence diagram’ to illustrate the behavior of this ‘use case’. Asgnt 3b: What path direction (clkw or cclkw) will be followed by the hominid, and how many total steps will it require to complete a cycle, as a function of each possible starting direction (d = 0,1,2,3)? Asgnt 3c: Write down the state sequence of <x,y,d> values from a start position of top center (x=2,y=4) and one starting direction d=0, 1, 2, or 3 (your choice). 02s522Asgnt3and4r2.ppt

  10. Test Path Display in BDE Bde can draw an arbitrary set of rectangular nodes. Therefore, it can be used to load and display the path grid. It only has to distinguish one particular cell as the hominid’s current position and orientation. This occupied cell can have a node name or a single HA-text-row child containing one (large) direction letter. Asgnt 4a: For the specific parameters W=5, H=5, provide the data (hominid.dat) that defines a BDE diagram for the test path, as one HG-row plus a child-set or table of 16 HN-rows, each HN being one square cell around the perimeter of the 5 by 5 grid (refer to the previous slide). Initialize the hominid’s state to <x,y,d > = <2,0,E>. 02s522Asgnt3and4r2.ppt

  11. Generating the Hominid’s Life Cycle We can use the ‘Model-View-Controller’ design paradigm of GUI window managers. In this case we want the GUI to pay attention to external as well as local mouse and keyboard events. Asgnt 4b: Implement class Hominidwithout changing FOOD’s method specifications. Implement each pre-specified public action method of its interface as a database transaction which updates the hominid’s private location (x or y) or direction (d) state variables. Hint: Create one HA-row containing the label character E,N,W or S. Change this text on each turn method call; change this HA’s HN parent to follow the hominid on each call to advance (do pr_delete then pr_add). 02s522Asgnt3and4r2.ppt

  12. Path History as a BDE Log File This task illustrates log-file preparation. Log-file ‘replay’ is a typical use of the ‘Notification’ Design Pattern (DP). The program or ‘Controller’ arranges for the database or model to notify the graphic display or ‘view’ of the database that something has changed and needs to be redrawn. Redrawing everything is the brute-force way to display the hominid move history using Bde. Asgnt 4c: Write the control program that implements FOOD’s traversal algorithm. It should call pr_load to import your hominid.dat file from Asgnt 4a, and enable logging to write a database change history file. Hint: Initialize the hominid to state <2,0,d>. Specify x,y and/or d in the file or on the command line. 02s522Asgnt3and4r2.ppt

  13. Change-Log Replay in BDE The final step is to call or ‘notify’ bde to redraw the diagram ‘view’ each time the hominid changes state. Ideally, this would be done by replaying your log file. This external change input is not yet implemented in Bde. It could become a new callback response to a keyboard input, by specializing and rebuilding bde for this use. I’d like 02s522 teams to use a more generic way of enhancing bde, which I will ask my 02s592 team to provide. This team’s goal is to add to bde a call to the X-Window function XtAppAddInput or XtAppAddWorkProc. This will divert XWindows from its exclusive focus on user keyboard and mouse inputs to effect bde log file playback. 02s522Asgnt3and4r2.ppt

  14. Displaying Your Path History Assume that XtAppAddWOrkProc has been added to bde, with a callback action that executes one pr_replay step (read and apply one log record), followed by an immediate and total redraw. Asgnt 4d Run this version of bde with replay input from the log file out of your controller in Asgnt 4c. If you turn on logging after pr_load of your hominid.dat from Asgnt 4a , the resulting log will include hominid.dat plus a pr_set_* command for each HN or HA field change made by the controller. 02s522Asgnt3and4r2.ppt

  15. The STD on slide 8 bypassed a problem by labeling transitions by commands AD, TL and TR - it did not specify how these commands originated. Consider the Controller’s state model: During each iteration (main loop cycle or step), one command is issued.When and why does this occur? If a clock is used to execute steps at equal intervals, then a timer can be asked to deliver a wake-up Event Type (ET) after timing (sleep/suspend for) each interval. The time-out event type is effectively neutral - it says when, but not what, to do in the next step. Commands as Event Instances 02s522Asgnt3and4r2.ppt

  16. If time-out (or wake-up) is the only event type, then how does the hominid controller determine which of the 3 commands to execute? The state (x,y,d) of the hominid is not sufficient, until it is related (associated) to the path specification. The single query isFacingWall(x,y,d) is sufficient for this association of the Hominid to a Path (provided it is a linear non-bifurcating tunnel or zig-zag path). But to which class should the isFacingWall method be assigned? As a hominid method, it needs access to the internal data structure of an alien object of class Path. [why is this bad?] The Query isFacingWall(x,y,d)? 02s522Asgnt3and4r2.ppt

  17. An Abstract Data Type (ADT) is a data structure with operations defined on it. A class is an ADT with multiple instances (each one is a separate identifiable object). Is Path a candidate for a class? The path as a data type has a rich structure (it is a linearly connected subset of a grid or 2D array of cells) and there can be many instances of path, so it is a candidate for a class. Could the path be a tunnel in a 3D grid? [Why not? :-)] Is class Path method-less? [Wait and see.] Is it stateless? [No - it has a rich structure or ‘data state’]. The Path as a Candidate Class 02s522Asgnt3and4r2.ppt

  18. Path is not an active class - it has no non-trivial behavior of its own, and it does not change over time. A passive class is one with a constant or invariant ‘data state’ in each object. It has a one-state STD. [How does constant data restrict its methods, if any?] A ‘stateless’ class is one with no data content at all (no data members or state). Path is not stateless. So Path is a passive class - the ‘data state’ of a Path instance determines the result of query isFacingWall(x,y,d). To hide this data content, we make isFacingWall() a (read-only)method of class Path. The other methods of class Path are its constructors and destructors, which we ignore here. Class Path: is it Active or Passive? 02s522Asgnt3and4r2.ppt

  19. Query function ‘IsFacingWall()’ is a read-only method that encapsulates (hides) the data content of class Path. A hominid controller is a ‘client’ of a path ‘server’. The isFacingWall(x,y,d) method call is equivalent to sending a ‘message’ to the path. [Messages must replace calls in a distributed system. Why?] This query will act as expected (return True or False ) iff this pre-condition is true: {(x,y) is inside some cell of the Path (a specified subset of the Grid array of cells). } Can the hominid controller be trusted to obey this pre-condition?Why?Suppose it does:what post-condition defines a correct response? What happens if the pre-condition is false? Pre-Conditions and Post-Conditions 02s522Asgnt3and4r2.ppt

  20. [Ref: B. Meyer O-O Software Construction, Ch. 11 (2 ed., A-W 1997] Meyer popularized the term ‘Desgn by Contract’ in his OOP language ‘Eiffel’. Eiffel includes the keywords ‘require’ and ‘ensure’. Each method has a ‘contract’. ‘Require’ introduces the pre-conditions which a client agrees to meet before each method call. ‘Ensure’ introduces the post-conditions which the server agrees to meet at a method exit or return. C/C++ includes the ‘assert’ macro to test pre- and post- conditions as a debugging aid: its default action is to abort with an error message. [Whatmessage? Read ‘man assert’ to find out.] Design by Contract 02s522Asgnt3and4r2.ppt

More Related