Search query is a simple way consistent with the fact that the conditions in the working memory. Thus, it contains LHS regular structures, thus neither specify "when" is assigned "then". Queries with a set of optional parameters, each type can be selected. If the type is not given, it is assumed to be of type Object. Drools engine will try to enforce the required value. Query names are global names KieBase of; so do not add the query to the same name in different packages of the same RuleBase.
To return results, use the ksession.getQueryResults("name")
"name" as the name of the query. This query will return a list of results that match the query allows you to retrieve the object.
The first example everyone over the age of 30 to provide a simple query. The second example uses the parameters in combination with age limit position.
query "people over the age of 30"
person : Person( age > 30 )
end
query "people over the age of x" (int x, String y)
person : Person( age > x, location == y )
end
We use the standard "for" loop through QueryResults returned. Each element is QueryResultsRow, we can use it to access each column tuple. These columns can be accessed through binding declaration name or index.
QueryResults results = ksession.getQueryResults( "people over the age of 30" );
System.out.println( "we have " + results.size() + " people over the age of 30" );
System.out.println( "These people are are over 30:" );
for ( QueryResultsRow row : results ) {
Person person = ( Person ) row.get( "person" );
System.out.println( person.getName() + "\n" );
}
Added support for syntax position for more compact code. By default, a sequence of the type declaration statements and parameters match position. But you can use @position comments override them. This allows the model parameter with the position, rather than the more detailed named parameters.
declare Cheese
name : String @position(1)
shop : String @position(2)
price : int @position(0)
end
Note org.drools.definition.type @Position package may be used to annotate the original pojos on the classpath. Currently, only the class of the field on the annotation. Support class inheritance, but the interface or method is not supported. The following demonstrates the use position isContainedIn query parameter mode; Location(x, y;)
instead ofLocation( thing == x, location == y).
Queries can now call other queries, which combined with the optional query parameters, providing backlinks derived query style. Support location parameters and naming syntax. The location and naming can be mixed, but the position must first be separated by semicolons. Literal expression can be used as a query parameter passing, but at this stage you can not mix with variable expression. The following is a sample query another call query. Please note, "z" here is always "out" variable. '? 'Symbol indicates that the query is only pulled once returns the result, you will not receive further results change when the underlying data.
declare Location
thing : String
location : String
end
query isContainedIn( String x, String y )
Location(x, y;)
or
( Location(z, y;) and ?isContainedIn(x, z;) )
end
As mentioned earlier, you can use real-time "open" to receive query results repeatedly over time, because the underlying data query it will change. Please note that the next call to inquire, "look" rule is not used in the "?" Situation.
query isContainedIn( String x, String y )
Location(x, y;)
or
( Location(z, y;) and isContainedIn(x, z;) )
end
rule look when
Person( $l : likes )
isContainedIn( $l, 'office'; )
then
insertLogical( $l 'is in the office' );
end
Drools supports unified derived queries, in a nutshell, this means that the parameter is optional. You can use static field org.drools.core.runtime.rule.Variable.v call query from Java without specifying parameters - Note that you must use an alternate example of 'v' instead of Variable. These are called 'out' parameter. Please note that the query itself will not declare the parameters are entered or appears at compile time, this can be purely defined at run time during each use. The following example will return all objects contained in the office.
results = ksession.getQueryResults( "isContainedIn", new Object[] { Variable.v, "office" } );
l = new ArrayList<List<String>>();
for ( QueryResultsRow r : results ) {
l.add( Arrays.asList( new String[] { (String) r.get( "x" ), (String) r.get( "y" ) } ) );
}
The algorithm uses a stack to handle recursion, so the stack method will not explode.
You may also be used as input parameters fact query fields, such as:
query contains(String $s, String $c)
$s := String( this.contains( $c ) )
end
rule PersonNamesWithA when
$p : Person()
contains( $p.name, "a"; )
then
end
And more generally valid expression of any type, such as:
query checkLength(String $s, int $l)
$s := String( length == $l )
end
rule CheckPersonNameLength
when
$i : Integer()
$p : Person()
checkLength( $p.name, 1 + $i + $p.age; )
then
end
It does not yet support the following:
-
List and Map unity
-
Expression Unity - pred (X, X + 1, X * Y / 7)