Beispiel #1
0
 def __init__(self,
              backend: Backend,
              output_atoms: OutputTable,
              facts: Iterable[Fact] = None):
     self._backend = backend
     self._map = {}
     for atom, sym in output_atoms.items():
         assert atom not in self._map
         self._map[atom] = self._backend.add_atom(sym)
     if facts is not None:
         for fact in facts:
             backend.add_rule([backend.add_atom(fact.symbol)])
Beispiel #2
0
    def add_to_backend(self,
                       backend: Backend,
                       mapping: Optional[AtomMap] = None) -> 'Program':
        '''
        Add the program to the given backend with an optional mapping.

        Note that the output table cannot be added to the backend for technical
        reasons. This has to be taken care of by the user. See for example the
        `Remapping` class, which provides functionality for this.

        Parameters
        ----------
        backend
            The backend.
        mapping
            A mapping function to remap literals.

        Returns
        -------
        A reference to self.

        See Also
        --------
        add_to_backend
        '''

        _add_stms_to_backend(self.shows, backend, mapping)
        _add_stms_to_backend(self.facts, backend, mapping)
        _add_stms_to_backend(self.rules, backend, mapping)
        _add_stms_to_backend(self.weight_rules, backend, mapping)
        _add_stms_to_backend(self.heuristics, backend, mapping)
        _add_stms_to_backend(self.edges, backend, mapping)
        _add_stms_to_backend(self.minimizes, backend, mapping)
        _add_stms_to_backend(self.externals, backend, mapping)
        if self.projects is not None:
            if self.projects:
                _add_stms_to_backend(self.projects, backend, mapping)
            else:
                backend.add_project([])

        backend.add_assume([
            _remap_lit(lit, mapping) if mapping else lit
            for lit in self.assumptions
        ])

        return self
Beispiel #3
0
def _add_to_backend_heuristic(stm: Heuristic, backend: Backend) -> None:
    '''
    Add a heurisitic statement to the backend.
    '''
    backend.add_heuristic(stm.atom, stm.type_, stm.bias, stm.priority,
                          stm.condition)
Beispiel #4
0
def _add_to_backend_edge(stm: Edge, backend: Backend) -> None:
    '''
    Add an edge statement to the backend remapping its literals.
    '''
    backend.add_acyc_edge(stm.u, stm.v, stm.condition)
Beispiel #5
0
def _add_to_backend_minimize(stm: Minimize, backend: Backend):
    '''
    Add a minimize statement to the backend.
    '''
    backend.add_minimize(stm.priority, stm.literals)
Beispiel #6
0
def _add_to_backend_external(stm: External, backend: Backend):
    '''
    Add an external statement to the backend remapping its atom.
    '''
    backend.add_external(stm.atom, stm.value)
Beispiel #7
0
def _add_to_backend_project(stm: Project, backend: Backend):
    '''
    Add a project statement to the backend.
    '''
    backend.add_project([stm.atom])
Beispiel #8
0
def _add_to_backend_weight_rule(stm: WeightRule, backend: Backend) -> None:
    '''
    Add a weight rule to the backend.
    '''
    backend.add_weight_rule(stm.head, stm.lower_bound, stm.body, stm.choice)
Beispiel #9
0
def _add_to_backend_rule(stm: Rule, backend: Backend) -> None:
    '''
    Add a rule to the backend.
    '''
    backend.add_rule(stm.head, stm.body, stm.choice)