Google Answers Logo
View Question
 
Q: Ada program ( Answered 5 out of 5 stars,   10 Comments )
Question  
Subject: Ada program
Category: Computers > Programming
Asked by: twbarrel-ga
List Price: $100.00
Posted: 10 Jan 2003 06:02 PST
Expires: 09 Feb 2003 06:02 PST
Question ID: 141183
I am trying to develop a sample program demonstrating concurrent tasks
in Ada. My idea is three colored balls continuously bouncing within a
frame when checking a "start" check box and they stop with the use of
a "stop" check box. I would also like to have a timer display
(preferably in seconds) in the upper right corner. Could anyone help
me with this? I would like to have an answer within the next 2-3 days.

Request for Question Clarification by mathtalk-ga on 10 Jan 2003 07:22 PST
Hi, twbarrel-ga:

What platform/operating system are you developing for, and what
compiler/development tools are you using?  The details of the
graphical display may be somewhat system dependent.

thanks, mathtalk-ga

Clarification of Question by twbarrel-ga on 10 Jan 2003 15:03 PST
Hi mathtalk-ga,

Thank you for responding to my request. Actually, my operating system
is Windows Me and the compiler I'm using is GNAT 3.14p; I'm also using
the JEWL-14 library.

Thanks again,
twbarrel-ga

Request for Question Clarification by mathtalk-ga on 12 Jan 2003 14:14 PST
Hi, twbarrel-ga:

I have downloaded the same Ada compiler and JEWL library (slightly
newer version 1.6), and I've also located some source code on the Web
in Ada for a single bouncing ball.  Windows ME is about the only MS
operating system that I don't have available for testing.  I do have
NT, 2000, XP, and Win98.  Given enough time I could also set up
Windows ME on a box.

I see you have increased the price offered, and I would like to know
more about what amount of help is needed.  In particular what is your
thinking about inter-task communication (assuming you want the three
balls to each have their own task, and to detect collisions between
balls)?

Obviously little time remains in your original timeframe of 2-3 days,
so I will be watching closely for your reply.

regards, mathtalk-ga

Request for Question Clarification by mathtalk-ga on 12 Jan 2003 17:25 PST
One more basic question (in hindsight): are you aiming to display 2D
or 3D bouncing balls?  I guess 1D bouncing balls are technically
possible, but I'd gues that's not what you have in mind.

-- mathtalk-ga

Clarification of Question by twbarrel-ga on 12 Jan 2003 18:59 PST
Hi mathtalk-ga,

The reason I increased the price offered is that I didn't find the
time to work on the program during the weekend as I had planned,
because of an unexpected personal problem. So, now I need a complete
program. I would like each ball having its own task, independently
from the other ones, and there is no need for collisions' detection.
Let's keep things simple! I also think that a 2D display is OK. I
think I can wait until tomorrow evening.

r/s 
twbarrel-ga

Request for Question Clarification by mathtalk-ga on 13 Jan 2003 09:00 PST
Hi, twbarrel:

Of the sample programs in John English's JEWL download, the pong demo
seems an especially good starting point for what you want to do.

It seems to me that the inter-task communication challenges are not
eliminated simply by omitting collision detection, so long as the goal
is to have the three balls bouncing in the same frame (as I read your
question).  I think that we could have three separate frames for the
three balls, put a separate timer display in each, and demonstrate
concurrent tasks that way with no inter-task communication.

regards, mathtalk

Clarification of Question by twbarrel-ga on 13 Jan 2003 12:27 PST
Hi mathtalk,

Your suggestion seems interesting to me. Although I would prefer to
have the three balls bouncing within the same frame (even if some
inter-task communication will still be present), if you think it will
be easier to create three separate frames you may proceed in this way,
it's OK with me.

Thanks,
twbarrel

Clarification of Question by twbarrel-ga on 15 Jan 2003 14:20 PST
Hi mathtalk,

I hope I didn't put you in too much trouble with my program! Are you
still working on it or did you give up? Please, let me know!

Regards,
twbarrel

Request for Question Clarification by mathtalk-ga on 15 Jan 2003 20:58 PST
Hi, twbarrel-ga:

I apologize for not having detailed my progress more clearly.

By no means have you put me to "too much trouble".  If you wish to
expire the question, as you have said it would be necessary to obtain
an answer within 2-3 days (as I have clearly failed to do), that would
be fair in every respect.  The work I've done so far has been
"voluntary", albeit in the hopes of finding something suitable for
your purpose, but equally because the question you ask is worth
answering.

I've tried to communicate my progress at a high level, so that you can
at your option follow along.  The "pong" example in John English's
compendium can be turned into a concurrent tasked example with little
effort.  Once a task is defined by a compilation unit, it becomes a
sort of "data type".  Variables can be instantiated with that data
type.  However you want stop and start buttons and a "timer", so I've
withheld posting any partial answer while I tried to address these
novel elements.

Because of my uncertainty over being able to conclude development in
the timeframe you require, I have not locked your question for any
significant period of time, which means that it was and is available
to all the researchers to answer.  Naturally yours is a fairly
specialized question, but I know there are several quite competent
programmers in the pool of researchers.

I feel confident in being able to produce something tested and useful
in two more days (COB Friday), and I regret not being able to offer
something more timely.  If it would be helpful to your own
understanding, I would be willing to post some intermediate stages of
development, though naturally this would tend to slow things down for
me slightly.

best wishes, mathtalk-ga

Clarification of Question by twbarrel-ga on 16 Jan 2003 00:31 PST
Hi mathtalk, 

Since you are still working on it, I think I can afford waiting two
more days. If you believe you can finish it by Friday evening, you can
lock the question, it's OK with me. There is no need to slow down your
effort by sending me intermediate stages of your work.

Regards, 
twbarrel
Answer  
Subject: Re: Ada program
Answered By: mathtalk-ga on 31 Jan 2003 05:01 PST
Rated:5 out of 5 stars
 
------------------------------
-- guiTimer.adb 
-- (mathtalk 2003-01-22)
-- 
-- GUI "stopwatch" application
-- built with JEWL library in
-- Ada (GNAT 3.16 compiler)
--
-- Bouncing balls in box added 
-- (mathtalk 2003-01-31)
------------------------------

with JEWL.Simple_Windows; use JEWL.Simple_Windows;
with Ada.Calendar; use Ada.Calendar;

procedure GuiTimer is

   -- declare the task to manage bouncing balls
   task BallBox is
      entry setFlag(Flag : in Integer);
   end BallBox;

   Time_Start   : Time;
   Time_Flag    : Integer     := 0;
   
   My_Frame     : Frame_Type  
         := Frame (180, 150, "GUI Timer", 'Q');
   Start_It     : Button_Type 
         := Button (My_Frame, (45,10), 80, 25, "Start", 'R');
   Stop_It      : Button_Type 
         := Button (My_Frame, (45,45), 80, 25, "Stop",  'S');
   Elapsed      : Label_Type  
         := Label (My_Frame, (20,95), 130, 25, "0 sec.", Center);
   
   -- define the task to manage three bouncing balls
   task body BallBox is

     F : Frame_Type  := Frame (300, 250, "Balls", 'Q');
     C : Canvas_Type := Canvas (F, (0,0), 0, 0, 'X', 'X');

     P : Integer := Get_Width(C)/2;
     W : Integer := Get_Width(C);
     H : Integer := Get_Height(C);

     B1 : Point_Type := (P, Get_Height(C) - 25);
     X1 : Integer := 5;
     Y1 : Integer := -10;
  
     B2 : Point_Type := (P - 20, Get_Height(C) - 25);
     X2 : Integer := -5;
     Y2 : Integer := 10;
  
     B3 : Point_Type := (P + 20, Get_Height(C) - 25);
     X3 : Integer := -5;
     Y3 : Integer := -10;
     
     M : Integer := 0;
     
      begin
        Set_Fill(C,Black);
        Save(C);
        loop
          select
            accept setFlag(Flag: in Integer) do 
               M := Flag;
            end setFlag;
          else
             delay 0.05;
          end select;
          if M=-1 then
             exit;
          elsif M=1 then
             Restore(C);
             Draw_Circle(C,B1,5);
             if B1.Y < 5 or B1.Y >= H-5 then
               Y1 := -Y1;
             end if;
             if B1.X < 5 or B1.X >= W-5 then
               X1 := -X1;
             end if;
             B1 := B1 + (X1,Y1);
             Draw_Circle(C,B2,5);
             if B2.Y < 5 or B2.Y >= H-5 then
               Y2 := -Y2;
             end if;
             if B2.X < 5 or B2.X >= W-5 then
               X2 := -X2;
             end if;
             B2 := B2 + (X2,Y2);
             Draw_Circle(C,B3,5);
             if B3.Y < 5 or B3.Y >= H-5 then
               Y3 := -Y3;
             end if;
             if B3.X < 5 or B3.X >= W-5 then
               X3 := -X3;
             end if;
             B3 := B3 + (X3,Y3);
          end if;
        end loop;
        Close(F);
      exception
        when others =>
          Close(F);
   end BallBox;
   
begin
  loop
   if Command_Ready then
    case Next_Command is
      when 'Q' =>
        BallBox.setFlag(-1);
        Close (My_Frame);
        exit;
      when 'R' =>
         if Time_Flag=0 then
           Time_Flag := 1;
           BallBox.setFlag(1);
           Time_Start := Clock;
           Set_Text (Elapsed, "0 sec.");
         end if;
      when 'S' =>
         if Time_Flag=1 then
           Set_Text (Elapsed, 
                 Duration'Image(Clock - Time_Start) & " sec.");
           Time_Flag := 0;
           BallBox.setFlag(0);
         end if;
      when others =>
        null;
    end case;
   elsif Time_Flag=1 then
     Set_Text (Elapsed, 
           Integer'Image(Integer(Clock - Time_Start)) & " sec.");
     Delay 0.1;
   end if;
  end loop;
end GuiTimer;

Clarification of Answer by mathtalk-ga on 31 Jan 2003 05:37 PST
Hi, twbarrel:

Above is the modified GUI timer program presented earlier, modified to
manage three bouncing balls in a box.  These balls do not collide with
each other, only with the edges of the box, per our previous
discussion.

Since you were able to compile and run the earlier program, I think
you will not have any difficulty with this one either.  However for
the sake of completeness here are a couple of notes on how I build the
application.

There is only one compilation unit, not the two units that I was
struggling with earlier.  I named it guitimer.adb, same as the earlier
program.  While I started development in the C:\JEWL\examples
directory on my machine, I realized that this ran a risk of clobbering
the sample programs there and moved the files to another directory
(TimerAda) under my personal documents.

I created a "batch" command file called setenv.bat in C:\JEWL\examples
with these lines:

set ADA_INCLUDE_PATH=C:\JEWL\source
set ADA_OBJECTS_PATH=C:\JEWL\source

Then, when I open a "DOS" command box to do builds, I can first run
this file, which alerts the gnatmake utility where to find the
necessary JEWL library contents.

Finally I execute:

gnatmake guiTimer -largs -mwindows

from within the same directory (TimerAda) as my application source.

I'll be happy to provide a more commented version of the program if
you like.  For now let me mention the "program limitations" that I'm
aware of.

The limited options in JEWL, specifically for the Frame( )
constructor, do not allow for the "child" window to be made
unresizable.  There is essentially only one window type available. 
The JEWL pong demo program I emulated handled the resizing in a
partial fashion. I decided not to try handling resizing at all, in the
sense that if you enlarge the window or move it, the balls continue to
"bounce" within boundaries congruent to the original window.  I
believe this to be a more stable approach than was present in the pong
code, but a "proper" handling of frame resizing would be possible at
the expense of more complicated logic.  [The big problem with resizing
in the original demo would be seen if the frame size is reduced so
that the ball is off the screen; the logic would not permit the ball
to return.]

The other limitation is a bit of a glitch in appearance.  The timer
"parent" window and the ball box "child" window seem to consistently
overlap when the program starts.  I don't believe that any of the
arguments to the Frame( ) constructor allow us to mitigate this. 
Instead I find it necessary to grab the title bar of the "Balls"
window and move it so that it does not overlap.

One final comment on JEWL:  I found it to be a very easy to use
package, just as author John English describes it, with a tradeoff of
limited functionality such as illustrated above.  I sent John an
email, thanking him for his efforts, and also reporting a minor bug
(the first two arguments of Frame( ) are reversed in the
documentation).

Thanks for posting this very challenging question, twbarrel.  Please
let me know what clarifications would be most useful to you.

regards, mathtalk

Clarification of Answer by mathtalk-ga on 31 Jan 2003 06:15 PST
Another comment to give credit where credit is due:  I found this PDF
document quite helpful in picking my way through many aspects of
concurrency in Ada:

[Concurrent Units in Ada]
http://www.cs.hope.edu/~dershem/book/book18.pdf

This is one chapter in a draft of a new edition of a book by Herb
Dershem and Mike Jipping on programming languages.

See also this earlier chapter in the draft, from a more language
independent perspective:

[Concurrency]
http://www.cs.hope.edu/~dershem/book/book9.pdf

regards, mathtalk-ga

Clarification of Answer by mathtalk-ga on 09 Feb 2003 08:46 PST
Hi, twbarrel:

Thanks for the kind words and generous rating (and tip!).

I've uploaded a commented version of the Ada source code here:

http://www.lucidmatrix.com/uploads/guiTimer.adb 

It should remain there for 30 days, but not indefinitely, so please
download the file as soon as convenient.

best wishes, mathtalk
twbarrel-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
He did a great job in a difficult and challenging question!

Comments  
Subject: Re: Ada program
From: mathtalk-ga on 18 Jan 2003 06:37 PST
 
Hi, twbarrel:

I'm here... still working!  The weekend should allow me the
uninterrupted time to clean up my project.

regards, mathtalk
Subject: Re: Ada program
From: mathtalk-ga on 19 Jan 2003 07:23 PST
 
The uninterrupted time has at least allowed me to see my hubris in
thinking I could easily mix and match features from the JEWL and GNAT
examples.  Hindsight suggests I fell into the "garden path" fallacy of
thinking that because I could compile and execute the demo programs
with little effort, combining features would "easily and quickly"
allow me to provide the sort of program you want.

While I will continue to grind away at this to clear up my own
frustrations, I no longer think it feasible to provide a good answer
to your question within the next day or so.  Part of the frustration
comes from not having a good way of thinking about the balance between
the desired functions (bouncing balls) and the principle being
demonstrated (concurrent tasks).  Certainly Ada is rife with
concurrent tasks, and one is tempted to propose a program that simply
focuses on the desired effect (balls bouncing in a box) without
preconceived constraints on how to accomplish this.  But it seems from
your wording that the motivation originally was to have sample code
demonstrating concurrent tasks, in some sense, and that the bouncing
balls are a means to that end.

I apologize for not having been more realistic in my thinking about
this problem.  I'll post my development notes later today in case as a
comment, in case they turn out to be helpful to you or another.

regards, mathtalk-ga
Subject: Re: Ada program
From: twbarrel-ga on 19 Jan 2003 11:04 PST
 
Hi mathtalk,

I really appreciate your effort and the time you have already spent in
order to provide an adequate answer to my question. But at this point
-as I think you can understand, since I have been waiting much more
than initially planned- it is too late for me to start working based
on just notes, no matter how helpful and detailed they are (and I'm
sure that you have reached some very useful conclusions!). So, given
the current situation, I would like to have just an executable
program, even if it is much simpler than the one I originally asked
and it doesn't demonstrate concurrent tasks in the way I had in mind
and simply focuses on the effect of 3 balls bouncing within a frame.
All I'm asking now is something that can be compiled and executed!
What do you thing about it?

Regards,  
twbarrel
Subject: Re: Ada program
From: mathtalk-ga on 19 Jan 2003 18:12 PST
 
I'll focus on the appearance you want -- bouncing balls, buttons, an
elapsed time clock -- and then we'll ask the question of what you
really need to modify to achieve your demonstration.


regards, mathtalk
Subject: Re: Ada program
From: mathtalk-ga on 22 Jan 2003 20:19 PST
 
Hi, twbarrel:

Here is a "stopwatch" timer program I wrote.  I plan to add a separate
compilation unit to control the bouncing balls, but you may as well
try to compile this one, to make sure we are on the same page as far
as using GNAT and JEWL.  If you have difficulty compiling it, let me
know and I can give a walk through of the steps I used.  Watch out for
the odd line that may have wrapped "badly" in this text window.

 = = = = = = = = = = = = = = = = = = = = = = = = =

------------------------------
-- guiTimer.adb (2003-01-22)
-- 
-- GUI "stopwatch" application
-- built with JEWL library in
-- Ada (GNAT 3.16 compiler)
------------------------------

with JEWL.Simple_Windows; use JEWL.Simple_Windows;
with Ada.Calendar; use Ada.Calendar;

procedure GuiTimer is

	Time_Start   : Time;
	Time_Flag    : Boolean     := False;
	
	My_Frame     : Frame_Type  
          := Frame (280, 150, "GUI Timer", 'Q');
	Start_It     : Button_Type 
          := Button (My_Frame, (45,10), 80, 25, "Start", 'R');
	Stop_It      : Button_Type 
          := Button (My_Frame, (45,45), 80, 25, "Stop",  'S');
	Elapsed      : Label_Type  
          := Label (My_Frame, (20,95), 130, 25, "0 sec.", Center);

begin
  loop
	if Command_Ready then
	 case Next_Command is
		when 'Q' =>
		  Close (My_Frame);
		  exit;
		when 'R' =>
			if not(Time_Flag) then
			  Time_Flag := True;
			  Time_Start := Clock;
			  Set_Text (Elapsed, "0 sec.");
			end if;
		when 'S' =>
			if Time_Flag then
			  Set_Text (Elapsed, Duration'Image(Clock - Time_Start)
                                & " sec.");
			  Time_Flag := False;
			end if;
		when others =>
		  null;
	 end case;
	elsif Time_Flag then
	  Set_Text (Elapsed, Integer'Image(Integer(Clock - Time_Start)) 
                & " sec.");
	  Delay 0.1;
	end if;
  end loop;
end GuiTimer;
Subject: Ada program
From: twbarrel-ga on 22 Jan 2003 23:36 PST
 
I got your timer program and I successfully compiled it; it looks
fine! I will be waiting for the rest of the program.

Regards,  
twbarrel
Subject: Ada program
From: twbarrel-ga on 29 Jan 2003 22:26 PST
 
Hi mathtalk,

Are you still there? I haven't heard from you for a while! Do you have
something for me?
Subject: Re: Ada program
From: mathtalk-ga on 30 Jan 2003 11:22 PST
 
Hi, twbarrel:

Yes, I have something for you.  I'm tweaking the redraw to make the
animation smoother.  I'll post my code as an answer tonight.

regards, mathtalk-ga
Subject: Ada program
From: twbarrel-ga on 01 Feb 2003 13:10 PST
 
Hi mathtalk, 

I received your work and I successfully compiled and run it. You did a
very good job! This is more or less what I had in mind and I think it
will provide me with a good basis to work on and learn more about
concurrency in Ada. I would also appreciate it if you sent me the more
commented version of the program that you mentioned.

Regards,   
twbarrel
Subject: Ada program
From: twbarrel-ga on 08 Feb 2003 23:12 PST
 
I suppose you didn't find the time to prepare and sent me the more
commented version of the program. It's OK with me, your work was great
anyway; so, since my question expires tomorrow, I'm going ahead to the
rating and closing procedure.

Thanks for your help,
twbarrel

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy