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.
var expert =_ = ;var domain = expertConcept = domainConceptRelation = domainRelationmammal = Conceptfish = Conceptdog = Conceptcat = Conceptmouse = Conceptwhale = Conceptsalmon = Conceptfur = Conceptbark = Conceptswim = Conceptisa = domainisaexample = domainexamplehas = RelationwhatHas = Relationcan = RelationwhatCan = RelationbiggerThan = 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 = expertConcept = domainConceptRelation = 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 = Conceptfish = Conceptdog = Conceptcat = Conceptmouse = Conceptwhale = Conceptsalmon = Conceptfur = Conceptbark = Conceptswim = 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 = domainisaexample = 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.
has = RelationwhatHas = Relationcan = RelationwhatCan = RelationbiggerThan = 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.
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;