Cleared startup script from imprecations..
[toni-reis.git] / src / simulator-track.adb
blob7431e09d9db8582ea13f6ed9f9da6ff652281341
1 with System;
2 with Ada.Text_IO; use Ada.Text_IO;
3 with Simulator.Controller;
4 with Simulator.Car;
5 with Simulator;
6 with Ada.Calendar;
7 use Ada.Calendar;
8 with Simulator.TrackGui; use Simulator.TrackGui;
9 with Simulator.Statistics; use Simulator.Statistics;
10 with Unicode.CES;
11 --with Ada.Real_Time;
12 --use Ada.Real_Time;
13 --with Ada.Numerics.Generic_Elementary_Functions;
15 use Simulator.Car;
16 with Input_Sources.File; use Input_Sources.File;
17 with Sax.Readers; use Sax.Readers;
18 with DOM.Readers; use DOM.Readers;
19 with DOM.Core; use DOM.Core;
20 with DOM.Core.Documents; use DOM.Core.Documents;
21 with DOM.Core.Nodes; use DOM.Core.Nodes;
22 with DOM.Core.Attrs; use DOM.Core.Attrs;
23 with Ada.Text_IO; use Ada.Text_IO;
24 with Ada.Numerics;
25 with Ada.Numerics.Generic_Elementary_Functions;
27 package body Simulator.Track is
28 package Float_Functions is new Ada.Numerics.Generic_Elementary_Functions (
29 Float);
31 --STRUTTURE DATI
32 MaxMultiplicity : Positive := 3;
33 n_registered : CarId_T :=1;
34 BoxSpeedLimit : Float := 22.2; -- m/s
35 PitStopSector : Positive;
37 type PitStopRequest_T is array (CarId_T) of Boolean ;
39 type PitStopRecord_T is record
40 CarFuel : CarFuel_T;
41 Tires : Tires_T;
42 end record;
43 type PitStopData_T is array (CarId_T) of PitStopRecord_T;
44 PitStopData : PitStopData_T;
45 PitStopRequest: PitStopRequest_T;
46 type RetireRequest_T is array (CarId_T) of Boolean ;
47 RetireRequest: PitStopRequest_T;
48 --TODO confornto tra indici di prestazioni
49 --Semantica: ritorna 1 se il primo argomento è maggiore del secondo, 0 altrimenti
50 function GreaterThan(id1: Natural; id2: Natural) return Boolean is
51 begin
52 if (id2 = 0) then
53 return True;
54 end if;
55 if (id1 = 0) then
56 return False;
57 end if;
58 if ( id1 > id2) then
59 return True;
60 else return False;
61 end if;
62 end GreaterThan;
65 procedure CalculateDriveTime(my_properties : CarProperties_T; my_length: Natural; my_level : Integer; weather: Weather_T; isbox : Boolean; my_fuel: out Float; my_consumption : out Float; my_speed : in out Float; finish : out Duration) is
66 Vmax : Float := 100.0;
67 difficulty : Integer := my_level;
68 -- dati per calcolo velocità e accelerazione
69 a : Float := 9.25;
70 Smax : Float:= Float(my_length);
71 S : Float;
72 V0 : Float := my_speed;
73 Vfinal : Float;
74 t : Float := 0.0;
75 begin
76 if (difficulty < 0) then
77 difficulty := - difficulty;
78 end if;
79 a:= a - a*0.8* ( 0.3 * (1.0 - Float(my_properties.CarPerformance) / 100.0) + 0.15 * Float(my_properties.TiresConsumption ) + 0.4*( (Float(difficulty) / Float(MaxLevel))) + 0.15 * ( Float(my_properties.CarFuel)/ Float(MaxFuel)));
80 if (weather = Wet) then
81 -- se piove diminuiso l'accelerazione massima di 1/3;
82 a := a / 3.0;
83 if (my_properties.Tires = Slick) then
84 -- se piove e monto le gomme da asciutto diminuisco l'accelerazione di un ulteriore terzo
85 a := a/3.0;
86 end if;
87 else
88 -- Il tracciato è asciutto
89 if (my_properties.Tires = Rain) then
90 -- monto le gomme da bagnato
91 a := a/3.0;
92 end if;
93 end if;
95 Vmax := Vmax * Float( Float(1 + MaxLevel - difficulty)/ Float(1 + MaxLevel));
96 Vmax := Vmax - ( Vmax * (10.0 * Float( my_properties.TiresConsumption) )) / 100.0;
98 Put_Line("Difficoltà della curva "& difficulty'Img);
99 Put_Line("a ="& a'Img&"m/s");
100 --TODO calcolare la durata di percorrenza in base ai parametri di auto e a quelli di sector e lane
101 if (isbox) then
102 Vmax := Float(BoxSpeedLimit);
103 end if;
104 Put_Line("Vmax ="& Float(Vmax* 3.6)'Img&"km/h");
105 if (V0 < Vmax) then
107 -- Parte in moto uniformemente accelerato
108 S := ((Vmax*Vmax) - (V0*V0))/(2.0*a);
109 -- Put_Line("Spazio di accelerazione necessario per raggiungere Vmax ="&S'Img);
111 if (S <= Smax) then
112 -- pezzo in moto uniformemente accelerato
113 -- Put_Line("La velocità che raggiungo è maggiore di quella consentita");
114 Vfinal := Vmax;
115 t := (Vmax - V0)/a;
116 -- pezzo in moto rettilineo uniforme
117 t := t + ((Smax - S) / Vmax);
119 else
120 -- percorro tutto il tratto accelerando
121 Vfinal := Float_Functions.Sqrt((V0*V0) + (2.0*a*Smax));
122 t := (Vfinal - V0) / a;
123 end if;
124 else
125 --arrivo a velocità troppo alta. Freno in maniera istantanea e percorro il tratto
126 --a velocità costante
127 t := Smax / Vmax;
128 Vfinal := Vmax;
129 end if;
130 -- Vfinal è la velocità di uscita dal tratto, t il tempo impiegato
131 -- in ogni caso, aggiorno il consumo
132 my_consumption := Float(my_length) / 150000.0;
133 my_fuel := - Float(my_length) / 1700.0;
134 if (isbox) then
135 if ( (Float( PitStopData(my_properties.CarId).CarFuel ) / 12.0) < 5.5) then
136 t := t + 5.5;
138 else
139 t := t + Float(Float(PitStopData(my_properties.CarId).CarFuel) / 12.0);
140 end if;
141 my_consumption := Float(- my_properties.TiresConsumption);
142 my_fuel := Float(PitStopData(my_properties.CarId).CarFuel);
143 end if;
144 -- Put_Line("Vfinal ="& Float(Vfinal* 3.6)'Img&" km/h");
145 Put_Line("Durata tratto "&Duration(t)'Img);
146 my_speed := Vfinal;
148 finish := Duration(t);
149 -- Put_Line("Calcolo Tempo di risveglio previsto "& Seconds(finish)'Img);
150 end CalculateDriveTime;
152 --TODO driveLane aggiorna i carproperties in base al lane percorso
154 protected type Semaphore is
155 entry Wait; -- P
156 procedure Signal(start : Boolean); -- V
157 private
158 Started : Boolean := false;
159 end Semaphore;
161 protected body Semaphore is
162 entry Wait when Started = True is
163 begin
164 Put_Line("Auto partita");
165 end Wait;-- P
166 procedure Signal(start : Boolean) is
167 begin
168 Started := start;
169 end Signal;
171 end Semaphore;
174 protected type Lane_T(my_length: Natural; my_level : Integer;my_weather : Weather_T; my_isbox : Boolean) is
175 entry Demand( my_properties : CarProperties_T; my_time : out Time; my_duration : out Duration; my_fuel: out Float; my_consumption : out Float; my_speed : in out Float);
176 private
177 Weather : Weather_T :=my_weather;
178 IsBox : Boolean := my_isbox;
179 exit_time : Time := Clock;
180 exit_speed : Float := 0.0;
181 Level : Integer := my_level;
182 Length : Natural := my_length;
183 max : Natural;
184 maxId : CarId_T;
185 tot : Integer :=0;
186 entered : Integer :=0;
187 open : Boolean := False;
188 end Lane_T;
189 type Corsie_T is array (Positive range <>) of access Lane_T;
190 type Corsie_Array_T is array (Positive range <>) of access Corsie_T;
191 Corsie : access Corsie_Array_T;
192 type PitLane_T is array(CarId_T) of Semaphore;
193 PitLane: access PitLane_T := new PitLane_T;
195 type LaneCounter_T is array (Positive range<>) of Natural;
196 type CarLane_T is array (CarId_T) of Positive;
198 type ExitRecord is record
199 CarId : CarId_T;
200 CarTime : Time := Clock;
201 Arrived : Boolean := False;
202 end record;
203 type ExitArray_T is array (Integer range <>) of ExitRecord;
205 protected type Sector(my_sector_id: Natural; my_multiplicity : Natural; my_length: Natural; my_level : Integer; my_isbox: Boolean ) is
206 --sceglie la corsia da percorrere in base alla strategia, calcola il tempo di percorrenza e effettua la requeue su di essa
207 entry Enter( my_properties : CarProperties_T; my_time : out Time; my_duration : out Duration; my_fuel: out Float; my_consumption : out Float; my_speed : in out Float);
208 entry Release(my_properties : CarProperties_T);
209 entry BookExit(my_properties: CarProperties_T; my_time : Time);
212 private
213 entry ExitLane(my_properties : CarProperties_T);
214 ExitArray : ExitArray_T(1 .. 5*MaxMultiplicity);
215 ExitCount : Natural := 0;
216 LaneCounter : LaneCounter_T(1 .. my_multiplicity) ;
217 CarLane : CarLane_T;
218 Free : Natural := 5 * my_multiplicity;
219 MaxCars : Natural := 5 * my_multiplicity;
220 Sector_Id: Natural :=my_sector_id;
221 Length : Natural := my_length;
222 Multiplicity: Natural := my_multiplicity;
223 Level : Integer := my_level;
224 IsBox : Boolean := my_isbox;
225 -- per la exit
226 Changed : Boolean := False;
227 max : Natural;
228 tot : Integer :=0;
229 entered : Integer :=0;
230 open : Boolean := False;
231 end Sector;
233 protected body Sector is
234 entry BookExit(my_properties: CarProperties_T; my_time : Time) when True is
235 --Temp : ExitRecord;
236 --temp : Integer;
237 begin
238 if (ExitCount <= 0) then
239 ExitArray(1) := (my_properties.CarId, my_time, False);
240 Put_Line("DEBUG : Book: accodo Auto "&my_properties.CarId'Img&" in posizione 1 perchè la lista uscite è vuota");
241 ExitCount := ExitCount + 1;
242 else
243 for Index in reverse 0 .. ExitCount loop
244 -- Put_Line("Debug: Book: loop esterno indice "& Index'Img);
245 if(Index = 0 ) then
246 Put_Line("DEBUG : Book: Inerisco l'auto in posizione 1");
247 for I in reverse (Index + 1) .. ExitCount loop
248 -- Put_Line("Debug: Book: loop interno");
249 ExitArray(I+1) := ExitArray(I);
250 end loop;
251 ExitArray(Index + 1) := (my_properties.CarId, my_time, False);
252 -- temp := Index + 1;
253 -- Put_Line("DEBUG : Book: accodo Auto "&my_properties.CarId'Img&" in posizione "&temp'Img);
254 ExitCount := ExitCount + 1;
255 exit;
256 else if(ExitArray(Index).CarTime < my_time) then
257 Put_Line("DEBUG : Book: Inerisco l'auto in posizione "&Integer(Index+1)'Img);
258 -- Put_Line("Debug: Book: dentro if");
259 for I in reverse (Index + 1) .. ExitCount loop
260 -- Put_Line("Debug: Book: loop interno");
261 ExitArray(I+1) := ExitArray(I);
262 end loop;
263 ExitArray(Index + 1) := (my_properties.CarId, my_time, False);
264 -- temp := Index + 1;
265 -- Put_Line("DEBUG : Book: accodo Auto "&my_properties.CarId'Img&" in posizione "&temp'Img);
266 ExitCount := ExitCount + 1;
267 exit;
268 end if;
269 end if;
270 end loop;
271 end if;
272 -- Put_Line("Non è stato preso ne l'if ne l'else");
273 end BookExit;
275 entry Enter( my_properties : CarProperties_T; my_time : out Time; my_duration : out Duration; my_fuel: out Float; my_consumption : out Float; my_speed : in out Float) when Free > 0 is
276 n_lane : Natural;
277 begin
278 -- Put_Line("DEBUG: entrato nella procedura Enter di Sector");
279 --Prima auto a entrare nel tracciato azzera i campi dato
280 if (MaxCars = Free) then
281 for Index in 1 .. Multiplicity loop
282 LaneCounter(Index) := 0;
283 end loop;
284 end if;
285 -- decido la corsia da percorrere. Trovo quella meno trafficata
287 n_lane := 1;
288 for Index in 1.. Multiplicity loop
289 if (LaneCounter(Index) < LaneCounter(n_lane)) then
290 n_lane := Index;
291 end if;
292 -- n_lane è l'id del lane meno trafficato
293 end loop;
294 LaneCounter(n_lane) := LaneCounter(n_lane) + 1;
295 Free := Free - 1;
296 CarLane(my_properties.CarId) := n_lane;
298 Print("Auto "&my_properties.CarId'Img&" percorre Settore: "&Sector_Id'Img&", Tratto:"&n_lane'Img&".");
299 requeue Corsie(Sector_Id)(n_lane).Demand with abort;
300 --Corsie(sector_number,n_lane).Demand(my_id);
301 end Enter;
303 entry Release(my_properties : CarProperties_T) when Free < MaxCars is
304 a : Integer := 0;
305 begin
306 Put_Line("Debug: Relase invocata");
307 if (ExitCount = 0) then
308 Put_Line("Bug del software: invocata Relase senza aver invocato BookExit");
309 end if;
310 if (ExitArray(1).CarId = my_properties.CarId) then
311 Put_Line("DEBUG: relase : l'auto è in prima posizione : sposto le altre");
312 for Index in 2 .. ExitCount loop
313 Put_Line("DEBUG: relase : sposto l'auto che era in posizione "& Index'Img);
314 ExitArray(Index -1) := ExitArray(Index);
315 end loop;
316 LaneCounter(CarLane(my_properties.CarId)) := LaneCounter(CarLane(my_properties.CarId)) -1;
317 Free := Free + 1;
318 ExitCount := ExitCount -1;
319 if (ExitCount > 0) then
320 if(ExitArray(1).Arrived = True) then
321 Changed := True;
322 else
323 Changed := False;
324 end if;
325 end if;
326 Put_Line("Debug: Relase terminata");
327 else
328 for Index in 1 .. ExitCount loop
329 if (ExitArray(Index).CarId = my_properties.CarId) then
330 ExitArray(Index).Arrived := True;
331 Print("DEBUG: Auto "&my_properties.CarId'Img&" ha superato dove non doveva..accodo in ExitLane");
332 Changed := False;
333 requeue ExitLane;
334 end if;
335 end loop;
337 end if;
338 end Release;
340 entry ExitLane(my_properties : CarProperties_T) when Changed = True is
341 begin
343 if (ExitArray(1).CarId = my_properties.CarId) then
344 -- Put_Line("DEBUG: Auto "&my_properties.CarId'Img&" in uscita da ExitLane");
345 for Index in 2 .. ExitCount loop
346 ExitArray(Index -1) := ExitArray(Index);
347 end loop;
348 LaneCounter(CarLane(my_properties.CarId)) := LaneCounter(CarLane(my_properties.CarId)) -1;
349 Free := Free + 1;
350 ExitCount := ExitCount -1;
351 if (ExitArray(1).Arrived = True) then
352 Changed := True;
353 else
354 Changed := False;
355 end if;
356 else
357 requeue ExitLane;
358 end if;
359 end ExitLane;
361 end Sector;
363 protected body Lane_T is
364 entry Demand(my_properties: CarProperties_T; my_time : out Time; my_duration : out Duration; my_fuel: out Float; my_consumption : out Float; my_speed : in out Float) when True is
365 start : Time;
366 t : Duration;
367 begin
368 start := Clock;
369 CalculateDriveTime(my_properties , my_length ,my_level ,Weather,IsBox ,my_fuel, my_consumption,my_speed, t );
370 my_time := start + t;
372 if (my_time > exit_time) then
373 exit_time := my_time;
374 exit_speed := my_speed;
375 elsif (my_time < exit_time) then
376 -- se l'auto è dietro, esce al tempo dell'auto che precede e al più alla sua stessa velocità
377 my_time := exit_time + 0.001;
378 if (my_speed > exit_speed) then
379 my_speed := exit_speed;
380 end if;
381 end if;
382 my_duration := my_time - start;
383 end Demand;
387 end Lane_T;
388 ----------------STRUTTURE DATI DI TRACK
389 --codice di test. Operazioni svolte dal lettore del file di configurazione xml
391 type Sector_Array_T is array (Positive range <>) of access Sector;
392 Sectors : access Sector_Array_T;
393 BoxSector : access Sector;
396 -- inserisce la macchina nel circuito, fornisce in ingresso le
397 -- caratteristiche iniziali dell'auto. Quando questo metodo termina vuol
398 -- dire che la macchina ha finito la gara (conclusa o per ritiro)
400 procedure PutOnPitLane(my_CarProperties : CarProperties_T) is
401 -- Temp :CarWeight_T;
402 my_time : Time;
403 my_fuel : Float;
404 my_consumption : Float;
405 CarProperties : CarProperties_T := my_CarProperties;
406 Speed : Float := 0.0;
407 temp : Float;
408 my_duration : Duration;
409 lap : Integer :=0;
410 n: Integer;
411 data : RankingArray_T;
412 begin
414 Print("Car " & CarId_T'Image(CarProperties.CarId) & " registered");
416 --Auto si mette nelle linee di partenza. Quando tornerà da questa ciamata, la corsa per l'auto sarà iniziata
417 PitLane(CarProperties.CarId).Wait;
418 --inizio la corsa iterando tra i vari Sector
419 while (RetireRequest(CarProperties.CarId) = False) loop
420 lap := lap + 1;
422 for Index in 1 .. Sectors'Length loop
424 if (RetireRequest(CarProperties.CarId) = True) then
425 exit;
426 end if;
427 if (Index = PitStopSector and PitStopRequest(CarProperties.CarId) = True) then
428 -- TODO effettua il pit stop
429 BoxSector.Enter(CarProperties, my_time,my_duration,my_fuel, my_consumption, Speed);
430 -- Put_Line("Auto ferma ai box fino al tempo"&CarProperties.CarId'Img&" :"& Seconds(my_time)'Img);
431 BoxSector.BookExit(CarProperties, my_time);
432 --aggiorno carproperties
433 temp:= Float(CarProperties.CarFuel) + my_fuel;
435 if (temp < Float(CarFuel_T'First)) then
436 CarProperties.CarFuel := 0.0;
437 elsif (temp > Float(CarFuel_T'Last)) then
438 CarProperties.CarFuel := CarFuel_T'Last;
439 else CarProperties.CarFuel := CarFuel_T(temp);
440 end if;
441 temp := Float(CarProperties.TiresConsumption) + my_consumption;
442 if (temp < Float(TiresConsumption_T'First)) then
443 CarProperties.TiresConsumption:=TiresConsumption_T'First ;
444 elsif (temp > Float(TiresConsumption_T'Last)) then
445 CarProperties.TiresConsumption := TiresConsumption_T'Last;
446 else CarProperties.TiresConsumption := TiresConsumption_T(temp);
447 end if;
448 CarProperties.Tires := PitStopData(CarProperties.CarId).Tires;
449 PitStopRequest(CarProperties.CarId) := False;
450 -- CarProperties.CarFuel := CarProperties.CarFuel - 50;
451 Simulator.Controller.GetCar(CarProperties.CarId).UpdateProperties(CarId => CarProperties.CarId,
452 Tires => CarProperties.Tires,
453 TiresConsumption => CarProperties.TiresConsumption,
454 CarFuel => CarProperties.CarFuel,
455 CarPerformance => CarProperties.CarPerformance,
456 Sector => 500,
457 Speed => Speed,
458 SectorDuration => my_duration);
459 -- Put_Line("!!! se stampa prima questo dei parametri auto aggiornati la chiamata è stata asincrona!!!");
460 delay until my_time;
461 BoxSector.Release(CarProperties);
462 Put_Line("Auto "&CarProperties.CarId'Img&" uscita dalla corsia dei Box");
463 null;
464 else
465 Sectors(Index).Enter(CarProperties, my_time, my_duration, my_fuel, my_consumption, Speed);
466 -- Put_Line("Velocità dell'auto "&CarProperties.CarId'Img&" è di "&Float(Speed * 3.6)'Img&" Km/h");
467 Put_Line("Tempo di risveglio previsto per l'auto"&CarProperties.CarId'Img&" :"& Seconds(my_time)'Img);
468 Sectors(Index).BookExit(CarProperties, my_time);
469 temp:= Float(CarProperties.CarFuel) + my_fuel;
471 if (temp < Float(CarFuel_T'First)) then
472 CarProperties.CarFuel := 0.0;
473 elsif (temp > Float(CarFuel_T'Last)) then
474 CarProperties.CarFuel := CarFuel_T'Last;
475 else CarProperties.CarFuel := CarFuel_T(temp);
476 end if;
477 temp := Float(CarProperties.TiresConsumption) + my_consumption;
478 if (temp < Float(TiresConsumption_T'First)) then
479 CarProperties.TiresConsumption:=TiresConsumption_T'First ;
480 elsif (temp > Float(TiresConsumption_T'Last)) then
481 CarProperties.TiresConsumption := TiresConsumption_T'Last;
482 else CarProperties.TiresConsumption := TiresConsumption_T(temp);
483 end if;
487 Simulator.Controller.GetCar(CarProperties.CarId).UpdateProperties(CarId => CarProperties.CarId,
488 Tires => CarProperties.Tires,
489 TiresConsumption => CarProperties.TiresConsumption,
490 CarFuel => CarProperties.CarFuel,
491 CarPerformance => CarProperties.CarPerformance,
492 Sector => Index,
493 Speed => Speed,
494 SectorDuration => my_duration);
495 -- Put_Line("!!! se stampa prima questo dei parametri auto aggiornati la chiamata è stata asincrona!!!");
496 --DEBUG
497 Simulator.Statistics.Ranking.Insert((CarProperties.CarId, lap,Index, my_duration, Speed));
499 delay until my_time;
500 Simulator.Statistics.Ranking.GetRanking(length => n,
501 my_ranking => data);
502 Put_Line("DEBUG RANKING: L'auto in prima posizione è la numero"&data(1).Id'Img&" auto totali " &n'Img&" chiamante auto"& CarProperties.CarId'Img);
503 Sectors(Index).Release(CarProperties);
504 if (CarProperties.CarFuel <= 0.0 or CarProperties.TiresConsumption >= 1.0) then
505 Put_Line("L'auto "&CarProperties.CarId'Img&" non è in grado di proseguire.");
506 RetireRequest(CarProperties.CarId) := True;
507 end if;
508 end if;
509 end loop;
511 end loop;
513 Put_Line("DEBUG: PutOnPitLane è ritornato dalla simulazione.. Auto "&CarProperties.CarId'Img&" ha terminato la corsa");
516 end PutOnPitLane;
519 procedure StartRace(n_cars : CarId_T) is
520 begin
521 -- inizializzo PitStopRequest.
522 for Index in 1 .. n_cars loop
523 PitStopRequest(Index) := False;
524 end loop;
526 -- sveglio le auto in attesa su PitLane
527 Put_Line("3...2...1... Go !!!");
528 for Index in 1 .. n_cars loop
529 PitLane(Index).Signal(True);
530 delay 0.001;
531 end loop;
533 Put_Line("DEBUG: Tutte le auto sono partite");
534 delay 10.0;
535 end StartRace;
537 -- Comunica di ritirare l'auto con id = CarId dal circuito
538 procedure Kill(CarId: in CarId_T) is
539 begin
540 Put_Line("Killing car "&CarId'Img&" ...");
541 RetireRequest(CarId) := True;
542 end Kill;
544 -- metodo di richiesta fermata ai box
545 -- l'invocazione di questo metodo porta il circuito a far fare una sosta
546 -- all'auto "CarId" appena possibile.
547 procedure CallForPitStop(CarId: in CarId_T; CarFuel :CarFuel_T; Tires :Tires_T) is
548 begin
549 PitStopRequest(CarId) := True;
550 PitStopData(CarId) := (CarFuel, Tires);
551 Put_Line("Car "&CarId_T'Image(CarId)&" requires Pit Stop.");
552 end CallForPitStop;
555 procedure ReadTrackConf(confFile : String) is
556 Input : File_Input;
557 Reader : Tree_Reader;
558 Doc : Document;
559 List : Node_List;
560 TrackList : Node_List;
561 BoxList : Node_List;
562 Settore : Node;
563 A : Attr;
564 -- C : Node;
565 Livello : Integer;
566 Lunghezza : Natural;
567 Multiplicity : Positive;
568 Weather : String := "adsfasfasfsdggsddg";
569 w : Weather_T;
570 begin
571 Put_Line("DEBUG: Leggo configurazione da file xml");
572 Set_Public_Id (Input, "input");
573 --da passare come parametro il percorso
574 Open (confFile, Input);
575 Set_Feature (Reader, Validation_Feature, False);
576 Set_Feature (Reader, Namespace_Feature, False);
577 Parse (Reader, Input);
578 Close (Input);
580 Doc := Get_Tree (Reader);
581 TrackList := Get_Elements_By_Tag_Name(Doc, "track");
582 if (Length(TrackList) /= 1) then
583 Put_Line("Errore nel file XML. Ogni file deve contenere esattamente 1 track");
585 end if;
586 List := Get_Elements_By_Tag_Name(Doc, "sector");
587 Put_Line("DEBUG TRACK:ci sono "&Length(List)'Img&" settori");
588 Sectors := new Sector_Array_T(1 .. Length(List));
589 Corsie:= new Corsie_Array_T (1 .. Length(List) + 1); -- + 1, 1 .. MaxId);
590 for Index in 1 .. Length (List) loop
591 Settore := Item (List, Index - 1);
593 A := Get_Named_Item (Attributes (Settore), "level");
594 Livello := Integer'Value (Node_Value(A)) ;
595 A := Get_Named_Item (Attributes (Settore), "length");
596 Lunghezza := Natural'Value (Node_Value(A));
597 A := Get_Named_Item (Attributes (Settore), "multiplicity");
598 Multiplicity := Positive'Value (Node_Value(A));
599 -- Creo l'array Corsie_T relativo al settore
600 Corsie(Index) := new Corsie_T( 1 .. Multiplicity);
601 --weather
602 A := Get_Named_Item (Attributes (Settore), "weather");
603 if (Specified(A)) then
604 if( Node_Value(A) = "wet") then
605 Put_Line("wet sector");
606 w := Wet;
607 else
608 Put_Line("dry sector");
609 w := Dry;
610 end if;
611 end if;
612 --Creo il settore e i lane
613 Sectors(Index) := new Sector(Index,Multiplicity,Lunghezza,Livello,False);
614 for i in 1 .. Multiplicity loop
615 Corsie(Index)(i) := new Lane_T(Lunghezza,Livello,w,False);
616 end loop;
617 Put_Line ("creato settore "& Index'Img);
619 BoxList := Child_Nodes( Settore);
620 if (Length(BoxList) /= 0) then
621 Put_Line("Inizializzo box");
622 PitStopSector := Index;
624 A := Get_Named_Item (Attributes (Item(BoxList,1)), "length");
625 Lunghezza := Natural'Value (Node_Value(A));
626 BoxSector := new Sector(Length( List) + 1, MaxId, Lunghezza, Livello, True);
627 -- Creo l'array Corsie_T relativo ai box .. Moltiplicità MaxId
628 Corsie(Length(List) + 1) := new Corsie_T( 1 .. MaxId);
629 for i in 1 .. MaxId loop
630 Corsie(Length( List ) + 1)(i) := new Lane_T(Lunghezza,Livello,w,True);
631 end loop;
632 Put_Line("Trovati box nel sttore "& PitStopSector'Img&" di lunghezza "&Lunghezza'Img);
633 end if;
635 end loop;
637 Free (List);
639 Free (Reader);
640 end ReadTrackConf;
643 end Simulator.Track;