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

inferencegraph

v0.1.2

Published

A knowledge graph based forward chain inferencing engine in typescript/node.

Downloads

8

Readme

inferencegraph

A knowledge graph based forward chain inferencing engine in typescript/node.

Overview

Notes

This is 'in development' and is only a working draft at the moment, not a complete system. Updates will come regularly. I have some interesting plans for this package.

Install

# From this cloned repo $ npm i

# From your node project $ npm i inferencegraph

Lint

# Within this cloned repo

$ npm run lint

Build

# Within this cloned repo

$ npm run build # Clean build & test $ npm run clean && npm run build && npm run lint && npm run test

Run

# Within this cloned repo

$ npm run test

Design Goals

  • Primitive typed fact values (string, boolean, number, object)
  • Operator evaluation (=,!=,>,<,true,false)
  • Cycle detection
  • Composition based. Can create separate fact graphs and add them to a knowledge graph at different times
  • Clean. Simple. Easy to understand
  • Extensible
  • Uses Classes. Typescript
  • Powerful rules with conditions (when), assertions, retractions and functional side-effects (do's)
  • Automatic planning. Functional attributes of rules are built into a functional "plan" during inference chaining. Once the engine has completed its inferences, an executable (and ordered) plan is returned that executes asynchronously returning Promises.
  • Ability to solve inferencing logic problems. A more detailed logic problem example will be forthcoming.
  • Low memory consumption, simple data structures
  • Work in Node and in Browser

Importing

From within your typescript module

import { Graph, Fact, Factoid, KnowledgeBase, KnowledgeGraph, Brain, Rule } from 'inferencegraph'

Classes

Factoids

An untyped data container

new Factoid({
        'name': 'barks',
        'value': true
    })

Facts

A typed data container used with Knowledge Bases

const newFact = new Fact(new Factoid({
    'name':'barks',
    'value':true
}))

Rules

Rule is a container for conditions, assertions and functions associated with facts that are evaluated by the inference engine. Each rule evaluates to either true or false based on it's when conditions, which all must be true for the rule to fire.

    new Rule({
        name: "dog",
        when: [{
            name:'hair',
            value: true,
            operator: '='
        },{
            name:'barks',
            value: true,
            operator: '='
        }],
        retract:[],
        do:[(function(callbacks) {
            return "DO: It's a dog!"+JSON.stringify(callbacks)
        })],
        assert: [{
            name:'specie',
            value: 'dog'
        }]
    })

KnowledgeBase

KnowledgeBase is a container for a collection of facts and operations on them

const corgiFacts = [ new Fact(new Factoid({
    'name':'hair',
    'value':true
})), new Fact(new Factoid({
    'name':'barks',
    'value':true
})), new Fact(new Factoid({
    'name':'fur',
    'value':'tan'
})), new Fact(new Factoid({
    'name':'legs',
    'value':'short'
}))]

const kb = new KnowledgeBase()
kb.assertFacts(corgiFacts, true);

Graph

Graph is a container for rules

const graph = new Graph([
    new Rule({
        name: "dog",
        when: [{
            name:'hair',
            value: true,
            operator: '='
        },{
            name:'barks',
            value: true,
            operator: '='
        }],
        retract:[],
        do:[(function(callbacks) { // Can receive callbacks object here, which might have other user data
            return "DO: It's a dog!"+JSON.stringify(callbacks)
        })],
        assert: [{
            name:'specie',
            value: 'dog'
        }]
    }),
    new Rule({
        name: "dalmation",
        when: [{
            name:'specie',
            value: 'dog',
            operator: '='
        },{
            name:'fur',
            value: 'spotted',
            operator: '='
        }],
        retract:[],
        do:[(function(callbacks) { // Can receive callbacks object here, which might have other user data
            return "DO: It's a dalmation!"+JSON.stringify(callbacks)
        })],
        assert: [{
            name:'breed',
            value: 'dalmation'
        }]
    })
]);

KnowledgeGraph

KnowledgeGraph is a container for graphs

const kg = new KnowledgeGraph(kb)
kg.addGraph(graph)

Callbacks

Callbacks are optional, but will be invoked during inferencing to trigger your business logic if using Plans is not the desired course.

Events

export class Callbacks extends Object {

    public onFactTrue: Function;
    public onFactFalse: Function;
    public onFactAsserted: Function;
    public onFactResolved: Function;
    public onFactRetracted: Function;

}

Usage

var callbacks = new Callbacks();

callbacks.onFactTrue = (fact, rule, when) => {
    console.log("callback: onFactTrue: ",fact,rule,when)
}
callbacks.onFactFalse = (fact, rule, when) => {
    console.log("callback: onFactFalse: ",fact,rule,when)
}
callbacks.onFactResolved = (fact) => {
    console.log("callback: onFactResolved! ",fact)
}
brain.assertFact(newFact, plan,callbacks);

Brain

Brain is a container for knowledge graphs and high-level API over them

const brain = new Brain(kg, true);

const newFact = new Fact(new Factoid({
    'name':'fur',
    'value':'spotted'
}))

var plan = [];

// Infer a plan stemming from this fact assertion
brain.assertFact(newFact, plan, callbacks);

console.log("PLAN:",plan);

plan.forEach( promise => {
    promise.then( (result) => {
        console.log("CALLBACK:", result)
    });  // Execute plan functions
})