001package org.hl7.fhir.r4.fhirpath;
002
003import org.hl7.fhir.exceptions.FHIRException;
004import org.hl7.fhir.exceptions.PathEngineException;
005import org.hl7.fhir.r4.model.Base;
006import org.hl7.fhir.r4.model.Identifier;
007import org.hl7.fhir.r4.model.ValueSet;
008import org.hl7.fhir.utilities.fhirpath.FHIRPathConstantEvaluationMode;
009
010import java.util.List;
011
012/**
013 * This interface allows the application making use of a FHIRPathEngine to provide
014 * additional services that can be used in the FHIRPath statements. The services fall into
015 * two categories: extending the FHIRPath engine with types, constants and functions, and
016 * performing work that the FHIRPath engine doesn't know how to do itself
017 *
018 * Functionality:
019 *  * resolveConstant()/resolveConstantType() - register constants
020 *  * log() - do something when .log() is used in the FHIRPath statement
021 *  * resolveFunction() / checkFunction() / executeFunction() - allow the application to define FHIRPath functions of it's own
022 *  * resolveReference() - called when a FHIRPath statement uses 'resolve()' so the host application can attempt to resolve the reference
023 *  * resolveValueSet() - called when memberOf() is used
024 *  * conformsToProfile() - check that the content conforms to the stated profile. Used by the ValidationEngine - not generally suitable for applications to provide their own interface
025 *  * paramIsType() - used when the FHIRPath statement
026 *
027 *  Note that his used to be called IEvaluationContext in the FHIRPathEngine class, but was renamed
028 *  due to a broken interface - see comments at https://github.com/hapifhir/org.hl7.fhir.core/releases/tag/6.6.0
029 */
030
031public interface IHostApplicationServices {
032
033
034  /**
035   * A constant reference - e.g. a reference to a name that must be resolved in context.
036   *
037   * resolveConstant is invoked under 3 different circumstances, which are reflected in the mode parameter:
038   *  * an explicit constant of the form %{token}, where the token isn't a known internal constant
039   *  * every evaluation of any expression to allow the Host Application to decide whether to interpret the expression as a constant even if it is not explicitly a constant
040   *  * at the start of evaluating an expression if the focus provided by the application when invoking the expression is {}
041   *
042   * The return value is a List<Base> - a collection, though most constants are singleton values
043   *
044   * note that variables created using defineVariable() in the FHIRPath expressions will not be processed by resolveConstant (or resolveConstantType)
045   *
046   * @param appContext - application context passed into the FHIRPath engine when first executed
047   * @param name - name reference to resolve. if mode = EXPLICIT, the % will NOT be in the name
048   * @param mode - what situation the reference comes from - see the documentation for @FHIRPathConstantEvaluationMode
049   * @return the value of the constant , or an empty list, though the host application can choose to throw an exception
050   */
051  public List<Base> resolveConstant(FHIRPathEngine engine, Object appContext, String name, FHIRPathConstantEvaluationMode mode) throws PathEngineException;
052
053
054  /**
055   * Compile time support for a constant reference - e.g. a reference to a name that must be resolved in context.
056   *
057   * resolveConstant is invoked under 3 different circumstances, which are reflected in the mode parameter:
058   *  * an explicit constant of the form %{token}, where the token isn't a known internal constant
059   *  * every evaluation of any expression to allow the Host Application to decide whether to interpret the expression as a constant even if it is not explicitly a constant
060   *  * at the start of evaluating an expression if the focus provided by the application when invoking the expression is {}
061   *
062   * The return value is a TypeDetails - a collection, though most constants are singleton values
063   *
064   * note that variables created using defineVariable() in the FHIRPath expressions will not be processed by resolveConstant (or resolveConstantType)
065   *
066   * @param appContext - application context passed into the FHIRPath engine when first executed
067   * @param name - name reference to resolve. if mode = EXPLICIT, the % will NOT be in the name
068   * @param mode - what situation the reference comes from - see the documentation for @FHIRPathConstantEvaluationMode
069   * @return the type of the constant, or null, though the host application can choose to throw an exception
070   */
071  public TypeDetails resolveConstantType(FHIRPathEngine engine, Object appContext, String name, FHIRPathConstantEvaluationMode mode) throws PathEngineException;
072
073  /**
074   * when the .log() function is called
075   *
076   * @param argument
077   * @param focus
078   * @return
079   */
080  public boolean log(String argument, List<Base> focus);
081
082  // extensibility for functions
083
084  /**
085   * @param functionName
086   * @return null if the function is not known
087   */
088  public FHIRPathUtilityClasses.FunctionDetails resolveFunction(FHIRPathEngine engine, String functionName);
089
090  /**
091   * Check the function parameters, and throw an error if they are incorrect, or return the type for the function
092   *
093   * @param functionName
094   * @param parameters
095   * @return
096   */
097  public TypeDetails checkFunction(FHIRPathEngine engine, Object appContext, String functionName, TypeDetails focus, List<TypeDetails> parameters) throws PathEngineException;
098
099  /**
100   * @param appContext
101   * @param functionName
102   * @param parameters
103   * @return
104   */
105  public List<Base> executeFunction(FHIRPathEngine engine, Object appContext, List<Base> focus, String functionName, List<List<Base>> parameters);
106
107  /**
108   * Implementation of resolve() function. Passed a string/identifier, return matching resource, if one is known - else null
109   *
110   * The signature of this method was changed as of version 6.8.2+. We could have added another routine, but that
111   * would make implementation really difficult where you might resolve either with url or identifier, and you'd still
112   * have to make the same amount of changes. If you don't use logical references, just ignore the identifier
113   *
114   * @param engine - the FHIRPath engine that is the operational context of the request
115   * @param appContext - the application provided context when FHIRPath was invoked
116   * @param url - the reference (Reference.reference or the value of the canonical)
117   * @param identifier - the identifier, if one was present (from Reference.identifier)
118   * @param refContext - the object that contained the reference - this can be used to help resolve the reference
119   * @return A Base that is the resource being referenced
120   * @throws FHIRException
121   * @appContext - passed in by the host to the FHIRPathEngine
122   */
123  public Base resolveReference(FHIRPathEngine engine, Object appContext, String url, Identifier identifier, Base refContext) throws FHIRException;
124
125  public boolean conformsToProfile(FHIRPathEngine engine, Object appContext, Base item, String url) throws FHIRException;
126
127  /*
128   * return the value set referenced by the url, which has been used in memberOf()
129   */
130  public ValueSet resolveValueSet(FHIRPathEngine engine, Object appContext, String url);
131
132  /**
133   * For the moment, there can only be one parameter if it's a type parameter
134   *
135   * @param name
136   * @return true if it's a type parameter
137   */
138  public boolean paramIsType(String name, int index);
139}