def test_hasVariables(): import Parser pred = "A(X,a,b,c)" predf = "A(a,d,e)" pred = Parser._parse_pred(pred) predf = Parser._parse_pred(predf) assert pred.hasVariables() == True assert predf.hasVariables() == False
def test_search(): import Parser source = "A(a):." CE = Parser._parse(source) assert search(CE, Parser._parse_pred('A(a)')) == True source = "A(X):." CE = Parser._parse(source) assert search(CE, Parser._parse_pred('A(a)')) == True
def test_search_unknown_not_true(): import Parser source = "A(X):B(X).\n A(c):." Pred = Parser._parse_pred('A(a)') CE = Parser._parse(source) assert search(CE, Pred) == 'Unknown' source = "A(a):B(X).\n A(c):." Pred = Parser._parse_pred('A(a)') CE = Parser._parse(source) assert search(CE, Pred) == 'Unknown'
def test_search_and_unify(): import Parser source = "A(a):B(X),C(X).\nB(c):.\nC(d):." Pred = Parser._parse_pred('A(a)') CE = Parser._parse(source) assert search(CE, Pred) is not True assert search(CE, Pred) is not False assert search(CE, Pred) == 'Unknown' source = "A(a):B(X),C(X).\nB(c):.\nC(c):." Pred = Parser._parse_pred('A(a)') CE = Parser._parse(source) assert search(CE, Pred) is True
def test_unify(): import Parser source = "A(X):." CE = Parser._parse(source) for state in CE: mapping = state.determines(Parser._parse_pred('A(a)')) assert len(mapping) > 0 for item in mapping: assert state.unify(item) != state
def test_search_compl_neg(): import Parser source = "A(X):!B(b).\n!B(b):." Pred = Parser._parse_pred('A(a)') b = Parser._parse_pred('!B(b)') CE = Parser._parse(source) assert search(CE, b) == True count_det = 0 for state in CE: if state.determines(Pred) is not None: count_det += 1 mapping = state.determines(Pred) new_state = state.unify(mapping[0]) assert count_det == 1 assert new_state.true(CE) == True assert search(CE, Pred) == True
def default(self, line): if len(line) == 0: return if line[-1] == '?': print Search.search(self.CE, Parser._parse_pred(line[:-1])) return try: self.CE.update(Parser._parse(line)) print 'Accepted' except: print traceback.format_exc()
def test_search_chaining_sub(): import Parser source = "A(a):.\nB(X):A(X).\nC(X),D(X):A(X)." CE = Parser._parse(source) count_det = 0 for state in CE: if state.determines(Parser._parse_pred('C(a)')) is not None: count_det += 1 mapping = state.determines(Parser._parse_pred('C(a)')) new_state = state.unify(mapping[0]) #print mapping #print new_state assert search(CE, Parser._parse_pred('B(a)')) == True assert search(CE, Parser._parse_pred('!B(a)')) == False assert search(CE, Parser._parse_pred('C(a)')) == True assert search(CE, Parser._parse_pred('!C(a)')) == False assert search(CE, Parser._parse_pred('C(b)')) == 'Unknown' assert search(CE, Parser._parse_pred('!C(b)')) == 'Unknown'
def test_recurse(): import Parser source = "A(a):A(a)." CE = Parser._parse(source) assert search(CE, Parser._parse_pred('A(a)')) == 'Unknown'
def test_determines(): import Parser source = "A(a):." CE = Parser._parse(source) for state in CE: assert state.determines(Parser._parse_pred('A(a)')) == [{}]