Class JFunctorFactory

  • All Implemented Interfaces:
    Serializable

    public class JFunctorFactory
    extends Object
    implements Serializable
    This factory will take JSON and convert it into a set of functors. This supplies basic logic and a few other things, so if you need more, be sure to extends this factory.

    Created by Jeff Gaynor
    on 2/27/18 at 9:07 AM

    See Also:
    Serialized Form
    • Constructor Detail

      • JFunctorFactory

        public JFunctorFactory()
      • JFunctorFactory

        public JFunctorFactory​(boolean verboseOn)
    • Method Detail

      • getReplacementTemplates

        public Map<String,​String> getReplacementTemplates()
        Get all the tmeplates for all replacements. This is usually the runtime environment but can be anything.
        Returns:
      • setEnvironment

        public void setEnvironment​(Map<String,​String> environment)
      • isVerboseOn

        public boolean isVerboseOn()
      • setVerboseOn

        public void setVerboseOn​(boolean verboseOn)
      • create

        public JFunctor create​(net.sf.json.JSONObject jsonObject)
        This will create a single functor from the object. If you have a full configuration file, use the createLogicBlock(JSONObject) method instead.
        Parameters:
        jsonObject -
        Returns:
      • createLogicBlocks

        public LogicBlocks createLogicBlocks​(String rawJSON)
        Convenience to create logic blocks from a string. This assumes that the string represents a JSON array. If this fails to resolve, this call returns null;
        Parameters:
        rawJSON -
        Returns:
      • createLogicBlock

        public LogicBlocks<? extends LogicBlock> createLogicBlock​(net.sf.json.JSONObject jsonObject)
        This creates a list of logic blocks from a JSONArray. There are a few cases for this. The basic format of the blocks is assumed to be
             [{"$if":[..],
                "$then":[...],
                "$else":[...]},
              {"$if":[..],
                "$then":[...],
                "$else":[...]},...
             ]
         
        Or a simple list of commands like
             [{"$functor_1":[args]},{"$functor_2":[args]},...]
         
        which is converted to the logical block of
             [{"$if":["$true"],"$then":[commands]}]
         
        I.e it is effectively always evaluated. A Third case that is handled is having these of the form
             [{"$if":...},[COMMANDS]]
         
        Now, the full format is a functor of the form
             {"connector":[array]}
         
        where connector is $or, $xor or $and. In the case of or or and, the entire set of blocks will evaluate and the final result will be available. In the case of xor, evaluation will cease when the first if block is found to be false. If there is simply an array and no connector, logical or is supplied as the default.
        Parameters:
        jsonObject -
        Returns:
      • doLBObject

        protected LogicBlock doLBObject​(net.sf.json.JSONObject json)
      • doLBArray

        protected LogicBlock doLBArray​(net.sf.json.JSONArray array)
      • create

        public List<JFunctor> create​(net.sf.json.JSONArray array)
        This takes an JSONArray of JSONObjects and turns it into a list of JFunctors.
        Parameters:
        array -
        Returns:
      • hasEnum

        protected boolean hasEnum​(net.sf.json.JSONObject rawJson,
                                  FunctorType type)
      • lookUpFunctor

        public JMetaMetaFunctor lookUpFunctor​(String name)
        This does the actual work of looking up the functor and creating a new one.
        Parameters:
        name -
        Returns:
      • figureOutFunctor

        protected JMetaMetaFunctor figureOutFunctor​(net.sf.json.JSONObject rawJson)
        This figures out which functor to create based on the key of the raw JSON object. Override this method to add your own creation code, but be sure to call super at some point to get all of the existing functors that come with this module.
        Parameters:
        rawJson -
        Returns:
      • fromJSON

        public JFunctor fromJSON​(net.sf.json.JSONObject rawJson)
      • preprocess

        protected String preprocess​(String x)
        This is invoked to allow for preprocessing each argument before it is added. The contract is that only scalar arguments (e.g. strings, so not functors) are pre-processed. This method simply returns the argument unchanged. Override as needed.
        Parameters:
        x -
        Returns:
      • addArgs

        protected void addArgs​(JFunctor ff,
                               net.sf.json.JSONObject jsonObject)
      • getArray

        protected net.sf.json.JSONArray getArray​(net.sf.json.JSONObject jsonObject)
      • isFunctor

        public boolean isFunctor​(net.sf.json.JSONObject jsonObject)