flow.rule should provide callback support #94

Closed
bthibault opened this Issue Dec 13, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@bthibault

I believe that is the issue here:

 // define the rule
    flow.rule("RULE", criteriaArray,
      function(facts) {
         _self = this;
         // do some fact modification asynchronously 
         ruleAction.run(facts.p, rg.then.params, function(err, results) {
                // alert the engine the rule has been modified
                _self.modify(facts.p); 
                _self.retract(facts.p); 
         });
  });

So whats happening here, is the async function ruleAction.run is run, BUT the rule has already fired, which moves the pointer on the linked list then we try to call _self.modify but the fact has already been unlinked.

So we have an async call, with no way to do a callback.

@bthibault

This comment has been minimized.

Show comment
Hide comment
@bthibault

bthibault Dec 13, 2013

admittedly i'm a node / js n00b so there could be another for me to structure it.

admittedly i'm a node / js n00b so there could be another for me to structure it.

@doug-martin

This comment has been minimized.

Show comment
Hide comment
@doug-martin

doug-martin Dec 13, 2013

Contributor

Try this

 // define the rule
    flow.rule("RULE", criteriaArray,
      function(facts, engine, next) {
         _self = this;
         // do some fact modification asynchronously 
         ruleAction.run(facts.p, rg.then.params, function(err, results) {
                if(err){
                    next(err);
                }else{
                // alert the engine the rule has been modified
                _self.modify(facts.p); 
                _self.retract(facts.p); 
                next();
               }
         });
  });
Contributor

doug-martin commented Dec 13, 2013

Try this

 // define the rule
    flow.rule("RULE", criteriaArray,
      function(facts, engine, next) {
         _self = this;
         // do some fact modification asynchronously 
         ruleAction.run(facts.p, rg.then.params, function(err, results) {
                if(err){
                    next(err);
                }else{
                // alert the engine the rule has been modified
                _self.modify(facts.p); 
                _self.retract(facts.p); 
                next();
               }
         });
  });
@doug-martin

This comment has been minimized.

Show comment
Hide comment
@doug-martin

doug-martin Dec 13, 2013

Contributor

I should also note if you are using the DSL then you can just use next. See the action section for a detailed explanation. I'll also update the readme to have a section dedicated about asynchronous actions in nools, since this isn't the first time some one has brought this up.

Contributor

doug-martin commented Dec 13, 2013

I should also note if you are using the DSL then you can just use next. See the action section for a detailed explanation. I'll also update the readme to have a section dedicated about asynchronous actions in nools, since this isn't the first time some one has brought this up.

@bthibault

This comment has been minimized.

Show comment
Hide comment
@bthibault

bthibault Dec 13, 2013

hey hey. it worked.

Thanks

hey hey. it worked.

Thanks

doug-martin added a commit to doug-martin/nools that referenced this issue Dec 17, 2013

v0.2.3
* Added new `getFacts` method to allow for querying of facts currently in session. #52;
* Added indexing on comparison operators (i.e. `>, <, >=, <=`).
* Updated documentation.
   * Added new section about retrieving facts from a session.
   * Created new section for async actions to address #94

@doug-martin doug-martin referenced this issue Dec 17, 2013

Merged

v0.2.3 #95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment