Example #1
0
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)
Example #2
0
 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
Example #3
0
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()            
Example #4
0
 def fromElements(self, elements):
     rule = Rule()
     rule.head = Atom.fromElements(elements[0])
     rule.body = Query.fromElements(elements[1])
     return rule