You can't insert arbitrary java statements directly, but you can create a custom helper class which you specify via HELPER with the code, and then insert a call to it in your DO line. See "User-Defined Rule Helpers" in the Programmers Guide.
As James said there are some limitations on what can go into the DO clause of a rule. You can use all the usual Java expressions apart from control flow statements i.e.
You can insert more than one expression in a DO clause so long as you separate them using a ';' -- for example
RULE ... DO traceln("rule fired"); traceStack() ENDRULE
To answer the second half of your question: no, you cannot insert a rule at any position in a Java method but you can insert in most places that are interesting. The list of location (AT) clauses is restricted to positions in the source code which clearly identify locations in the corresponding bytecode. For example
- AT ENTRY
- AT EXIT
- AT INVOKE calledMethod
- AT WRITE fieldName =
- AT READ $localVarName
- AT LINE nnn
Some of these locations take a count to identify which of several calls, reads, writes etc you are interested in (or ALL to inject at every occurence)
If you want to see the full list of all the expressions which can be written into a rule bindings, condition or action clause (including all the default Helper methods) and all the terms you can employ in a location clause everything is fully documented in the Programmer's Guide.