org.openmrs.logic
Interface LogicService

All Known Implementing Classes:
LogicServiceImpl

public interface LogicService

The Logic Service provides a mechanism for both registering and consuming business logic in the form of logic rules. Rules may be run against a single patient or a set of patients. Rules are registered under a unique string token. Later evaluation and/or retrieval of the rule is done through the token. Tokens can be tagged with any number of string word/phrases to simplify organization and lookup of tokens. Data source results can be obtained directly by using a token in the form @foo.bar, where foo is the logic data source name and bar is the key for that data source. For example, the token @person.gender is a direct reference to the gender key of the person logic data source.

Example Usage

   Patient myPatient = Context.getPatientService().getPatient(123);
   LogicService logicService = Context.getLogicService();
   Result result = logicService.eval(myPatient, "HIV POSITIVE");
   if (result.toBoolean()) {
     // patient is HIV positive
   }
 
Results can be derived with specific criteria as well. For example, to fetch the maximum CD4 count within the past six months:
   Result result = logicService.eval(myPatient, new LogicCriteria("CD4 COUNT")
     .within(Duration.months(6)).max();
 
or within 6 months of 11-November-2006:
   Date targetDate = new GregorianCalendar(2006, 11, 11).getTime();
   Result result = logicService.eval(myPatient, new LogicCriteria("CD4 COUNT")
     .asOf(targetDate).within(Duration.months(6)).max();
 

See Also:
Rule, LogicCriteria, LogicDataSource

Method Summary
 void addRule(java.lang.String token, Rule rule)
          Registers a new rule with the logic service.
 void addRule(java.lang.String token, java.lang.String[] tags, Rule rule)
          Registers a new rule with the logic service, associating the tags with the given token
 void addTokenTag(java.lang.String token, java.lang.String tag)
          Adds a tag to the given token.
 java.util.Map<LogicCriteria,java.util.Map<java.lang.Integer,Result>> eval(Cohort who, java.util.List<LogicCriteria> criterias)
          Evaluates a collection of queries for a set of patients
 java.util.Map<java.lang.Integer,Result> eval(Cohort who, LogicCriteria criteria)
          Evaluates a query over a list of patients
 java.util.Map<java.lang.Integer,Result> eval(Cohort who, LogicCriteria criteria, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Evaluates a query over a list of patients
 java.util.Map<java.lang.Integer,Result> eval(Cohort who, java.lang.String token)
          Evaluates a query over a list of patients
 java.util.Map<java.lang.Integer,Result> eval(Cohort who, java.lang.String token, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Evaluates a query over a list of patients
 Result eval(Patient who, LogicCriteria criteria)
          Evaluates a query for a given patient
 Result eval(Patient who, LogicCriteria criteria, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Evaluates a query for a given patient
 Result eval(Patient who, java.lang.String token)
          Evaluates a rule for a given patient, given the token for the rule.
 Result eval(Patient who, java.lang.String token, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Evaluates a rule for a given patient, given a token and parameters for the rule.
 java.util.Set<java.lang.String> findTags(java.lang.String partialTag)
          Performs a partial match search for token tags among all known tokens.
 java.util.Set<java.lang.String> findToken(java.lang.String token)
          Fetch all known (registered) tokens matching a given string
 Result.Datatype getDefaultDatatype(java.lang.String token)
          Fetches the default datatype this token will return when fed to an eval() call.
 LogicDataSource getLogicDataSource(java.lang.String name)
          Get a logic data source by name
 java.util.Map<java.lang.String,LogicDataSource> getLogicDataSources()
          Get all registered logic data sources
 java.util.Set<RuleParameterInfo> getParameterList(java.lang.String token)
          Fetches the parameters expected by a given rule
 Rule getRule(java.lang.String token)
          Gets the rule registered under a given token
 java.util.Collection<java.lang.String> getTagsByToken(java.lang.String token)
          Gets all tags associated with this token.
 java.util.Set<java.lang.String> getTokens()
          Fetch all known (registered) tokens
 java.util.Set<java.lang.String> getTokensByTag(java.lang.String tag)
          Gets all tokens associated with this tag.
 void registerLogicDataSource(java.lang.String name, LogicDataSource logicDataSource)
          Adds a data source to the logic service.
 void removeLogicDataSource(java.lang.String name)
          Remove a logic data source by name
 void removeRule(java.lang.String token)
          Removes a rule from the logic service
 void removeTokenTag(java.lang.String token, java.lang.String tag)
          Removes a token's previously assigned tag.
 void setLogicDataSources(java.util.Map<java.lang.String,LogicDataSource> logicDataSources)
          Adds the given logic data sources to the list of current data sources on this logic service
 void updateRule(java.lang.String token, Rule rule)
          Update a rule that has previously been registered
 

Method Detail

getTokens

java.util.Set<java.lang.String> getTokens()
Fetch all known (registered) tokens

Returns:
all known (registered) tokens

findToken

java.util.Set<java.lang.String> findToken(java.lang.String token)
Fetch all known (registered) tokens matching a given string

Parameters:
token - full or partial token name
Returns:
all tokens containing the given string

addRule

void addRule(java.lang.String token,
             Rule rule)
             throws LogicException
Registers a new rule with the logic service.

Parameters:
token - the lookup key ("token") for this rule
rule - new rule to be registered
Throws:
LogicException
See Also:
Rule

addRule

void addRule(java.lang.String token,
             java.lang.String[] tags,
             Rule rule)
             throws LogicException
Registers a new rule with the logic service, associating the tags with the given token

Parameters:
token - the unique lookup key ("token") for this rule
tags - words or phrases associated with this token (do not need to be unique)
rule - new rule to be registered
Throws:
LogicException

getRule

Rule getRule(java.lang.String token)
             throws LogicException
Gets the rule registered under a given token

Parameters:
token - lookup key ("token") under which the rule is registered
Returns:
rule registered under the given token
Throws:
LogicException - if no rule by that name is found

updateRule

void updateRule(java.lang.String token,
                Rule rule)
                throws LogicException
Update a rule that has previously been registered

Parameters:
token - lookup key ("token") for the rule to be updated
rule - new version of rule (replaces existing rule)
Throws:
LogicException

removeRule

void removeRule(java.lang.String token)
                throws LogicException
Removes a rule from the logic service

Parameters:
token - lookup key ("token") under which rule to be removed is registered
Throws:
LogicException

eval

Result eval(Patient who,
            java.lang.String token)
            throws LogicException
Evaluates a rule for a given patient, given the token for the rule.

Parameters:
who - patient for whom the rule is to be calculated
token - lookup key for rule to be calculated
Returns:
patient-specific result from given rule
Throws:
LogicException

eval

Result eval(Patient who,
            java.lang.String token,
            java.util.Map<java.lang.String,java.lang.Object> parameters)
            throws LogicException
Evaluates a rule for a given patient, given a token and parameters for the rule.

Parameters:
who - patient for whom the rule is to be calculated
token - lookup key for rule to be calculated
parameters - parameters to be passed to the rule
Returns:
patient-specific result from given rule
Throws:
LogicException

eval

Result eval(Patient who,
            LogicCriteria criteria)
            throws LogicException
Evaluates a query for a given patient

Parameters:
who - patient for whom the query is to be run
criteria - question to be answered (along with the token) for the given patient
Returns:
result of query
Throws:
LogicException

eval

Result eval(Patient who,
            LogicCriteria criteria,
            java.util.Map<java.lang.String,java.lang.Object> parameters)
            throws LogicException
Evaluates a query for a given patient

Parameters:
who - patient for whom the query is to be run
criteria - question to be answered (along with the token) for the given patient
args - arguments to be passed to the rule
Returns:
result of query
Throws:
LogicException

eval

java.util.Map<java.lang.Integer,Result> eval(Cohort who,
                                             java.lang.String token)
                                             throws LogicException
Evaluates a query over a list of patients

Parameters:
who - patients for whom the query is to be run
token - concept to be looked up for each patient
Returns:
result for each patient
Throws:
LogicException

eval

java.util.Map<java.lang.Integer,Result> eval(Cohort who,
                                             java.lang.String token,
                                             java.util.Map<java.lang.String,java.lang.Object> parameters)
                                             throws LogicException
Evaluates a query over a list of patients

Parameters:
who - patients for whom the query is to be run
token - concept to be looked up for each patient
parameters - parameters to be passed to the rule
Returns:
result for each patient
Throws:
LogicException

eval

java.util.Map<java.lang.Integer,Result> eval(Cohort who,
                                             LogicCriteria criteria)
                                             throws LogicException
Evaluates a query over a list of patients

Parameters:
who - patients for whom the query is to be run
criteria - question to be answered (along with the token) for each patient
Returns:
result for each patient
Throws:
LogicException

eval

java.util.Map<java.lang.Integer,Result> eval(Cohort who,
                                             LogicCriteria criteria,
                                             java.util.Map<java.lang.String,java.lang.Object> parameters)
                                             throws LogicException
Evaluates a query over a list of patients

Parameters:
who - patients for whom the query is to run
criteria - question to be answered (along with the token) for each patient
parameters - arguments to be passed to the rule
Returns:
result for each patient
Throws:
LogicException

eval

java.util.Map<LogicCriteria,java.util.Map<java.lang.Integer,Result>> eval(Cohort who,
                                                                          java.util.List<LogicCriteria> criterias)
                                                                          throws LogicException
Evaluates a collection of queries for a set of patients

Parameters:
who - patients for whom the queries are to be run
tokens - tokens to be calculated
criterias - parallel list of criteria to be applied for each token
Returns:
results for each patient
Throws:
LogicException

addTokenTag

void addTokenTag(java.lang.String token,
                 java.lang.String tag)
Adds a tag to the given token.

Parameters:
token -
tag -

removeTokenTag

void removeTokenTag(java.lang.String token,
                    java.lang.String tag)
Removes a token's previously assigned tag.

Parameters:
token -
tag -

getTagsByToken

java.util.Collection<java.lang.String> getTagsByToken(java.lang.String token)
Gets all tags associated with this token.

Parameters:
token - token to look up by
Returns:
collection of tags

getTokensByTag

java.util.Set<java.lang.String> getTokensByTag(java.lang.String tag)
Gets all tokens associated with this tag.

Parameters:
tag - tag to look up by
Returns:
collection of tokens

findTags

java.util.Set<java.lang.String> findTags(java.lang.String partialTag)
Performs a partial match search for token tags among all known tokens.

Parameters:
partialTag - partial match string
Returns:
collection of tags

getDefaultDatatype

Result.Datatype getDefaultDatatype(java.lang.String token)
Fetches the default datatype this token will return when fed to an eval() call. Results (returned by the logic service) are loosely typed by design; however, the default datatype can be a useful hint for managing user interfaces or providing default behavior when working with rules.

Parameters:
token - token to look the datatype up for
Returns:
datatype of the given token

getParameterList

java.util.Set<RuleParameterInfo> getParameterList(java.lang.String token)
Fetches the parameters expected by a given rule

Returns:
list of parameters

registerLogicDataSource

void registerLogicDataSource(java.lang.String name,
                             LogicDataSource logicDataSource)
                             throws LogicException
Adds a data source to the logic service. Data sources provide access to granular data that can be combined by rules to derive higher level information.

Parameters:
name - name for the data source
logicDataSource - the data source
Throws:
LogicException

getLogicDataSources

java.util.Map<java.lang.String,LogicDataSource> getLogicDataSources()
Get all registered logic data sources

Returns:
all registered logic data sources

setLogicDataSources

void setLogicDataSources(java.util.Map<java.lang.String,LogicDataSource> logicDataSources)
                         throws LogicException
Adds the given logic data sources to the list of current data sources on this logic service

Parameters:
logicDataSources -
Throws:
LogicException

getLogicDataSource

LogicDataSource getLogicDataSource(java.lang.String name)
Get a logic data source by name

Parameters:
name - name of the desired logic data source
Returns:
the logic data source with the given name or null if there is no data source registered under the given name (must be an exact match)

removeLogicDataSource

void removeLogicDataSource(java.lang.String name)
Remove a logic data source by name

Parameters:
name - name of the logic data source to be unregistered

OpenMRS-trunk

Generated May 29 2008 02:01 AM. NOTE - these libraries are in active development and subject to change