Esempio n. 1
0
def test_or_and_fact_inside_or():
    from pyknow import Fact, OR, Rule
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(OR(Fact(a=1), OR(Fact(a=2), Fact(a=3))))
    output = Rule(OR(Fact(a=1), Fact(a=2), Fact(a=3)))
    assert dnf(input_) == output
Esempio n. 2
0
def test_or_inside_and():
    from pyknow import Fact, Rule, AND, OR
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(AND(Fact(a=1), OR(Fact(a=2), Fact(a=3))))
    output = Rule(OR(AND(Fact(a=1), Fact(a=2)), AND(Fact(a=1), Fact(a=3))))
    assert dnf(input_[0]) == output[0]
Esempio n. 3
0
def test_prepare_rule__or_starting_with_not():
    from pyknow import Rule, InitialFact, NOT, Fact, OR, AND

    rule = Rule(OR(NOT(Fact(1)), NOT(Fact(2))))(lambda: None)

    assert list(utils.prepare_rule(rule)) == [
        OR(AND(InitialFact(), NOT(Fact(1))), AND(InitialFact(), NOT(Fact(2))))
    ]
Esempio n. 4
0
def test_or_inside_or_inside_and():
    from pyknow import Fact, OR, AND, Rule
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(AND(Fact(b=1), OR(Fact(a=1), OR(Fact(a=3), Fact(a=4)))))
    output = Rule(OR(AND(Fact(b=1), Fact(a=1)),
                     AND(Fact(b=1), Fact(a=3)),
                     AND(Fact(b=1), Fact(a=4))))
    result = dnf(input_)
    assert result == output
Esempio n. 5
0
def test_multiple_or_inside_rule():
    from pyknow import Fact, OR, Rule, AND
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(Fact(a=1),
                  OR(Fact(b=1),
                     Fact(b=2)),
                  OR(Fact(c=1),
                     Fact(c=2)))
    output_ = Rule(OR(AND(Fact(a=1), Fact(b=1), Fact(c=1)),
                      AND(Fact(a=1), Fact(b=1), Fact(c=2)),
                      AND(Fact(a=1), Fact(b=2), Fact(c=1)),
                      AND(Fact(a=1), Fact(b=2), Fact(c=2))))

    result = dnf(input_)
    assert result == output_
Esempio n. 6
0
def test_not_and_inside_fact():
    from pyknow import Fact, OR, Rule, L
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(Fact(a=~(L(1) & L(2))))
    output = Rule(OR(Fact(a=~L(1)), Fact(a=~L(2))))

    result = dnf(input_)
    assert result == output
Esempio n. 7
0
def test_prepare_rule_is_dnf():
    from pyknow import Rule, NOT, AND, OR, Fact
    from pyknow.matchers.rete.dnf import dnf

    rule = Rule(AND(Fact(0), NOT(OR(Fact(1), Fact(2)))))(lambda: None)

    assert list(
        utils.prepare_rule(rule)) == [Fact(0),
                                      NOT(Fact(1)),
                                      NOT(Fact(2))]
Esempio n. 8
0
    class Test(KnowledgeEngine):
        """ Test KE """
        @DefFacts()
        def some_facts(self):
            yield Fact(other=1)
            yield Fact(other=2)

        @Rule(OR(Fact(something=1), Fact(something=2)))
        def rule1(self):
            """ First rule, something=1 and something=2"""
            pass
Esempio n. 9
0
def test_and_inside_or_inside_fact():
    from pyknow import Fact, OR, Rule, L
    from pyknow.fieldconstraint import ANDFC
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(Fact(a=L(1) | (L(2) & L(3))))
    output = Rule(OR(Fact(a=L(1)),
                     Fact(a=ANDFC(L(2), L(3)))))

    result = dnf(input_)
    assert result == output
Esempio n. 10
0
def test_or_inside_or_inside_fact():
    from pyknow import Fact, OR, Rule, L
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(Fact(a=L(1) | (L(2) | L(3))))
    output = Rule(OR(Fact(a=L(1)),
                     Fact(a=L(2)),
                     Fact(a=L(3))))

    result = dnf(input_)
    assert result == output
Esempio n. 11
0
def test_or_inside_fact():
    from pyknow import Fact, OR, Rule, L
    from pyknow.matchers.rete.dnf import dnf

    input_ = Rule(Fact(a=L(1) | L(2),
                       b=3))
    output = Rule(OR(Fact(a=L(1), b=3),
                     Fact(a=L(2), b=3)))

    result = dnf(input_)
    assert set(result[0]) == set(output[0])
Esempio n. 12
0
 class KE(KnowledgeEngine):
     @Rule(Fact(1), OR(Fact('a'), Fact('b')), OR(Fact('x'), Fact('y')))
     def r1(self):
         pass
Esempio n. 13
0
 class KE(KnowledgeEngine):
     @Rule(EXISTS(OR(Fact(), Fact())))
     def r1(self):
         pass
Esempio n. 14
0
class Bipolar(KnowledgeEngine):
    """
    Coleta dos dados
    """
    @Rule(Sintomas(verifica_humor_deprimido=None))
    def ask_verifica_humor_deprimido(self):
        self.declare(
            Sintomas(
                verifica_humor_deprimido=ask("verifica_humor_deprimido? ")))

    @Rule(Sintomas(fisiologico=None))
    def ask_data_fisiologico(self):
        self.declare(Sintomas(fisiologico=ask("fisiologico? ")))

    @Rule(Sintomas(prejuizo_social=None))
    def ask_data_prejuizo_social(self):
        self.declare(Sintomas(prejuizo_social=ask("prejuizo_social? ")))

    @Rule(Sintomas(prejuiso_profissional=None))
    def ask_data_prejuiso_profissional(self):
        self.declare(
            Sintomas(prejuiso_profissional=ask("prejuiso_profissional? ")))

    @Rule(Sintomas(prejuizo_si=None))
    def ask_data_prejuizo_si(self):
        self.declare(Sintomas(prejuizo_si=ask("prejuizo_si? ")))

    @Rule(Sintomas(prejuizo_outros=None))
    def ask_data_prejuizo_outros(self):
        self.declare(Sintomas(prejuizo_outros=ask("prejuizo_outros? ")))

    @Rule(Sintomas(psicose=None))
    def ask_data_psicose(self):
        self.declare(Sintomas(psicose=ask("psicose? ")))

    @Rule(NOT(Sintomas(autoestima_inflada=W())))
    def ask_data_autoestima_inflada(self):
        self.declare(Sintomas(autoestima_inflada=ask("autoestima_inflada? ")))

    @Rule(NOT(Sintomas(grandiosidade=W())))
    def ask_data_grandiosidade(self):
        self.declare(Sintomas(grandiosidade=ask("grandiosidade? ")))

    @Rule(NOT(Sintomas(loquaz=W())))
    def ask_data_loquaz(self):
        self.declare(Sintomas(loquaz=ask("loquaz? ")))

    @Rule(NOT(Sintomas(pressao_continuar_falando=W())))
    def ask_data_pressao_continuar_falando(self):
        self.declare(
            Sintomas(
                pressao_continuar_falando=ask("pressao_continuar_falando? ")))

    @Rule(NOT(Sintomas(fuga_ideias=W())))
    def ask_data_fuga_ideias(self):
        self.declare(Sintomas(fuga_ideias=ask("fuga_ideias? ")))

    @Rule(NOT(Sintomas(pensamento_acelerado=W())))
    def ask_data_pensamento_acelerado(self):
        self.declare(
            Sintomas(pensamento_acelerado=ask("pensamento_acelerado? ")))

    @Rule(NOT(Sintomas(aumento_atividade_objetivo=W())))
    def ask_data_aumento_atividade_objetivo(self):
        self.declare(
            Sintomas(aumento_atividade_objetivo=ask(
                "aumento_atividade_objetivo? ")))

    @Rule(NOT(Sintomas(agitacao_psicomotora=W())))
    def ask_data_agitacao_psicomotora(self):
        self.declare(
            Sintomas(agitacao_psicomotora=ask("agitacao_psicomotora? ")))

    @Rule(NOT(Sintomas(reducao_sono=W())))
    def ask_data_reducao_sono(self):
        self.declare(Sintomas(reducao_sono=ask("reducao_sono? ")))

    @Rule(NOT(Sintomas(distrabilidade=W())))
    def ask_data_distrabilidade(self):
        self.declare(Sintomas(distrabilidade=ask("distrabilidade? ")))

    @Rule(NOT(Sintomas(envolvimento_atividade_risco=W())))
    def ask_data_envolvimento_atividade_risco(self):
        self.declare(
            Sintomas(envolvimento_atividade_risco=ask(
                "envolvimento_atividade_risco? ")))

    @Rule(NOT(Sintomas(irritavel=W())))
    def ask_data_irritavel(self):
        self.declare(Sintomas(irritavel=ask("irritavel? ")))

    @Rule(NOT(Sintomas(verifica_perda_interesse=W())))
    def aks_perda_interesse(self):
        self.declare(
            Sintomas(verifica_perda_interesse=ask("perda_interesse? ")))

    @Rule(NOT(Sintomas(perda_prazer=W())))
    def ask_data_perda_prazer(self):
        self.declare(Sintomas(perda_prazer=ask("perda_prazer? ")))

    @Rule(NOT(Sintomas(perda_peso=W())))
    def ask_data_perda_peso(self):
        self.declare(Sintomas(perda_peso=ask("perda_peso? ")))

    @Rule(NOT(Sintomas(ganho_peso=W())))
    def ask_data_ganho_peso(self):
        self.declare(Sintomas(ganho_peso=ask("ganho_peso? ")))

    @Rule(NOT(Sintomas(reducao_alimentacao=W())))
    def ask_data_reducao_alimentacao(self):
        self.declare(
            Sintomas(reducao_alimentacao=ask("reducao_alimentacao? ")))

    @Rule(NOT(Sintomas(aumento_alimentacao=W())))
    def ask_data_aumento_alimentacao(self):
        self.declare(
            Sintomas(aumento_alimentacao=ask("aumento_alimentacao? ")))

    @Rule(NOT(Sintomas(insonia=W())))
    def ask_data_insonia(self):
        self.declare(Sintomas(insonia=ask("insonia? ")))

    @Rule(NOT(Sintomas(hipersonia=W())))
    def ask_data_hipersonia(self):
        self.declare(Sintomas(hipersonia=ask("hipersonia? ")))

    @Rule(NOT(Sintomas(retardo_psicomotor=W())))
    def ask_data_retardo_psicomotor(self):
        self.declare(Sintomas(retardo_psicomotor=ask("retardo_psicomotor? ")))

    @Rule(NOT(Sintomas(fadiga=W())))
    def ask_data_fadiga(self):
        self.declare(Sintomas(fadiga=ask("fadiga? ")))

    @Rule(NOT(Sintomas(perda_energia=W())))
    def ask_data_perda_energia(self):
        self.declare(Sintomas(perda_energia=ask("perda_energia? ")))

    @Rule(NOT(Sintomas(inutilidade=W())))
    def ask_data_inutilidade(self):
        self.declare(Sintomas(inutilidade=ask("inutilidade? ")))

    @Rule(NOT(Sintomas(culpa_excessiva=W())))
    def ask_data_culpa_excessiva(self):
        self.declare(Sintomas(culpa_excessiva=ask("culpa_excessiva? ")))

    @Rule(NOT(Sintomas(culpa_inapropriada=W())))
    def ask_data_culpa_inapropriada(self):
        self.declare(Sintomas(culpa_inapropriada=ask("culpa_inapropriada? ")))

    @Rule(NOT(Sintomas(capacidade_diminuida=W())))
    def ask_data_capacidade_diminuida(self):
        self.declare(
            Sintomas(capacidade_diminuida=ask("capacidade_diminuida? ")))

    @Rule(NOT(Sintomas(indecisao=W())))
    def ask_data_indecisao(self):
        self.declare(Sintomas(indecisao=ask("indecisao? ")))

    @Rule(NOT(Sintomas(pensamentos_morte=W())))
    def ask_data_pensamentos_morte(self):
        self.declare(Sintomas(pensamentos_morte=ask("pensamentos_morte? ")))

    @Rule(NOT(Sintomas(sofrimento_clinico=W())))
    def ask_data_sofrimento_clinico(self):
        self.declare(Sintomas(sofrimento_clinico=ask("sofrimento_clinico? ")))

    @Rule(NOT(Sintomas(prejuiso_social=W())))
    def ask_data_prejuiso_social(self):
        self.declare(Sintomas(prejuiso_social=ask("prejuiso_social? ")))

    @Rule(NOT(Sintomas(prejuiso_area_importancia=W())))
    def ask_data_prejuiso_area_importancia(self):
        self.declare(
            Sintomas(
                prejuiso_area_importancia=ask("prejuiso_area_importancia? ")))

    @Rule(
        AND(
            NOT(Sintomas(verifica_mudanca_comportamental=W())),
            AND(
                OR(Sintomas(autoestima_excessiva=0),
                   Sintomas(autoestima_excessiva=1)),
                Sintomas(autoestima_excessiva=MATCH.autoestima_excessiva)),
            AND(OR(Sintomas(reducao_sono=0), Sintomas(reducao_sono=1)),
                Sintomas(reducao_sono=MATCH.reducao_sono)),
            AND(OR(Sintomas(aumento_fala=0), Sintomas(aumento_fala=1)),
                Sintomas(aumento_fala=MATCH.aumento_fala)),
            AND(
                OR(Sintomas(mudanca_modo_pensar=0),
                   Sintomas(mudanca_modo_pensar=1)),
                Sintomas(mudanca_modo_pensar=MATCH.mudanca_modo_pensar)),
            AND(OR(Sintomas(distrabilidade=0), Sintomas(distrabilidade=1)),
                Sintomas(distrabilidade=MATCH.distrabilidade)),
            AND(OR(Sintomas(agitacao=0), Sintomas(agitacao=1)),
                Sintomas(agitacao=MATCH.agitacao)),
            AND(
                OR(Sintomas(envolvimento_atividade_risco=0),
                   Sintomas(envolvimento_atividade_risco=1)),
                Sintomas(envolvimento_atividade_risco=MATCH.
                         envolvimento_atividade_risco))))
    def define_mudanca_comportamental(self, autoestima_excessiva, reducao_sono,
                                      aumento_fala, mudanca_modo_pensar,
                                      distrabilidade, agitacao,
                                      envolvimento_atividade_risco):
        self.declare(
            Sintomas(verifica_mudanca_comportamental=verifica_conjunto([
                autoestima_excessiva, reducao_sono, aumento_fala,
                mudanca_modo_pensar, distrabilidade, agitacao,
                envolvimento_atividade_risco
            ], 3)))

    @Rule(
        AND(
            NOT(Sintomas(verifica_sintomas_depressivos=W())),
            AND(OR(Sintomas(humor_deprimido=0), Sintomas(humor_deprimido=1)),
                Sintomas(humor_deprimido=MATCH.humor_deprimido)),
            AND(OR(Sintomas(perda_interesse=0), Sintomas(perda_interesse=1)),
                Sintomas(perda_interesse=MATCH.perda_interesse)),
            AND(
                OR(Sintomas(alteracao_alimentacao=0),
                   Sintomas(alteracao_alimentacao=1)),
                Sintomas(alteracao_alimentacao=MATCH.alteracao_alimentacao)),
            AND(OR(Sintomas(alteracao_sono=0), Sintomas(alteracao_sono=1)),
                Sintomas(alteracao_sono=MATCH.alteracao_sono)),
            AND(
                OR(Sintomas(alteracao_comportamentao=0),
                   Sintomas(alteracao_comportamentao=1)),
                Sintomas(
                    alteracao_comportamentao=MATCH.alteracao_comportamentao)),
            AND(OR(Sintomas(cansaco=0), Sintomas(cansaco=1)),
                Sintomas(cansaco=MATCH.cansaco)),
            AND(
                OR(Sintomas(sentimento_depressivo=0),
                   Sintomas(sentimento_depressivo=1)),
                Sintomas(sentimento_depressivo=MATCH.sentimento_depressivo)),
            AND(
                OR(Sintomas(alteracao_pensamento=0),
                   Sintomas(alteracao_pensamento=1)),
                Sintomas(alteracao_pensamento=MATCH.alteracao_pensamento)),
            AND(
                OR(Sintomas(pensamentos_morte=0),
                   Sintomas(pensamentos_morte=1)),
                Sintomas(pensamentos_morte=MATCH.pensamentos_morte))))
    def define_sintomas_depressivos(self, humor_deprimido, perda_interesse,
                                    alteracao_alimentacao, alteracao_sono,
                                    alteracao_comportamentao, cansaco,
                                    sentimento_depressivo,
                                    alteracao_pensamento, pensamentos_morte):
        self.declare(
            Sintomas(verifica_sintomas_depressivos=verifica_conjunto([
                humor_deprimido, perda_interesse, alteracao_alimentacao,
                alteracao_sono, alteracao_comportamentao, cansaco,
                sentimento_depressivo, alteracao_pensamento, pensamentos_morte
            ], 5)))

    """
    Verificação das regras 
    """

    @Rule(
        AND(Sintomas(mudanca_comportamental=1), Sintomas(fisiologico=1),
            Sintomas(psicose=1)))
    def mania(self):
        self.declare(Sintomas(mania=1))

    @Rule(
        AND(Sintomas(mudanca_comportamental=1), Sintomas(fisiologico=0),
            Sintomas(psicose=1)))
    def nao_mania(self):
        self.declare(Sintomas(mania=0))

    @Rule(
        AND(Sintomas(mudanca_comportamental=0), Sintomas(fisiologico=0),
            Sintomas(psicose=W())))
    def nao_2_mania(self):
        self.declare(Sintomas(mania=0))

    @Rule(
        AND(Sintomas(mudanca_comportamental=1), Sintomas(fisiologico=1),
            Sintomas(psicose=0)))
    def hipomania(self):
        self.declare(Sintomas(hipomania=1))

    @Rule(
        AND(Sintomas(mudanca_comportamental=1), Sintomas(fisiologico=0),
            Sintomas(psicose=0)))
    def nao_hipomania(self):
        self.declare(Sintomas(hipomania=0))

    @Rule(
        AND(Sintomas(mudanca_comportamental=0), Sintomas(fisiologico=W()),
            Sintomas(psicose=0)))
    def nao_2_hipomania(self):
        self.declare(Sintomas(hipomania=0))

    @Rule(OR(Sintomas(prejuizo_social=1), Sintomas(prejuiso_profissional=1)))
    def prejuiso_acentuado(self):
        self.declare(Sintomas(prejuizo_acentuado=1))

    @Rule(OR(Sintomas(prejuizo_si=1), Sintomas(prejuizo_outros=1)))
    def risco_potencial(self):
        self.declare(Sintomas(risco_potencial=1))

    @Rule(NOT(OR(Sintomas(prejuizo_si=1), Sintomas(prejuizo_outros=1))))
    def risco_potencial(self):
        self.declare(Sintomas(risco_potencial=0))

    @Rule(
        OR(OR(Sintomas(prejuizo_acentuado=1), Sintomas(risco_potencial=1)),
           Sintomas(psicose=1)))
    def perturbacao_humor(self):
        self.declare(Sintomas(perturbacao_humor=1))

    @Rule(
        NOT(
            OR(OR(Sintomas(prejuizo_acentuado=1), Sintomas(risco_potencial=1)),
               Sintomas(psicose=1))))
    def perturbacao_humor(self):
        self.declare(Sintomas(perturbacao_humor=0))

    @Rule(OR(Sintomas(autoestima_inflada=1), Sintomas(grandiosidade=1)))
    def autoestima_excessiva(self):
        self.declare(Sintomas(autoestima_excessiva=1))

    @Rule(OR(Sintomas(loquaz=1), Sintomas(pressao_continuar_falando=1)))
    def aumento_fala(self):
        self.declare(Sintomas(aumento_fala=1))

    @Rule(OR(Sintomas(fuga_ideias=1), Sintomas(pensamento_acelerado=1)))
    def mudanca_modo_pensar(self):
        self.declare(Sintomas(mudanca_modo_pensar=1))

    @Rule(
        OR(Sintomas(aumento_atividade_objetivo=1),
           Sintomas(agitacao_psicomotora=1)))
    def agitacao(self):
        self.declare(Sintomas(agitacao=1))

    @Rule(Sintomas(verifica_mudanca_comportamental=1))
    def mudanca_comportamental(self):
        self.declare(Sintomas(mudanca_comportamental=1))

    @Rule(OR(Sintomas(verifica_humor_deprimido=1), Sintomas(irritavel=1)))
    def humor_deprimido(self):
        self.declare(Sintomas(humor_deprimido=1))

    @Rule(OR(Sintomas(verifica_perda_interesse=1), Sintomas(perda_prazer=1)))
    def perda_interesse(self):
        self.declare(Sintomas(perda_interesse=1))

    @Rule(
        OR(
            OR(Sintomas(perda_peso=1), Sintomas(ganho_peso=1)),
            OR(Sintomas(reducao_alimentacao=1),
               Sintomas(aumento_alimentacao=1))))
    def alteracao_alimentacao(self):
        self.declare(Sintomas(Sintomas(alteracao_alimentacao=1)))

    @Rule(OR(Sintomas(insonia=1), Sintomas(hipersonia=1)))
    def alteracao_sono(self):
        self.declare(Sintomas(alteracao_sono=1))

    @Rule(OR(Sintomas(agitacao=1), Sintomas(retardo_psicomotor=1)))
    def alteracao_comportamentao(self):
        self.declare(Sintomas(alteracao_comportamentao=1))

    @Rule(OR(Sintomas(fadiga=1), Sintomas(perda_energia=1)))
    def cansaco(self):
        self.declare(Sintomas(cansaco=1))

    @Rule(
        OR(Sintomas(inutilidade=1), Sintomas(culpa_excessiva=1),
           Sintomas(culpa_inapropriada=1)))
    def sentimento_depressivo(self):
        self.declare(Sintomas(sentimento_depressivo=1))

    @Rule(OR(Sintomas(capacidade_diminuida=1), Sintomas(indecisao=1)))
    def alteracao_pensamento(self):
        self.declare(Sintomas(alteracao_pensamento=1))

    @Rule(
        AND(OR(Sintomas(humor_deprimido=1), Sintomas(perda_interesse=1)),
            Sintomas(verifica_sintomas_depressivos=1)))
    def sintomas_depressivos(self):
        self.declare(Sintomas(sintomas_depressivos=1))

    @Rule(
        OR(
            OR(Sintomas(sofrimento_clinico=1), Sintomas(prejuiso_social=1)),
            OR(Sintomas(prejuiso_profissional=1),
               Sintomas(prejuiso_area_importancia=1))))
    def transtorno(self):
        self.declare(Sintomas(transtorno=1))

    @Rule(AND(Sintomas(sintomas_depressivos=1), Sintomas(fisiologico=1)))
    def depressao(self):
        self.declare(Sintomas(depressao=1))
        self.facts()
        self.get_rules()

    @Rule(AND(Sintomas(mania=1), Sintomas(depressao=1)))
    def bipolar_i(self):
        self.declare(Sintomas(bipolar_i=1))

    @Rule(AND(Sintomas(hipomania=1), Sintomas(depressao=1)))
    def bipolar_ii(self):
        self.declare(Sintomas(bipolar_ii=1))

    @Rule(
        AND(
            OR(Sintomas(pensamentos_morte=1), Sintomas(psicose=1),
               Sintomas(transtorno=1)), Sintomas(mania=0),
            Sintomas(hipomania=0), Sintomas(depressao=0)))
    def outro_transtorno(self):
        self.declare(Sintomas(outro_transtorno=1))

    @Rule(Sintomas(bipolar_i=1))
    def tem_bipolar_i(self):
        self.halt()

    @Rule(Sintomas(bipolar_ii=1))
    def tem_bipolar_ii(self):
        self.halt()

    @Rule(
        OR(AND(Sintomas(mania=0), Sintomas(depressao=1)),
           AND(Sintomas(hipomania=0), Sintomas(depressao=1))))
    def tem_depressao(self):
        self.halt()

    @Rule(Sintomas(outro_transtorno=1))
    def tem_outro_transtorno(self):
        self.halt()

    @Rule(Sintomas(outro_transtorno=0))
    def nao_tem_outro_transtorno(self):
        self.halt()
Esempio n. 15
0
 class Test(KnowledgeEngine):
     """ Test KE """
     @Rule(OR(Fact(something=L(1)), Fact(something=L(2))))
     def rule1(self):
         """ First rule, something=1 and something=2"""
         pass
class expertMotor(KnowledgeEngine):

    #------ First Rules ------
    #-------------------------
    #rule for decided client
    @Rule(Client(typeClient=1))
    def determined_client(self):
        self.declare(SalePhone(make_sale=True))

    #rule for client that exposes requirements
    @Rule(Client(typeClient=2))
    def exposes_requirements(self):
        self.declare(Expert(consult_features=True))

    #rule for cliente undecided
    @Rule(OR(Client(typeClient=3), Client(clear_needs=False)))
    def consult_use(self):
        self.declare(Expert(define_features=False))

    #------ Rules for requirements and uses ------
    #------------------------------------
    #rule for cliente that consult features
    @Rule(Expert(consult_features=True))
    def consult_features(self):
        global req
        req = dialogClientReq()
        self.declare(
            Requeriments(memory=req[0],
                         cam=req[1],
                         storage=req[2],
                         software=req[3],
                         battery=req[4],
                         color=req[5],
                         screen=req[6],
                         undecided=req[7]))

    #rule for client with clear needs
    @Rule(Requeriments(undecided=MATCH.a), TEST(lambda a: a < 4))
    def clear_needs(self):
        self.declare(Client(clear_needs=True))

    #rule for client with NO clear needs
    @Rule(Requeriments(undecided=MATCH.a), TEST(lambda a: a >= 4))
    def no_clear_needs(self):
        self.declare(Client(clear_needs=False))

    #rule for check availability if clear needs or define features
    @Rule(AND(Client(clear_needs=True), Expert(consult_features=True)))
    def check_availability(self):
        global req
        global phone_recomendation_expert
        phone_recomendation_expert = dialog_check_availability(req)

        if len(phone_recomendation_expert) != 0:
            self.declare(Expert(show_options=True))
        else:
            self.declare(SalePhone(make_sale=False))

    #rule for show options
    @Rule(Expert(show_options=True))
    def show_options(self):
        dialog_show_options(phone_recomendation_expert)
        self.declare(Expert(evaluate_budget=True))

    #rule for evaluate budget
    @Rule(Expert(evaluate_budget=True))
    def evaluate_budget(self):
        global budget

        budget = dialog_evaluate_budget(phone_recomendation_expert)

        if budget == 1:
            self.declare(Consult(budget_OK=False))
        else:
            self.declare(Consult(budget_OK=True))

    #rule for budget OK
    @Rule(AND(Expert(evaluate_budget=True), Consult(budget_OK=True)))
    def budget_OK(self):
        self.declare(SalePhone(make_sale=True))

    #rule for budget no OK
    @Rule(AND(Expert(evaluate_budget=True), Consult(budget_OK=False)))
    def budget_no_OK(self):
        print("Desea replantear su consulta?: ")
        print("1 - SI")
        print("2 - NO")

        budget_election = 0
        while budget_election < 1 or budget_election > 2:
            budget_election = checkInt("Elegir opcion: ")

        if budget_election == 1:
            self.declare(Consult(rethink_budget=True))
        else:
            self.declare(Consult(rethink_budget=False))

    #rule for define features
    @Rule(Expert(define_features=False))
    def define_features(self):
        #falta criterios para definir las caracteisticas
        global req_max
        global req_min
        use_requeriments = dialog_consult_use()
        req_min = use_requeriments[0]
        req_max = use_requeriments[1]
        self.declare(Expert(define_features=True))

    #rule for check availability if clear needs or define features
    @Rule(Expert(define_features=True))
    def check_availability_to_use(self):
        global req
        global phone_recomendation_expert
        phone_recomendation_expert = dialog_check_availability(req_min)
        phone_max = dialog_check_availability(req_max)

        for item in phone_max:
            bandera = True
            for items in phone_recomendation_expert:
                if item['ID'] == items['ID']:
                    bandera = False
            if bandera:
                phone_recomendation_expert.insert(
                    len(phone_recomendation_expert), item)

        if len(phone_recomendation_expert) != 0:
            self.declare(Expert(show_options=True))
        else:
            self.declare(SalePhone(make_sale=False))

    #------ Rules for make sale or no make sale ------
    #-------------------------------------------------
    #rule for a sale made
    @Rule(SalePhone(make_sale=True))
    def make_sale(self):
        dialogMakeSale()

    #rule for a no sale made
    @Rule(OR(SalePhone(make_sale=False), Consult(rethink_budget=False)))
    def no_make_sale(self):
        dialogNoMakeSale()

    @Rule(Consult(rethink_budget=True))
    def rethink_budget(self):
        motor.reset()
        motor.declare(Client(typeClient=2))
        motor.run()
        motor.facts
Esempio n. 17
0
class Bipolar(KnowledgeEngine):
    """
    Coleta dos dados
    """
    @Rule(NOT(Sintomas(pensamentos_morte=W())))
    def ask_pensamentos_morte(self):
        self.declare(
            Sintomas(pensamentos_morte=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(alteracao_pensamento=W())))
    def ask_alteracao_pensamento(self):
        self.declare(
            Sintomas(alteracao_pensamento=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(sentimento_depressivo=W())))
    def ask_sentimento_depressivo(self):
        self.declare(
            Sintomas(sentimento_depressivo=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(cansaco=W())))
    def ask_cansaco(self):
        self.declare(Sintomas(cansaco=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(alteracao_comportamentao=W())))
    def ask_alteracao_comportamentao(self):
        self.declare(
            Sintomas(
                alteracao_comportamentao=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(alteracao_sono=W())))
    def ask_alteracao_sono(self):
        self.declare(
            Sintomas(alteracao_sono=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(alteracao_alimentacao=W())))
    def ask_alteracao_alimentacao(self):
        self.declare(
            Sintomas(alteracao_alimentacao=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(perda_interesse=W())))
    def ask_perda_interesse(self):
        self.declare(
            Sintomas(perda_interesse=ask("prejuiso_area_importancia? ")))

    @Rule(NOT(Sintomas(humor_deprimido=W())))
    def ask_humor_deprimido(self):
        self.declare(
            Sintomas(humor_deprimido=ask("prejuiso_area_importancia? ")))

    @Rule(
        AND(
            NOT(Sintomas(verifica_sintomas_depressivos=W())),
            AND(OR(Sintomas(humor_deprimido=0), Sintomas(humor_deprimido=1)),
                Sintomas(humor_deprimido=MATCH.humor_deprimido)),
            AND(OR(Sintomas(perda_interesse=0), Sintomas(perda_interesse=1)),
                Sintomas(perda_interesse=MATCH.perda_interesse)),
            AND(
                OR(Sintomas(alteracao_alimentacao=0),
                   Sintomas(alteracao_alimentacao=1)),
                Sintomas(alteracao_alimentacao=MATCH.alteracao_alimentacao)),
            AND(OR(Sintomas(alteracao_sono=0), Sintomas(alteracao_sono=1)),
                Sintomas(alteracao_sono=MATCH.alteracao_sono)),
            AND(
                OR(Sintomas(alteracao_comportamentao=0),
                   Sintomas(alteracao_comportamentao=1)),
                Sintomas(
                    alteracao_comportamentao=MATCH.alteracao_comportamentao)),
            AND(OR(Sintomas(cansaco=0), Sintomas(cansaco=1)),
                Sintomas(cansaco=MATCH.cansaco)),
            AND(
                OR(Sintomas(sentimento_depressivo=0),
                   Sintomas(sentimento_depressivo=1)),
                Sintomas(sentimento_depressivo=MATCH.sentimento_depressivo)),
            AND(
                OR(Sintomas(alteracao_pensamento=0),
                   Sintomas(alteracao_pensamento=1)),
                Sintomas(alteracao_pensamento=MATCH.alteracao_pensamento)),
            AND(
                OR(Sintomas(pensamentos_morte=0),
                   Sintomas(pensamentos_morte=1)),
                Sintomas(pensamentos_morte=MATCH.pensamentos_morte))))
    def define_sintomas_depressivos(self, humor_deprimido, perda_interesse,
                                    alteracao_alimentacao, alteracao_sono,
                                    alteracao_comportamentao, cansaco,
                                    sentimento_depressivo,
                                    alteracao_pensamento, pensamentos_morte):
        self.declare(
            Sintomas(verifica_sintomas_depressivos=verifica_conjunto([
                humor_deprimido, perda_interesse, alteracao_alimentacao,
                alteracao_sono, alteracao_comportamentao, cansaco,
                sentimento_depressivo, alteracao_pensamento, pensamentos_morte
            ], 5)))

    @Rule(
        AND(OR(Sintomas(humor_deprimido=1), Sintomas(perda_interesse=1)),
            Sintomas(verifica_sintomas_depressivos=1)))
    def sintomas_depressivos(self):
        print("sintomas_depressivos")
        self.declare(Sintomas(sintomas_depressivos=1))
Esempio n. 18
0
def test_extract_facts():
    from pyknow import Rule, NOT, AND, OR, Fact

    rule = Rule(OR(AND(Fact(1), NOT(Fact(2))), Fact(3)))

    assert utils.extract_facts(rule) == {Fact(1), Fact(2), Fact(3)}
Esempio n. 19
0
class Mania(KnowledgeEngine):
    """
    Coleta dos dados
    """

    result = {
        'variable': None,
        'isFinalAnswer': False,
        'answer': None,
    }
    is_rest = True

    def ask(self, variable):
        if not self.is_rest:
            return ask(variable)
        else:
            self.result['variable'] = variable
            self.result['isFinalAnswer'] = False
            self.result['answer'] = None
            self.halt()

    def send(self, message):
        if not self.is_rest:
            print(message)
        else:
            self.result['variable'] = None
            self.result['isFinalAnswer'] = True
            self.result['answer'] = message
        self.halt()

    @Rule(Sintomas(fisiologico=None))
    def ask_data_fisiologico(self):
        self.declare(Sintomas(fisiologico=self.ask("fisiologico")))

    @Rule(Sintomas(prejuizo_social=None))
    def ask_data_prejuizo_social(self):
        self.declare(Sintomas(prejuizo_social=self.ask("prejuizo_social")))

    @Rule(Sintomas(prejuiso_profissional=None))
    def ask_data_prejuiso_profissional(self):
        self.declare(
            Sintomas(prejuiso_profissional=self.ask("prejuiso_profissional")))

    @Rule(Sintomas(psicose=None))
    def ask_data_psicose(self):
        self.declare(Sintomas(psicose=self.ask("psicose")))

    @Rule(Sintomas(autoestima_inflada=None))
    def ask_data_autoestima_inflada(self):
        self.declare(
            Sintomas(autoestima_inflada=self.ask("autoestima_inflada")))

    @Rule(Sintomas(grafndiosidade=None))
    def ask_data_grandiosidade(self):
        self.declare(Sintomas(grandiosidade=self.ask("grandiosidade")))

    @Rule(Sintomas(loquaz=None))
    def ask_data_loquaz(self):
        self.declare(Sintomas(loquaz=self.ask("loquaz")))

    @Rule(Sintomas(pressao_continuar_falando=None))
    def ask_data_pressao_continuar_falando(self):
        self.declare(
            Sintomas(pressao_continuar_falando=self.ask(
                "pressao_continuar_falando")))

    @Rule(Sintomas(fuga_ideias=None))
    def ask_data_fuga_ideias(self):
        self.declare(Sintomas(fuga_ideias=self.ask("fuga_ideias")))

    @Rule(Sintomas(pensamento_acelerado=None))
    def ask_data_pensamento_acelerado(self):
        self.declare(
            Sintomas(pensamento_acelerado=self.ask("pensamento_acelerado")))

    @Rule(Sintomas(aumento_atividade_objetivo=None))
    def ask_data_aumento_atividade_objetivo(self):
        self.declare(
            Sintomas(aumento_atividade_objetivo=self.ask(
                "aumento_atividade_objetivo")))

    @Rule(Sintomas(agitacao_psicomotora=None))
    def ask_data_agitacao_psicomotora(self):
        self.declare(
            Sintomas(agitacao_psicomotora=self.ask("agitacao_psicomotora")))

    @Rule(Sintomas(reducao_sono=None))
    def ask_data_reducao_sono(self):
        self.declare(Sintomas(reducao_sono=self.ask("reducao_sono")))

    @Rule(Sintomas(distrabilidade=None))
    def ask_data_distrabilidade(self):
        self.declare(Sintomas(distrabilidade=self.ask("distrabilidade")))

    @Rule(Sintomas(envolvimento_atividade_risco=None))
    def ask_data_envolvimento_atividade_risco(self):
        self.declare(
            Sintomas(envolvimento_atividade_risco=self.ask(
                "envolvimento_atividade_risco")))

    @Rule(
        AND(
            NOT(Sintomas(mudanca_comportamental=W())),
            AND(
                OR(Sintomas(autoestima_excessiva=0),
                   Sintomas(autoestima_excessiva=1)),
                Sintomas(autoestima_excessiva=MATCH.autoestima_excessiva)),
            AND(OR(Sintomas(reducao_sono=0), Sintomas(reducao_sono=1)),
                Sintomas(reducao_sono=MATCH.reducao_sono)),
            AND(OR(Sintomas(aumento_fala=0), Sintomas(aumento_fala=1)),
                Sintomas(aumento_fala=MATCH.aumento_fala)),
            AND(
                OR(Sintomas(mudanca_modo_pensar=0),
                   Sintomas(mudanca_modo_pensar=1)),
                Sintomas(mudanca_modo_pensar=MATCH.mudanca_modo_pensar)),
            AND(OR(Sintomas(distrabilidade=0), Sintomas(distrabilidade=1)),
                Sintomas(distrabilidade=MATCH.distrabilidade)),
            AND(OR(Sintomas(agitacao=0), Sintomas(agitacao=1)),
                Sintomas(agitacao=MATCH.agitacao)),
            AND(
                OR(Sintomas(envolvimento_atividade_risco=0),
                   Sintomas(envolvimento_atividade_risco=1)),
                Sintomas(envolvimento_atividade_risco=MATCH.
                         envolvimento_atividade_risco))))
    def define_mudanca_comportamental(self, autoestima_excessiva, reducao_sono,
                                      aumento_fala, mudanca_modo_pensar,
                                      distrabilidade, agitacao,
                                      envolvimento_atividade_risco):
        self.declare(
            Sintomas(mudanca_comportamental=verifica_conjunto([
                autoestima_excessiva, reducao_sono, aumento_fala,
                mudanca_modo_pensar, distrabilidade, agitacao,
                envolvimento_atividade_risco
            ], 3)))

    """
    Verificação das regras 
    """

    @Rule(
        AND(Sintomas(mudanca_comportamental=1), Sintomas(fisiologico=1),
            Sintomas(psicose=1)))
    def mania(self):
        self.declare(Sintomas(mania=1))
        self.send("mania:1")

    @Rule(
        AND(Sintomas(mudanca_comportamental=1), Sintomas(fisiologico=1),
            Sintomas(psicose=0)))
    def hipomania(self):
        self.declare(Sintomas(hipomania=1))
        self.send("hipomania:1")

    @Rule(Sintomas(fisiologico=0))
    def nao_fisiologico(self):
        self.declare(Sintomas(mania=0))
        self.declare(Sintomas(hipomania=0))
        self.send(":0")

    @Rule(Sintomas(mudanca_comportamental=0))
    def nao_mudanca_comportamental(self):
        self.declare(Sintomas(mania=0))
        self.declare(Sintomas(hipomania=0))
        self.send(":0")

    @Rule(OR(Sintomas(prejuizo_social=1), Sintomas(prejuiso_profissional=1)))
    def prejuiso_acentuado(self):
        self.declare(Sintomas(prejuizo_acentuado=1))

    @Rule(AND(Sintomas(prejuizo_social=0), Sintomas(prejuiso_profissional=0)))
    def nao_prejuiso_acentuado(self):
        self.declare(Sintomas(prejuizo_acentuado=0))

    @Rule(OR(Sintomas(autoestima_inflada=1), Sintomas(grandiosidade=1)))
    def autoestima_excessiva(self):
        self.declare(Sintomas(autoestima_excessiva=1))

    @Rule(AND(Sintomas(autoestima_inflada=0), Sintomas(grandiosidade=0)))
    def nao_autoestima_excessiva(self):
        self.declare(Sintomas(autoestima_excessiva=0))

    @Rule(OR(Sintomas(loquaz=1), Sintomas(pressao_continuar_falando=1)))
    def aumento_fala(self):
        self.declare(Sintomas(aumento_fala=1))

    @Rule(AND(Sintomas(loquaz=0), Sintomas(pressao_continuar_falando=0)))
    def nao_aumento_fala(self):
        self.declare(Sintomas(aumento_fala=0))

    @Rule(OR(Sintomas(fuga_ideias=1), Sintomas(pensamento_acelerado=1)))
    def mudanca_modo_pensar(self):
        self.declare(Sintomas(mudanca_modo_pensar=1))

    @Rule(AND(Sintomas(fuga_ideias=0), Sintomas(pensamento_acelerado=0)))
    def nao_mudanca_modo_pensar(self):
        self.declare(Sintomas(mudanca_modo_pensar=0))

    @Rule(
        OR(Sintomas(aumento_atividade_objetivo=1),
           Sintomas(agitacao_psicomotora=1)))
    def agitacao(self):
        self.declare(Sintomas(agitacao=1))

    @Rule(
        AND(Sintomas(aumento_atividade_objetivo=0),
            Sintomas(agitacao_psicomotora=0)))
    def nao_agitacao(self):
        self.declare(Sintomas(agitacao=0))