ASSIGN = 'assign'
class VarInfo(object):
class GoalInfo(object):
class BPCompiler(object):
  def __init__(self,v):
  def __repr__(self):
  def __init__(self,j):
  def __str__(self):
    def _ss(s): return "<" + ",".join(map(str,s)) + ">"
  def __repr__(self):
  def __init__(self,lhsMode,tensorlogProg,depth,rule):
  def getFunction(self):
  def showVars(self):
      def _gs(j):
  def showRule(self):
    def goalStr(j): return str(self.goalDict[j])
  def showOps(self):
  def inferredTypes(self):
  def compile(self):
  def reorderRHS(self,rule,lhsMode):
    def readyToExecute(goal):
    def varsIn(goal):
  def validateRuleBeforeAnalysis(self):
  def inferFlow(self):
  def inferTypes(self):
    def informativeType(t): return (t is not None) and (t != matrixdb.THING)
  def compileDefinedPredicates(self):
  def toMode(self,j):
    def argIOMode(x):
  def collectInputTypes(self,j):
  def generateOps(self):
    def addOp(op,traceDepth,msgFrom,msgTo):
      def jToGoal(msg): return str(self.goals[msg]) if type(msg)==type(0) else msg
    def makeMessageName(stem,v,j=None,j2=None):
    def msgGoal2Var(j,v,traceDepth):
        def hasOutputVarUsedElsewhere(gin):
    def msgVar2Goal(v,j,traceDepth):
def _only(c): class Config(object):
class ConfigHelp(object):
    def __init__(self):
    def pprint(self,depth=0):
    def _explain(self,depth,key,val): def parseCommandLine(argv,extraArgConsumer=None,extraArgSpec=[],extraArgUsage=[]):
    def usage():
    def status(msg): logging.info('%s time %.3f sec mem %.3f Gb' % (msg,time.time()-startTime,util.memusage()))
def isUncachefromSrc(s): return s.find("|")>=0
def getCacheSrcPair(s): return s.split("|")
def makeCacheSrcPair(s1,s2): return "%s|%s" % (s1,s2)
def parseDatasetSpec(spec,db):
def parseDBSpec(spec):
def parseProgSpec(spec,db,proppr=False): def asMode(spec):
    def __init__(self,goal):
    def args(self):
    def arg(self,i):
    def getArity(self):
    def getFunctor(self):
    def __str__(self):
    def __repr__(self):
    def __hash__(self):
    def __eq__(self,other):
    def __init__(self,goal,strict=True):
    def isInput(self,i):
    def isOutput(self,i):
    def isConst(self,i):
    def __str__(self):
    def __init__(self,goal):
    def getType(self,i):
    def typeSet(self):
class AbstractDeclaration(object):
class ModeDeclaration(AbstractDeclaration):
class TypeDeclaration(AbstractDeclaration): class Function(object):
class OpSeqFunction(Function):
class NullFunction(Function):
class LogFunction(Function):
class SumFunction(Function):
class SoftmaxFunction(Function):
    def __init__(self):
    def eval(self,db,values,pad):
    def backprop(self,delta,gradAccum,pad):
    def _checkDuplications(self):
    def install(self,nextId=1):
    def copy(self):
    def pprint(self,depth=0):
    def pprintSummary(self):
    def pprintComment(self):
    def children(self):
    def __init__(self,opInputs,opOutput,ops,rule=None,inputTypes=None,outputType=None):
    def __repr__(self):
    def pprintSummary(self):
    def pprintComment(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,lhsMode):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,fun):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,funs):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,fun):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,pad):
    def children(self):
    def copy(self): NULL_ENTITY_NAME = dbschema.NULL_ENTITY_NAME
THING = dbschema.THING
OOV_ENTITY_NAME = dbschema.OOV_ENTITY_NAME
TRAINABLE_DECLARATION_FUNCTOR = 'trainable'
class MatrixDB(object):
  def __init__(self,initSchema=None):
  def checkTyping(self):
  def isTypeless(self):
  def _fillDefault(self,typeName):
  def dim(self,typeName=None):
  def onehot(self,s,typeName=None,outOfVocabularySymbolsAllowed=False):
  def zeros(self,numRows=1,typeName=None):
  def ones(self,typeName=None):
  def nullMatrix(self,numRows=1,typeName=None,numCols=0):
  def transposeNeeded(mode,transpose=False):
  def matrix(self,mode,transpose=False):
  def vector(self,mode):
  def matrixPreimage(self,mode):
  def matrixPreimageMat(self,mode):
  def matrixPreimageOnesType(self,mode):
  def matrixPreimageOnes(self,mode):
  def isParameter(self,mode):
  def markAsParam(self,functor,arity):
  def markAsParameter(self,functor,arity):
  def clearParameterMarkings(self):
  def getParameter(self,functor,arity):
  def parameterIsInitialized(self,functor,arity):
  def setParameter(self,functor,arity,replacement):
  def asSymbol(self,symbolId,typeName=None):
  def asSymbolId(self,symbol,typeName=None):
  def rowAsSymbolDict(self,row,typeName=None):
  def arrayAsSymbolDict(self,arr,typeName=None):
  def matrixAsSymbolDict(self,m,typeName=None):
  def matrixAsPredicateFacts(self,functor,arity,m):
  def inDB(self,functor,arity):
  def summary(self,functor,arity):
  def listing(self):
  def numMatrices(self):
  def size(self):
  def parameterSize(self):
  def createPartner(self):
  def serialize(self,direc):
  def serializeDataTo(self,fileLike,filter=None):
  def importSerializedDataFrom(self,fileLike):
  def deserializeDataFrom(fileLike):
  def _saveMatDictWithScipy(fileLike,d):
  def _restoreMatDictWithScipy(fileLike):
  def deserialize(direc):
  def uncache(dbFile,factFile,initSchema=None):
  def addLines(self,lines):
  def loadFile(filenames,initSchema=None):
  def startBuffers(self):
  def bufferFile(self,filename):
  def flushBuffers(self):
  def _flushBuffer(self,functor,arity):
  def _bufferTriplet(self,functor,arity,a1,a2,w,filename,k):
  def _bufferLine(self,line,filename,k):
    def _atof(s): NONETYPE=type(None) 
        D = SS.csr_matrix((mat.data,mat.indices-loIndex,mat.indptr),shape=newShape,dtype='float32').todense()
    D1 = SS.csr_matrix((m1.data,m1.indices-loIndex,m1.indptr),shape=newShape1,dtype='float32').todense()
    D2 = SS.csr_matrix((m2.data,m2.indices-loIndex,m2.indptr),shape=newShape2,dtype='float32').todense()
def summary(mat):
def pprintSummary(mat):
def checkCSR(mat,context='unknown'):
def checkNoNANs(mat,context='unknown'):
def maxValue(mat):
def densify(mat,maxExpandFactor=-1,maxExpandIntercept=-1):
def denseSize(m,loIndex,hiIndex):
def sparseSize(m,loIndex,hiIndex):
def codensify(m1,m2,maxExpandFactor=-1,maxExpandIntercept=-1):
def undensify(denseMat, info):
def mean(mat):
def rowsum(mat):
def mapData(dataFun,mat):
def stack(mats):
def numRows(m):
def numCols(m):
def nzCols(m,i):
def repeat(row,n):
def alterMatrixRows(mat,alterationFun):
def softmax(db,mat):
        def softMaxAlteration(data,lo,hi,unused):
def denseSoftmax(m):
def broadcastAndComponentwiseMultiply(m1,m2):
def multiplyByBroadcastRowVec(m,v):
def broadcastAndWeightByRowSum(m1,m2):
def shuffleRows(m,shuffledRowNums=None):
def selectRows(m,lo,hi): class OperatorOrFunction(object):
class MutableObject(object):
class Scratchpad(object):
class Envir(object):
    def pprint(self,depth=0):
    def pprintSummary(self):
    def pprintComment(self):
    def install(self,nextId=1):
    def children(self):
    def __init__(self):
    def __getitem__(self,key):
    def __setitem__(self,key,val):
    def __init__(self,db):
    def bindList(self,vars,vals):
    def __repr__(self):
    def __getitem__(self,key):
    def __setitem__(self,key,val): def isProcessedConstant(a):
def isProcessedVariable(a):
def isVariableAtom(a):
    def __init__(self,functor,args):
    def _setArgs(self,args):
    def __str__(self):
    def __repr__(self):
    def __init__(self,lhs,rhs,features=None,findall=None):
    def variabilize(self):
            def convertArgs(args):
            def convertGoal(g):
    def __str__(self):
    def asString(self,syntax=None):
    def __init__(self,syntax=None):
    def _key(self,g):
    def add(self,r):
    def size(self):
    def rulesFor(self,g):
    def mapRules(self,mapfun):
    def listing(self):
    def __iter__(self):
    def equals(self,other):
  def __init__(self,syntax=None):
  def setSyntax(self,syntax):
  def _convertGoal(self,ptree):
  def _convertRule(self,ptree):
  def parseGoal(self,s):
  def parseGoalList(self,s):
  def parseRule(self,s):
  def parseQuery(self,s):
  def parseFile(self,filename,rules=None):
  def parseStream(self,fileLike,rules=None):
class Goal(object):
class Rule(object):
class RuleCollection(object):
class Parser(object): def memusage():
def linesIn(fileLike):
import sys
import logging
import collections
import numpy as np
import os
from tensorlog import bpcompiler
from tensorlog import config
from tensorlog import comline
from tensorlog import declare
from tensorlog import funs
from tensorlog import matrixdb
from tensorlog import mutil
from tensorlog import opfunutil
from tensorlog import parser
from tensorlog import util
        else:
            predDef = self.findPredDef(mode)
            if predDef is None or len(list(predDef))==0:
                assert False,'no rules match mode %s' % mode
            elif len(predDef)==1:
                #instead of a sum of one function, just find the function
                #for this single predicate
                c = bpcompiler.BPCompiler(mode,self,depth,predDef[0])
                self.function[(mode,depth)] = c.getFunction()
            else:
                #compute a function that will sum up the values of the
                #clauses
                ruleFuns = [bpcompiler.BPCompiler(mode,self,depth,r).getFunction() for r in predDef]
                self.function[(mode,depth)] = funs.SumFunction(ruleFuns)
            if depth==0:
                if self.normalize=='softmax':
                    self.function[(mode,0)] = funs.SoftmaxFunction(self.function[(mode,0)])
                elif self.normalize=='log+softmax':
                    self.function[(mode,0)] = funs.SoftmaxFunction(funs.LogFunction(self.function[(mode,0)]))
                elif self.normalize=='none':
                    pass
                else:
                    assert not self.normalize, 'bad value of self.normalize: %r' % self.normalize
                # label internal nodes/ops of function with ids
                self.function[(mode,0)].install()
        return self.function[(mode,depth)]

    def getPredictFunction(self,mode):
        if (mode,0) not in self.function: self.compile(mode)
        fun = self.function[(mode,0)]
        return fun

    def getParamList(self):
        """ Return a set of (functor,arity) pairs corresponding to the parameters """
        return self.db.paramList

    def getFunction(self,mode):
        """ Return the compiled function for a mode """
        if (mode,0) not in self.function: self.compile(mode)
        return self.function[(mode,0)]

    def evalSymbols(self,mode,symbols,typeName=None):
        """ After compilation, evaluate a function.  Input is a list of
        symbols that will be converted to onehot vectors, and bound to
        the corresponding input arguments.
        """
        return self.eval(mode, [self.db.onehot(s,typeName=typeName) for s in symbols])

    def eval(self,mode,inputs):
        """ After compilation, evaluate a function.  Input is a list of onehot
        vectors, which will be bound to the corresponding input
        arguments.
        """
        if (mode,0) not in self.function: self.compile(mode)
        fun = self.function[(mode,0)]
        return fun.eval(self.db, inputs, opfunutil.Scratchpad())

    def evalGradSymbols(self,mode,symbols):
        """ After compilation, evaluate a function.  Input is a list of
        symbols that will be converted to onehot vectors, and bound to
        the corresponding input arguments.
        """
        assert self.db.isTypeless(),'cannot evalSymbols on db with declared types'
        return self.evalGrad(mode, [self.db.onehot(s) for s in symbols])

    def evalGrad(self,mode,inputs):
        """ After compilation, evaluate a function.  Input is a list of onehot
        vectors, which will be bound to the corresponding input
        arguments.
        """
        if (mode,0) not in self.function: self.compile(mode)
        fun = self.function[(mode,0)]
        return fun.evalGrad(self.db, inputs)

    def setAllWeights(self):
        """ Set all parameter weights to a plausible value - mostly useful for proppr programs,
        where parameters are known. """
        logging.debug('setting feature weights %.3f Gb' % util.memusage())
        self.setFeatureWeights()
        logging.debug('setting rule weights %.3f Gb' % util.memusage())
        self.setRuleWeights()
        self.db.checkTyping()

    def setFeatureWeights(self,epsilon=1.0):
        """ Set feature weights to a plausible value - mostly useful for proppr programs,
        where parameters are known. """
        logging.warn('trying to call setFeatureWeights on a non-ProPPR program')

    def setRuleWeights(self,weights=None,epsilon=1.0):
        """ Set rule feature weights to a plausible value - mostly useful for proppr programs,
        where parameters are known. """
        logging.warn('trying to call setFeatureWeights on a non-ProPPR program')

    @staticmethod
    def _loadRules(fileNames):
        ruleFiles = fileNames.split(":")
        rules = parser.Parser().parseFile(ruleFiles[0])
        for f in ruleFiles[1:]:
            rules = parser.Parser().parseFile(f,rules)
        return rules

    @staticmethod
    def loadRules(fileNames,db,plugins=None):
        return Program(db,Program._loadRules(fileNames),plugins=plugins)

    def serialize(self,direc):
      """ Serialize the rules to a file-like object
      """
      if self.plugins and not self.plugins.isempty():
        logging.warn('plugins can NOT be serialized in %r, so semantics after deserialization might be different!' % direc)
      if not os.path.exists(direc):
        os.makedirs(direc)
      with open(os.path.join(direc,"rules.tlog"),'w') as fp:
        self.serializeRulesTo(fp)
      self.db.serialize(os.path.join(direc,"database.db"))

    @staticmethod
    def deserialize(direc):
      """ Serialize the rules to a file-like object
      """

      db =  matrixdb.MatrixDB.deserialize(os.path.join(direc,"database.db"))