(updated to Drools 5.0)
Drools 5 introduces standardized error messages. This standardization aims to help users to find and resolve problems in a easier and faster way. In this section you will learn how to identify and interpret those error messages as well some tips on how to solve the problems associated with them.
The standardization includes the error message format and to better explain this format, let's use the following example:
1st Block: This area identifies the error code.
2nd Block: Line:column information.
3rd Block: Some text describing the problem.
4th Block: This is the first context. Usually indicates the rule, function, template or query where the error occurred. This block is not mandatory.
5th Block: Identifies the pattern where the error occurred. This block is not mandatory.
Most common kind of error, means that the parser came to a decision point but it couldn't identify an alternative. Here as some examples:
The above example generates this message:
[ERR 101] Line 4:4 no viable alternative at input 'exits' in rule one
At first moment seems to be a valid syntax, but it is not (exits != exists). Let's take a look at next example:
Example 7.3.
1: package org.drools; 2: rule 3: when 4: Object() 5: then 6: System.out.println("A RHS"); 7: end
Now the above code generates this message:
[ERR 101] Line 3:2 no viable alternative at input 'WHEN'
This message means that the parser could identify the WHEN hard keyword, but it is not an option here. In this case, it is missing the rule name.
No viable alternative error occurs also when you have lexical problems. Here is a sample of lexical problem:
The above code misses to close the quotes and because of this the parser generates this error message:
[ERR 101] Line 0:-1 no viable alternative at input '<eof>' in rule simple_rule in pattern Student
Usually the Line and Column information are accurate, but in some cases (like unclosed quotes), the parser generates a 0:-1 position. In this case you should check if you didn't forget to close quotes nor brackets.
Indicates that the parser was looking for a particular symbol that it didn’t find at the current input position. Here are some samples:
The above example generates this message:
[ERR 102] Line 0:-1 mismatched input '<eof>' expecting ')' in rule simple_rule in pattern Bar
To fix this problem, it is necessary to complete the rule statement.
Usually when you get a 0:-1 position, it means that parser reached the end of source.
The following code generates more than one error messages:
Example 7.6.
1: package org.drools; 2: 3: rule "Avoid NPE on wrong syntax" 4: when 5: not( Cheese( ( type == "stilton", price == 10 ) || ( type == "brie", price == 15 ) ) from $cheeseList ) 6: then 7: System.out.println("OK"); 8: end
These are the errors associated with this source:
[ERR 102] Line 5:36 mismatched input ',' expecting ')' in rule "Avoid NPE on wrong syntax" in pattern Cheese
[ERR 101] Line 5:57 no viable alternative at input 'type' in rule "Avoid NPE on wrong syntax"
[ERR 102] Line 5:106 mismatched input ')' expecting 'then' in rule "Avoid NPE on wrong syntax"
Note that the second problem is related to the first. To fix it, just replace the commas (",") by AND operator ("&&").
Some situations you can get more than one error message. Try to fix one by one, starting at the first one. Some error messages are generated as consequences of others.
A validating semantic predicates evaluated to false. Usually this semantic predicates are used to identify soft keywords. This sample shows exactly this situation:
Example 7.7.
1: package nesting;
2: dialect "mvel"
3:
4: import org.drools.Person
5: import org.drools.Address
6:
7: fdsfdsfds
8:
9: rule "test something"
10: when
11: p: Person( name=="Michael" )
12: then
13: p.name = "other";
14: System.out.println(p.name);
15: end
With this sample, we get this error message:
[ERR 103] Line 7:0 rule 'rule_key' failed predicate: {(validateIdentifierKey(DroolsSoftKeywords.RULE))}? in rule
The fdsfdsfds text is invalid and the parser couldn’t identify it as the RULE soft keyword.
This error is very similar to 102: Mismatched input but usually involve soft keywords.
This error is associated with EVAL clause (at EVAL expression you cannot use a semi-colon). Check this sample:
Due its trailling semi-colon at eval, we get this error message:
[ERR 104] Line 3:4 trailing semi-colon not allowed in rule simple_rule
This is the simpler problem to fix: just remove the semi-colon.
The recognizer came to a (..)+ EBNF subrule that must match an alternative at least once, but the subrule did not match anything. To simplify: the parser followed a path that there is no way out. This example tries to illustrates it:
This is the message associated to the above sample:
[ERR 105] Line 2:2 required (...)+ loop did not match anything at input 'aa' in template test_error
To fix this problem it is necessary to remove the numeric value (as it is not a valid sentence nor a valid data type to begin a new slot).