Package com.osohq.oso
Class Polar
- java.lang.Object
-
- com.osohq.oso.Polar
-
-
Constructor Summary
Constructors Constructor Description Polar()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clearRules()
Clear the rules from the KB, but maintain all registered classes and calls.void
loadFile(String filename)
Deprecated.loadFile(String)
has been deprecated in favor ofloadFiles(String[])
as of the 0.20 release.void
loadFiles(String[] filenames)
Load Polar policy files.void
loadFilesFromResources(String... filenames)
Load Polar policy files from resources.void
loadStr(String str)
Load a Polar string into the KB (without filename).void
loadStr(String str, String filename)
Load a Polar string into the KB (with filename).static void
main(String[] args)
Query
query(Predicate query)
Query for a predicate.Query
query(Predicate query, boolean acceptExpression)
Query for a predicate, optionally accepting expressions in the result.Query
query(Predicate query, Map<String,Object> bindings, boolean acceptExpression)
Query for a predicate, applying bindings and optionally accepting the expression type as a result.Query
query(String query)
Query for a predicate, parsing it first.Query
query(String query, boolean acceptExpression)
Query for a predicate, parsing it first and optionally accepting an expression.Query
query(String query, Map<String,Object> bindings)
Query for a predicate, parsing it first and applying bindingsQuery
query(String query, Map<String,Object> bindings, boolean acceptExpression)
Query for a predicate, parsing it first, applying bindings and optionally accepting an expression.Query
queryRule(String rule, Object... args)
Query for a rule.Query
queryRule(String rule, Map<String,Object> bindings, Object... args)
Query for a rule.boolean
queryRuleOnce(String rule, Object... args)
Query for a rule, and check if it has any results.void
registerClass(Class<?> cls)
Register a Java class with Polar.void
registerClass(Class<?> cls, String name)
Register a Java class with Polar using an alias.void
registerConstant(Object value, String name)
Registers `value` as a Polar constant variable called `name`.void
repl()
Start the Polar REPL.void
repl(String[] files)
Load the given files and start the Polar REPL.
-
-
-
Field Detail
-
host
protected Host host
-
-
Constructor Detail
-
Polar
public Polar() throws Exceptions.OsoException
- Throws:
Exceptions.OsoException
-
-
Method Detail
-
clearRules
public void clearRules() throws Exceptions.OsoException
Clear the rules from the KB, but maintain all registered classes and calls.- Throws:
Exceptions.OsoException
-
loadFiles
public void loadFiles(String[] filenames) throws IOException, Exceptions.OsoException
Load Polar policy files. File contents are loaded into a String and saved here, so changes to a file made after a call to loadFiles will not be recognized.- Throws:
Exceptions.PolarFileExtensionError
- On incorrect file extension.Exceptions.PolarFileNotFoundError
- On nonexistent file.Exceptions.InlineQueryFailedError
- On a failed inline query.IOException
- If unable to open or read the file.Exceptions.OsoException
-
loadFilesFromResources
public void loadFilesFromResources(String... filenames) throws IOException, Exceptions.OsoException
Load Polar policy files from resources. File contents are loaded into a String and saved here, so changes to a file made after a call to loadFiles will not be recognized.- Throws:
Exceptions.PolarFileExtensionError
- On incorrect file extension.Exceptions.PolarFileNotFoundError
- On nonexistent file.Exceptions.InlineQueryFailedError
- On a failed inline query.IOException
- If unable to open or read the file.Exceptions.OsoException
-
loadFile
@Deprecated public void loadFile(String filename) throws IOException, Exceptions.OsoException
Deprecated.loadFile(String)
has been deprecated in favor ofloadFiles(String[])
as of the 0.20 release. Please see changelog for migration instructions: https://docs.osohq.com/project/changelogs/2021-09-15.htmlLoad a Polar policy file. File contents are loaded into a String and saved here, so changes to the file made after a call to loadFile will not be recognized.- Throws:
Exceptions.PolarFileExtensionError
- On incorrect file extension.Exceptions.PolarFileNotFoundError
- On nonexistent file.Exceptions.InlineQueryFailedError
- On a failed inline query.IOException
- If unable to open or read the file.Exceptions.OsoException
-
loadStr
public void loadStr(String str, String filename) throws Exceptions.OsoException
Load a Polar string into the KB (with filename).- Parameters:
str
- Polar string to be loaded.filename
- Name of the source file.- Throws:
Exceptions.InlineQueryFailedError
- On a failed inline query.Exceptions.OsoException
-
loadStr
public void loadStr(String str) throws Exceptions.OsoException
Load a Polar string into the KB (without filename).- Parameters:
str
- Polar string to be loaded.- Throws:
Exceptions.InlineQueryFailedError
- On a failed inline query.Exceptions.OsoException
-
query
public Query query(String query) throws Exceptions.OsoException
Query for a predicate, parsing it first.- Throws:
Exceptions.OsoException
-
query
public Query query(String query, boolean acceptExpression) throws Exceptions.OsoException
Query for a predicate, parsing it first and optionally accepting an expression.- Throws:
Exceptions.OsoException
-
query
public Query query(String query, Map<String,Object> bindings) throws Exceptions.OsoException
Query for a predicate, parsing it first and applying bindings- Throws:
Exceptions.OsoException
-
query
public Query query(String query, Map<String,Object> bindings, boolean acceptExpression) throws Exceptions.OsoException
Query for a predicate, parsing it first, applying bindings and optionally accepting an expression.- Throws:
Exceptions.OsoException
-
query
public Query query(Predicate query) throws Exceptions.OsoException
Query for a predicate.- Throws:
Exceptions.OsoException
-
query
public Query query(Predicate query, boolean acceptExpression) throws Exceptions.OsoException
Query for a predicate, optionally accepting expressions in the result.- Throws:
Exceptions.OsoException
-
query
public Query query(Predicate query, Map<String,Object> bindings, boolean acceptExpression) throws Exceptions.OsoException
Query for a predicate, applying bindings and optionally accepting the expression type as a result.- Parameters:
acceptExpression
- Set to true to accept an Expression as a result from the VM.- Throws:
Exceptions.OsoException
-
queryRule
public Query queryRule(String rule, Object... args) throws Exceptions.OsoException
Query for a rule.- Parameters:
rule
- Rule name, e.g. "f" for rule "f(x)".args
- Variable list of rule arguments.- Throws:
Exceptions.OsoException
-
queryRule
public Query queryRule(String rule, Map<String,Object> bindings, Object... args) throws Exceptions.OsoException
Query for a rule.- Parameters:
rule
- Rule name, e.g. "f" for rule "f(x)".args
- Variable list of rule arguments.- Throws:
Exceptions.OsoException
-
queryRuleOnce
public boolean queryRuleOnce(String rule, Object... args) throws Exceptions.OsoException
Query for a rule, and check if it has any results. Returns true if there are results, and false if not.- Parameters:
rule
- Rule name, e.g. "f" for rule "f(x)".args
- Variable list of rule arguments.- Throws:
Exceptions.OsoException
-
repl
public void repl() throws Exceptions.OsoException, IOException
Start the Polar REPL.- Throws:
Exceptions.OsoException
IOException
-
repl
public void repl(String[] files) throws Exceptions.OsoException, IOException
Load the given files and start the Polar REPL.- Throws:
Exceptions.OsoException
IOException
-
main
public static void main(String[] args) throws Exceptions.OsoException, IOException
- Throws:
Exceptions.OsoException
IOException
-
registerClass
public void registerClass(Class<?> cls) throws Exceptions.DuplicateClassAliasError, Exceptions.OsoException
Register a Java class with Polar.
-
registerClass
public void registerClass(Class<?> cls, String name) throws Exceptions.DuplicateClassAliasError, Exceptions.OsoException
Register a Java class with Polar using an alias.
-
registerConstant
public void registerConstant(Object value, String name) throws Exceptions.OsoException
Registers `value` as a Polar constant variable called `name`.- Throws:
Exceptions.OsoException
-
-