Interface CompileContext
RulesEngineBuilder.imports(String...), the class loader to look classes up
with, the type of the output object, this language's options and its own imports, and a way to report warnings. A
language uses what it needs and ignores the rest.
Implemented by the engine, which passes it to a language. It's sealed, so a language can't implement it; a
language's unit tests create one with io.github.brantunger.unruly.test.LanguageTestContexts, from the
unruly-engine-test artifact. Because only the engine implements it, a later release can add methods to it
without breaking languages.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns whether a run may supply only the facts indeclaredFacts(), which the engine was told withRulesEngineBuilder.requireDeclaredFacts().Returns the classes imported one by one.Returns the class loader to look up classes inpackageImports()with: the context class loader of the thread that calledload(), or the engine's own class loader if that thread has none.Returns the facts the engine was declared with, throughRulesEngineBuilder.fact(String, Class), by fact name.Returns the imports given for this language only, withRulesEngineBuilder.languageImports(String, String...), as written and in order, duplicates included.options()Returns this language's options, set withRulesEngineBuilder.option(String, String, String).Class<?> Returns the type of the output object, which the engine was built with throughRulesEngineBuilder.outputType(Class).Returns the imported packages, whose classes rules can refer to by their simple names.voidwarn(Expression source, InvalidExpressionException.Issue issue) Reports a problem that doesn't stop an expression compiling, such as use of a deprecated function.
-
Method Details
-
packageImports
Returns the imported packages, whose classes rules can refer to by their simple names.- Returns:
- Package names such as
java.util; unmodifiable
-
classImports
Returns the classes imported one by one.- Returns:
- The classes; unmodifiable
-
classLoader
ClassLoader classLoader()Returns the class loader to look up classes inpackageImports()with: the context class loader of the thread that calledload(), or the engine's own class loader if that thread has none.- Returns:
- The class loader, never
null
-
outputType
Class<?> outputType()Returns the type of the output object, which the engine was built with throughRulesEngineBuilder.outputType(Class). A language may use it, for example to check the properties its actions return; the engine doesn't.- Returns:
- The output type, or
Objectif the engine wasn't told one
-
declaredFacts
Returns the facts the engine was declared with, throughRulesEngineBuilder.fact(String, Class), by fact name. A language may check its expressions against them, for example to reject a misspelled property when the rules load, or ignore them; the engine checks a run's values against them whatever the language does. A fact declared with a primitive type, such asint, is given as its wrapper,Integer.A declared type is what the value a language is given is an instance of; for a fact declared with a primitive type, the engine first widens a narrower boxed primitive the run supplied, as
RulesEngineBuilder.fact(String, Class)describes. It isn't a promise that the fact is present, unlessallFactsDeclared()istrue.- Returns:
- The declared type of each fact, by name, empty if the engine was told none; unmodifiable
-
allFactsDeclared
boolean allFactsDeclared()Returns whether a run may supply only the facts indeclaredFacts(), which the engine was told withRulesEngineBuilder.requireDeclaredFacts(). When it'strue, every name a rule can legitimately refer to is declared, so a language may reject an expression that refers to anything else. When it'sfalse, a run may supply facts nobody declared, so an undeclared name isn't a mistake.- Returns:
trueif every fact a run may supply is declared
-
options
Returns this language's options, set withRulesEngineBuilder.option(String, String, String). What an option means is up to the language.- Returns:
- The values by option name, empty if this language was given none; unmodifiable
-
languageImports
Returns the imports given for this language only, withRulesEngineBuilder.languageImports(String, String...), as written and in order, duplicates included. The engine doesn't resolve or check them, beyond their length, and the engine's other languages don't see them. What one means, such as a module, a file or a namespace, is up to the language, which can reject one it can't use by throwing fromExpressionLanguage.newCompiler(CompileContext). The engine creates a language's compiler when a rule list given toload()orvalidate()has a rule in that language, and creates the default language's compiler for a list with no rules too. A throw fromnewCompilerthen fails that rule list:load()throws, andvalidate()returns, aRuleCompilationExceptionwith the throw as its cause. A language no such rule list uses never has its imports checked.Unlike
packageImports()andclassImports(), which hold the engine's Java imports for every language, these suit a language whose imports aren't Java classes or packages.- Returns:
- The imports, empty if this language was given none; unmodifiable
-
warn
Reports a problem that doesn't stop an expression compiling, such as use of a deprecated function. The engine logs it at WARN, naming the rule, whether the expression is its condition or its action, and where the problem is, and loading carries on. While the rules are only validated, withRulesEngine.validate(java.util.List), the warning is discarded. To stop the expression compiling, throw anInvalidExpressionExceptioninstead.- Parameters:
source- The expression the problem is inissue- The problem, reported as a warning whatever its severity- Throws:
NullPointerException- ifsourceorissueisnull
-