Samples > Rule Studio Business Rule Management Extensions > How to Execute a Query on a Rule Project

This sample shows how to execute a query on your business rules using the query API. It takes as input a query and a target project which contains the rules. The input query can be supplied in one of the following ways:

Key Features

Key features include:

Importing this Sample

This sample extends the Rule Studio API, and the required sample plug-ins are delivered as pre-deployed. If you have not already done so, you must install the samples extension site in Eclipse before running the sample.

To install the plug-in samples extension site:

  1. Click Help > Software Updates > Manage Configuration
  2. In the Product Configuration dialog, click the Add an Extension Location link.
  3. In the Browse dialog, select the folder <InstallDir>/studio/samples/eclipse.
  4. If prompted to restart the Eclipse Workbench, click Yes.
  5. Eclipse shuts down and restarts.

To check that the Query Execution Sample feature is enabled:

  1. In the Rule Explorer, select the loanvalidation-rules project.
  2. Click Help > Software Updates > Manage Configuration.
  3. In the left pane of the Product Configuration page, expand C:\ILOG\JRules\studio\samples\eclipse and check that Query Execution Sample 1.0.0. is listed.

To import the queryexecution sample:

  1. Click File > Import.
  2. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials then click Next.
  3. Select the sample brmanagement > queryexecution.
  4. Click Finish.
  5. If one or more files already exist in your workspace, a warning message is displayed indicating the file(s) were not re-imported. Close the message window.
  6. The imported sample project files are displayed in the Rule Explorer. You should see the following new projects:
    - loanvalidation-rules. The target sample rule project. This project contains rules on which the query will be executed.
    - loanvalidation-xom. A Java project containing the execution model of loanvalidation_rules.
    - queryexecution-application. A Java project including an Ant build file (build.xml) which contains different Ant tasks that will be used in order to demonstrate different execution scenarios. Notice that the project includes a zip file containing a query, which will be used in one of the sample scenarios
    - queryexecution-queries-container. A rule project containing a query that will be executed.
    Note
    The target init.runtime was added in the build.xml file to help Eclipse set the runtime properties: os (operating system), ws (window system) and arch (architecture) before executing the query. This should work for most common configurations. If it does not match your runtime environment, you can modify the Ant target to force the values of these properties.

Running the Sample

You need to be in the Rule Perspective to run this sample.

To switch to the Rule perspective:

  1. Click Window > Open Perspective > Other.
  2. Select the Rule perspective then click OK.

To run the sample:

  1. Click Run > External Tools > External Tools.
  2. In the External Tools dialog, in the Configurations list, click Ant Build > queryexecution-application.
  3. Click Run.
  4. Output is displayed in the Console view:
executionWithZipFile:
   [echo] executing query on loanvalidation-rules project with arguments:
zip:querycontainer.zip/queries-container/queries/completeness/
documentation scope: true
   [java] Arguments : project='loanvalidation-rules' query='querycontainer.zip/
queries-container/queries/completeness/documentation' mode='zip' scope='true'
   [java] <?xml version="1.0" encoding="UTF-8"?> 
   [java] <queryexecution> 
   ... 
   [java] </queryexecution>
   [java] Query (mode=zip) successfully executed [1 element(s) processed].
executionWithQueryPath:
   [echo] executing query on loanvalidation-rules project with arguments:
path:queryexecution-queries-container/queries/completeness/documentation scope: true
   [java] Arguments : project='loanvalidation-rules' query='queryexecution
-queries-container/queries/completeness/documentation' mode='path' scope='true'
   [java] <?xml version="1.0" encoding="UTF-8"?> 
   [java] <queryexecution> 
   ... 
   [java] </queryexecution>
   [java] Query (mode=path) successfully executed [1 element(s) processed]. 
executionWithQueryString:
   [echo] executing query on loanvalidation-rules project with arguments:
text:Find all business rules such that the documentation of each business
rule is empty scope: true
   [java] Arguments : project='loanvalidation-rules' query='Find all
business rules such that the documentation of each business rule is empty'
mode='text' scope='true'
   [java] <?xml version="1.0" encoding="UTF-8"?> 
   [java] <queryexecution>
   ... 
   [java] </queryexecution> 
   [java] Query (mode=text) successfully executed [1 element(s) processed].

Source Files

To display the source code of the queryexecution Eclipse plug-in:

  1. Click File > Import.
  2. In the Import Wizard, select External Plug-ins and Fragments and click Next.
  3. In the Import As section, select the Projects with source folders option and click Next.
  4. In the Plug-ins and Fragments Found list, scroll down and select the ilog.rules.studio.samples.queryexecution plug-in (be careful not to select ilog.rules.studio.samples.queryexecution.source), then click Add.
  5. Click Finish.
  6. The plug-in source code is imported and appears in the Rule Explorer.
  7. Switch to the Plug-in Development perspective.
  8. Click Window > Open Perspective > Other. Select the plug-in Development perspective and then click OK.
  9. In the Package Explorer, navigate to the Java source files in the ilog.rules.studio.samples.queryexecution > src-queryexecution folder.
  10. The following source files are of interest and are described below in the Highlights section of this sample:
    ilog.rules.studio.samples.queryexecution.helper.Model.java
    ilog.rules.studio.samples.queryexecution.helper.ProjectElementprocessorDefaultImpl.java

Highlights

The query is executed using the class ilog.rules.studio.samples.queryexecution.Query. This class is defined in the eclipse plug-in description file as an extension for org.eclipse.core.runtime.applications and implements org.eclipse.core.runtime.IPlatformRunnable.

It is executed from within the build.xml file as an Eclipse headless application by using:

ilog.rules.studio.samples.queryexecution.headless.application

Notice that the real query execution is delegated to the ilog.rules.studio.samples.queryexecution.helper.Model class.

This application expects the following mandatory parameters (see the supplied build.xml file for a full list of parameters):

To define the application flow in Query.java:

  1. Check the input parameters.
  2. Check if the rule project exists in the workspace then use it for the next steps.
  3. If the path: mode is used, check if the query exists in the workspace and initialize a query service from it.
  4. If the text: or zip: mode is used, initialize a query service from the query string.
  5. Execute the query from the query service.
  6. Process the result returned by the query.

Related Concepts

Queries
Rule Model API for Rule Studio

Related Tasks

Automating Queries with the Rule Studio API

Related Samples and Tutorials

Import Samples and Tutorials Wizard