Calamari / BehaviorTree.js

An JavaScript implementation of Behavior Trees.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Import JSON BT files into behaviortree

gsommavilla opened this issue · comments

Import JSON BT files into behaviortree

1. How to use BehaviorTreeImporter?

I am facing problems on how to use BehaviorTreeImporter for importing a JSON file.

I am trying to compile a nodejs source like this:

import { BehaviorTreeImporter, BehaviorTree, Sequence, Task, SUCCESS, FAILURE } from 'behaviortree'

const treeFromJSON = BehaviorTreeImporter;

I have a BT in bt_sample.json (taken from the example I found in the README.md).

I naively tried the following commands, without success:

treeFromJSON.parse("bt_sample.json")

treeFromJSON("bt_sample.json")

How can I load the JSON file tree structure into behaviortree?

2. BT JSON documentation

Is there any documentation on how to structure data in a JSON BT file?

Thank you very much for your attention.

I progressed a bit in this issue. It was easier than I thought.
Anyway, as it may be useful for other users, I share some minimal
working examples here.

1. How to use BehaviorTreeImporter?

I took a look at src/BehaviorTreeImporter.spec.js and I found an
example on how to import a sample BT JSON file.

First load JSON data into memory:

const json = JSON.parse(fs.readFileSync('bt_sample.json', 'utf8'));

Then import and parse into a BehaviorTree object:

let importer = new BehaviorTreeImporter();
const bTree = new BehaviorTree({
  tree: importer.parse(json),
})

2. BT JSON documentation

I am now trying to implement the several BT node types in JSON
structure. As soon as I come up with a MWE with all the different
types, I will post it here and close the issue.

MWE of behaviortree with JSON file

bt_ball__MWE.js:

import { BehaviorTreeImporter, BehaviorTree, Sequence, Task, SUCCESS, FAILURE } from 'behaviortree'

BehaviorTree.register(
  'findBall', new Task({
    run: function () {
      console.log("Finding a ball");
      return SUCCESS
    }
  })
)

BehaviorTree.register(
  'goToBall', new Task({
    run: function () {
      console.log("Going closer to the ball");
      return SUCCESS
    }
  })
)

BehaviorTree.register(
  'graspBall', new Task({
    run: function () {
      console.log("Grasping the ball");
      return SUCCESS
    }
  })
)

let importer = new BehaviorTreeImporter();

const fs = require('fs');
const json = JSON.parse(fs.readFileSync('bt_sample.json', 'utf8'));

const bTree = new BehaviorTree({
  tree: importer.parse(json),
//  blackboard:
})

// The "game" loop:
setInterval(function () {
  bTree.step()
}, 1000 )

bt_sample.json:

{
  "type": "sequence",
  "name": "the root",
  "nodes": [
    {
      "type": "findBall",
      "name": "look around and try to see a ball"
    },
    {
      "type": "goToBall",
      "name": "go to the ball"
    },
    {
      "type": "graspBall",
      "name": "grasp the ball"
    }
  ]
}

Execution:

$ babel-node bt_ball__MWE.js
Finding a ball
Going closer to the ball
Grasping the ball
Finding a ball
Going closer to the ball
Grasping the ball
Finding a ball
Going closer to the ball
Grasping the ball
^C

A good, that you found a solution.
You are right, how to load a file is not mentioned in the README, but I assume, that is knowledge everyone should already have. But I could add a line where to add it.
Btw: A nice trick that helps me often is, to just check the tests files, if any. I always try to have tests for everything, so there you can always see how I think it is going to be used.