Created
September 5, 2011 19:14
-
-
Save chrisdone/1195706 to your computer and use it in GitHub Desktop.
ll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////// | |
// Simple logic demo, no point in it. But just 'cuz. | |
var Unifier = require('./unifier.js'); | |
var unify = Unifier.unify; | |
var parse = Unifier.parse; | |
/***************************************************************************** | |
* A simple logic language querier. | |
*/ | |
var search = exports.search = function(db,exprStr){ | |
var results = [], expr = parse(exprStr); | |
db.map(function(entry){ | |
var result; | |
try { | |
if(typeof entry != 'string') { | |
// First, ensure that the conclusion of the rule matches the | |
// query. | |
// E.g. Query: (Person a) | |
// Conclusion: (Person x) | |
var conclusion = parse(entry[0]); | |
unify({exp1:conclusion,exp2:expr}); | |
// Second, search for an instantiation of the rule's body in | |
// the DB. | |
var body = entry[1]; | |
var dbSansThisRule = db; | |
queryResult = search(dbSansThisRule,body)[0]; | |
// Third, unify to get the /frame/ for the unification of the | |
// body and the result. | |
var frame; | |
var bod = unify({exp1:queryResult,exp2:parse(body),returnFrame:true}); | |
// Finally, merely instantiate the conclusion against the | |
// body's frame. | |
result = unify({ | |
exp1:conclusion,exp2:conclusion,frame:bod.frame | |
}).result; | |
} | |
else { | |
result = unify({exp1:parse(entry),exp2:expr}).result; | |
} | |
} catch (e){ | |
return false; | |
} | |
results.push(result); | |
}); | |
return results.length? results : 'No.'; | |
} | |
// Example | |
// console.log(search([ | |
// 'Programmer Foo', | |
// ['MeetingsVisitor person','SupremeManager person'] | |
// 'Programmer Bar', | |
// 'SupremeManager Mu', | |
// 'Programmer Zot' | |
// ],'MeetingsVisitor who')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment