本文整理汇总了Python中atom.Atom.fromElements方法的典型用法代码示例。如果您正苦于以下问题:Python Atom.fromElements方法的具体用法?Python Atom.fromElements怎么用?Python Atom.fromElements使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类atom.Atom
的用法示例。
在下文中一共展示了Atom.fromElements方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from atom import Atom [as 别名]
# 或者: from atom.Atom import fromElements [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: fromElements
# 需要导入模块: from atom import Atom [as 别名]
# 或者: from atom.Atom import fromElements [as 别名]
def fromElements(self, elements):
query = Query()
query.subqueries = []
if elements[0] == '!':
# negated query
query.operator = '!'
subquery = Query.fromElements(elements[1])
query.subqueries.append(subquery)
elif elements[0] == '~':
# inverted query
query.operator = '~'
subquery = Query.fromElements(elements[1])
query.subqueries.append(subquery)
elif elements[0] == '(':
# infix query
if elements[2] == '^':
# conjunction
query.operator = '^'
for subqueryElements in elements[1:-1:2]:
subquery = Query.fromElements(subqueryElements)
query.subqueries.append(subquery)
elif elements[2] in ['-false->', '-bot->', '-top->', '-true->']:
return Query.fromElements(Query.getOverride(elements[1], elements[2][1:-2], elements[3]))
elif elements[2] == '-plus-':
return Query.fromElements(Query.getPlus(elements[1], elements[3]))
elif elements[2] == '-times-':
return Query.fromElements(Query.getTimes(elements[1], elements[3]))
elif elements[2] == '<':
return Query.fromElements(['!', ['(', ['!', ['(', Query.getEq(elements[3], 'true'), '^', elements[1], ')'] ], '^', ['!', ['(', ['!', Query.getEq(elements[3], 'true')], '^', elements[5], ')'] ], ')' ] ])
else:
# atomic query
query.operator = ''
query.subqueries.append(Atom.fromElements(elements))
return query
示例3: main
# 需要导入模块: from atom import Atom [as 别名]
# 或者: from atom.Atom import fromElements [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()
示例4: fromElements
# 需要导入模块: from atom import Atom [as 别名]
# 或者: from atom.Atom import fromElements [as 别名]
def fromElements(self, elements):
rule = Rule()
rule.head = Atom.fromElements(elements[0])
rule.body = Query.fromElements(elements[1])
return rule