How to Use a Decision Point with a Pre-loaded Dictionary How to Use Executor Service to Run Threads with Decision Point

Working with Rules SDK Decision Point API 7-11 Example 7–1 shows the DecisionPointBuilder supports a fluent interface pattern, so all methods can easily be chained together when you create a Decision Point. The three most common methods for configuring the Decision Point with DecisionPointBuilder are overloaded to have the name with. Each with method takes a single argument of type RuleDictionary, DictionaryFQN, or String . The DecisionPointBuilder also supports similar set and get methods: getDecisionFunction , setDecisionFunction, getDictionary, setDictionary , getDictionaryFQN, setDictionaryFQN. This chain shown in Example 7–1 includes the following steps: 1. The first step is to create a DecisionPointBuilder instance with code such as the following: new DecisionPointBuilder 2. The with method using a String argument defines the name of the decision function that the Decision Point executes. Calling this method is mandatory. .withDF_NAME The DF_NAME specifies the name of the decision function you define for your application. For example for the sample car rental application DF_NAME is defined in CarRental.java as CarRentalDecisionFunction. 3. Call only one of the other two with methods. In this case the sample code uses a pre-loaded Rule Dictionary instance, containing the specified decision function. The loadDictionary method loads an instance of RuleDictionary from a file. Example 7–2 shows the loadDictionary method. For more information, see Section 7.3.2, How to Use a Decision Point with a Pre-loaded Dictionary . .withloadRuleDictionary 4. Call the build method to construct and return a DecisionPoint instance. The DecisionPoint instance is shared among all instances of the application, which is why it is a static attribute and created in a static block. Another way of initializing the DecisionPoint would be to initialize the m_decisionPoint attribute with a static method that created and returned a DecisionPoint instance.

7.3.2 How to Use a Decision Point with a Pre-loaded Dictionary

Example 7–2 shows the loadRuleDictionary method that loads an instance of RuleDictionary from a file. When reading or writing a dictionary directly from a file as shown in Example 7–2 , ensure to set the encoding to UTF-8. If this is not done, Unicode characters used in the dictionary are corrupted. The UTF-8 option must be set explicitly in the FileInputStream or OutputStreamWriter constructor. Do not use Java classes such as FileReader and FileWriter, as these classes always use the platform default encoding which is usually an ASCII variant rather than a Unicode variant. Example 7–2 Load Rule Dictionary Method private static RuleDictionary loadRuleDictionary{ RuleDictionary dict = null; BufferedReader reader = null; try { reader = new BufferedReader new InputStreamReader new FileInputStream 7-12 Oracle Fusion Middleware Users Guide for Oracle Business Rules new FileDICT_LOCATION, UTF-8; dict = RuleDictionary.readDictionaryreader, new DecisionPointDictionaryFindernull; ListSDKWarning warnings = new ArrayListSDKWarning; dict.updatewarnings; if warnings.size 0 { System.err.printlnValidation warnings: + warnings; } } catch SDKException e{ System.err.printlne; } catch FileNotFoundException e{ System.err.printlne; } catch IOException e{ System.err.printlne; } finally { if reader = null { try { reader.close; } catch IOException ioe {ioe.printStackTrace;}} } return dict; }

7.3.3 How to Use Executor Service to Run Threads with Decision Point

The car rental sample allows you to use Oracle Business Rules and simulate multiple concurrent users. Example 7–3 shows use of the Java ExecutorService interface to execute multiple threads that invoke the Decision Point. The ExecutorService is not part of the Rules SDK Decision Point API. Example 7–3 Checking Drivers with Threads that Invoke Decision Point ExecutorService exec = Executors.newCachedThreadPool; ListDriver drivers = createDrivers; for int i = 0; i NUM_CONCURRENT; i++ { Driver driver = drivers.geti drivers.size; exec.executenew DriverCheckerRunnabledriver; } Example 7–3 includes the following code for the sample application: ■ Create the Executor Service: ExecutorService exec = Executors.newCachedThreadPool; ■ Call method createDrivers, defined in CarRental.java, to create a list of Driver instances. ListDriver drivers = createDrivers; ■ A loop through a list of Driver instances to fill the driver list with drivers. ■ A loop to start multiple threads from DriverCheckerRunnable instances. These instances open a Decision Point and run the rules on each driver. For information on this code, see Section 7.3.4, How to Create and Use Decision Point Instances . Example 7–4 shows the code that waits for the threads to complete. Working with Rules SDK Decision Point API 7-13 Example 7–4 Code to Await Thread Termination try { exec.awaitTermination5, TimeUnit.SECONDS; } catch InterruptedException e { e.printStackTrace; } exec.shutdown; }

7.3.4 How to Create and Use Decision Point Instances