def evaluate_base(self, rule, engine, index=None):
     query_database = self.database.extend()
     for clause in rule.asLP(with_substitutions=True):
         query_database += clause
     gp = engine.ground_all(query_database)
     result = SDD.createFrom(gp).evaluate()
     return EvaluationResult(index, result)
示例#2
0
    def test(self) :
        try :
            parser = DefaultPrologParser(ExtendedPrologFactory())
            sdd = SDD.createFrom(PrologFile(filename, parser=parser))

            if logspace :
                semiring = SemiringLogProbability()
            else :
                semiring = SemiringProbability()

            computed = sdd.evaluate(semiring=semiring)
            computed = { str(k) : v for k,v in computed.items() }
        except Exception as err :
            e = err
            computed = None

        if computed is None :
            self.assertEqual(correct, type(e).__name__)
        else :
            self.assertIsInstance( correct, dict )
            self.assertSequenceEqual(correct, computed)

            for query in correct :
                self.assertAlmostEqual(correct[query], computed[query], msg=query)
示例#3
0
def main(filename, with_dot, knowledge):

    dotprefix = None
    if with_dot:
        dotprefix = os.path.splitext(filename)[0] + "_"

    model = PrologFile(filename)

    engine = DefaultEngine(label_all=True)

    with Timer("parsing"):
        db = engine.prepare(model)

    print("\n=== Database ===")
    print(db)

    print("\n=== Queries ===")
    queries = engine.query(db, Term("query", None))
    print("Queries:", ", ".join([str(q[0]) for q in queries]))

    print("\n=== Evidence ===")
    evidence = engine.query(db, Term("evidence", None, None))
    print("Evidence:", ", ".join(["%s=%s" % ev for ev in evidence]))

    print("\n=== Ground Program ===")
    with Timer("ground"):
        gp = engine.ground_all(db)
    print(gp)

    if dotprefix != None:
        with open(dotprefix + "gp.dot", "w") as f:
            print(gp.toDot(), file=f)

    print("\n=== Acyclic Ground Program ===")
    with Timer("acyclic"):
        gp = gp.makeAcyclic()
    print(gp)

    if dotprefix != None:
        with open(dotprefix + "agp.dot", "w") as f:
            print(gp.toDot(), file=f)

    if knowledge == "sdd":
        print("\n=== SDD compilation ===")
        with Timer("compile"):
            nnf = SDD.createFrom(gp)

        if dotprefix != None:
            nnf.saveSDDToDot(dotprefix + "sdd.dot")

    else:
        print("\n=== Conversion to CNF ===")
        with Timer("convert to CNF"):
            cnf = CNF.createFrom(gp)

        print("\n=== Compile to d-DNNF ===")
        with Timer("compile"):
            nnf = DDNNF.createFrom(cnf)

    if dotprefix != None:
        with open(dotprefix + "nnf.dot", "w") as f:
            print(nnf.toDot(), file=f)

    print("\n=== Evaluation result ===")
    with Timer("evaluate"):
        result = nnf.evaluate()

    for it in result.items():
        print("%s : %s" % (it))