Class KBcache

java.lang.Object
com.articulate.sigma.KBcache
All Implemented Interfaces:
Serializable

public class KBcache extends Object implements Serializable
See Also:
  • Field Details

    • _cacheFileSuffix

      public static final String _cacheFileSuffix
      See Also:
    • intendedTransRels

      public static final List<String> intendedTransRels
    • debug

      public static boolean debug
    • errors

      public static Set<String> errors
      Errors found during processing formulas
    • kb

      public KB kb
    • relations

      public Set<String> relations
    • functions

      public Set<String> functions
    • predicates

      public Set<String> predicates
    • transRels

      public Set<String> transRels
    • instRels

      public Set<String> instRels
    • instTransRels

      public Set<String> instTransRels
    • parents

      public Map<String,Map<String,Set<String>>> parents
      All the cached "parent" relations of all transitive relations meaning the relations between all first arguments and the transitive closure of second arguments. The external HashMap pairs relation name String keys to values that are the parent relationships. The interior HashMap is the set of terms and their transitive closure of parents.
    • instanceOf

      public Map<String,Set<String>> instanceOf
      Parent relations from instances, including those that are transitive through (instance,instance) relations, such as subAttribute and subrelation. May not do what you think since the key is the child (instance)
    • instances

      public Map<String,Set<String>> instances
    • insts

      public Set<String> insts
      A temporary list of instances built during creation of the children map, in order to efficiently create the instances map
    • children

      public Map<String,Map<String,Set<String>>> children
      All the cached "child" relations of all transitive relations meaning the relations between all first arguments and the transitive closure of second arguments. The external HashMap pairs relation name String keys to values that are the child relationships. The interior HashMap is the set of terms and their transitive closure of children.
    • signatures

      public Map<String,List<String>> signatures
      Relation name keys and argument types with 0th arg always "" except in the case of Functions where the 0th arg will be the function range. Variable arity relations may have a type for the last argument, which will be the type repeated for all extended arguments. Note that types can be functions, rather than just terms. Note that types (when there's a domainSubclass etc) are designated by a '+' appended to the class name.
    • valences

      public Map<String,Integer> valences
    • explicitDisjoint

      public Map<String,Set<String>> explicitDisjoint
      Disjoint relationships which were explicitly defined in "partition", "disjoint", and "disjointDecomposition" expressions
    • disjoint

      public Set<String> disjoint
    • disjointRelations

      public Set<String> disjointRelations
    • initialized

      public boolean initialized
    • logOpValences

      public static HashMap<String,Integer> logOpValences
  • Constructor Details

    • KBcache

      public KBcache()
      empty constructor for testing only
    • KBcache

      public KBcache(KB kbin)
    • KBcache

      public KBcache(KBcache kbCacheIn, KB kbIn)
  • Method Details

    • mergeCaches

      public void mergeCaches(KBcache kbCacheIn)
      Experimental: Utility method to perform a merge with the KBcache input
      Parameters:
      kbCacheIn - the incoming cache to merge
    • clearCaches

      public void clearCaches()
      Experimental: Utility method to clear the KBcache
    • getArity

      public int getArity(String rel)
      An ArrayList utility method
    • childOfP

      public boolean childOfP(String rel, String parent, String child)
      Find whether the given child has the given parent for the given transitive relation.
      Returns:
      false if they are equal
    • isInstanceOf

      public boolean isInstanceOf(String i, String c)
      Returns true if i is an instance of c, else returns false.
      Parameters:
      i - A String denoting an instance.
      c - A String denoting a Class.
      Returns:
      true or false.
    • transInstOf

      public boolean transInstOf(String child, String parent)
      Find whether the given instance has the given parent class. Include paths the have transitive relations between instances such as an Attribute that is a subAttribute of another instance, which in turn then is an instance of the given class. Return false if they are equal.
    • subclassOf

      public boolean subclassOf(String child, String parent)
      Find whether the given class has the given parent class.
    • subAttributeOf

      public boolean subAttributeOf(String child, String parent)
      Find whether the given class is the subAttribute of the given parent class.
    • addInstance

      public void addInstance(String child, String parent)
    • extendInstance

      public void extendInstance(String term, String suffix)
      Add a new instance from an existing one plus a suffix, updating the caches
    • buildDirectInstances

      public void buildDirectInstances()
      Record instances and their explicitly defined parent classes
    • addTransitiveInstances

      public void addTransitiveInstances()
      Add transitive relationships to instances in the "instances" map
    • buildDisjointRelationsMap

      public void buildDisjointRelationsMap()
    • buildExplicitDisjointMap

      public void buildExplicitDisjointMap()
      build a disjoint-relations-map which were explicitly defined in "partition", "exhaustiveDecomposition", "disjointDecomposition" and "disjoint" expressions;
    • buildDisjointMap

      public void buildDisjointMap()
      cache the transitive closure of disjoint relations
    • checkDisjoint

      public boolean checkDisjoint(KB kb, Set<String> typeSet)
      check if there are any two types in typeSet are disjoint or not;
    • checkDisjoint

      public boolean checkDisjoint(KB kb, String c1, String c2)
      Check if given classes are disjoint
      Parameters:
      kb - the current knowledge base
      c1 - class #1
      c2 - class #2
      Returns:
      true if c1 and c2 are disjoint; otherwise return false. TODO: can find spurious type conflict when in scope of disjunctions
    • isExplicitDisjoint

      public boolean isExplicitDisjoint(Map<String,Set<String>> explicitDisjointRelations, String c1, String c2)
      return true if rel1 and rel2 are explicitly defined as disjoint relations; otherwise return false.
    • buildTransInstOf

      public void buildTransInstOf()
      Cache whether a given instance has a given parent class. Include paths the have transitive relations between instances such as an Attribute that is a subAttribute of another instance, which in turn then is an instance of the given class. TODO: make sure that direct instances are recorded too
    • correctValences

      public void correctValences()
      since domains are collected before we know the instances of VariableArityRelation we need to go back and correct valences
    • mostSpecificParent

      public String mostSpecificParent(Set<String> p1)
      Returns:
      the most specific parent of a set of classes
    • getCommonParent

      public String getCommonParent(String t1, String t2)
      Returns:
      the most specific parent of the two parameters or null if there is no common parent. TODO: Take into account that there are instances, classes, relations, and attributes,
    • getCommonChild

      public String getCommonChild(Set<String> t2)
    • getParentClasses

      public Set<String> getParentClasses(String cl)
      return parent classes for the given cl from subclass expressions.
    • getChildRelations

      public Set<String> getChildRelations(String rel)
      return child relations for the given rel from subrelation expressions.
    • getChildClasses

      public Set<String> getChildClasses(String cl)
      return child classes for the given cl from subclass expressions.
    • getChildTerms

      public Set<String> getChildTerms(String cl, String rel)
      return child term for the given cl from rel expressions.
    • getChildInstances

      public Set<String> getChildInstances(String cl)
      return child classes for the given cl from subclass expressions.
    • getParentClassesOfInstance

      public Set<String> getParentClassesOfInstance(String cl)
      return classes for the given instance cl. For example, if we know (instance UnitedStates Nation), then getParentClassesOfInstances(UnitedStates) returns Nation and its super classes from subclass expressions.
    • getInstancesForType

      public Set<String> getInstancesForType(String cl)
      Get all instances for the given input class For example, given the class "Nation", getInstancesForType(Nation) returns all instances, like "America", "Austria", "Albania", etc. Follow instances through transitive relations if applicable from the set of [subAttribute, subrelation]. TODO: do we need a DownwardHeritableRelation so that this list doesn't need to be hardcoded?
    • getSignature

      public List<String> getSignature(String rel)
    • getRange

      public String getRange(String f)
      Get the range (return type) of a Function.
      Returns:
      null if argument is not a function
    • collectArgFromFormulas

      public static Set<String> collectArgFromFormulas(int arg, List<Formula> forms)
      Get the HashSet of the given arguments from an ArrayList of Formulas.
    • buildTransitiveRelationsSet

      public void buildTransitiveRelationsSet()
      Do a proper search for relations (including Functions), utilizing the formal definitions, rather than the convention of initial lower case letter. This means getting any instance of Relation tracing back through subclasses as well.
    • isTransitivePredicate

      public boolean isTransitivePredicate(String pred)
    • buildRelationsSet

      public void buildRelationsSet()
      Do a proper search for relations (including Functions), utilizing the formal definitions, rather than the convention of initial lower case letter. This means getting any instance of Relation tracing back through subclasses as well.
    • buildFunctionsSet

      public void buildFunctionsSet()
    • buildInsts

      public void buildInsts()
      Find all instances
    • buildParents

      public void buildParents()
      For each transitive relation, find its transitive closure. If rel is transitive, and (rel A B) and (rel B C) then the entry for rel is a HashMap where the key A has value ArrayList of {B,C}.
    • buildChildren

      public void buildChildren()
      For each transitive relation, find its transitive closure. If rel is transitive, and (rel A B) and (rel B C) then the entry for rel is a HashMap where the key A has value ArrayList of {B,C}. Note that this routine builds "up" from the leaves
    • collectDomains

      public void collectDomains()
      Build the argument type list for every relation. If the argument is a domain subclass, append a "+" to the argument type. If no domain is defined for the given relation and argument position, inherit it from the parent. If there is no argument type, send an error to the Sigma error list. Relation name keys and argument types with 0th arg always "" except for functions which will have the range type as their 0th argument public Map<String,List<String>> signatures = new HashMap<>();
    • writeCacheFile

      @Deprecated public void writeCacheFile()
      Deprecated.
      This is not needed since we have storeCacheAsFormulas()
      Delete and writes the cache .kif file then call addConstituent() so that the file can be processed and loaded by the inference engine.
    • storeCacheAsFormulas

      public void storeCacheAsFormulas()
      Add the cached formulas as though they were from a file. There's no need to write the file since if it hasn't been created, it must be created new. If it has been created already, then it will be written out as part of the serialized info.
    • inheritDomains

      public void inheritDomains()
      Find domain and domainSubclass definitions that impact a child relation. If the type of an argument is less specific than the same type of a parent's argument, use that of the parent.
    • buildInstTransRels

      public void buildInstTransRels()
      Compile the set of transitive relations that are between instances
    • buildCaches

      public void buildCaches()
      Main entry point for the class.
    • copyNewPredFromVariableArity

      public void copyNewPredFromVariableArity(String pred, String oldPred, int arity)
      Copy all relevant information from a VariableArityRelation to a new predicate that is a particular fixed arity. Fill the signature from final argument type in the predicate
    • variableArityType

      public String variableArityType(String r)
      Returns:
      the type of the last argument to the given relation, which will be the type of all the expanded row variables
    • showState

      public static void showState(KBcache nkbc)
    • showAll

      public static void showAll(KBcache nkbc)
    • showChildrenOf

      public static void showChildrenOf(KBcache nkbc, String term)
    • showChildren

      public static void showChildren(KBcache nkbc)
    • showSizes

      public static void showSizes(KBcache nkbc)
      Informational routine to show the sizes of the caches as a way to determine what might be the best sizes to pre-allocate, relative to the number of statements in a knowledge base
    • printHelp

      public static void printHelp()
    • main

      public static void main(String[] args)
      Command line entry point for the class
      Parameters:
      args - command line arguments