Package org.prolog4j

Class ConversionPolicy

  • Direct Known Subclasses:
    SWIPrologConversionPolicy, TuPrologConversionPolicy

    public abstract class ConversionPolicy
    extends Object
    An instance of this class represents how terms are converted to regular Java objects (POJOs) and vice versa. The transformation is performed by converters. The implementations of the Prolog4J API should provide a fully functional instance of this class.

    Prolog terms are converted to regular Java object by term converters. Unbound variables are always converted to null. For bound variables their value is converted. Terms representing numbers are always converted to an instance of java.lang.Number.

    For other terms the default way of conversion can be redefined. The default conversion is the following. Atoms are converted to strings, lists to java.util.List objects and other compound terms to Compound objects.

    You can redefine this behavior by registering your own term converter to the policy. Term converters can be assigned to a string. If an atom with the same name or a compound term with the same functor has to be converted then this custom converter will be used. Otherwise, the default converter will be used.

    The conversion of objects to terms is performed in a similar way. null is converted to a new, unbound variable. Other objects will be converted to ground terms. java.lang.Number objects are converted to Prolog numbers, java.lang.String objects to atoms and java.util.List objects to Prolog lists. For other objects there is no default way of conversion.

    You can add your own object converter to the policy assigning it to a class. If an object has to be converted to a term, first a converter assigned to its class is looked for. If not found, then a converter assigned to its superclasses and the implemented interfaces are looked for, recursively. If none found and there is no default way of conversion, then an exception is thrown.

    See Also:
    Converter
    • Constructor Detail

      • ConversionPolicy

        protected ConversionPolicy()
        Constructs an empty ConversionPolicy.
    • Method Detail

      • addTermConverter

        protected <T> void addTermConverter​(Class<T> class_,
                                            Converter<T> converter)
        Registers a new term converter into the policy. The converter will be used to convert terms of the specified class to objects. This method is intended to be used by the implementation of the conversion policy specific to a particular Prolog implementation.
        Type Parameters:
        T - the class of the terms to convert
        Parameters:
        class_ - the object that represents the given type
        converter - the converter
      • addTermConverter

        public void addTermConverter​(String functor,
                                     Converter<Object> converter)
        Registers a new term converter into the policy. The converter will be used for atoms having the given name and compound terms having the given functor. If a converter has already been assigned to the functor, the new converter will override it.
        Parameters:
        functor - the functor to select the converter to use later
        converter - the converter
      • addObjectConverter

        public <T> void addObjectConverter​(Class<T> class_,
                                           Converter<T> converter)
        Registers a new object converter into the policy. The converter will be used to convert objects of the specified type to terms (subtypes included). If a converter has already been assigned to the type, the new converter will override it.
        Type Parameters:
        T - the type of the terms to convert
        Parameters:
        class_ - the object that represents the given type
        converter - the converter
      • addListConverter

        public <T> void addListConverter​(Class<T> class_,
                                         Converter<List<?>> converter)
      • convertTerm

        public <T> Object convertTerm​(T term)
        Converts a term to a regular Java object. If the term is null (that represents an unbound Prolog variable) then null is returned.

        The method starts to match the term with the patterns of the term converters in their reverse insertion order. If the term matches one then it will be converted by the converter assigned to the pattern. If the converter returns null then another applicable converter will be looked for.

        An exception is thrown if there is no applicable converter. (The implementations of the Prolog4J API should prevent this situation.)

        Parameters:
        term - the term to convert
        Returns:
        the result of the conversion
      • convertTerm

        public <U,​T> T convertTerm​(U term,
                                         Class<T> type)
        Converts a term to a regular Java object. If the term is null (that represents an unbound Prolog variable) then null is returned.

        The method starts to match the term with the patterns of the term converters in their reverse insertion order. If the term matches one then it will be converted by the converter assigned to the pattern. If the converter returns null then another applicable converter will be looked for.

        An exception is thrown if there is no applicable converter. (The implementations of the Prolog4J API should prevent this situation.)

        Type Parameters:
        T - the type to convert to
        Parameters:
        term - the term to convert
        type - the type to convert to
        Returns:
        the result of the conversion
      • convertObject

        public Object convertObject​(Object object)
        Converts a regular Java object to a term. The null value will be converted to null (that represents an unbound Prolog variable).

        The method starts to match the class of the object with the patterns of the object converters in their reverse insertion order. If the class is the subclass of a pattern (equality is allowed) then the object will be converted by the converter assigned to the pattern. If the converter returns null then another applicable converter will be looked for.

        An exception is thrown if there is no applicable converter. (The implementations of the Prolog4J API should prevent this situation.)

        Parameters:
        object - the object to convert
        Returns:
        the result of the conversion
      • match

        public abstract boolean match​(Object term1,
                                      Object term2)
        Decides whether two terms match or not. The comparison is similar to Object.equals(Object) apart of that null matches any object.
        Parameters:
        term1 - the first term
        term2 - the second object
        Returns:
        true if the terms match, otherwise false
      • isInteger

        public abstract boolean isInteger​(Object term)
        Determines whether the Prolog term is an integer value.
        Parameters:
        term - the term
        Returns:
        true if the value is integer, otherwise false
      • isDouble

        public abstract boolean isDouble​(Object term)
        Determines whether the Prolog term is a double value.
        Parameters:
        term - the term
        Returns:
        true if the value is double, otherwise false
      • isAtom

        public abstract boolean isAtom​(Object term)
        Determines whether the Prolog term is an atom.
        Parameters:
        term - the term
        Returns:
        true if the value is integer, otherwise false
      • isCompound

        public abstract boolean isCompound​(Object term)
        Determines whether the Prolog term is compound. Atoms are regarded as compound values with 0 arity.
        Parameters:
        term - the term
        Returns:
        true if the value is compound, otherwise false
      • term

        public abstract Object term​(int value)
        Creates a integer term according to the actual implementation.
        Parameters:
        value - the integer value
        Returns:
        the created Prolog integer term
      • term

        public abstract Object term​(double value)
        Creates a real term according to the actual implementation.
        Parameters:
        value - the double value
        Returns:
        the created Prolog real term
      • term

        public abstract Object term​(String name)
        Creates an atom according to the actual implementation.
        Parameters:
        name - the name of the atom
        Returns:
        the created atom
      • term

        public abstract Object term​(String pattern,
                                    Object... args)
        Creates a compound term according to the actual implementation.
        Parameters:
        pattern - the pattern
        args - the arguments of the pattern
        Returns:
        the created term
      • intValue

        public abstract int intValue​(Object term)
        Converts an integer term to an int value.
        Parameters:
        term - a term representing an integer value
        Returns:
        the int value of the term
      • doubleValue

        public abstract double doubleValue​(Object term)
        Converts a floating point term to a double value.
        Parameters:
        term - a term representing a floating point value
        Returns:
        the double value of the term
      • getName

        protected abstract String getName​(Object compound)
        Returns the functor of a compound term or the name of an atom.
        Parameters:
        compound - the compound term or atom
        Returns:
        the functor of the term
      • getArity

        public abstract int getArity​(Object compound)
        Returns the arity of a compound term or atom. For atoms it returns zero.
        Parameters:
        compound - the compound term or atom
        Returns:
        the arity of the term
      • getArg

        public abstract Object getArg​(Object compound,
                                      int index)
        Returns an argument of a compound term. The numbering starts from zero.
        Parameters:
        compound - the compound
        index - the index of the argument (>= 0)
        Returns:
        the argth argument of the compound