本文整理汇总了Python中grammar.Grammar.parseAtom方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.parseAtom方法的具体用法?Python Grammar.parseAtom怎么用?Python Grammar.parseAtom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.parseAtom方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import parseAtom [as 别名]
def main():
webStr = None
queryString = None
opts, args = getopt.getopt(sys.argv[1:], "i:q:", ["input", "query"])
for o, a in opts:
if o == "-i":
webStr = a
elif o == "-q":
queryString = a
if webStr is None or queryString is None:
print "Incorrect usage"
sys.exit(-1)
xsb = XSB()
try:
webStr = webStr.replace("<newline>", "\n")
polStr = "\n".join([l for l in webStr.split("\n") if ":-" in l])
policy = Policy.fromString(escapeCharacters(polStr))
query = Atom.fromElements(Grammar.parseAtom(escapeCharacters(queryString)))
policy.processPolicy()
policy.checkQuery(query)
xsb.loadPolicy(policy)
print xsb.query(query)
xsb.close()
except Exception as e:
print "Error:", e
xsb.close()
sys.exit(-1)
示例2: main
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import parseAtom [as 别名]
def main():
bellogFilename = None
queryString = None
datalogFilename = None
opts, args = getopt.getopt(sys.argv[1:], 'i:q:o:', ['input', 'query'])
for o, a in opts:
if o == '-i':
bellogFilename = a
elif o == '-q':
queryString = a
elif o == '-o':
datalogFilename = a
if bellogFilename is None and (queryString is None or datalogFilename is None):
print 'Usage: python', sys.argv[0], '-i <BelLog file> -q <query> [-o <Datalog filename>]'
sys.exit(-1)
fileStr = open(bellogFilename, 'r').read().strip()
polStr = '\n'.join([l for l in fileStr.split('\n') if ':-' in l])
try:
policy = Policy.fromString(escapeCharacters(polStr))
if queryString is not None:
query = Atom.fromElements(Grammar.parseAtom(escapeCharacters(queryString)))
policy.processPolicy()
except Exception as e:
print 'Error parsing the policy:', e
sys.exit(-1)
if queryString is not None:
xsb = XSB()
try:
policy.checkQuery(query)
xsb.loadPolicy(policy)
print 'Query', queryString, ':', xsb.query(query)
xsb.close()
except Exception as e:
print 'Error loading the policy:', e
sys.exit(-1)
xsb.close()
if datalogFilename is not None:
if os.path.isfile(datalogFilename):
msg = 'Override ' + datalogFilename + '?'
shall = True if raw_input("%s (y/N) " % msg).lower() == 'y' else False
if not shall:
sys.exit(-1)
outFile = open(datalogFilename, 'w')
for rule in policy.rules:
for datalogRule in rule.toDatalogRules():
outFile.write(datalogRule + '.\n')
for datalogRule in XSB.STATIC_RULES:
outFile.write(datalogRule + '.\n')
outFile.close()
示例3: fromString
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import parseAtom [as 别名]
def fromString(cls, s):
return Atom.fromElements(Grammar.parseAtom(s))