edu.cs.ai.kreator.logic
Class LanguageFactory

java.lang.Object
  extended by edu.cs.ai.kreator.logic.LanguageFactory

public class LanguageFactory
extends java.lang.Object

An instance of this class is responsible for maintaining a unique object status of symbolic names of constants, predicates, etc., within a single language. Thus an instance defines a whole logical language namespace and should be assigned to any structure that uses a common langauge, e.g. knowledge bases.

Author:
Matthias Thimm

Nested Class Summary
static class LanguageFactory.GrammarStyle
           
 
Field Summary
static org.apache.log4j.Logger LOG
           
 
Constructor Summary
LanguageFactory()
          Default constructor.
LanguageFactory(boolean dontCreateThingAndEqualPredicate)
          Creates a LanguageFactory without creating the sort Thing and the '=(_,_)' predicate.
LanguageFactory(LanguageFactory.GrammarStyle grammarStyle)
           
LanguageFactory(LanguageFactory.GrammarStyle grammarStyle, boolean dontCreateThingAndEqualPredicate)
           
 
Method Summary
 boolean contains(Constant constant)
          Checks if the given constant is contained in this language.
 boolean contains(Functor functor)
          Checks if the given functor is contained in this language.
 boolean contains(edu.cs.ai.kreator.logic.LanguageFactoryObject o)
          Checks if the given object is contained in this language.
 boolean contains(Sort sort)
          Checks if the given sort is contained in this language.
 boolean containsAll(java.util.Collection<edu.cs.ai.kreator.logic.LanguageFactoryObject> objects)
          Checks if all elements of the given list are contained in this language.
 boolean containsConstant(java.lang.String constantName)
          Checks if a constant with the given name is contained in this language.
 boolean containsConstant(java.lang.String constantName, Sort sort)
          Checks if a constant with the given name and sort is contained in this language.
 boolean containsFunctor(java.lang.String functorName)
          Checks if a functor with the given name is contained in this language.
 boolean containsFunctor(java.lang.String functorName, int arity)
          Checks if a functor with the given name and arity is contained in this language.
 boolean containsPredicate(GeneralizedPredicate predicate)
          Checks if the given predicate is contained in this language.
 boolean containsPredicate(java.lang.String id)
          Checks if a predicate with the given name is contained in this language.
 boolean containsPredicate(java.lang.String name, int arity, java.util.List<Sort> argSorts, java.util.List<java.lang.String> range, CombiningRule combiningRule, java.util.List<java.lang.Integer> mutuallyExclusiveExhaustiveArgs)
          Checks if a predicate with the given name and parameters is contained in this language.
 boolean containsSort(java.lang.String sortName)
          Checks if a sort with given name is contained in this language.
 LanguageFactory deepCopy(boolean forceLower)
          Creates a new LanguageFactory as deep copy of this instance.
 java.util.Set<java.util.Map<Variable,Constant>> expandSubstitution(java.util.Map<Variable,Constant> substitution, java.util.Set<Variable> variables)
          Completes the given substitution by assigning mappings from all given variables to all possible constants.
 java.util.Set<java.util.Map<Variable,Constant>> expandSubstitution(java.util.Map<Variable,Constant> substitution, java.util.Set<Variable> variables, java.util.Set<MetaConstraint> constraints)
          Completes the given substitution by assigning mappings from all given variables to all possible constants with respect to the given set of MetaConstraint.
 java.util.Set<java.util.Map<Variable,Constant>> expandSubstitution(java.util.Map<Variable,Constant> substitution, java.util.Set<Variable> variables, java.util.Set<MetaConstraint> constraints, java.util.Map<Sort,java.util.Set<Constant>> additionalConstants)
          Completes the given substitution by assigning mappings from all given variables to all possible constants with respect to the given set of MetaConstraint.
 java.util.Set<java.util.Map<Variable,Constant>> expandSubstitutionWithAdditionalConstants(java.util.Map<Variable,Constant> substitution, java.util.Set<Variable> variables, java.util.Map<Sort,java.util.Set<Constant>> additionalConstants)
          Completes the given substitution by assigning mappings from all given variables to all possible constants.
 java.util.List<Constant> getAllConstants()
          Returns a list of all constants in this language.
 java.util.List<Constant> getAllConstants(Sort sort)
          Returns a list of all constants with the given Sort in this language.
 java.util.List<Functor> getAllFunctors()
          Returns a list of all functors in this language.
 java.util.List<GeneralizedPredicate> getAllPredicates()
          Returns a list of all predicates in this language.
 java.util.List<Sort> getAllSorts()
          Returns a list of all sorts in this language.
 Constant getExistingConstant(java.lang.String constantName)
          Look for the constant with the given name.
 Functor getExistingFunctor(java.lang.String functorName)
          Look for the functor with the given name.
 GeneralizedPredicate getExistingPredicate(java.lang.String predicateName)
          Look for the predicate with the given name.
 Sort getExistingSort(java.lang.String sortName)
          Look for the sort with the given name.
 LanguageFactory.GrammarStyle getGrammarStyle()
           
 java.util.List<java.lang.String> getLogicalRange()
           
 Sort getSortThing()
          Returns the "Thing" sort.
 LanguageFactory getSubLanguage(java.util.Set<Sort> sorts, java.util.Set<Constant> consts, java.util.Set<GeneralizedPredicate> preds, java.util.Set<Functor> functors)
          Generates a new LanguageFactory which is a subset of this LanguageFactory.
 boolean isSampleDataValid(SampleCollection sample)
          Tests if the given sample data is in the language.
 boolean isSubSet(LanguageFactory otherFac)
          WARNING: UNTESTED!
 boolean isValid(AtomExpression atomexpression)
          Checks if the given atom expression is expressable in this language
 boolean isValid(Clause clause)
          Checks if the given clause is expressable in this language
 boolean isValid(Literal literal)
          Checks if the given literal is expressable in this language
 boolean isWriteProtected()
           
 boolean mergeWithSampleData(SampleCollection sample)
          Merge the given language with the language in the sample collection.
 void register(GeneralizedPredicate pred)
           
 void removeAllConstants()
          Removes all constants from this language.
 void setWriteProtected(boolean writeProtected)
           
 LanguageFactory shallowCopy()
          Deprecated. 
 java.lang.String toRmeDeclarationString()
          Returns a RME Knowlegbase syntax conform String of the KB's declarations.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.log4j.Logger LOG
Constructor Detail

LanguageFactory

public LanguageFactory()
Default constructor. Initializes an (almost) empty factory, just containing the sort Thing and the '=(_,_)' predicate.


LanguageFactory

public LanguageFactory(boolean dontCreateThingAndEqualPredicate)
Creates a LanguageFactory without creating the sort Thing and the '=(_,_)' predicate.

Parameters:
dontCreateThingAndEqualPredicate -

LanguageFactory

public LanguageFactory(LanguageFactory.GrammarStyle grammarStyle)

LanguageFactory

public LanguageFactory(LanguageFactory.GrammarStyle grammarStyle,
                       boolean dontCreateThingAndEqualPredicate)
Method Detail

getGrammarStyle

public LanguageFactory.GrammarStyle getGrammarStyle()
Returns:
the grammarStyle

getAllConstants

public java.util.List<Constant> getAllConstants()
Returns a list of all constants in this language.

Returns:
a list of all constants in this language.

getAllConstants

public java.util.List<Constant> getAllConstants(Sort sort)
Returns a list of all constants with the given Sort in this language.

Returns:
a list of all constants with the given Sort in this language.

getAllFunctors

public java.util.List<Functor> getAllFunctors()
Returns a list of all functors in this language.

Returns:
a list of all functors in this language.

getAllPredicates

public java.util.List<GeneralizedPredicate> getAllPredicates()
Returns a list of all predicates in this language.

Returns:
a list of all predicates in this language.

getAllSorts

public java.util.List<Sort> getAllSorts()
Returns a list of all sorts in this language.

Returns:
a list of all sorts in this language.

getSortThing

public Sort getSortThing()
Returns the "Thing" sort.

Returns:
a sort.

getExistingConstant

public Constant getExistingConstant(java.lang.String constantName)
Look for the constant with the given name. If no corresponding constant exists, the method returns null.

Parameters:
constantName -
Returns:
the corresponding constant or null.

register

public void register(GeneralizedPredicate pred)
              throws WriteProtectionException
Throws:
WriteProtectionException

getExistingPredicate

public GeneralizedPredicate getExistingPredicate(java.lang.String predicateName)
Look for the predicate with the given name. If no corresponding predicate exists, the method returns null.

Parameters:
predicateName -
Returns:
the corresponding predicate or null.

getExistingSort

public Sort getExistingSort(java.lang.String sortName)
Look for the sort with the given name. If no corresponding sort exists, the method returns null.

Parameters:
sortName -
Returns:
the corresponding sort or null.

getExistingFunctor

public Functor getExistingFunctor(java.lang.String functorName)
Look for the functor with the given name. If no corresponding functor exists, the method returns null.

Parameters:
functorName -
Returns:
the corresponding functor or null.

getSubLanguage

public LanguageFactory getSubLanguage(java.util.Set<Sort> sorts,
                                      java.util.Set<Constant> consts,
                                      java.util.Set<GeneralizedPredicate> preds,
                                      java.util.Set<Functor> functors)
                               throws java.lang.IllegalArgumentException
Generates a new LanguageFactory which is a subset of this LanguageFactory. All Objects in the sub language are the same (by reference) as the objects in this language. The sort set must contain all sorts of the constants in the constant set and also all sorts of the predicates arguments.

This is not a clone method.

Parameters:
sorts - a subset of sorts
consts - a subset of constants
preds - a subset of predicates
functors - a subset of functors.
Returns:
Throws:
java.lang.IllegalArgumentException - if this LanguageFactory does not contain the given Objects.

shallowCopy

@Deprecated
public LanguageFactory shallowCopy()
                            throws java.lang.IllegalArgumentException
Deprecated. 

This method makes a shallow copy of this language factory, i.e. it creates a new language factory with the same elements. WARNING: The objects in the shallow copy still contain references to their "original" LanguageFactory they are registered to.

Returns:
a language factory.
Throws:
java.lang.IllegalArgumentException

deepCopy

public LanguageFactory deepCopy(boolean forceLower)
Creates a new LanguageFactory as deep copy of this instance.

Returns:
reference to the new generated LanguageFactory.
Throws:
WriteProtectionException

isSubSet

public boolean isSubSet(LanguageFactory otherFac)
WARNING: UNTESTED! Tests if the other LanguageFactory contains all Sorts, Constants, GeneralizedPredicate and Functors of this LanguageFactory (by reference)

Parameters:
otherFac - the other LanguageFactory
Returns:
true, if this LanguageFactory is a subset of the other LanguageFactory.

contains

public boolean contains(Constant constant)
Checks if the given constant is contained in this language.

Parameters:
constant - a constant.
Returns:
a boolean.

containsConstant

public boolean containsConstant(java.lang.String constantName)
Checks if a constant with the given name is contained in this language.

Parameters:
constantName -
Returns:
a boolean.

containsConstant

public boolean containsConstant(java.lang.String constantName,
                                Sort sort)
Checks if a constant with the given name and sort is contained in this language.

Parameters:
constantName -
sort -
Returns:
a boolean.

containsPredicate

public boolean containsPredicate(GeneralizedPredicate predicate)
Checks if the given predicate is contained in this language.

Parameters:
predicate - a predicate.
Returns:
a boolean.

containsPredicate

public boolean containsPredicate(java.lang.String id)
Checks if a predicate with the given name is contained in this language.

Parameters:
id - the name of the predicate
Returns:
a boolean.

containsPredicate

public boolean containsPredicate(java.lang.String name,
                                 int arity,
                                 java.util.List<Sort> argSorts,
                                 java.util.List<java.lang.String> range,
                                 CombiningRule combiningRule,
                                 java.util.List<java.lang.Integer> mutuallyExclusiveExhaustiveArgs)
Checks if a predicate with the given name and parameters is contained in this language.

Parameters:
id - the name of the predicate
Returns:
a boolean.

contains

public boolean contains(Functor functor)
Checks if the given functor is contained in this language.

Parameters:
constant - a functor.
Returns:
a boolean.

containsFunctor

public boolean containsFunctor(java.lang.String functorName)
Checks if a functor with the given name is contained in this language.

Parameters:
constantName -
Returns:
a boolean.

containsFunctor

public boolean containsFunctor(java.lang.String functorName,
                               int arity)
Checks if a functor with the given name and arity is contained in this language.

Parameters:
constantName -
sort -
Returns:
a boolean.

contains

public boolean contains(Sort sort)
Checks if the given sort is contained in this language.

Parameters:
sort - a sort.
Returns:
a boolean.

containsSort

public boolean containsSort(java.lang.String sortName)
Checks if a sort with given name is contained in this language.

Parameters:
sortName -
Returns:
a boolean.

contains

public boolean contains(edu.cs.ai.kreator.logic.LanguageFactoryObject o)
Checks if the given object is contained in this language.

Parameters:
o - an object.
Returns:
a boolean.

containsAll

public boolean containsAll(java.util.Collection<edu.cs.ai.kreator.logic.LanguageFactoryObject> objects)
Checks if all elements of the given list are contained in this language.

Parameters:
objects - a list of objects.
Returns:
a boolean.

isValid

public boolean isValid(Literal literal)
Checks if the given literal is expressable in this language

Parameters:
literal - a literal.
Returns:
a boolean.

isValid

public boolean isValid(AtomExpression atomexpression)
Checks if the given atom expression is expressable in this language

Parameters:
atomexpression - an atom expression.
Returns:
a boolean.

isValid

public boolean isValid(Clause clause)
Checks if the given clause is expressable in this language

Parameters:
clause - a clause.
Returns:
a boolean.

expandSubstitution

public java.util.Set<java.util.Map<Variable,Constant>> expandSubstitution(java.util.Map<Variable,Constant> substitution,
                                                                          java.util.Set<Variable> variables)
Completes the given substitution by assigning mappings from all given variables to all possible constants.

Parameters:
substitution - a map from variables to constants.
variables - a set of variables.
Returns:
a set of maps from variables to constants or "null", if at least one variable from "variables" already appears in "subsitution" as key.

expandSubstitutionWithAdditionalConstants

public java.util.Set<java.util.Map<Variable,Constant>> expandSubstitutionWithAdditionalConstants(java.util.Map<Variable,Constant> substitution,
                                                                                                 java.util.Set<Variable> variables,
                                                                                                 java.util.Map<Sort,java.util.Set<Constant>> additionalConstants)
Completes the given substitution by assigning mappings from all given variables to all possible constants.

Parameters:
substitution - a map from variables to constants.
variables - a set of variables.
Returns:
a set of maps from variables to constants or "null", if at least one variable from "variables" already appears in "subsitution" as key.

expandSubstitution

public java.util.Set<java.util.Map<Variable,Constant>> expandSubstitution(java.util.Map<Variable,Constant> substitution,
                                                                          java.util.Set<Variable> variables,
                                                                          java.util.Set<MetaConstraint> constraints)
Completes the given substitution by assigning mappings from all given variables to all possible constants with respect to the given set of MetaConstraint.

Parameters:
substitution - a map from variables to constants.
variables - a set of variables.
constraints - a set of MetaConstraints
Returns:
a set of maps from variables to constants or "null", if at least one variable from "variables" already appears in "subsitution" as key.

expandSubstitution

public java.util.Set<java.util.Map<Variable,Constant>> expandSubstitution(java.util.Map<Variable,Constant> substitution,
                                                                          java.util.Set<Variable> variables,
                                                                          java.util.Set<MetaConstraint> constraints,
                                                                          java.util.Map<Sort,java.util.Set<Constant>> additionalConstants)
Completes the given substitution by assigning mappings from all given variables to all possible constants with respect to the given set of MetaConstraint.

Parameters:
substitution - a map from variables to constants.
variables - a set of variables.
constraints - a set of MetaConstraints
Returns:
a set of maps from variables to constants or "null", if at least one variable from "variables" already appears in "subsitution" as key.

getLogicalRange

public java.util.List<java.lang.String> getLogicalRange()
Returns:
the logicalRange

isWriteProtected

public boolean isWriteProtected()
Returns:
the writeProtected

setWriteProtected

public void setWriteProtected(boolean writeProtected)
Parameters:
writeProtected - the writeProtected to set

toRmeDeclarationString

public java.lang.String toRmeDeclarationString()
Returns a RME Knowlegbase syntax conform String of the KB's declarations.

Returns:
a RME Knowlegbase syntax conform String of the KB's declarations.

removeAllConstants

public void removeAllConstants()
Removes all constants from this language.


isSampleDataValid

public boolean isSampleDataValid(SampleCollection sample)
                          throws ParsingException,
                                 java.io.IOException
Tests if the given sample data is in the language. This is a special test cause the sample data is always typeless but some plugins like rme and problog define types for the predicates.

Parameters:
sample - Reference to the sample data
Returns:
true if the sample data is in the language otherwise false.
Throws:
java.io.IOException
ParsingException

mergeWithSampleData

public boolean mergeWithSampleData(SampleCollection sample)
                            throws ParsingException,
                                   java.io.IOException,
                                   WriteProtectionException
Merge the given language with the language in the sample collection.

Parameters:
sample -
Returns:
Throws:
ParsingException
java.io.IOException
WriteProtectionException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object