ILOG JRules User Guide > Customizing JRules > Tasks > Customizing Rule Studio > Automating Queries with the Rule Studio API

Services on top of the rule model API allow you to automate queries. You can automate queries by API with the entry point IlrQueryService.

You run a query in two or three steps, depending on whether you want to run the actions of the query or not:

  1. Fetch the query service.
  2. Initialize the query.
  3. Run the query (this can be done repeatedly).
  4. Run the actions of the query.

Fetching the Query Service

Use the following to fetch the query service:

IlrQueryService queryService = (IlrQueryServiceImpl) IlrStudioQueryPlugin.getQueryService(ruleProject);

If you want to set a BOM parser other than the default, use:

queryService.setBom(IlrBOMGetter.getInstance().getBOM());

Initializing the Query Service

Depending on the elements you have, you can initialize the query service from:

Initializing from IlrQuery

Following is an example of how to initialize a query service with an IlrQuery element obtained from an IlrProject:

IlrQuery myQuery;
queryService.initQuery(myQuery, null);

To pass your own IlrRuleQueryMatchCollector as second argument:

IlrQuery myQuery;
IlrRuleQueryMatchCollector matchCollector;
queryService.initQuery(myQuery, matchCollector);

Initializing with Query Text

Following is an example of how to initialize a query service with query text:

String myQuery = "Find all business rules such that the name of each business rule contains \"foobar\"";
boolean useLocalScope = false;
IProject currentProject;
queryService.initQuery(myQuery, useLocalScope, currentProject, null);

The second Boolean parameter determines whether the query scope is restricted to the specified project, or if it includes project dependencies.

As with an IlrQuery, you can also pass an IlrRuleQueryMatchCollector as last parameter:

queryService.initQuery(myQuery, useLocalScope, currentProject, matchCollector);

Running the Query

To run a query, use:

List results = queryService.runQuery();

The contents of the list returned depends on the implementation of IlrRuleQueryMatchCollector. Using the default provided, a list of objects of one of the following type is returned:

Running Query Actions

If the query has actions (checked using IlrQueryService.queryHasActions()), you can run the actions with:

queryService.runQueryActions(results)

The results are those returned by runQuery(). If needed, you can change the list before passing it to runQueryActions().

Related Concepts

Rule Model API
Queries

Related Tasks

Customizing Rule Studio

Related Samples and Tutorials

Rule Studio Business Rule Management Extensions