コード例 #1
0
    def init(self, target_rule, x='x', y='y'):
        """Initializes the grammar.

        A grammar can only be used for sampling after initialization.
        The initialization takes time linear in the size of the grammar.

        Parameters
        ----------
        target_rule : str
            Rule to be sampled from (sampling will also be possible for all
            rules the target depends on).
        x : str, optional (default='x')
            Symbolic x-argument.
        y : str, optional (default='y')
            Symbolic y-argument.
        """
        if target_rule not in self.rules:
            self._missing_rule_error(target_rule)
        self._target_rule, self._target_x, self._target_y = target_rule, x, y
        # Initialize the alias samplers, i.e. set their referenced grammar to
        # this grammar.
        self._init_alias_samplers()
        # Find out which rules are recursive.
        self._find_recursive_rules()
        # Automatically set target class labels of transformation samplers
        # where possible.
        self._infer_target_class_labels()
        # Get the needed oracle queries and check if they are present in the
        # oracle.
        self._initialized = True
        self._needed_oracle_queries = self._collect_oracle_queries()
        if not pybo.BoltzmannSamplerBase.oracle.contains_all(
                self._needed_oracle_queries):
            raise pybo.PyBoltzmannError(
                "The following evals are needed: {}".format(
                    self._needed_oracle_queries))
        # Precompute the evaluations for all intermediate classes.
        self._precompute_evals()
コード例 #2
0
 def __init__(self, base_class_object, marked_atom=None):
     if type(self) is DerivedClass:
         raise pybo.PyBoltzmannError(
             "Instantiate objects of LDerivedClass or UDerivedClass")
     self._base_class_object = base_class_object
     self._marked_atom = marked_atom
コード例 #3
0
 def u_atoms(self):
     raise pybo.PyBoltzmannError(
         "Cannot iterate over atoms from dummy class")
コード例 #4
0
 def _missing_rule_error(alias):
     msg = "Not a rule in the grammar: {}".format(alias)
     raise pybo.PyBoltzmannError(msg)
コード例 #5
0
 def _grammar_not_initialized_error():
     msg = "Grammar not initialized"
     raise pybo.PyBoltzmannError(msg)