Ejemplo n.º 1
0
 def compositeToBasicRules(self, pred, body, varOrder = None):
     variables = set()
     rules = []
     rule = Rule()
     passOperator = ''
     for literal in body:
         newLiteral = None        
         if isinstance(literal, string_types):
             for literalString in literal.split('^'):
                 if re.match('[!#]?[a-z]+[_0-9\([a-zA-Z,]+\)]?' , literalString):
                     newLiteral = Literal.fromString(literalString)
                     rule.body.append(newLiteral)
                     variables = variables.union(newLiteral.vars())
                 elif re.match('[!#]', literalString):
                     passOperator = literalString
         else:            
             freshPred = freshPredicate()
             (newLiteralVars, newLiteralRules) = Rule.compositeToBasicRules(freshPred, literal)
             rules += newLiteralRules
             newLiteral = Literal.fromString( passOperator + freshPred + '(' + ','.join(newLiteralVars) + ')')
             rule.body.append(newLiteral)
             variables = variables.union(newLiteral.vars())
             passOperator = ''
     if varOrder is not None:
         rule.head = Atom.fromString(pred + '(' + varOrder + ')')        
     else:
         rule.head = Atom.fromString(pred + '(' + ','.join(map(str, variables)) + ')')
     rules.append(rule)  
     return (variables, rules)
Ejemplo n.º 2
0
 def fromString(self, string):
     newLiteral = Literal()
     if string[0] == '!':
         newLiteral.op = '!'
         newLiteral.atom = Atom.fromString(string[1:])
     elif string[0] == '#':
         newLiteral.op = '#'
         newLiteral.atom = Atom.fromString(string[1:])
     else:
         newLiteral.op = None
         newLiteral.atom = Atom.fromString(string)
     return newLiteral
Ejemplo n.º 3
0
 def fromCompositeString(self, string):
     newRule = Rule()
     newRule.head = Atom.fromString(string.split(':-')[0])
     body = string.split(':-')[1].replace(' ', '')
     expressions = nestedExpr('[', ']').parseString(body).asList()
     (notused, rules) = Rule.compositeToBasicRules(newRule.head.pred, expressions[0], varOrder = ','.join(newRule.head.args))
     return reversed(rules)
Ejemplo n.º 4
0
 def fromString(self, string):
     newRule = Rule()
     string = string.replace(' ','')
     tmp = string.split(':-')
     newRule.head = Atom.fromString(tmp[0])        
     if len(tmp) > 1:
         for literalString in tmp[1].split('^'):
             newRule.body.append(Literal.fromString(literalString))
     return newRule