tuffy.mln
Class Clause

java.lang.Object
  extended by tuffy.mln.Clause
All Implemented Interfaces:
java.lang.Cloneable

public class Clause
extends java.lang.Object
implements java.lang.Cloneable

A first-order logic clause, namely a disjunct of literals.


Nested Class Summary
private  class Clause.ClauseInstance
          Class of an instance of a clause.
 
Field Summary
private  java.util.ArrayList<Literal> bilits
          List of built-in literals in this clause.
private  java.util.ArrayList<Expression> constraints
          The set of boolean expressions that must all be TRUE; otherwise the corresponding grounding is always true, and is useless for inference -- and will be discarded.
 double cost
          The cost ascribed to this clause.
private  java.util.ArrayList<java.lang.String> existentialVars
          List of variables that are existentially quantified.
private  java.lang.String exprWeight
           
private  int id
          id of this cluase.
private  java.util.ArrayList<Clause.ClauseInstance> instances
          List of instances of this clause.
private  boolean isTemplate
          Indicates whether this clause contains constants.
private  java.util.ArrayList<Literal> lits
          List of literals in this clause.
static java.util.HashMap<java.lang.Integer,java.lang.String> mappingFromID2Const
          Map from Constant ID to Constant Name.
static java.util.HashMap<java.lang.String,java.lang.String> mappingFromID2Desc
          Map from clause ID to its description.
private  java.util.ArrayList<Type> metaTypes
          Types of meta variables.
private  java.util.ArrayList<java.lang.String> metaVars
          Variables corresponding to constants in this clause.
private  java.lang.String name
          name of this clause.
private  java.util.HashMap<Predicate,java.util.ArrayList<Literal>> predIndex
          The index of predicate to set of literals referencing that predicate.
private  java.util.ArrayList<Literal> reglits
          List of regular literals in this clause.
private  java.lang.String relIntanceClauses
          The database table storing the clause instances.
private  java.lang.String signature
          The signature of this clause.
private  java.util.ArrayList<java.lang.String> specText
          Lines in the MLN rule file specifying this clause.
 java.lang.String sqlFromList
          The From sub-clause of SQL for grounding.
 java.lang.String sqlPivotAttrsList
          The list of attributes that are NOT existential variables.
 java.lang.String sqlWhereBindings
          The Where sub-clause of SQL for grounding.
private  java.util.ArrayList<java.lang.String> uNames
          user provided names
private  java.lang.String varWeight
          FO variable that is used as clause weights
 java.util.ArrayList<GClause> violatedGClauses
          Violated ground clauses.
 double violations
          The number of violations on this clause.
private  double weight
          weight of this clause.
 
Constructor Summary
Clause()
          Construct an empty clause.
 
Method Summary
 void absorb(Clause c)
          "Absorb" another clause of the same pattern into this clause.
 void addConstraint(Expression e)
          Add a constraint that must hold.
 boolean addExistentialVariable(java.lang.String v)
          Existentially quantify a variable.
 void addLiteral(Literal lit)
          Add a literal to this clause.
private  boolean addMetaVariable(java.lang.String v, Type t)
          Add a meta variable into this clause.
 void addSpecText(java.lang.String s)
           
 void addUserProvidedName(java.lang.String nm)
          Add user provided names to this clause.
 void checkVariableSafety()
          Check for unsafe variables in the clause, and mark the corresponding Predicates.
 Clause clone()
           
 void generateSQL()
          Generate the SQL command for grounding this clause.
private  java.lang.String getConstaintStringAsLits()
           
 java.util.ArrayList<Expression> getConstraints()
           
 int getId()
          Get clause ID.
 java.util.ArrayList<Literal> getLiteralsOfPredicate(Predicate pred)
          Return the member literals of a particular predicate.
 java.lang.String getName()
          Return the assigned name of this clause.
 java.util.Set<Predicate> getReferencedPredicates()
          Return the set of predicates referenced by this clause.
 java.util.ArrayList<Literal> getRegLiterals()
          Return the list of non-built-in literals (i.e., regular literals).
 java.lang.String getSignature()
          Return the "signature" of this clause.
 java.util.ArrayList<java.lang.String> getSpecText()
           
private  java.lang.String getSpecTextFlat()
           
 java.lang.String getVarWeight()
          Get the variable in this clause that is used as clause weights
 double getWeight()
          Return the weight of this clause.
 java.lang.String getWeightExp()
          Return the expression of clause weights to be used in SQL.
 boolean hasEmbeddedWeight()
          Check if the weight of this clause comes from a variable in the clause
 boolean hasExistentialQuantifiers()
          Check if any variable in this clause is existentially quantified.
 boolean isHardClause()
          Return whether this clause is a hard rule.
 boolean isPositiveClause()
          Check if the weight is positive.
 boolean isTemplate()
          Return true iff this clause contains constant.
 Clause normalize()
          Return a normalized version of this clause.
 void prepareForDB(RDB db)
          Initialize database objects for this clause.
private  void sealClauseInstances(RDB db)
          Flush the instance of this clause into database.
 void setHardWeight()
          Specify this clause as a hard rule.
 void setId(int id)
          Set clause ID.
 void setName(java.lang.String aname)
          Assign a name for this clause.
 void setVarWeight(java.lang.String vw)
           
 void setWeight(double wt)
          Set the weight of this clause.
 java.lang.String toString()
          Return the definition of this clause.
 java.lang.String toString(int ni)
          Return the definition of clause instance.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mappingFromID2Desc

public static java.util.HashMap<java.lang.String,java.lang.String> mappingFromID2Desc
Map from clause ID to its description. This is used in learning part to dump out the answers. Here by id it means a string like .. This variable is materialized in Infer.setUp(CommandOptions).


mappingFromID2Const

public static java.util.HashMap<java.lang.Integer,java.lang.String> mappingFromID2Const
Map from Constant ID to Constant Name. This map is filled in MarkovLogicNetwork.getSymbolID(String, Type). This variable is materialized in Infer.setUp(CommandOptions).


constraints

private java.util.ArrayList<Expression> constraints
The set of boolean expressions that must all be TRUE; otherwise the corresponding grounding is always true, and is useless for inference -- and will be discarded. In other words, this is the set of constraints that must be satisfied by the grounding process.


lits

private java.util.ArrayList<Literal> lits
List of literals in this clause.


reglits

private java.util.ArrayList<Literal> reglits
List of regular literals in this clause.


bilits

private java.util.ArrayList<Literal> bilits
List of built-in literals in this clause.


predIndex

private java.util.HashMap<Predicate,java.util.ArrayList<Literal>> predIndex
The index of predicate to set of literals referencing that predicate.


existentialVars

private java.util.ArrayList<java.lang.String> existentialVars
List of variables that are existentially quantified.


metaVars

private java.util.ArrayList<java.lang.String> metaVars
Variables corresponding to constants in this clause.


metaTypes

private java.util.ArrayList<Type> metaTypes
Types of meta variables.


instances

private java.util.ArrayList<Clause.ClauseInstance> instances
List of instances of this clause. Here by instance, we mean the possible bindings of meta-variables to constants.


weight

private double weight
weight of this clause.


name

private java.lang.String name
name of this clause.


id

private int id
id of this cluase.


uNames

private java.util.ArrayList<java.lang.String> uNames
user provided names


specText

private java.util.ArrayList<java.lang.String> specText
Lines in the MLN rule file specifying this clause.


relIntanceClauses

private java.lang.String relIntanceClauses
The database table storing the clause instances.


isTemplate

private boolean isTemplate
Indicates whether this clause contains constants.


signature

private java.lang.String signature
The signature of this clause. Clauses with the same signature have the same pattern, and thus can be consolidated. See normalize().


sqlFromList

public java.lang.String sqlFromList
The From sub-clause of SQL for grounding.


sqlWhereBindings

public java.lang.String sqlWhereBindings
The Where sub-clause of SQL for grounding.


sqlPivotAttrsList

public java.lang.String sqlPivotAttrsList
The list of attributes that are NOT existential variables.


cost

public double cost
The cost ascribed to this clause. For auditing purposes.

See Also:
MRF.auditClauseViolations()

violations

public double violations
The number of violations on this clause. For auditing purposes.

See Also:
MRF.auditClauseViolations()

violatedGClauses

public java.util.ArrayList<GClause> violatedGClauses
Violated ground clauses.

See Also:
MRF.auditClauseViolations()

varWeight

private java.lang.String varWeight
FO variable that is used as clause weights


exprWeight

private java.lang.String exprWeight
Constructor Detail

Clause

public Clause()
Construct an empty clause. Initial weight = 0.

Method Detail

clone

public Clause clone()
Overrides:
clone in class java.lang.Object

addConstraint

public void addConstraint(Expression e)
Add a constraint that must hold.

Parameters:
e - A bool expression that must be TRUE.

getConstraints

public java.util.ArrayList<Expression> getConstraints()

getConstaintStringAsLits

private java.lang.String getConstaintStringAsLits()

getSpecTextFlat

private java.lang.String getSpecTextFlat()

setVarWeight

public void setVarWeight(java.lang.String vw)

getVarWeight

public java.lang.String getVarWeight()
Get the variable in this clause that is used as clause weights


hasEmbeddedWeight

public boolean hasEmbeddedWeight()
Check if the weight of this clause comes from a variable in the clause


getWeight

public double getWeight()
Return the weight of this clause. If this clause contains multiple instances, the returned value only indicates the signum.


isTemplate

public boolean isTemplate()
Return true iff this clause contains constant. Note that the result of this function is meaningful iff this clause is a normalized clause.


addUserProvidedName

public void addUserProvidedName(java.lang.String nm)
Add user provided names to this clause.

Parameters:
nm - user provided name

getName

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


setName

public void setName(java.lang.String aname)
Assign a name for this clause.


getSignature

public java.lang.String getSignature()
Return the "signature" of this clause.

See Also:
normalize()

normalize

public Clause normalize()
Return a normalized version of this clause. The variables and constants are replaced standardized variable names, yielding a signature that can be used to identify clauses of the same pattern. If there are constants in the original clause, the resulting clause is called a template. Clauses of the same pattern will be consolidated under the same template. For example, clauses "!likes(x, Candy) v has(x, Diabetes)" and "!likes(x, WeightLifting) v has(x, Muscles)" would be consolidated into the template "!likes(v1, c1) v has(v1, c2)". Zero-weight clauses will be ignored.

See Also:
MarkovLogicNetwork.registerClause(Clause)

absorb

public void absorb(Clause c)
"Absorb" another clause of the same pattern into this clause. If this clause is a template, then adding instances into the instance list. Otherwise, add its weight to current clause.

Parameters:
c - the clause to be absorbed
See Also:
normalize()

sealClauseInstances

private void sealClauseInstances(RDB db)
Flush the instance of this clause into database. Here "instance" means all the constant ID in instance with its weight as a row in DB.

Parameters:
db - the database object used to store clause instances.

addMetaVariable

private boolean addMetaVariable(java.lang.String v,
                                Type t)
Add a meta variable into this clause.

Parameters:
v - name this this meta variable
t - type of this meta variable
Returns:
whether this inserting succeeded.
See Also:
normalize()

addExistentialVariable

public boolean addExistentialVariable(java.lang.String v)
Existentially quantify a variable.

Parameters:
v - the variable to be existentially quantified

setHardWeight

public void setHardWeight()
Specify this clause as a hard rule. Currently hard rules are treated as soft rules with a very large weight.

See Also:
Config.hard_weight

isHardClause

public boolean isHardClause()
Return whether this clause is a hard rule.


setWeight

public void setWeight(double wt)
Set the weight of this clause.


getWeightExp

public java.lang.String getWeightExp()
Return the expression of clause weights to be used in SQL. For template clauses, it's the name of a table attribute; for non-template clauses, it's a floating number.


isPositiveClause

public boolean isPositiveClause()
Check if the weight is positive.


prepareForDB

public void prepareForDB(RDB db)
Initialize database objects for this clause.


checkVariableSafety

public void checkVariableSafety()
Check for unsafe variables in the clause, and mark the corresponding Predicates. A variable is unsafe if it appears only in a positive literal; i.e., if it does not appear in the body in the Datalog form.


generateSQL

public void generateSQL()
Generate the SQL command for grounding this clause. For each meta-variable, bind them to the clause instance (in FROM and WHERE clause) For each regular-predicate's variable that is not meta-variable, use their grounded atom table to bind the clause (in FROM CLAUSE), with inter-predicate constraints introduced in the clause (in WHERE CLAUSE / JOIN CONDITION IN FROM CLAUSE). For each built-in predicate, write its semantic into SQL conditions directly. The sqlPivotAttrsList contains a list of variables that are not existential variables in the form of $table_name.$column_name. Here $table_name is consistent to other SQL sub-clauses generated by this function.


toString

public java.lang.String toString()
Return the definition of this clause.

Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(int ni)
Return the definition of clause instance.

Parameters:
ni - The ID of instance.

getLiteralsOfPredicate

public java.util.ArrayList<Literal> getLiteralsOfPredicate(Predicate pred)
Return the member literals of a particular predicate.


getReferencedPredicates

public java.util.Set<Predicate> getReferencedPredicates()
Return the set of predicates referenced by this clause.


getRegLiterals

public java.util.ArrayList<Literal> getRegLiterals()
Return the list of non-built-in literals (i.e., regular literals).


hasExistentialQuantifiers

public boolean hasExistentialQuantifiers()
Check if any variable in this clause is existentially quantified.


addLiteral

public void addLiteral(Literal lit)
Add a literal to this clause.


setId

public void setId(int id)
Set clause ID.

Parameters:
id -

getId

public int getId()
Get clause ID.


addSpecText

public void addSpecText(java.lang.String s)

getSpecText

public java.util.ArrayList<java.lang.String> getSpecText()