# Path: tensorlog/declare.py
# 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):
# 
# Path: tensorlog/comline.py
# 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):
# 
# Path: tensorlog/matrixdb.py
# 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):


import sys
import getopt
from tensorlog import declare
from tensorlog import comline
from tensorlog import matrixdb


if __name__ == "__main__":
    def usage():
        print("usage: python -m list --db dbspec [--mode mode]")
        print("  without mode specified: lists the relations in the database")
        print("  with mode specified: lists the facts in one relation in .cfacts format")
        print("usage: python -m list --prog progspec [--ruleIds]")
        print("  list the all rule ids")
    argspec = ["db=","mode=","prog=","ruleIds"]
    try:
        optlist,args = getopt.getopt(sys.argv[1:], 'x', argspec)
    except getopt.GetoptError:
        usage()
        raise
    optdict = dict(optlist)

    db = comline.parseDBSpec(optdict['--db']) if '--db' in optdict else None
    if db and (not '--mode' in optdict):
        db.listing()
    elif db and ('--mode' in optdict):
        functor,rest = optdict['--mode'].split("/")
        arity = int(rest)
        m = db.matEncoding.get((functor,arity))
        assert m is not None,'mode should be of the form functor/arity for something in the database'
        for goal,weight in list(db.matrixAsPredicateFacts(functor,arity,m).items()):
            print(('\t'.join([goal.functor] + goal.args + ['%g' % (weight)])))
    elif '--prog' in optdict:

# Path: tensorlog/declare.py
# 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):
# 
# Path: tensorlog/comline.py
# 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):
# 
# Path: tensorlog/matrixdb.py
# 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):


# Path: tensorlog/list.py
import sys
import getopt
from tensorlog import declare
from tensorlog import comline
from tensorlog import matrixdb



if __name__ == "__main__":
    def usage():
        print("usage: python -m list --db dbspec [--mode mode]")
        print("  without mode specified: lists the relations in the database")
        print("  with mode specified: lists the facts in one relation in .cfacts format")
        print("usage: python -m list --prog progspec [--ruleIds]")
        print("  list the all rule ids")
    argspec = ["db=","mode=","prog=","ruleIds"]
    try:
        optlist,args = getopt.getopt(sys.argv[1:], 'x', argspec)
    except getopt.GetoptError:
        usage()
        raise
    optdict = dict(optlist)

    db = comline.parseDBSpec(optdict['--db']) if '--db' in optdict else None
    if db and (not '--mode' in optdict):
        db.listing()
    elif db and ('--mode' in optdict):
        functor,rest = optdict['--mode'].split("/")
        arity = int(rest)
        m = db.matEncoding.get((functor,arity))
        assert m is not None,'mode should be of the form functor/arity for something in the database'
        for goal,weight in list(db.matrixAsPredicateFacts(functor,arity,m).items()):
            print(('\t'.join([goal.functor] + goal.args + ['%g' % (weight)])))
    elif '--prog' in optdict:

        prog = comline.parseProgSpec(optdict['--prog'],db,proppr=True)