comp.lang.ada
 help / color / mirror / Atom feed
* Help with tasks
@ 2016-05-07 14:53 danifreecs
  2016-05-07 17:43 ` Dennis Lee Bieber
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: danifreecs @ 2016-05-07 14:53 UTC (permalink / raw)


Hi! i'm a student, and got stucked with an ada home project, and i need a lot of help sadly, i can't even ask a specific question, because i have a lot, but it's about tasks and protected objects.

The project: There's a garden with 10 plots, and a locust gets in 1 of the plots, so the gardener wants to kill it with poison. The locust randomly changes plots in a time, and the gardener can spray 1 plot/day (either randomly), which gets clean from poison after that day has passed. The locust dies if it jumps into the sprayed plot, or is in it already. The simulation goes until the locust dies. For the random number generation and the console output use a common protected object. (Monitor)

I've tried all day to implement it with no luck, and feel that i need a complete solution to analyze and learn from its code lines, but any help is appreciated.
Thanks!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Help with tasks
  2016-05-07 14:53 Help with tasks danifreecs
@ 2016-05-07 17:43 ` Dennis Lee Bieber
  2016-05-07 18:50 ` Anh Vo
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Dennis Lee Bieber @ 2016-05-07 17:43 UTC (permalink / raw)


On Sat, 7 May 2016 07:53:28 -0700 (PDT), danifreecs@gmail.com declaimed the
following:

>
>The project: There's a garden with 10 plots, and a locust gets in 1 of the plots, so the gardener wants to kill it with poison. The locust randomly changes plots in a time, and the gardener can spray 1 plot/day (either randomly), which gets clean from poison after that day has passed. The locust dies if it jumps into the sprayed plot, or is in it already. The simulation goes until the locust dies. For the random number generation and the console output use a common protected object. (Monitor)
>
	Well, strictly speaking -- protected objects are not to perform
blocking operations; console output is a potentially blocking operation.

	Now -- using the protected object to hold the state of the plots, with
a separate display task (could be the main program) using a locking access
to retrieve the entire state at once for output/display might work...

	Question: "locust randomly changes plots in a time" is unclear. The
gardener is a one per simulated day, but what is the simulation time frame
for the locust? Also one move per day?

	Is that the actual text of the assignment, or did you paraphrase it?

	If both are one move per day, the whole idea of using tasks and
protected objects becomes moot. Since the poison clears out after a day,
one can assume every day starts with a clean slate. The only way to end is
for the gardener and the locust to move to the same plot (doesn't matter
who moves first since they effectively stay there until the next day). That
is: 

	loop
		gardener: = random(10)
		locust := random(10)
		display "gardener is in plot" gardener
		display "locust is in plot" locust
		exit if gardener = locust
		display "locust survived the day"
		display " "
	end loop
	display "locust is dead"


	If the locust can move multiple times in a day, then you need some sort
of simulation clock (say the locust moves every 6 hours -- the simulation
clock would be modulo 4 [0, 1, 2, 3, 0 ...], and the gardener moves only on
[0] which would also be when the plot(s) clear of the previous poison)

	Still doesn't really need tasks...

	clock := 0
	loop
		if clock = 0 then
			gardener = random(10)
		end if
		locust := random(10)

		-- since the only poisoned plot is the one the gardener is in

		display "gardener is in plot" gardener
		display "locust is in plot" locust
		exit if gardener = locust
		display "locust survived the move"
		display " "
	end loop
	display "locust is dead"
		

	Now, if the poison actually lasts the day of the spraying AND the next
day... Then you need some structure to represent the 10 plots and if they
are poisoned (a value in the range of 0..2 would suffice; when a plot is
sprayed, that entry is set to 2; on each day [still need a simulation
clock] non-zero entries are decremented by 1. If the locust enters a plot
with a non-zero entry, it dies.



	As for a working example? Sorry -- most of the ethical folks won't do
one's homework for them. Show us what you've written, tell us what you
expected it to do, and what it really is doing, and we'll tender
explanations of  what may be in error.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Help with tasks
  2016-05-07 14:53 Help with tasks danifreecs
  2016-05-07 17:43 ` Dennis Lee Bieber
@ 2016-05-07 18:50 ` Anh Vo
  2016-05-07 20:44 ` Jeffrey R. Carter
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Anh Vo @ 2016-05-07 18:50 UTC (permalink / raw)


On Saturday, May 7, 2016 at 7:53:29 AM UTC-7, danif...@gmail.com wrote:
> Hi! i'm a student, and got stucked with an ada home project, and i need a lot of help sadly, i can't even ask a specific question, because i have a lot, but it's about tasks and protected objects.
> 
> The project: There's a garden with 10 plots, and a locust gets in 1 of the plots, so the gardener wants to kill it with poison. The locust randomly changes plots in a time, and the gardener can spray 1 plot/day (either randomly), which gets clean from poison after that day has passed. The locust dies if it jumps into the sprayed plot, or is in it already. The simulation goes until the locust dies. For the random number generation and the console output use a common protected object. (Monitor)
> 
> I've tried all day to implement it with no luck, and feel that i need a complete solution to analyze and learn from its code lines, but any help is appreciated.
> Thanks!

I am glad with your honesty.

I suggest to create three tasks, gardener task, locust task and monitor task. In addition, these tasks will operate on an array of data structure (you should design it) through protected object. Task monitor will monitor to see if the gardener task and locust task operate at the same plot at the same time. If it does, mark the plot as completed and check for the remaining plots. When all 10 plots are reached, the work is done. 

Anh Vo


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Help with tasks
  2016-05-07 14:53 Help with tasks danifreecs
  2016-05-07 17:43 ` Dennis Lee Bieber
  2016-05-07 18:50 ` Anh Vo
@ 2016-05-07 20:44 ` Jeffrey R. Carter
  2016-05-08 12:23 ` danifreecs
  2016-05-09  2:24 ` rieachus
  4 siblings, 0 replies; 7+ messages in thread
From: Jeffrey R. Carter @ 2016-05-07 20:44 UTC (permalink / raw)


On 05/07/2016 07:53 AM, danifreecs@gmail.com wrote:
> Hi! i'm a student, and got stucked with an ada home project, and i need a lot
> of help sadly, i can't even ask a specific question, because i have a lot,
> but it's about tasks and protected objects.

It would be nice to have some idea of how much training/experience you have with 
Ada, and how much with other programming languages. I fear that if your attitude 
is that you "got stucked [sic]" with an Ada project, you'll never develop a 
reasonable solution. Ada is the language of choice for knowledgeable software 
engineers, especially for safety- or security-critical systems, and has features 
that make solving problems like yours simple compared to every other mainstream 
language. If you welcome the opportunity to find out why Ada is the language of 
choice, what those features are, and how they work, it will make you a better 
software engineer, even if you end up using another language.

> The project: There's a garden with 10 plots, and a locust gets in 1 of the
> plots, so the gardener wants to kill it with poison. The locust randomly
> changes plots in a time, and the gardener can spray 1 plot/day (either
> randomly), which gets clean from poison after that day has passed. The locust
> dies if it jumps into the sprayed plot, or is in it already. The simulation
> goes until the locust dies. For the random number generation and the console
> output use a common protected object. (Monitor)

So you have a Gardener that does

Until_Locust_Dead : loop
    wait until locust is dead or next spray time

    if locust is dead then
       exit Until_Locust_Dead;
    end if;

    update next spray time
    Plot := Random.Plot;
    display "Gardener sprays plot " & Image (Plot)

    if Plot = locust plot then
       set Locust as dead
       display "Locust is dead"

       exit Until_Locust_Dead;
    end if;
end loop Until_Locust_Dead;

and a Locust that does

Until_I_Am_Dead : loop
    wait until locust is dead or next jump time

    if locust is dead then
       exit Until_I_Am_Dead;
    end if;

    update next jump time
    Plot := Random.Plot;
    display "Locust in plot " & Image (Plot)

    if Plot = poisoned plot then
       set Locust as dead
       display "Locust is dead"

       exit Until_I_Am_Dead;
    end if;
end loop Until_I_Am_Dead;

Each of these operates independently, so would be a task.

Ada has features for waiting for either of an event or a time (see select 
statements), so you'll want to read about those. How they work will help you 
decide how to structure your solution.

I'd think in terms of a protected object for the plot states, allowing the 
Gardener to set which plot is poisoned and to find out which plot the Locust is 
in, and the Locust to set which plot it is in and to find out which plot is 
poisoned.

Another protected object would control the Locust's live/dead state, allowing 
either actor to set the state to dead, and allowing both to wait until the state 
is dead.

Finally, I'd have a task for the display operations, since I/O is not task safe, 
you can't do I/O from a protected operation, and you don't need to queue messages.

HTH

-- 
Jeff Carter
"I'm particularly glad that these lovely children were
here today to hear that speech. Not only was it authentic
frontier gibberish, it expressed a courage little seen
in this day and age."
Blazing Saddles
88

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Help with tasks
  2016-05-07 14:53 Help with tasks danifreecs
                   ` (2 preceding siblings ...)
  2016-05-07 20:44 ` Jeffrey R. Carter
@ 2016-05-08 12:23 ` danifreecs
  2016-05-08 18:28   ` Anh Vo
  2016-05-09  2:24 ` rieachus
  4 siblings, 1 reply; 7+ messages in thread
From: danifreecs @ 2016-05-08 12:23 UTC (permalink / raw)


Thank you both, i've managed to do it finally! I'm beginner in programming, and most comfortable with c++, but i get used to like ada either (started to learn 2,5 months ago), it seems a very powerful and great language, and my attitude is good, i enjoy overcoming problems, but i lacked the time for it now that's why asked other ppl to help. Take care, and thank you again!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Help with tasks
  2016-05-08 12:23 ` danifreecs
@ 2016-05-08 18:28   ` Anh Vo
  0 siblings, 0 replies; 7+ messages in thread
From: Anh Vo @ 2016-05-08 18:28 UTC (permalink / raw)


On Sunday, May 8, 2016 at 5:23:06 AM UTC-7, danif...@gmail.com wrote:
> Thank you both, i've managed to do it finally! I'm beginner in programming, and most comfortable with c++, but i get used to like ada either (started to learn 2,5 months ago), it seems a very powerful and great language, and my attitude is good, i enjoy overcoming problems, but i lacked the time for it now that's why asked other ppl to help. Take care, and thank you again!

I am very pleased that you have completed your task that quick. Just like you, Ada task feature is one of my favorite features. In addition, with Ada 2012, preconditions and postconditions are the top ones.

Anh Vo


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Help with tasks
  2016-05-07 14:53 Help with tasks danifreecs
                   ` (3 preceding siblings ...)
  2016-05-08 12:23 ` danifreecs
@ 2016-05-09  2:24 ` rieachus
  4 siblings, 0 replies; 7+ messages in thread
From: rieachus @ 2016-05-09  2:24 UTC (permalink / raw)


On Saturday, May 7, 2016 at 10:53:29 AM UTC-4, danif...@gmail.com wrote:
> Hi! i'm a student, and got stucked with an ada home project, and i need a lot of help sadly, i can't even ask a specific question, because i have a lot, but it's about tasks and protected objects.

In general in Ada think about modeling the problem space, then implement the solution in this model. In this case you have two active entities, the farmer and the locust.  The garden plots, being passive entities can be modeled in lots of ways.  I don't know why the instructions require a common protected object for the RNG and the output, but it is no more or less difficult than having separate protected objects.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-05-09  2:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-07 14:53 Help with tasks danifreecs
2016-05-07 17:43 ` Dennis Lee Bieber
2016-05-07 18:50 ` Anh Vo
2016-05-07 20:44 ` Jeffrey R. Carter
2016-05-08 12:23 ` danifreecs
2016-05-08 18:28   ` Anh Vo
2016-05-09  2:24 ` rieachus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox