Class ConversionPolicy
- java.lang.Object
-
- org.prolog4j.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 toCompound
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
ConversionPolicy.TermPattern
-
Constructor Summary
Constructors Modifier Constructor Description protected
ConversionPolicy()
Constructs an emptyConversionPolicy
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <T> void
addListConverter(Class<T> class_, Converter<List<?>> converter)
<T> void
addObjectConverter(Class<T> class_, Converter<T> converter)
Registers a new object converter into the policy.protected <T> void
addTermConverter(Class<T> class_, Converter<T> converter)
Registers a new term converter into the policy.void
addTermConverter(String functor, Converter<Object> converter)
Registers a new term converter into the policy.Object
convertObject(Object object)
Converts a regular Java object to a term.<T> Object
convertTerm(T term)
Converts a term to a regular Java object.<U,T>
TconvertTerm(U term, Class<T> type)
Converts a term to a regular Java object.abstract double
doubleValue(Object term)
Converts a floating point term to a double value.abstract Object
getArg(Object compound, int index)
Returns an argument of a compound term.abstract int
getArity(Object compound)
Returns the arity of a compound term or atom.protected abstract String
getName(Object compound)
Returns the functor of a compound term or the name of an atom.abstract int
intValue(Object term)
Converts an integer term to an int value.abstract boolean
isAtom(Object term)
Determines whether the Prolog term is an atom.abstract boolean
isCompound(Object term)
Determines whether the Prolog term is compound.abstract boolean
isDouble(Object term)
Determines whether the Prolog term is a double value.abstract boolean
isInteger(Object term)
Determines whether the Prolog term is an integer value.abstract boolean
match(Object term1, Object term2)
Decides whether two terms match or not.abstract Object
term(double value)
Creates a real term according to the actual implementation.abstract Object
term(int value)
Creates a integer term according to the actual implementation.abstract Object
term(String name)
Creates an atom according to the actual implementation.abstract Object
term(String pattern, Object... args)
Creates a compound term according to the actual implementation.protected ConversionPolicy.TermPattern
tp(String pattern, Object... args)
-
-
-
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 typeconverter
- 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 laterconverter
- 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 typeconverter
- the converter
-
convertTerm
public <T> Object convertTerm(T term)
Converts a term to a regular Java object. If the term isnull
(that represents an unbound Prolog variable) thennull
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 isnull
(that represents an unbound Prolog variable) thennull
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 converttype
- 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. Thenull
value will be converted tonull
(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 toObject.equals(Object)
apart of thatnull
matches any object.- Parameters:
term1
- the first termterm2
- the second object- Returns:
true
if the terms match, otherwisefalse
-
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, otherwisefalse
-
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, otherwisefalse
-
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, otherwisefalse
-
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, otherwisefalse
-
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 patternargs
- the arguments of the pattern- Returns:
- the created term
-
tp
protected ConversionPolicy.TermPattern tp(String pattern, Object... args)
-
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
-
-