3.0.1 • Published 5 years ago

@dxworks/bprogram v3.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

BProgram

npm i @dxworks/bprogram

Behavioral Programming (BP) is an approach and technique for software development, which enables incremental development in a natural way. A behavioral application consists of threads of behavior each of which represents an independent scenario that the system should and shouldn't follow. ~weizmann;

SETUP

When you import from @dxworks/bprogram you are importing the dev build which will auto log in both node and browser enviorment. To use the production build you'll want to do the following.

Development

/** NODE JS **/
const { bprogram } = require('@dxworks/bprogram');
// or if using esm
import { bprogram } from '@dxworks/bprogram/dist/bprogram';

/** BROWSER **/
import { bprogram } from '@dxworks/dist/bprogram.esm';
// or if using esm or bundler that resolves the module key in package.json
import { bprogram } from '@dxworks/bprogram';

Prod

/** NODE JS **/
const { bprogram } = require('@dxworks/dist/bprogram.prod');
// or if using esm
import { bprogram } from '@dxworks/bprogram/dist/bprogram.prod';

/** BROWSER **/
import { bprogram } from '@dxworks/dist/bprogram.prod.esm';

It is recommended you use the aliasing feature in rollup or webpack when building your applications

USE

Create bProgram

Instantiate a behaviroal program and destructure utility functions.

 const { addAll, trigger, addBThread } = bProgram('programName', loggerCallback, alternativeSortFunction);

Create BThread

Create generator functions to add to bProgram who yields are objects containing keys wait, block, or request

const bThread = function* () {
  yield {
    wait: ['events to wait upon'],
    block: ['events to be blocked'],
    request: ['event to be request']
  };
};

Add BThread(s) to bProgram

Add these function to the bProgram using either addAll or addBthread Option 1: addAll

addAll(bThread, ...restOfYourBThreads);

Option 2: addBThread

addBThread({
  name: 'bThread', //name is an optional field
  priorit: 1,
  bThread: bThread,
})

Start/Super-Step bProgram

To start the bProgram you must call trigger with an event that is being waited upon by your bThreads

triggger('eventWaitedOn', 3 /* optional priority number */)

debugging


Paper and Source Links

This package is a fork of Ashrov, A., Marron, A., Weiss, G., & Wiener, G. behavioral progaming JS implementation using a functional programing pattern.

In their paper "A Decentralized Approach for Programming Interactive Applications with JavaScript and Blockly", they demonstrate how this approach could be used to incrementally develop of a system for controlling water level in a tank with hot and cold water sources. Below is a test demonstrating the above scenario using this library.

test('Hot/Cold', t => {
  class MyEmitter extends EventEmitter {}
  const myEmitter = new MyEmitter();
  const faucet = [];
  const { addAll, trigger } = bProgram('hot/cold');

  const addHot = function* () {
    while (faucet.length < 5) {
      yield {
        wait: ['hot'],
      };
      faucet.push('Add hot');
    }
  };
  const addCold = function* () {
    while (faucet.length < 6) {
      yield {
        wait: ['cold'],
      };
      faucet.push('Add cold');
    }
  };
  const interleave = function* () {
    while (true) {
      yield { wait: ['hot'], block: ['cold'] };
      yield { wait: ['cold'], block: ['hot'] };
    }
  };
  addAll(addHot, addCold, interleave);
  const requestHot = () => trigger('hot');
  const requestCold = () => trigger('cold');
  myEmitter.on('requestHot', requestHot);
  myEmitter.on('requestCold', requestCold);
  myEmitter.emit('requestHot');
  myEmitter.emit('requestCold');
  myEmitter.emit('requestHot');
  myEmitter.emit('requestCold');
  myEmitter.emit('requestHot');
  myEmitter.emit('requestCold');
  myEmitter.emit('requestHot');
  myEmitter.emit('requestCold');
  t.deepEqual(faucet, [
    'Add hot',
    'Add cold',
    'Add hot',
    'Add cold',
    'Add hot',
    'Add cold',
  ]);
});

Original Source Code

3.0.1

5 years ago

3.0.0

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago