Пример #1
0
 def __str__(self):
     if self.index is None:
         return utils.math('{0} \\to {1}'.format(
             ','.join(self.premises), self.result))
     else:
         return utils.math('{2}: {0} \\to {1}'.format(
             ','.join(self.premises), self.result, self.index))
    def run(self, rules, facts, goal):
        """ Ieško tikslo naudodama tiesioginį išvedimą.
        """
        if goal in facts:   # \ref{fc:pseudo:while_condition}
            self.trace.append('Rąstas tikslas.')
            return True
        were_used = True
        while were_used:                # \ref{fc:pseudo:while_condition}
            were_used = False
            for rule in rules:          # \ref{fc:pseudo:next_rule}
                if hasattr(rule, 'used'):
                    continue
                if (all(premise in facts for premise in rule.premises) and
                        rule.result not in facts):
                                        # \ref{fc:pseudo:if_condition}
                    facts.add(rule.result)
                                        # \ref{fc:pseudo:add_fact}
                    self.trace.append(
                            'Pritaikome taisyklę: {0}. '
                            'Faktų aibė po pritaikymo: '
                            '$\\{{${1}$\\}}$',
                            rule,
                            ', '.join(utils.math(fact) for fact in facts))
                    self.solution.append(rule)
                                        # \ref{fc:pseudo:add_rule}
                    rule.used = True
                    were_used = True

                    if goal in facts:   # \ref{fc:pseudo:while_condition}
                        self.trace.append('Rąstas tikslas.')
                        return True
    def solve(self):
        """ Bando surasti tikslą naudodama tiesioginį išvedimą.
        """

        self.trace = utils.EnumerateEnvironment()
        if self.run(
                self.production_system.rules[:],
                self.production_system.facts.copy(),
                self.production_system.goal,):
            self.file.write('\n\nAtsakymas: ')
            if self.solution:
                self.file.write(
                    ', '.join(
                        utils.math(rule.index)
                        for rule in self.solution))
            else:
                self.file.write(utils.math('\\emptyset'))
        else:
            self.file.write('\n\nIšvedimas neegzistuoja.')
        self.file.write('\n\nAtliktų veiksmų seka:')
        self.file.write(str(self.trace))
    def solve(self):
        """ Bando surasti tikslą naudodama atbulinį išvedimą.
        """
        self.fact_counter = 0
        self.rule_counter = 0
        self.text_counter = 0

        self.counter = 0
        label = 'graph:{0}'.format(int(self.invoke_counter) + 100)
        env = utils.Environment('pythonaienv',
                ('graph|{0}|Semantinis grafas.'.format(label), True))
        self.graph = env
        env.append(
                'digraph G {{ // graph-invoke-bc-solve-1: {0} \n',
                self.invoke_counter)
        env.append('edge [arrowsize="1.5"];\n')

        self.trace = utils.EnumerateEnvironment()
        self.facts = self.production_system.facts
        self.new_facts = set()
        self.add_fact(self.production_system.goal)
        self.solution = self.recursion(
                self.production_system.rules,
                self.production_system.goal,
                {self.production_system.goal},)
        if self.solution is not None:
            self.file.write('\n\nAtsakymas: ')
            if self.solution:
                self.file.write(
                    ', '.join(
                        utils.math(rule.index)
                        for rule in self.solution))
            else:
                self.file.write(utils.math('\\emptyset'))
        else:
            self.file.write('\n\nIšvedimas neegzistuoja.')
            self.solution = ()
        self.file.write('\n\nAtliktų veiksmų seka:')
        self.file.write(str(self.trace))
        env.append('}\n')
        self.file.write((
            '\n\nSemantinis grafas pateiktas \\ref{{{0}}} '
            'paveikslėlyje.\n').format(label))
        self.file.write(str(env) + '\n\n')

        return
        if int(self.invoke_counter) == 26:
            return
        label = 'graph:{0}'.format(int(self.invoke_counter) + 200)
        env = utils.Environment('pythonaienv',
                ('graph|{0}|Grafas.'.format(label), True))
        env.append(
                'digraph G {{ // graph-invoke-bc-solve-2: {0} \n',
                self.invoke_counter)
        env.append('node [fixedsize="true", fontsize=8, '
                   'width="0.2cm", height="0.2cm"];\n')
        env.append('edge [arrowsize="1.5", fontsize=8];\n')
        node = 'node [shape="{0}"]; {1}; \n'
        edge = 'edge [arrowsize="0.7", label="{0}"]; {1} -> {2}; \n'
        edge_solution = (
            'edge [arrowsize="1.5", label="{0}"]; {1} -> {2}; \n')
        rules = set()
        facts = set()
        edges = set()
        def add_fact(fact):
            if fact not in facts:
                env.append(node, 'box', fact)
                facts.add(fact)
        def add_edge(a, b, rule):
            if (a, b) not in edges:
                if rule in self.solution:
                    env.append(edge_solution, rule.index, a, b)
                else:
                    env.append(edge, rule.index, a, b)
                edges.add((a, b))
        for fact in self.production_system.facts:
            add_fact(fact)
        for rule in self.production_system.rules:
            for fact in rule.premises:
                add_fact(fact)
                add_edge(fact, rule.result, rule)
        env.append('}\n')
        self.file.write(str(env))