当前位置: 首页>>代码示例>>Python>>正文


Python Atom.fromElements方法代码示例

本文整理汇总了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)
开发者ID:ptsankov,项目名称:bellog,代码行数:31,代码来源:runWeb.py

示例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
开发者ID:ptsankov,项目名称:bellog,代码行数:36,代码来源:query.py

示例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()            
开发者ID:ptsankov,项目名称:bellog,代码行数:56,代码来源:run.py

示例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
开发者ID:ptsankov,项目名称:bellog,代码行数:7,代码来源:rule.py


注:本文中的atom.Atom.fromElements方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。