11package org .acme .foodpackaging .bootstrap ;
22
3+ import io .quarkus .runtime .StartupEvent ;
4+ import jakarta .enterprise .context .ApplicationScoped ;
5+ import jakarta .enterprise .event .Observes ;
6+ import jakarta .transaction .Transactional ;
7+ import org .acme .foodpackaging .domain .Job ;
8+ import org .acme .foodpackaging .domain .Line ;
9+ import org .acme .foodpackaging .domain .Operator ;
10+ import org .acme .foodpackaging .domain .PackagingSchedule ;
11+ import org .acme .foodpackaging .domain .Product ;
12+ import org .acme .foodpackaging .domain .WorkCalendar ;
13+ import org .acme .foodpackaging .persistence .PackagingScheduleRepository ;
14+ import org .eclipse .microprofile .config .inject .ConfigProperty ;
15+
316import java .time .DayOfWeek ;
417import java .time .Duration ;
518import java .time .LocalDate ;
1023import java .util .Comparator ;
1124import java .util .HashMap ;
1225import java .util .List ;
13- import java .util .Map ;
1426import java .util .Random ;
1527import java .util .Set ;
1628
17- import io .quarkus .runtime .StartupEvent ;
18- import jakarta .enterprise .context .ApplicationScoped ;
19- import jakarta .enterprise .event .Observes ;
20- import jakarta .inject .Inject ;
21- import jakarta .transaction .Transactional ;
22- import org .acme .foodpackaging .domain .Job ;
23- import org .acme .foodpackaging .domain .Line ;
24- import org .acme .foodpackaging .domain .PackagingSchedule ;
25- import org .acme .foodpackaging .domain .Product ;
26- import org .acme .foodpackaging .domain .WorkCalendar ;
27- import org .acme .foodpackaging .persistence .PackagingScheduleRepository ;
28- import org .eclipse .microprofile .config .inject .ConfigProperty ;
29-
3029@ ApplicationScoped
3130public class DemoDataGenerator {
3231
33- @ Inject
34- PackagingScheduleRepository repository ;
32+ private final PackagingScheduleRepository repository ;
3533
3634 @ ConfigProperty (name = "demo-data.line-count" , defaultValue = "5" )
3735 int lineCount ;
3836 @ ConfigProperty (name = "demo-data.job-count" , defaultValue = "100" )
3937 int jobCount ;
4038
39+ public DemoDataGenerator (PackagingScheduleRepository repository ) {
40+ this .repository = repository ;
41+ }
42+
4143 @ Transactional
4244 public void generateDemoData (@ Observes StartupEvent startupEvent ) {
43- int noCleaningMinutes = 10 ;
44- int cleaningMinutesMinimum = 30 ;
45- int cleaningMinutesMaximum = 60 ;
46- int jobDurationMinutesMinimum = 120 ;
47- int jobDurationMinutesMaximum = 300 ;
48- int averageCleaningAndJobDurationMinutes =
45+ var noCleaningMinutes = 10 ;
46+ var cleaningMinutesMinimum = 30 ;
47+ var cleaningMinutesMaximum = 60 ;
48+ var jobDurationMinutesMinimum = 120 ;
49+ var jobDurationMinutesMaximum = 300 ;
50+ var averageCleaningAndJobDurationMinutes =
4951 (2 * noCleaningMinutes + cleaningMinutesMinimum + cleaningMinutesMaximum ) / 4
50- + (jobDurationMinutesMinimum + jobDurationMinutesMaximum ) / 2 ;
52+ + (jobDurationMinutesMinimum + jobDurationMinutesMaximum ) / 2 ;
5153
52- final LocalDate START_DATE = LocalDate .now ().with (TemporalAdjusters .nextOrSame (DayOfWeek .MONDAY ));
53- final LocalDateTime START_DATE_TIME = LocalDateTime .of (START_DATE , LocalTime .MIDNIGHT );
54- final LocalDate END_DATE = START_DATE .plusWeeks (2 );
55- final LocalDateTime END_DATE_TIME = LocalDateTime .of (END_DATE , LocalTime .MIDNIGHT );
54+ final var START_DATE = LocalDate .now ().with (TemporalAdjusters .nextOrSame (DayOfWeek .MONDAY ));
55+ final var START_DATE_TIME = LocalDateTime .of (START_DATE , LocalTime .MIDNIGHT );
56+ final var END_DATE = START_DATE .plusWeeks (2 );
5657
57- Random random = new Random (37 );
58- PackagingSchedule solution = new PackagingSchedule ();
58+ var random = new Random (37 );
59+ var solution = new PackagingSchedule ();
5960
6061 solution .setWorkCalendar (new WorkCalendar (START_DATE , END_DATE ));
6162
62- Map < Product , Set < String >> ingredientMap = new HashMap <>(INGREDIENT_LIST .size () * PRODUCT_VARIATION_LIST .size () * 3 );
63- long productId = 0 ;
64- for (int i = 0 ; i < INGREDIENT_LIST .size (); i ++) {
65- String ingredient = INGREDIENT_LIST .get (i );
66- int r = random .nextInt (INGREDIENT_LIST .size () - 4 );
67- String ingredientA = INGREDIENT_LIST .get ((i + r + 1 ) % INGREDIENT_LIST .size ());
68- String ingredientB = INGREDIENT_LIST .get ((i + r + 2 ) % INGREDIENT_LIST .size ());
69- String ingredientC = INGREDIENT_LIST .get ((i + r + 3 ) % INGREDIENT_LIST .size ());
70- for (String productVariation : PRODUCT_VARIATION_LIST ) {
71- ingredientMap .put (new Product (Long .toString (productId ++), ingredient + " " + productVariation ), Set .of (ingredient ));
63+ var ingredientMap = new HashMap <Product , Set < String > >(INGREDIENT_LIST .size () * PRODUCT_VARIATION_LIST .size () * 3 );
64+ var productId = 0L ;
65+ for (var i = 0 ; i < INGREDIENT_LIST .size (); i ++) {
66+ var ingredient = INGREDIENT_LIST .get (i );
67+ var r = random .nextInt (INGREDIENT_LIST .size () - 4 );
68+ var ingredientA = INGREDIENT_LIST .get ((i + r + 1 ) % INGREDIENT_LIST .size ());
69+ var ingredientB = INGREDIENT_LIST .get ((i + r + 2 ) % INGREDIENT_LIST .size ());
70+ var ingredientC = INGREDIENT_LIST .get ((i + r + 3 ) % INGREDIENT_LIST .size ());
71+ for (var productVariation : PRODUCT_VARIATION_LIST ) {
72+ ingredientMap .put (new Product (Long .toString (productId ++), ingredient + " " + productVariation ), java . util . Set .of (ingredient ));
7273 }
73- ingredientMap .put (new Product (Long .toString (productId ++), ingredient + " and " + ingredientA + " " + PRODUCT_VARIATION_LIST .get (1 )), Set .of (ingredient , ingredientA ));
74- ingredientMap .put (new Product (Long .toString (productId ++), ingredient + " and " + ingredientB + " " + PRODUCT_VARIATION_LIST .get (2 )), Set .of (ingredient , ingredientB ));
75- ingredientMap .put (new Product (Long .toString (productId ++), ingredient + ", " + ingredientA + " and " + ingredientC + " " + PRODUCT_VARIATION_LIST .get (1 )), Set .of (ingredient , ingredientA , ingredientC ));
74+ ingredientMap .put (new Product (Long .toString (productId ++), ingredient + " and " + ingredientA + " " + PRODUCT_VARIATION_LIST .get (1 )), java . util . Set .of (ingredient , ingredientA ));
75+ ingredientMap .put (new Product (Long .toString (productId ++), ingredient + " and " + ingredientB + " " + PRODUCT_VARIATION_LIST .get (2 )), java . util . Set .of (ingredient , ingredientB ));
76+ ingredientMap .put (new Product (Long .toString (productId ++), ingredient + ", " + ingredientA + " and " + ingredientC + " " + PRODUCT_VARIATION_LIST .get (1 )), java . util . Set .of (ingredient , ingredientA , ingredientC ));
7677 }
77- List < Product > products = new ArrayList <>(ingredientMap .keySet ());
78- for (Product product : products ) {
79- Map < Product , Duration > cleaningDurationMap = new HashMap <>(products .size ());
80- Set < String > ingredients = ingredientMap .get (product );
81- for (Product previousProduct : products ) {
82- boolean noCleaning = ingredients .containsAll (ingredientMap .get (previousProduct ));
83- Duration cleaningDuration = Duration .ofMinutes (product == previousProduct ? 0
78+ var products = new ArrayList <>(ingredientMap .keySet (). stream (). sorted ( Comparator . comparing ( Product :: getId )). toList ());
79+ for (var product : products ) {
80+ var cleaningDurationMap = new HashMap <Product , Duration >(products .size ());
81+ var ingredients = ingredientMap .get (product );
82+ for (var previousProduct : products ) {
83+ var noCleaning = ingredients .containsAll (ingredientMap .get (previousProduct ));
84+ var cleaningDuration = Duration .ofMinutes (product == previousProduct ? 0
8485 : noCleaning ? noCleaningMinutes
8586 : cleaningMinutesMinimum + random .nextInt (cleaningMinutesMaximum - cleaningMinutesMinimum ));
8687 cleaningDurationMap .put (previousProduct , cleaningDuration );
@@ -89,24 +90,25 @@ public void generateDemoData(@Observes StartupEvent startupEvent) {
8990 }
9091 solution .setProducts (products );
9192
92- List < Line > lines = new ArrayList <>(lineCount );
93- for ( int i = 0 ; i < lineCount ; i ++) {
94- String name = "Line " + ( i + 1 );
95- String operator = "Operator " + (( char ) ( 'A' + ( i / 2 ) ));
96- lines .add (new Line ( Integer . toString ( i ), name , operator , START_DATE_TIME ));
93+ var lines = new ArrayList <Line >(lineCount );
94+ var operators = new ArrayList < Operator >( lineCount );
95+ for ( var i = 0 ; i < lineCount ; i ++) {
96+ lines . add ( new Line ( Integer . toString ( i ), "Line " + (i + 1 ), START_DATE_TIME ));
97+ operators .add (new Operator ( "Operator " + ( i + 1 ) ));
9798 }
9899 solution .setLines (lines );
100+ solution .setOperators (operators );
99101
100- List < Job > jobs = new ArrayList <>(jobCount );
101- for (int i = 0 ; i < jobCount ; i ++) {
102+ var jobs = new ArrayList <Job >(jobCount );
103+ for (var i = 0 ; i < jobCount ; i ++) {
102104 Product product = products .get (random .nextInt (products .size ()));
103105 String name = product .getName ();
104106 Duration duration = Duration .ofMinutes (jobDurationMinutesMinimum
105- + random .nextInt ( jobDurationMinutesMaximum - jobDurationMinutesMinimum ));
107+ + random .nextLong (( long ) jobDurationMinutesMaximum - jobDurationMinutesMinimum ));
106108 int targetDayIndex = (i / lineCount ) * averageCleaningAndJobDurationMinutes / (24 * 60 );
107109 LocalDateTime minStartTime = START_DATE .plusDays (random .nextInt (Math .max (1 , targetDayIndex - 2 ))).atTime (LocalTime .MIDNIGHT );
108- LocalDateTime idealEndTime = START_DATE .plusDays (targetDayIndex + random .nextInt (3 )).atTime (16 , 0 );
109- LocalDateTime maxEndTime = idealEndTime .plusDays (1 + random .nextInt (3 ));
110+ LocalDateTime idealEndTime = START_DATE .plusDays (targetDayIndex + random .nextLong (3 )).atTime (16 , 0 );
111+ LocalDateTime maxEndTime = idealEndTime .plusDays (1 + random .nextLong (3 ));
110112 jobs .add (new Job (Integer .toString (i ), name , product , duration , minStartTime , idealEndTime , maxEndTime , 1 , false ));
111113 }
112114 jobs .sort (Comparator .comparing (Job ::getName ));
0 commit comments