Example #1
0
 def main(self, ctl, files):
     with ast.ProgramBuilder(ctl) as bld:
         ptf = ProgramTransformer(Function("__t"))
         ast.parse_files(files, lambda stm: bld.add(ptf(stm)))
     ctl.add("initial", ["t"], "initially(t).")
     ctl.add("static", ["t"], "#external finally(t).")
     self._main(ctl)
Example #2
0
def parse_fact_string(aspstr,
                      unifier,
                      *,
                      factbase=None,
                      raise_nomatch=False,
                      raise_nonfact=False):
    '''Parse a string of ASP facts into a FactBase

    Facts must be of a simple form that can correspond to clorm predicate
    instances. Rules that are NOT simple facts include: any rule with a body, a
    disjunctive fact, a choice rule, a theory atom, a literal with an external
    @-function reference, a literal that requires some mathematical calculation
    (eg., "p(1+1).")

    NOTE: Currently, this function is not safe when running in NOCLINGO mode and
    will raise a NotImplementedError if called.

    Args:
      aspstr: an ASP string containing the facts
      factbase: if no factbase is specified then create a new one
      unifier: a list of clorm.Predicate classes to unify against
      raise_nomatch: raise UnifierNoMatchError on a fact that cannot unify
      raise_nonfact: raise FactParserError on any non simple fact (eg. complex rules)

    '''

    if get_symbol_mode() == SymbolMode.NOCLINGO:
        if not raise_nonfact:
            raise NotImplementedError(
                "Non-fact parsing not supported in NOCLINGO mode")
        return lark_parse_fact_string(aspstr=aspstr,
                                      unifier=unifier,
                                      factbase=factbase,
                                      raise_nomatch=raise_nomatch)

    ctrl = clingo.Control()
    un = Unifier(unifier)
    try:
        if raise_nonfact:
            with clast.ProgramBuilder(ctrl) as bld:
                nfv = NonFactVisitor()

                def on_rule(ast: AST):
                    nonlocal nfv, bld
                    if nfv: nfv(ast)
                    bld.add(ast)

                clast.parse_string(aspstr, on_rule)
        else:
            ctrl.add("base", [], aspstr)

    except ClingoParserWrapperError as e:
        raise e.exc

    ctrl.ground([("base", [])])

    return un.unify([sa.symbol for sa in ctrl.symbolic_atoms if sa.is_fact],
                    factbase=factbase,
                    raise_nomatch=raise_nomatch)
Example #3
0
    def main(self, ctl, files):
        self.__theory.register(ctl)

        with ast.ProgramBuilder(ctl) as bld:
            ast.parse_files(files, lambda stm: self.__theory.rewrite_ast(stm, bld.add))

        ctl.ground([("base", [])])
        self.__theory.prepare(ctl)

        ctl.solve(on_model=self.__on_model, on_statistics=self.__on_statistics)
Example #4
0
    def main(self, prg, files):
        """
        Implements the incremental solving loop.

        This function implements the Application.main() function as required by
        clingo.clingo_main().
        """
        with _ast.ProgramBuilder(prg) as b:
            files = [open(f) for f in files]
            if len(files) == 0:
                files.append(_sys.stdin)
            future_sigs, program_parts = _tf.transform(
                [f.read() for f in files], b.add)

        imain(prg, future_sigs, program_parts, self.__on_model, self.__imin,
              self.__imax, self.__istop)