npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wbt

v1.1.2

Published

Behavior Tree Implementation for NodeJS

Downloads

6

Readme

WBT

Node.js Behavior Tree, this a Node.js library to design a Behavior Tree. This is inspired from http://guineashots.com/2014/09/24/implementing-a-behavior-tree-part-1/

Example

First let's define some Actions. We start by creating a DoRandom action and then we will simulate a success action SuccessAction and a fail action FailAction.

// dorandom.js
"use strict";

let BaseNode = require('./basenode');
let Status = require('./status');

let DoRandom = function(text, time) {
	this.text = text;
  
  // this is to simulate a long time execution task
	this.endTime = time;
	
  this.initialize();
}

DoRandom.prototype = new BaseNode();
DoRandom.prototype.open = function(tick) {
	let startTime = (new Date()).getTime();
  
  // Save the starttime value
	tick.blackboard.set('startTime', startTime, tick.tree.id, this.id);
  
  // Check if event variable exists if not set it to 0
	if (tick.blackboard.get('event', tick.tree.id) == undefined) {
		tick.blackboard.set('event', 0, tick.tree.id);
	}

	console.log("Starting " + this.text);
}

DoRandom.prototype.tick = function(tick) {
	let currTime = (new Date()).getTime();
	let startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);

	if (currTime - startTime > this.endTime) {
		console.log("Finished " + this.text);
    
    // Increment event variable when the execution succeed
		let e = tick.blackboard.get('event', tick.tree.id) + 1;
		tick.blackboard.set('event', e, tick.tree.id);

		return Status.SUCCESS;
	}

	return Status.RUNNING;
}

module.exports = DoRandom;
// successaction.js
"use strict";

let BaseNode = require('./basenode');
let Status = require('./status');

let SuccessAction = function(text) {
	this.text = text;
	this.initialize(arguments);
}

SuccessAction.prototype = new BaseNode();
SuccessAction.prototype.tick = function(tick) {
	console.info('Success Action : ' + this.text);
  
  // reinitialize event variable
	tick.blackboard.set('event', 0, tick.tree.id);
	return Status.SUCCESS;
}

module.exports = SuccessAction;
// failaction.js
"use strict";

let BaseNode = require('./basenode');
let Status = require('./status');

let FailAction = function(text) {
	this.text = text;
	this.initialize();
}

FailAction.prototype = new BaseNode();
FailAction.prototype.tick = function(tick) {
	console.error(this.text);
	return Status.FAILURE;
}

module.exports = FailAction;

Now let's create a condition node that check whether the value of the variable event in blackboard is greater than 5 or not.

// iseventreceived.js
"use strict";

let BaseNode = require('./basenode');
let Status = require('./status');

let IsEventReceived = function() {
	this.initialize();
}

IsEventReceived.prototype = new BaseNode();
IsEventReceived.prototype.tick = function(tick) {
	let e = tick.blackboard.get('event', tick.tree.id);

	if (e && e > 5) {
		console.info("Event Received");
		return Status.SUCCESS;
	}

	console.error("No Event " + e);
	return Status.FAILURE;
}

module.exports = IsEventReceived;

And the main file

// main.js
"use strict";

let Blackboard = require('./blackboard');
let BehaviorTree = require('./behaviortree');

let Selector = require('./selector');
let Sequence = require('./sequence');
let MemSelector = require('./memselector');
let Random = require('./random');

let IsEventReceived = require('./iseventreceived');
let FailAction = require('./failaction');
let SuccessAction = require('./successaction');
let DoRandom = require('./dorandom');

let blackboard, tree;

blackboard = new Blackboard();
tree = new BehaviorTree();

tree.root = new Selector(
	new Sequence(
		new IsEventReceived(),
		new MemSelector(
			new FailAction("Fail 1"),
			new SuccessAction("Success 1"),
			new FailAction("Fail 2")
		)
	),
	new Random(
		new DoRandom("Random 1", 2000), // Simulate a 2 seconds task
		new DoRandom("Random 2", 500), // Simulate a 0.5 second task
		new DoRandom("Random 3", 4000), // Simulate a 4 seconds task
		new DoRandom("Random 4", 6000) // Simulate a 6 seconds task
	)
);

// Run the Behavior Tree each 300ms (this can be changed to what the designer want)
var intervalID = setInterval(function() {
	tree.tick(null, blackboard);
}, 300);