Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result will be arbitrary when only NOT is in the rule #10

Closed
loseraitc opened this issue Nov 1, 2019 · 7 comments
Closed

Result will be arbitrary when only NOT is in the rule #10

loseraitc opened this issue Nov 1, 2019 · 7 comments
Assignees

Comments

@loseraitc
Copy link

loseraitc commented Nov 1, 2019

You can just keep running the code below, the result will be arbitrary, which is wierd.

from experta import *


class Light(Fact):
    """Info about the traffic light."""
    pass


class RobotCrossStreet(KnowledgeEngine):
    @Rule(NOT(Light(color=L('red'))))
    def red_light(self):
        print("dont")

    @Rule(NOT(Light(color=L('yellow'))))
    def yellow_light(self):
        print("wait")

engine = RobotCrossStreet()
engine.reset()
engine.declare(Light(color='red'))
engine.run()
@nilp0inter nilp0inter self-assigned this Nov 1, 2019
@grizonic
Copy link

grizonic commented Nov 3, 2019

I think I'm running into similar issues with a different example - the greet example of the docs. I noticed that if I explicitly set priority via salience, then there are no issues. If not, I run into issues on both examples.

In the greet example I managed to find that activations are being wrongly removed from the list in the _update_agenda method in strategies.py, because the keys seem to be the same for different activations (when salience not specified!).

Basically, I see that the location question is popped from the list to be fired and then _update_agenda removes the name question from the list, mistakenly. Also, I didn't manage to understand why it is popping activations and then deleting them in the _update_agenda. I'm sure there's a reason for that, but I can't see it.

Commenting out the del statement in _update_agenda makes the greet example work fine as well. Let me know if you think that they are different issues. I can create another one. I'll leave the greet code here, as it might be helpful:

from experta import *

class Greetings(KnowledgeEngine):
    @DefFacts()
    def _initial_action(self):
        yield Fact(action="greet")

    @Rule(Fact(action='greet'),
          NOT(Fact(name=W())))
    def ask_name(self):
        self.declare(Fact(name=input("What's your name? ")))

    @Rule(Fact(action='greet'))
    def ask_test(self):
        self.declare(Fact(test=input("What's your test? ")))

    @Rule(Fact(action='greet'),
          NOT(Fact(location=W())))
    def ask_location(self):
        self.declare(Fact(location=input("Where are you? ")))

    @Rule(Fact(action='greet'),
          Fact(name=MATCH.name),
          Fact(location=MATCH.location))
    def greet(self, name, location):
        print("Hi %s! How is the weather in %s?" % (name, location))

engine = Greetings()
engine.reset()  # Prepare the engine for the execution.
engine.run()  # Run it!

@grizonic
Copy link

grizonic commented Nov 3, 2019

I just installed directly from github now (was running 1.9.2 installed with pip) and the bug @orenwangtencent reported has been fixed. I can't reproduce it anymore and I see that the activations' keys are now different :) I will check the greetings example again and if I find something I'll report on a different issue.

@loseraitc
Copy link
Author

Oh thank u a lot, @grizonic . I tried to install from the develop branch of the GitHub, yet still no luck. But assigning a randint for salience definitely do the trick. I am trying to figure out the activation keys now!

@nilp0inter
Copy link
Owner

HI! Sorry for the delay and thank you for reporting this.

I am working on it and will be fix soon.

@nilp0inter
Copy link
Owner

nilp0inter commented Nov 16, 2019

I've just published v1.9.4, the random behavior should be fixed now. Please check if this works for you.

On the other hand, the new tests that I wrote to fix this issue arose two new issues #12 and #13.

#12 in particular causes that the order of execution between runs can be undefined when the strategy doesn't have strict rules. This will be the case for example with your code if you remove the declare statement. If you do so both rules will match but with the current implementation the order in which red_light and yellow_light get run can be different between different executions of the Python interpreter.

@loseraitc
Copy link
Author

Yeah it worked normally now, both the example above and my own code. @nilp0inter thx very much! BTW Really Good library, to learn from and integrate into projects.

@nilp0inter
Copy link
Owner

Glad to hear that! Thank you both @orenwangtencent and @grizonic .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants