tuffy.mln
Class Predicate

java.lang.Object
  extended by tuffy.mln.Predicate

public class Predicate
extends java.lang.Object

Predicate in First Order Logic.


Nested Class Summary
private  class Predicate.FunctionalDependency
          TODO: look into the implications of FDs
 
Field Summary
private  java.util.ArrayList<java.lang.String> argNameList
           
private  java.util.HashMap<java.lang.String,java.lang.Integer> argNameMap
           
private  java.util.ArrayList<java.lang.String> args
          The list of arguments of this predicate.
private static java.util.HashMap<java.lang.String,Predicate> builtInMap
          Map from name to built-in predicates, e.g., same.
private  boolean closedWorld
          Whether this predicate obeys closed-world assumption.
private  RDB db
          DB object associated with this predicate.
private  java.util.ArrayList<java.lang.Integer> dependentAttributes
           
private  java.util.ArrayList<Predicate.FunctionalDependency> fds
          Functional dependencies for this predicate.
private  boolean hasSoftEvidence
           
private  java.util.HashSet<Clause> iclauses
          Set of clauses referencing this predicate.
private  int id
          The assigned ID for this predicate in its parent MLN mln.
private  boolean isAllQuery
          Whether all unknown atoms of this predicate are queries.
private  boolean isBuiltIn
          Whether this predicate is a built-in predicate.
private  boolean isCompletelySpecified
           
 boolean isCurrentlyView
           
private  java.io.File loadingFile
          The file object used to load evidence to DB
private  java.io.BufferedWriter loadingFileWriter
          The buffer writer object used to flush evidence to file
private  MarkovLogicNetwork mln
          The parent MLN containing this predicate.
private  java.lang.String name
          Name of this predicate.
private  java.util.ArrayList<Atom> queries
          Set of queries referencing this predicate.
private  java.lang.String relName
          Name of the table of this predicate in DB.
private  boolean safeRefOnly
          TODO: if unsat then {if scope then do scope, else do cross product}
private  java.util.ArrayList<Type> types
          List of argument types of this predicate.
 
Constructor Summary
Predicate(MarkovLogicNetwork mln, java.lang.String aname, boolean aClosedWorld)
          Constructor of Predicate.
 
Method Summary
 void addDependentAttrPosition(int i)
          Set the attribute at position i to be dependent.
 void addEvidence(Atom a)
          Store an evidence in the "buffer".
private  void addEvidenceTuple(Atom a)
          Add evidence tuple related to this predicate.
 void addFunctionalDependency(java.util.List<java.lang.String> determinant, java.lang.String dependent)
          Add a functional dependency for the attributes of this predicate
 void addQuery(Atom q)
          Register a query atom.
 void addRelatedClause(Clause c)
          Register a clause referencing this predicate
 void appendArgument(Type t)
          Append a new argument without a user-provided name.
 void appendArgument(Type t, java.lang.String name)
          Append a new argument with a user provided name.
 int arity()
          Return the arity of this predicate.
 void closeFiles()
          Close all file handles.
private  void createTable()
          Create table for storing groundings of this predicate.
 void flushEvidence()
          Flush the evidence buffer to the predicate table, using the COPY statement in PostgreSQL.
 int getArgPositionByName(java.lang.String aname)
          Return the position of the given argument name.
 java.util.ArrayList<java.lang.String> getArgs()
          Return argument names of this predicate.
static Predicate getBuiltInPredByName(java.lang.String s)
          Return the predicate object with the name as the argument string.
 java.util.ArrayList<java.lang.Integer> getDependentAttrPositions()
           
 java.util.ArrayList<java.lang.String> getDependentAttrs()
          Get attributes whose value depend on other attributes in any possible world.
 int getID()
          Return the assigned ID of this predicate in its parent MLN.
 java.util.ArrayList<java.lang.Integer> getKeyAttrPositions()
           
 java.util.ArrayList<java.lang.String> getKeyAttrs()
          Get attributes that form a possible world key.
 java.lang.String getName()
          Return the name of this predicate.
 java.util.ArrayList<Atom> getQueryAtoms()
          Return query atoms of this predicate.
 java.lang.String getRelAct()
          Return the name of relational table containing the ID of active atoms associated with this predicate.
 java.util.HashSet<Clause> getRelatedClauses()
          Return clauses referencing this predicate.
 java.lang.String getRelName()
          Return the relational table name of this predicate..
 Type getTypeAt(int k)
          Return the type of the k-th argument.
 void groundAndStoreAtom(Atom a)
          Ground an atom and store the result in the database.
 boolean hasDependentAttributes()
           
 boolean hasMoreToGround()
          Determine whether this predicate can ground more atoms.
 boolean hasQuery()
          Checks if there are any queries associated with this predicate.
 boolean hasSoftEvidence()
           
 boolean isBuiltIn()
           
static boolean isBuiltInPredName(java.lang.String s)
          Return true if the argument is the name of a built-in predicate.
 boolean isClosedWorld()
          Check if this predicate makes the closed-world assumption.
 boolean isCompletelySepcified()
           
 boolean isImmutable()
           
 boolean isSafeRefOnly()
           
 boolean noNeedToGround()
          Check if we need to ground this predicate on top of its evidence.
 void prepareDB(RDB adb)
          Initialize database objects for this predicate.
 void sealDefinition()
          Mark the point when all arguments have been given.
 void setAllQuery()
          Specify that all atoms of this predicate are queries.
 void setClosedWorld(boolean t)
          Specify whether this predicate obeys the closed world assumption.
 void setCompeletelySpecified(boolean t)
           
 void setDB(RDB adb)
           
 void setHasSoftEvidence(boolean hasSoftEvidence)
           
 void setID(int aid)
          Assign an ID for this predicate.
 void setMLN(MarkovLogicNetwork _mln)
           
 void setSafeRefOnly(boolean safeRefOnly)
          Set whether all references to this predicate are safe; i.e., all variables in corresponding positive literals are bound to other literals in the same clause.
 void storeQueries()
          Ground query atoms and store the result in the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

isCompletelySpecified

private boolean isCompletelySpecified

isCurrentlyView

public boolean isCurrentlyView

dependentAttributes

private java.util.ArrayList<java.lang.Integer> dependentAttributes

builtInMap

private static java.util.HashMap<java.lang.String,Predicate> builtInMap
Map from name to built-in predicates, e.g., same.


name

private java.lang.String name
Name of this predicate.


closedWorld

private boolean closedWorld
Whether this predicate obeys closed-world assumption.


hasSoftEvidence

private boolean hasSoftEvidence

types

private java.util.ArrayList<Type> types
List of argument types of this predicate.


safeRefOnly

private boolean safeRefOnly
TODO: if unsat then {if scope then do scope, else do cross product}


isBuiltIn

private boolean isBuiltIn
Whether this predicate is a built-in predicate.


db

private RDB db
DB object associated with this predicate.


relName

private java.lang.String relName
Name of the table of this predicate in DB.


args

private java.util.ArrayList<java.lang.String> args
The list of arguments of this predicate. The K-th argument is named "TypeK" by default, where "Type" if the type name of this argument, unless explicitly named.


id

private int id
The assigned ID for this predicate in its parent MLN mln.


loadingFile

private java.io.File loadingFile
The file object used to load evidence to DB


loadingFileWriter

private java.io.BufferedWriter loadingFileWriter
The buffer writer object used to flush evidence to file


mln

private MarkovLogicNetwork mln
The parent MLN containing this predicate.


iclauses

private java.util.HashSet<Clause> iclauses
Set of clauses referencing this predicate.


queries

private java.util.ArrayList<Atom> queries
Set of queries referencing this predicate.


isAllQuery

private boolean isAllQuery
Whether all unknown atoms of this predicate are queries.


argNameMap

private java.util.HashMap<java.lang.String,java.lang.Integer> argNameMap

argNameList

private java.util.ArrayList<java.lang.String> argNameList

fds

private java.util.ArrayList<Predicate.FunctionalDependency> fds
Functional dependencies for this predicate.

Constructor Detail

Predicate

public Predicate(MarkovLogicNetwork mln,
                 java.lang.String aname,
                 boolean aClosedWorld)
Constructor of Predicate.

Parameters:
mln - the parent MLN that hosts this predicate
aname - the name; must be unique
aClosedWorld - indicates whether to make the closed-world asssumption
Method Detail

setCompeletelySpecified

public void setCompeletelySpecified(boolean t)

isCompletelySepcified

public boolean isCompletelySepcified()

addDependentAttrPosition

public void addDependentAttrPosition(int i)
Set the attribute at position i to be dependent. Non-dependent attributes form a possible-world key.

Parameters:
i -

getDependentAttrPositions

public java.util.ArrayList<java.lang.Integer> getDependentAttrPositions()

getKeyAttrPositions

public java.util.ArrayList<java.lang.Integer> getKeyAttrPositions()

hasDependentAttributes

public boolean hasDependentAttributes()

getDependentAttrs

public java.util.ArrayList<java.lang.String> getDependentAttrs()
Get attributes whose value depend on other attributes in any possible world.

See Also:
getKeyAttrs()

getKeyAttrs

public java.util.ArrayList<java.lang.String> getKeyAttrs()
Get attributes that form a possible world key.

See Also:
getDependentAttrs()

isBuiltInPredName

public static boolean isBuiltInPredName(java.lang.String s)
Return true if the argument is the name of a built-in predicate.

Parameters:
s - name of queried predicate
Returns:
true if s is a built-in predicate.

getBuiltInPredByName

public static Predicate getBuiltInPredByName(java.lang.String s)
Return the predicate object with the name as the argument string.

Parameters:
s - name of queried predicate
Returns:
the predicate object with name s.

isImmutable

public boolean isImmutable()

getRelAct

public java.lang.String getRelAct()
Return the name of relational table containing the ID of active atoms associated with this predicate.


setAllQuery

public void setAllQuery()
Specify that all atoms of this predicate are queries.


setClosedWorld

public void setClosedWorld(boolean t)
Specify whether this predicate obeys the closed world assumption.


getID

public int getID()
Return the assigned ID of this predicate in its parent MLN.


getArgs

public java.util.ArrayList<java.lang.String> getArgs()
Return argument names of this predicate. The K-th argument is named "TypeK", where "Type" if the type name of this argument.


noNeedToGround

public boolean noNeedToGround()
Check if we need to ground this predicate on top of its evidence. A predicate needs not to ground if 1) it only appears in negative literal and 2) it follows closed world assumption.

Returns:
true if the closed world assumption is made on this predicate, and all literals of this predicate are negative

setID

public void setID(int aid)
Assign an ID for this predicate. This predicate ID is used to encode tuple IDs of this predicate.


getQueryAtoms

public java.util.ArrayList<Atom> getQueryAtoms()
Return query atoms of this predicate. Used by KBMC.


getRelatedClauses

public java.util.HashSet<Clause> getRelatedClauses()
Return clauses referencing this predicate.


addQuery

public void addQuery(Atom q)
Register a query atom.

Parameters:
q - the query atom; could contain variables
See Also:
storeQueries()

storeQueries

public void storeQueries()
Ground query atoms and store the result in the database.


groundAndStoreAtom

public void groundAndStoreAtom(Atom a)
Ground an atom and store the result in the database. Repetitive invocations of this method could be expensive, since it involves both updates and inserts to the predicate table. First, for the grounded tuples satisfying this atom $a$ and already existing in database, it only update its club values. If this $a$ is query, then add query to club (0->1, 2->3). If this $a$ is evidence, then add evidence to club (0->2, 1->3). Then, for the grounded tuples satisfying this atom $a$ and not existing in database, it 1) select them with a $arity-way join in corresponding type instance table; 2) left join them with current version database; 3) select those not matching with any existing tuples; and 4) insert into the database.


addEvidence

public void addEvidence(Atom a)
Store an evidence in the "buffer". There is a buffer (in the form of a CSV file) for each predicate that holds the DB tuple formats of its evidence; this buffer will be flushed into the database once all evidence has been read.

Parameters:
a - the evidence; following Alchemy, it must be a ground atom
See Also:
flushEvidence()

hasMoreToGround

public boolean hasMoreToGround()
Determine whether this predicate can ground more atoms. This predicate does not have more to ground if the number of unknown grounds (none and query) in this predicate is smaller than the number of active atoms of this predicate. I.e., all the unknown atoms of this predicate is activated.

Returns:
whether there are more groundings can be generated

addEvidenceTuple

private void addEvidenceTuple(Atom a)
Add evidence tuple related to this predicate. The output of this function is written to file in format like: $tuple_id,$truth_value,$prior,$club_value,$variable_name,......

Parameters:
a - the atom as evidence. This atom need to be a grounded atom.

closeFiles

public void closeFiles()
Close all file handles.


flushEvidence

public void flushEvidence()
Flush the evidence buffer to the predicate table, using the COPY statement in PostgreSQL.

See Also:
addEvidence(Atom)

hasQuery

public boolean hasQuery()
Checks if there are any queries associated with this predicate.


setDB

public void setDB(RDB adb)

prepareDB

public void prepareDB(RDB adb)
Initialize database objects for this predicate.


createTable

private void createTable()
Create table for storing groundings of this predicate. club: 1 = NONE; 2 = EVIDENCE: atom in evidence; 3 = QUERY: atom in query; 4 = QUEVID: atom in query as evidence.


arity

public int arity()
Return the arity of this predicate.


addRelatedClause

public void addRelatedClause(Clause c)
Register a clause referencing this predicate

Parameters:
c - a clause referencing this predicate

appendArgument

public void appendArgument(Type t)
Append a new argument without a user-provided name.

Parameters:
t - the type of the new argument

appendArgument

public void appendArgument(Type t,
                           java.lang.String name)
Append a new argument with a user provided name.

Parameters:
t - the type of the new argument
name - user-provided name for this argument/attribute

getArgPositionByName

public int getArgPositionByName(java.lang.String aname)
Return the position of the given argument name.

Parameters:
aname - argument name

sealDefinition

public void sealDefinition()
Mark the point when all arguments have been given. Go through the arguments again to try to give unnamed arguments names.


setMLN

public void setMLN(MarkovLogicNetwork _mln)

addFunctionalDependency

public void addFunctionalDependency(java.util.List<java.lang.String> determinant,
                                    java.lang.String dependent)
Add a functional dependency for the attributes of this predicate

Parameters:
determinant -
dependent -

getTypeAt

public Type getTypeAt(int k)
Return the type of the k-th argument.


isClosedWorld

public boolean isClosedWorld()
Check if this predicate makes the closed-world assumption.


getName

public java.lang.String getName()
Return the name of this predicate.


getRelName

public java.lang.String getRelName()
Return the relational table name of this predicate..


setSafeRefOnly

public void setSafeRefOnly(boolean safeRefOnly)
Set whether all references to this predicate are safe; i.e., all variables in corresponding positive literals are bound to other literals in the same clause.

Parameters:
safeRefOnly -

isSafeRefOnly

public boolean isSafeRefOnly()

setHasSoftEvidence

public void setHasSoftEvidence(boolean hasSoftEvidence)

hasSoftEvidence

public boolean hasSoftEvidence()

isBuiltIn

public boolean isBuiltIn()