import clingo def liet_ke(chuong_trinh): bo_giai = clingo.Control(["0"]) bo_giai.add("base", [], chuong_trinh) bo_giai.ground([("base", [])]) nghiem = [] ket_qua = bo_giai.solve(on_model=lambda m: nghiem.append(set(map(str, m.symbols(shown=True))))) if not ket_qua.satisfiable: raise ValueError("Bài toán vô nghiệm") return nghiem nghiem = liet_ke(""" ca(1..2). lop(a). 1 { xep(L,C) : ca(C) } 1 :- lop(L). #show lop/1. #show xep/2. """) co_the = set.union(*nghiem) tat_yeu = set.intersection(*nghiem) assert len(nghiem) == 2 assert co_the == {"lop(a)", "xep(a,1)", "xep(a,2)"} assert tat_yeu == {"lop(a)"} print("Có thể", sorted(co_the), "| Tất yếu", sorted(tat_yeu))