Expert.js
Expert.js is miniature semantic network framework written in JavaScript.
Motivation
Representing knowledge for artificial intelligence applications can be simplified by the flexibility of scripting languages. Expert.js provides a DSL-like constructs for building semantic networks. The result is readable code, that's easy to work with.
Quick Example
var expert = _ = ; var domain = expert Concept = domainConcept Relation = domainRelation mammal = Concept fish = Concept dog = Concept cat = Concept mouse = Concept whale = Concept salmon = Concept fur = Concept bark = Concept swim = Concept isa = domainisa example = domainexample has = Relation whatHas = Relation can = Relation whatCan = Relation biggerThan = Relation; smallerThan = Relation; salmon ; whale ; dog ; cat ; mouse ; console;var answer1 = ;console; console;var answer2 = _;console;
Step by Step
We begin by including Expert.js and Underscore.js.
var expert = _ = ;
To begin working with Expert.js you must first create a Domain object. Concepts and Relations are part of a Domain. You can separate different semantic networks using different Domains.
The Concept and Relation objects are mere references and are declared for brevity.
var domain = expert Concept = domainConcept Relation = domainRelation
Concepts are the building blocks of your semantic network. You can also provide a unique identifier that can help you identify the concepts later on. Concepts can be tangible or abstract. We will soon see how you can describe the abstraction hierarchy of related Concepts using an "isa" relation.
mammal = Concept fish = Concept dog = Concept cat = Concept mouse = Concept whale = Concept salmon = Concept fur = Concept bark = Concept swim = Concept
Expert.js comes with 2 predefined relations: "isa" and "example". You can use an "isa" relation to express abstraction hierarchies of Concepts. In this example we will use it to indicate that a dog, a cat, a whale and a mouse are kinds of mammal.
isa = domainisa example = domainexample
The "example" relation is the inverse relation of "isa". Inverse relations maintain a back link between related concepts. This will allow you to query relations from an inverse direction in the semantic network.
You can define as many relation types as you wish. Relations allow you to introduce facts to your network of Concepts. Relations can also have unique identifiers. If you use an identifier that is also a valid JavaScript function name, Expert.js will create a method for your Domain's Concept prototype - for syntactic sugar.
has = Relation whatHas = Relation can = Relation whatCan = Relation biggerThan = Relation; smallerThan = Relation;
We can now use our defined relations to establish facts. In this example we use the shorthand method. This method was made possible because we used valid function names as our relations identifiers.
salmon ; whale ; dog ; cat ; mouse ;
Finally, we can inspect our semantic network. Note how we use Underscore.js to do a primitive "join" over inverse relations:
console;var answer1 = ;console; console;var answer2 = _;console;