Chapter 14. Using Red Hat build of OptaPlanner in an IDE: an employee rostering example As a business rules developer, you can use an IDE to build, run, and modify the optaweb-employee-rostering starter application that uses the Red Hat build of OptaPlanner functionality. Prerequisites You use an integrated development environment, such as Red Hat CodeReady Studio or IntelliJ IDEA. You have an understanding of the Java language. You have an understanding of React and TypeScript. This requirement is necessary to develop the OptaWeb UI. 14.1. Overview of the employee rostering starter application The employee rostering starter application assigns employees to shifts on various positions in an organization. For example, you can use the application to distribute shifts in a hospital between nurses, guard duty shifts across a number of locations, or shifts on an assembly line between workers. Optimal employee rostering must take a number of variables into account. For example, different skills can be required for shifts in different positions. Also, some employees might be unavailable for some time slots or might prefer a particular time slot. Moreover, an employee can have a contract that limits the number of hours that the employee can work in a single time period. The Red Hat build of OptaPlanner rules for this starter application use both hard and soft constraints. During an optimization, the planning engine may not violate hard constraints, for example, if an employee is unavailable (out sick), or that an employee cannot work two spots in a single shift. The planning engine tries to adhere to soft constraints, such as an employee's preference to not work a specific shift, but can violate them if the optimal solution requires it. 14.2. Building and running the employee rostering starter application You can build the employee rostering starter application from the source code and run it as a JAR file. Alternatively, you can use your IDE, for example, Eclipse (including Red Hat CodeReady Studio), to build and run the application. 14.2.1. Preparing deployment files You must download and prepare the deployment files before building and deploying the application. Procedure Navigate to the Software Downloads page in the Red Hat Customer Portal (login required), and select the product and version from the drop-down options: Product: Process Automation Manager Version: 7.13.5 Download Red Hat Process Automation Manager 7.13.5 Kogito and OptaPlanner 8 Decision Services Quickstarts ( rhpam-7.13.5-kogito-and-optaplanner-quickstarts.zip ). Extract the rhpam-7.13.5-kogito-and-optaplanner-quickstarts.zip file. Download Red Hat Process Automation Manager 7.13 Maven Repository Kogito and OptaPlanner 8 Maven Repository ( rhpam-7.13.5-kogito-maven-repository.zip ). Extract the rhpam-7.13.5-kogito-maven-repository.zip file. Copy the contents of the rhpam-7.13.5-kogito-maven-repository/maven-repository subdirectory into the ~/.m2/repository directory. Navigate to the optaweb-8.13.0.Final-redhat-00013/optaweb-employee-rostering directory. This folder is the base folder in subsequent parts of this document. Note File and folder names might have higher version numbers than specifically noted in this document. 14.2.2. Running the Employee Rostering starter application JAR file You can run the Employee Rostering starter application from a JAR file included in the Red Hat Process Automation Manager 7.13.5 Kogito and OptaPlanner 8 Decision Services Quickstarts download. Prerequisites You have downloaded and extracted the rhpam-7.13.5-kogito-and-optaplanner-quickstarts.zip file as described in Section 14.2.1, "Preparing deployment files" . A Java Development Kit is installed. Maven is installed. The host has access to the Internet. The build process uses the Internet for downloading Maven packages from external repositories. Procedure In a command terminal, change to the rhpam-7.13.5-kogito-and-optaplanner-quickstarts/optaweb-8.13.0.Final-redhat-00013/optaweb-employee-rostering directory. Enter the following command: mvn clean install -DskipTests Wait for the build process to complete. Navigate to the rhpam-7.13.5-kogito-and-optaplanner-quickstarts/optaweb-8.13.0.Final-redhat-00013/optaweb-employee-rostering/optaweb-employee-rostering-standalone/target directory. Enter the following command to run the Employee Rostering JAR file: java -jar quarkus-app/quarkus-run.jar Note The value of the quarkus.datasource.db-kind parameter is set to H2 by default at build time. To use a different database, you must rebuild the standalone module and specify the database type on the command line. For example, to use a PostgreSQL database, enter the following command: mvn clean install -DskipTests -Dquarkus.profile=postgres To access the application, enter http://localhost:8080/ in a web browser. 14.2.3. Building and running the Employee Rostering starter application using Maven You can use the command line to build and run the employee rostering starter application. If you use this procedure, the data is stored in memory and is lost when the server is stopped. To build and run the application with a database server for persistent storage, see Section 14.2.4, "Building and running the employee rostering starter application with persistent data storage from the command line" . Prerequisites You have prepared the deployment files as described in Section 14.2.1, "Preparing deployment files" . A Java Development Kit is installed. Maven is installed. The host has access to the Internet. The build process uses the Internet for downloading Maven packages from external repositories. Procedure Navigate to the optaweb-employee-rostering-backend directory. Enter the following command: mvn quarkus:dev Navigate to the optaweb-employee-rostering-frontend directory. Enter the following command: npm start Note If you use npm to start the server, npm monitors code changes. To access the application, enter http://localhost:3000/ in a web browser. 14.2.4. Building and running the employee rostering starter application with persistent data storage from the command line If you use the command line to build the employee rostering starter application and run it, you can provide a database server for persistent data storage. Prerequisites You have prepared the deployment files as described in Section 14.2.1, "Preparing deployment files" . A Java Development Kit is installed. Maven is installed. The host has access to the Internet. The build process uses the Internet for downloading Maven packages from external repositories. You have a deployed MySQL or PostrgeSQL database server. Procedure In a command terminal, navigate to the optaweb-employee-rostering-standalone/target directory. Enter the following command to run the Employee Rostering JAR file: java \ -Dquarkus.datasource.username=<DATABASE_USER> \ -Dquarkus.datasource.password=<DATABASE_PASSWORD> \ -Dquarkus.datasource.jdbc.url=<DATABASE_URL> \ -jar quarkus-app/quarkus-run.jar In this example, replace the following placeholders: <DATABASE_URL> : URL to connect to the database <DATABASE_USER> : The user to connect to the database <DATABASE_PASSWORD> : The password for <DATABASE_USER> Note The value of the quarkus.datasource.db-kind parameter is set to H2 by default at build time. To use a different database, you must rebuild the standalone module and specify the database type on the command line. For example, to use a PostgreSQL database, enter the following command: mvn clean install -DskipTests -Dquarkus.profile=postgres 14.2.5. Building and running the employee rostering starter application using IntelliJ IDEA You can use IntelliJ IDEA to build and run the employee rostering starter application. Prerequisites You have downloaded the Employee Rostering source code, available from the Employee Rostering GitHub page. IntelliJ IDEA, Maven, and Node.js are installed. The host has access to the Internet. The build process uses the Internet for downloading Maven packages from external repositories. Procedure Start IntelliJ IDEA. From the IntelliJ IDEA main menu, select File Open . Select the root directory of the application source and click OK . From the main menu, select Run Edit Configurations . In the window that appears, expand Templates and select Maven . The Maven sidebar appears. In the Maven sidebar, select optaweb-employee-rostering-backend from the Working Directory menu. In Command Line , enter mvn quarkus:dev . To start the back end, click OK . In a command terminal, navigate to the optaweb-employee-rostering-frontend directory. Enter the following command to start the front end: To access the application, enter http://localhost:3000/ in a web browser. 14.3. Overview of the source code of the employee rostering starter application The employee rostering starter application consists of the following principal components: A backend that implements the rostering logic using Red Hat build of OptaPlanner and provides a REST API A frontend module that implements a user interface using React and interacts with the backend module through the REST API You can build and use these components independently. In particular, you can implement a different user interface and use the REST API to call the server. In addition to the two main components, the employee rostering template contains a generator of random source data (useful for demonstration and testing purposes) and a benchmarking application. Modules and key classes The Java source code of the employee rostering template contains several Maven modules. Each of these modules includes a separate Maven project file ( pom.xml ), but they are intended for building in a common project. The modules contain a number of files, including Java classes. This document lists all the modules, as well as the classes and other files that contain the key information for the employee rostering calculations. optaweb-employee-rostering-benchmark module: Contains an additional application that generates random data and benchmarks the solution. optaweb-employee-rostering-distribution module: Contains README files. optaweb-employee-rostering-docs module: Contains documentation files. optaweb-employee-rostering-frontend module: Contains the client application with the user interface, developed in React. optaweb-employee-rostering-backend module: Contains the server application that uses OptaPlanner to perform the rostering calculation. src/main/java/org.optaweb.employeerostering.service.roster/rosterGenerator.java : Generates random input data for demonstration and testing purposes. If you change the required input data, change the generator accordingly. src/main/java/org.optaweb.employeerostering.domain.employee/EmployeeAvailability.java : Defines availability information for an employee. For every time slot, an employee can be unavailable, available, or the time slot can be designated a preferred time slot for the employee. src/main/java/org.optaweb.employeerostering.domain.employee/Employee.java : Defines an employee. An employee has a name, a list of skills, and works under a contract. Skills are represented by skill objects. src/main/java/org.optaweb.employeerostering.domain.roster/Roster.java : Defines the calculated rostering information. src/main/java/org.optaweb.employeerostering.domain.shift/Shift.java : Defines a shift to which an employee can be assigned. A shift is defined by a time slot and a spot. For example, in a diner there could be a shift in the Kitchen spot for the February 20 8AM-4PM time slot. Multiple shifts can be defined for a specific spot and time slot. In this case, multiple employees are required for this spot and time slot. src/main/java/org.optaweb.employeerostering.domain.skill/Skill.java : Defines a skill that an employee can have. src/main/java/org.optaweb.employeerostering.domain.spot/Spot.java : Defines a spot where employees can be placed. For example, a Kitchen can be a spot. src/main/java/org.optaweb.employeerostering.domain.contract/Contract.java : Defines a contract that sets limits on work time for an employee in various time periods. src/main/java/org.optaweb.employeerostering.domain.tenant/Tenant.java : Defines a tenant. Each tenant represents an independent set of data. Changes in the data for one tenant do not affect any other tenants. *View.java : Classes related to domain objects that define value sets that are calculated from other information; the client application can read these values through the REST API, but not write them. *Service.java : Interfaces located in the service package that define the REST API. Both the server and the client application separately define implementations of these interfaces. optaweb-employee-rostering-standalone module: Contains the assembly configurations for the standalone application. 14.4. Modifying the employee rostering starter application To modify the employee rostering starter application to suit your needs, you must change the rules that govern the optimization process. You must also ensure that the data structures include the required data and provide the required calculations for the rules. If the required data is not present in the user interface, you must also modify the user interface. The following procedure outlines the general approach to modifying the employee rostering starter application. Prerequisites You have a build environment that successfully builds the application. You can read and modify Java code. Procedure Plan the required changes. Answer the following questions: What are the additional scenarios that must be avoided? These scenarios are hard constraints . What are the additional scenarios that the optimizer must try to avoid when possible? These scenarios are soft constraints . What data is required to calculate if each scenario is happening in a potential solution? Which of the data can be derived from the information that the user enters in the existing version? Which of the data can be hardcoded? Which of the data must be entered by the user and is not entered in the current version? If any required data can be calculated from the current data or can be hardcoded, add the calculations or hardcoding to existing view or utility classes. If the data must be calculated on the server side, add REST API endpoints to read it. If any required data must be entered by the user, add the data to the classes representing the data entities (for example, the Employee class), add REST API endpoints to read and write the data, and modify the user interface to enter the data. When all of the data is available, modify the rules. For most modifications, you must add a new rule. The rules are located in the src/main/java/org/optaweb/employeerostering/service/solver/EmployeeRosteringConstraintProvider.java file of the optaweb-employee-rostering-backend module. After modifying the application, build and run it.

Commands:
mvn clean install -DskipTests
java -jar quarkus-app/quarkus-run.jar
mvn quarkus:dev
npm start
java -Dquarkus.datasource.username=<DATABASE_USER> -Dquarkus.datasource.password=<DATABASE_PASSWORD> -Dquarkus.datasource.jdbc.url=<DATABASE_URL> -jar quarkus-app/quarkus-run.jar
npm start