tuffy.mln
Class MarkovLogicNetwork

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

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

An MLN. Holds the symbol table.


Field Summary
private  java.util.ArrayList<Predicate> clusteringPredicates
           
(package private)  java.util.ArrayList<ConjunctiveQuery> datalogRules
           
private  RDB db
          The db connection associated with this MLN.
 java.util.HashSet<ConjunctiveQuery> dedupalogRules
           
private  int id
           
private static int idGen
           
(package private)  java.util.ArrayList<ConjunctiveQuery> intermediateRules
           
private  java.util.ArrayList<Clause> listClauses
          List of normalized clauses.
private  java.util.ArrayList<Predicate> listPred
          List of all predicates appearing in this MLN.
private  java.util.HashMap<java.lang.String,java.lang.Integer> mapConstantID
          Map from string name to integer constant ID.
private  java.util.Hashtable<java.lang.String,Function> nameMapFunc
           
private  java.util.Hashtable<java.lang.String,Predicate> nameMapPred
          Map from string name to Predicate object.
private  java.util.Hashtable<java.lang.String,Type> nameMapType
          Map from string name to Type object.
private  InputParser parser
          Parser of input.
(package private)  java.util.ArrayList<ConjunctiveQuery> postprocRules
           
 java.lang.String relAtomPart
           
 java.lang.String relAtoms
           
 java.lang.String relClausePart
           
 java.lang.String relClauses
          Database tables storing intermediate data.
private  java.util.HashSet<Clause> relevantClauses
          List of clauses marked as relevant.
 java.lang.String relTrueAtoms
           
private  java.util.HashMap<Predicate,java.util.ArrayList<ConjunctiveQuery>> scopes
           
private  java.util.ArrayList<ConjunctiveQuery> scopingRules
           
private  java.util.Hashtable<java.lang.String,Clause> sigMap
          Map from signature of clauses to Clause object.
private  java.util.HashMap<Clause,Clause> unnormal2normal
           
 java.util.ArrayList<Clause> unnormalizedClauses
          List of unnormalized clauses.
 
Constructor Summary
MarkovLogicNetwork()
          Constructor of MLN.
 
Method Summary
 boolean applyAllScopes()
          Execute all scoping rules
private  void applyScopeForPred(Predicate p)
          Execute the scoping rules for a predicate
 boolean cleanUp()
          Clean up temporary data in DB and working dir, including 1) drop schema in PostgreSQL; 2) remove directory.
 java.lang.Object clone()
           
 void closeFiles()
          Close all file handles used by each predicate in listPred.
 void executeAllDatalogRules()
          Execute all Datalog rules
 void executeAllIntermediateRules()
           
 void executeAllPostprocRules()
          Execute all Postprocessing rules
 void finalizeClauseDefinitions(RDB adb)
          Finalize the definitions of all clauses, i.e., prepare the database table used by each clause, including 1) instance table for each clause; 2) SQL needed to ground this clause.
 java.util.ArrayList<ConjunctiveQuery> getAllDatalogRules()
           
 java.util.ArrayList<Clause> getAllNormalizedClauses()
          Return all normalized clauses.
 java.util.HashSet<Predicate> getAllPred()
          Return the set of all predicates.
 java.util.ArrayList<Predicate> getAllPredOrderByName()
           
 java.util.ArrayList<Clause> getAllUnnormalizedClauses()
          Return all unnormalized clauses as read from the input file.
 Clause getClauseById(int id)
          Get the clause object by integer ID.
 RDB getDB()
           
 Function getFunctionByName(java.lang.String name)
          Get a function by its name; can be built-in.
 int getID()
           
 Type getOrCreateTypeByName(java.lang.String name)
          Return the type of a given name; create if this type does not exist.
 Predicate getPredByName(java.lang.String name)
          Return the predicate of the given name; null if such predicate does not exist.
 RDB getRDB()
          Returns the RDB used by this MLN.
 java.util.HashSet<Clause> getRelevantClauses()
          Returns the set of relevant clauses.
 int getSymbolID(java.lang.String symbol, Type type)
          Return assigned ID of a constant symbol.
 boolean isScoped(Predicate p)
          Test whether a predicate is scoped
 void loadEvidences(java.lang.String[] evidFiles)
          Parse multiple MLN evidence files.
 void loadPrograms(java.lang.String[] progFiles)
          Parse multiple MLN program files.
 void loadProgramsButNotNormalizeClauses(java.lang.String[] progFiles)
           
 void loadQueries(java.lang.String[] queryFiles)
          Parse multiple MLN query files.
private  void materializeAllTypes(RDB adb)
          Call materialize() for all types.
 void materializeTables()
          Stores constants and evidence into database table.
 void normalizeClauses()
          Normalize all clauses.
 void parseQueryCommaList(java.lang.String queryAtoms)
          Read in the query atoms provided by the command line.
 void prepareDB(RDB adb)
          Prepare the database for each predicate and clause.
 void registerClause(Clause c)
          Registers a new, unnormalized clause.
 void registerDatalogRule(ConjunctiveQuery cq)
          Add a datalog rule
 void registerIntermediateRule(ConjunctiveQuery cq)
           
 void registerPostprocRule(ConjunctiveQuery cq)
           
 void registerPred(Predicate p)
          Register a new predicate.
 void registerScopingRule(ConjunctiveQuery cq)
          Add a scoping rule
 void setClauseAsRelevant(Clause c)
          Marks a clause as relevant.
 void setDB(RDB adb)
           
 void storeAllEvidence()
          Store all evidences into the database by flushing the "buffers".
 void storeAllQueries()
          Ground and store all query atoms.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

idGen

private static int idGen

id

private int id

db

private RDB db
The db connection associated with this MLN.


relClauses

public java.lang.String relClauses
Database tables storing intermediate data.


relAtoms

public java.lang.String relAtoms

relTrueAtoms

public java.lang.String relTrueAtoms

relClausePart

public java.lang.String relClausePart

relAtomPart

public java.lang.String relAtomPart

parser

private InputParser parser
Parser of input.


listPred

private java.util.ArrayList<Predicate> listPred
List of all predicates appearing in this MLN.


nameMapPred

private java.util.Hashtable<java.lang.String,Predicate> nameMapPred
Map from string name to Predicate object.


nameMapFunc

private java.util.Hashtable<java.lang.String,Function> nameMapFunc

nameMapType

private java.util.Hashtable<java.lang.String,Type> nameMapType
Map from string name to Type object.


relevantClauses

private java.util.HashSet<Clause> relevantClauses
List of clauses marked as relevant.


unnormal2normal

private java.util.HashMap<Clause,Clause> unnormal2normal

listClauses

private java.util.ArrayList<Clause> listClauses
List of normalized clauses.


unnormalizedClauses

public java.util.ArrayList<Clause> unnormalizedClauses
List of unnormalized clauses.


sigMap

private java.util.Hashtable<java.lang.String,Clause> sigMap
Map from signature of clauses to Clause object. For the definition of ``signature'', see Clause.normalize().


mapConstantID

private java.util.HashMap<java.lang.String,java.lang.Integer> mapConstantID
Map from string name to integer constant ID.


scopes

private java.util.HashMap<Predicate,java.util.ArrayList<ConjunctiveQuery>> scopes

scopingRules

private java.util.ArrayList<ConjunctiveQuery> scopingRules

clusteringPredicates

private java.util.ArrayList<Predicate> clusteringPredicates

dedupalogRules

public java.util.HashSet<ConjunctiveQuery> dedupalogRules

datalogRules

java.util.ArrayList<ConjunctiveQuery> datalogRules

intermediateRules

java.util.ArrayList<ConjunctiveQuery> intermediateRules

postprocRules

java.util.ArrayList<ConjunctiveQuery> postprocRules
Constructor Detail

MarkovLogicNetwork

public MarkovLogicNetwork()
Constructor of MLN. parser will be constructed here.

Method Detail

getDB

public RDB getDB()

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

getAllDatalogRules

public java.util.ArrayList<ConjunctiveQuery> getAllDatalogRules()

getRDB

public RDB getRDB()
Returns the RDB used by this MLN.


getID

public int getID()

setClauseAsRelevant

public void setClauseAsRelevant(Clause c)
Marks a clause as relevant. Called by KBMC.

See Also:
KBMC.run()

getRelevantClauses

public java.util.HashSet<Clause> getRelevantClauses()
Returns the set of relevant clauses.


registerClause

public void registerClause(Clause c)
Registers a new, unnormalized clause.

Parameters:
c - the clause to be registered

registerScopingRule

public void registerScopingRule(ConjunctiveQuery cq)
Add a scoping rule

Parameters:
cq -

registerDatalogRule

public void registerDatalogRule(ConjunctiveQuery cq)
Add a datalog rule

Parameters:
cq -

registerPostprocRule

public void registerPostprocRule(ConjunctiveQuery cq)

registerIntermediateRule

public void registerIntermediateRule(ConjunctiveQuery cq)

isScoped

public boolean isScoped(Predicate p)
Test whether a predicate is scoped


applyScopeForPred

private void applyScopeForPred(Predicate p)
Execute the scoping rules for a predicate

Parameters:
p - the target predicate

executeAllDatalogRules

public void executeAllDatalogRules()
Execute all Datalog rules


executeAllPostprocRules

public void executeAllPostprocRules()
Execute all Postprocessing rules


executeAllIntermediateRules

public void executeAllIntermediateRules()

applyAllScopes

public boolean applyAllScopes()
Execute all scoping rules

Returns:
true iff there is at least one scoping rule

getClauseById

public Clause getClauseById(int id)
Get the clause object by integer ID. Accepts negative id, and will translate it into positive. Does not accept zero id or id larger than the number of clauses, and will return null.

Parameters:
id - ID of wanted clause.

normalizeClauses

public void normalizeClauses()
Normalize all clauses. If the signature of this clause is as the same as some some existing clauses in listClauses, then Clause.absorb(Clause) this new clause. If not absorbed, this new clause is set an ID sequentially and a name Clause$id. Predicates in this clause is registered by Predicate.addRelatedClause(Clause).

See Also:
Clause.normalize(), Clause.absorb(Clause)

finalizeClauseDefinitions

public void finalizeClauseDefinitions(RDB adb)
Finalize the definitions of all clauses, i.e., prepare the database table used by each clause, including 1) instance table for each clause; 2) SQL needed to ground this clause. Call this when all clauses have been parsed.


getOrCreateTypeByName

public Type getOrCreateTypeByName(java.lang.String name)
Return the type of a given name; create if this type does not exist.


materializeAllTypes

private void materializeAllTypes(RDB adb)
Call materialize() for all types. This will put the domain members of each type into corresponding database tables.

See Also:
Type.storeConstantList(RDB)

getAllPred

public java.util.HashSet<Predicate> getAllPred()
Return the set of all predicates.


getAllPredOrderByName

public java.util.ArrayList<Predicate> getAllPredOrderByName()

registerPred

public void registerPred(Predicate p)
Register a new predicate. Here by ``register'' it means 1) set ID for this predicate sequentially; 2) push it into listPred; 3) building the map from predicate name to this predicate.


getPredByName

public Predicate getPredByName(java.lang.String name)
Return the predicate of the given name; null if such predicate does not exist.


getFunctionByName

public Function getFunctionByName(java.lang.String name)
Get a function by its name; can be built-in.

Parameters:
name -

getAllUnnormalizedClauses

public java.util.ArrayList<Clause> getAllUnnormalizedClauses()
Return all unnormalized clauses as read from the input file.


getAllNormalizedClauses

public java.util.ArrayList<Clause> getAllNormalizedClauses()
Return all normalized clauses.


getSymbolID

public int getSymbolID(java.lang.String symbol,
                       Type type)
Return assigned ID of a constant symbol. If this symbol is a new one, a new ID will be assigned to it, and the symbol table will be updated.


storeAllQueries

public void storeAllQueries()
Ground and store all query atoms.

See Also:
Predicate.addQuery(Atom), Predicate.groundAndStoreAtom(Atom)

storeAllEvidence

public void storeAllEvidence()
Store all evidences into the database by flushing the "buffers". These tuples are pushed into the relational table Predicate.getRelName() in the database.


closeFiles

public void closeFiles()
Close all file handles used by each predicate in listPred.


loadPrograms

public void loadPrograms(java.lang.String[] progFiles)
Parse multiple MLN program files.

Parameters:
progFiles - list of MLN program files (in Alchemy format)

loadProgramsButNotNormalizeClauses

public void loadProgramsButNotNormalizeClauses(java.lang.String[] progFiles)

loadEvidences

public void loadEvidences(java.lang.String[] evidFiles)
Parse multiple MLN evidence files. If file size is larger than 1MB, then uses a file stream incrementally parse this file. Can also accept .gz file (see GZIPInputStream.GZIPInputStream(InputStream)).

Parameters:
evidFiles - list of MLN evidence files (in Alchemy format)

loadQueries

public void loadQueries(java.lang.String[] queryFiles)
Parse multiple MLN query files.

Parameters:
queryFiles - list of MLN query files (in Alchemy format)

parseQueryCommaList

public void parseQueryCommaList(java.lang.String queryAtoms)
Read in the query atoms provided by the command line.


prepareDB

public void prepareDB(RDB adb)
Prepare the database for each predicate and clause.

See Also:
Predicate.prepareDB(RDB), finalizeClauseDefinitions(RDB)

setDB

public void setDB(RDB adb)

cleanUp

public boolean cleanUp()
Clean up temporary data in DB and working dir, including 1) drop schema in PostgreSQL; 2) remove directory.

Returns:
true on success

materializeTables

public void materializeTables()
Stores constants and evidence into database table.

See Also:
materializeAllTypes(RDB), storeAllEvidence()