nars.language
Class CompoundTerm

java.lang.Object
  extended by nars.language.Term
      extended by nars.language.CompoundTerm
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<Term>
Direct Known Subclasses:
Conjunction, DifferenceExt, DifferenceInt, Disjunction, ImageExt, ImageInt, IntersectionExt, IntersectionInt, Negation, Product, SetExt, SetInt, Statement

public abstract class CompoundTerm
extends Term

A CompoundTerm is a Term with internal (syntactic) structure

A CompoundTerm consists of a term operator with one or more component Terms.

This abstract class contains default methods for all CompoundTerms.


Field Summary
protected  short complexity
          syntactic complexity of the compound, the sum of those of its components plus 1
protected  java.util.ArrayList<Term> components
          list of (direct) components
protected  java.util.ArrayList<Variable> openVariables
          list of open variables in the compound
 
Fields inherited from class nars.language.Term
name
 
Constructor Summary
protected CompoundTerm()
          Default constructor
protected CompoundTerm(java.lang.String n, java.util.ArrayList<Term> arg)
          Constructor called from subclasses constructors to initialize the fields
protected CompoundTerm(java.lang.String n, java.util.ArrayList<Term> cs, java.util.ArrayList<Variable> open, short i)
          Constructor called from subclasses constructors to clone the fields
 
Method Summary
static Term addComponents(CompoundTerm t1, Term t2)
          Try to add a component into a compound
protected static java.util.ArrayList<Term> argumentsToList(Term t1, Term t2)
          build a component list from two terms
private  void calcComplexity()
          The complexity of the term is the sum of those of the components plus 1
abstract  java.lang.Object clone()
          Abstract clone method
 java.util.ArrayList<Term> cloneComponents()
          Clone the component list
static java.util.ArrayList cloneList(java.util.ArrayList original)
          Deep clone an array list of terms
 Term componentAt(int i)
          get a component by index
 boolean containAllComponents(Term t)
          Check whether the compound contains all components of another term, or that term as a whole
 boolean containComponent(Term t)
          Check whether the compound contains a certain component
 int getComplexity()
          report the term's syntactic complexity
 java.util.ArrayList<Term> getComponents()
          Get the component list
 java.lang.String getConstantName()
          skip all variable names to produce stable sorting order among components, not for display
 java.util.ArrayList<Variable> getOpenVariables()
          get the OpenVariables list
private static boolean isBuiltInOperator(java.lang.String s)
          Check built-in operator name
 boolean isCommutative()
          Check if the order of the components matters
 boolean isConstant()
          check if the term contains free variable
static boolean isOperator(java.lang.String s)
          Check CompoundTerm operator symbol
static Term make(CompoundTerm compound, java.util.ArrayList<Term> components)
          Try to make a compound term from a template and a list of components
static Term make(java.lang.String op, java.util.ArrayList<Term> arg)
          Try to make a compound term from an operator and a list of components
protected static java.lang.String makeCompoundName(java.lang.String op, java.util.ArrayList<Term> arg)
          default method to make the name of a compound term from given fields
protected static java.lang.String makeImageName(java.lang.String op, java.util.ArrayList<Term> arg, int relationIndex)
          default method to make the name of an image term from given fields
protected  java.lang.String makeName()
          default method to make the name of the current term from existing fields
protected static java.lang.String makeSetName(char opener, java.util.ArrayList<Term> arg, char closer)
          make the name of an ExtensionSet or IntensionSet
private  java.lang.String makeVarName(int size, Variable v)
          Sequentially generate new variable names
private  void markVariables()
          Register open and closed variables in a CompoundTerm
abstract  java.lang.String operator()
          Abstract method to get the operator of the compound
 java.util.ArrayList<TermLink> prepareComponentLinks()
          Build TermLink templates to constant components and subcomponents
private  void prepareComponentLinks(java.util.ArrayList<TermLink> componentLinks, short type, CompoundTerm term)
          Collect TermLink templates into a list, go down one level except in special cases
static Term reduceComponents(CompoundTerm t1, Term t2)
          Try to remove a component from a compound
 void renameVariables()
          Rename variables in input sentence
protected  void renameVariables(java.util.HashMap<java.lang.String,java.lang.String> varMap)
          Recursively rename variables by their appearing order in the CompoundTerm
static Term replaceComponent(CompoundTerm compound, int index, Term t)
          Try to replace a component in a compound at a given index by another one
static Term replaceComponent(CompoundTerm compound, Term oldComponent, Term newComponent)
          Try to replace a given component in a compound by another one
 int size()
          get the number of components
 void substituteComponent(java.util.HashMap<java.lang.String,Term> subs, boolean first)
          Substitute a variable component according to a given substitution
 
Methods inherited from class nars.language.Term
compareTo, equals, getName, getOrder, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

components

protected java.util.ArrayList<Term> components
list of (direct) components


openVariables

protected java.util.ArrayList<Variable> openVariables
list of open variables in the compound


complexity

protected short complexity
syntactic complexity of the compound, the sum of those of its components plus 1

Constructor Detail

CompoundTerm

protected CompoundTerm()
Default constructor


CompoundTerm

protected CompoundTerm(java.lang.String n,
                       java.util.ArrayList<Term> cs,
                       java.util.ArrayList<Variable> open,
                       short i)
Constructor called from subclasses constructors to clone the fields

Parameters:
n - Name
cs - Component list
open - Open variable list
i - Complexity

CompoundTerm

protected CompoundTerm(java.lang.String n,
                       java.util.ArrayList<Term> arg)
Constructor called from subclasses constructors to initialize the fields

Parameters:
n - Name, to be re-built
arg - Component list
Method Detail

clone

public abstract java.lang.Object clone()
Abstract clone method

Overrides:
clone in class Term
Returns:
A clone of the compound term

operator

public abstract java.lang.String operator()
Abstract method to get the operator of the compound

Returns:
The operator in a String

calcComplexity

private void calcComplexity()
The complexity of the term is the sum of those of the components plus 1


make

public static Term make(CompoundTerm compound,
                        java.util.ArrayList<Term> components)
Try to make a compound term from a template and a list of components

Parameters:
compound - The template
components - The components
Returns:
A compound term or null

make

public static Term make(java.lang.String op,
                        java.util.ArrayList<Term> arg)
Try to make a compound term from an operator and a list of components

Called from StringParser

Parameters:
op - Term operator
arg - Component list
Returns:
A compound term or null

isBuiltInOperator

private static boolean isBuiltInOperator(java.lang.String s)
Check built-in operator name

Parameters:
s - The String to be checked
Returns:
if the given String is an operator name

isOperator

public static boolean isOperator(java.lang.String s)
Check CompoundTerm operator symbol

Parameters:
s - The String to be checked
Returns:
if the given String is an operator symbol

argumentsToList

protected static java.util.ArrayList<Term> argumentsToList(Term t1,
                                                           Term t2)
build a component list from two terms

Parameters:
t1 - the first component
t2 - the second component
Returns:
the component list

makeName

protected java.lang.String makeName()
default method to make the name of the current term from existing fields

Returns:
the name of the term

makeCompoundName

protected static java.lang.String makeCompoundName(java.lang.String op,
                                                   java.util.ArrayList<Term> arg)
default method to make the name of a compound term from given fields

Parameters:
op - the term operator
arg - the list of components
Returns:
the name of the term

makeSetName

protected static java.lang.String makeSetName(char opener,
                                              java.util.ArrayList<Term> arg,
                                              char closer)
make the name of an ExtensionSet or IntensionSet

Parameters:
opener - the set opener
closer - the set closer
arg - the list of components
Returns:
the name of the term

makeImageName

protected static java.lang.String makeImageName(java.lang.String op,
                                                java.util.ArrayList<Term> arg,
                                                int relationIndex)
default method to make the name of an image term from given fields

Parameters:
op - the term operator
arg - the list of components
relationIndex - the location of the place holder
Returns:
the name of the term

getConstantName

public java.lang.String getConstantName()
skip all variable names to produce stable sorting order among components, not for display

Overrides:
getConstantName in class Term
Returns:
the constant part of the term name

getComplexity

public int getComplexity()
report the term's syntactic complexity

Overrides:
getComplexity in class Term
Returns:
the comlexity value

isConstant

public boolean isConstant()
check if the term contains free variable

Overrides:
isConstant in class Term
Returns:
if the term is a constant

isCommutative

public boolean isCommutative()
Check if the order of the components matters

commutative CompoundTerms: Sets, Intersections; communative Statements: Similarity, Equivalence, EquivalenceWhen; communative CompoundStatements: Disjunction, Conjunction, ConjunctionParallel

Returns:
The default value is false

size

public int size()
get the number of components

Returns:
the size of the component list

componentAt

public Term componentAt(int i)
get a component by index

Parameters:
i - index of the component
Returns:
the component

getComponents

public java.util.ArrayList<Term> getComponents()
Get the component list

Returns:
The component list

cloneComponents

public java.util.ArrayList<Term> cloneComponents()
Clone the component list

Returns:
The cloned component list

cloneList

public static java.util.ArrayList cloneList(java.util.ArrayList original)
Deep clone an array list of terms

Parameters:
original - The original component list
Returns:
an identical and separate copy of the list

containComponent

public boolean containComponent(Term t)
Check whether the compound contains a certain component

Parameters:
t - The component to be checked
Returns:
Whether the component is in the compound

containAllComponents

public boolean containAllComponents(Term t)
Check whether the compound contains all components of another term, or that term as a whole

Parameters:
t - The other term
Returns:
Whether the components are all in the compound

addComponents

public static Term addComponents(CompoundTerm t1,
                                 Term t2)
Try to add a component into a compound

Parameters:
t1 - The compound
t2 - The component
Returns:
The new compound

reduceComponents

public static Term reduceComponents(CompoundTerm t1,
                                    Term t2)
Try to remove a component from a compound

Parameters:
t1 - The compound
t2 - The component
Returns:
The new compound

replaceComponent

public static Term replaceComponent(CompoundTerm compound,
                                    int index,
                                    Term t)
Try to replace a component in a compound at a given index by another one

Parameters:
compound - The compound
index - The location of replacement
t - The new component
Returns:
The new compound

replaceComponent

public static Term replaceComponent(CompoundTerm compound,
                                    Term oldComponent,
                                    Term newComponent)
Try to replace a given component in a compound by another one

Parameters:
compound - The compound
oldComponent - The component to remove
newComponent - The component to add
Returns:
The new compound

getOpenVariables

public java.util.ArrayList<Variable> getOpenVariables()
get the OpenVariables list

Returns:
the open variables list

markVariables

private void markVariables()
Register open and closed variables in a CompoundTerm

An open variable only appears in one components, while a closed variable appears in multiple components

May need further testing and revising on various situations


renameVariables

public void renameVariables()
Rename variables in input sentence

only called from Sentence


renameVariables

protected void renameVariables(java.util.HashMap<java.lang.String,java.lang.String> varMap)
Recursively rename variables by their appearing order in the CompoundTerm

Since each occurance of a variable is processed exactly once, there will be no confusion between new names and old names.

Parameters:
varMap - the mapping built so far

makeVarName

private java.lang.String makeVarName(int size,
                                     Variable v)
Sequentially generate new variable names

Parameters:
size - The current size of the variable list
v - The variable to be renamed
Returns:
A new variable name

substituteComponent

public void substituteComponent(java.util.HashMap<java.lang.String,Term> subs,
                                boolean first)
Substitute a variable component according to a given substitution

Parameters:
subs - The substitution
first - Whether it is the first term in the mapping

prepareComponentLinks

public java.util.ArrayList<TermLink> prepareComponentLinks()
Build TermLink templates to constant components and subcomponents

The compound type determines the link type; the component type determines whether to build the link.

Returns:
A list of TermLink templates

prepareComponentLinks

private void prepareComponentLinks(java.util.ArrayList<TermLink> componentLinks,
                                   short type,
                                   CompoundTerm term)
Collect TermLink templates into a list, go down one level except in special cases

Parameters:
componentLinks - The list of TermLink templates built so far
type - The type of TermLink to be built
term - The CompoundTerm for which the links are built