Automating queries with the Rule Designer 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:
-
Fetch the query service.
-
Initialize the query.
-
Run the query (this can be done repeatedly).
-
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:
-
an IlrQuery project element
-
a string containing the text of the query (for example,
Find all business rules such that...)
Initializing from IlrQuery
The following example shows 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
The following example shows 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:
-
IlrQueryProjectElementWrapper
-
IlrQueryParameterWrapper
-
IlrQueryVariableWrapper
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().