Skip to content

Commit

Permalink
getters and setters
Browse files Browse the repository at this point in the history
  • Loading branch information
dotcypress committed Mar 18, 2013
1 parent 2a83752 commit 2ec2bd4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
15 changes: 9 additions & 6 deletions lib/engine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ class AiEngine
constructor: (@roomName, @topics, botData) ->
throw "Topics not found" unless @topics
throw "Room name is undefined not found" unless @roomName
@roomData = { topic: null}
@view =
topic: null
bot: botData
set: (name, value) => @roomData[name] = value
get: (name) => @roomData[name] or ''
set: (name, value) =>
console.log 'dfdfdfdf'
@view[name] = value
get: (name) => @view[name] or ''

_.each @topics, (topic) =>
_.each topic.categories, (category) =>
category["room:#{@roomName}"] = new RegExp (category.pattern.replace '*', '(.*)'), "i"
category["room:#{@roomName}"] = new RegExp (category.pattern.replace '*', '([^/?!.;:$]*)'), "i"

getCurrentTopic: () ->
_.find @topics, (topic) => topic.name is @roomData.topic
_.find @topics, (topic) => topic.name is @view.topic

findCategory: (message) ->
topic = @getCurrentTopic()
return @roomData.topic = null unless topic
return @view.topic = null unless topic
_.find topic.categories, (category) => category["room:#{@roomName}"].test message

reply: (authorData, message, cb) ->
Expand All @@ -31,6 +33,7 @@ class AiEngine
return @reply authorData, category.template.link, cb if category.template?.link
match = category["room:#{@roomName}"].exec message
@view.star = match[1] if match and match.length > 0
category.template.do @view, @view.star if category.template.do
responce = mustache.render category.template.text, @view
cb null, responce

Expand Down
11 changes: 11 additions & 0 deletions lib/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ path = require 'path'
_ = require 'underscore'
async = require 'async'
DomJS = require("dom-js").DomJS
mustache = require 'mustache'
engine = require './engine'

parse = (xml, cb) ->
Expand Down Expand Up @@ -72,17 +73,27 @@ parseMixedTemplateContentContainer = (node) ->
return undefined unless node
linkNode = _.find node.children, (subNode) -> subNode.name is 'srai'
return link: linkNode.children[0].text if linkNode
setterNode = _.find node.children, (subNode) -> subNode.name is 'set'
simpleNodes = _.filter node.children, (subNode) ->
subNode.name is 'bot' or subNode.name is 'star' or subNode.text
text: trim _.reduce simpleNodes, ((acc, next) -> "#{acc}#{parseTemplateExpression next}"), ''
do: processSetter setterNode if setterNode

parseTemplateExpression = (node) ->
if node.name
return "{{bot.#{node.attributes.name}}}" if node.name is 'bot'
return "{{star}}" if node.name is 'star'
return "{{#{node.attributes.name}}}" if node.name is 'get'
return ''
node.text

processSetter = (node) ->
value = parseMixedTemplateContentContainer node
content = value.text
if (content.indexOf '{{star}}') != -1
return (state, star) -> state[node.attributes.name] = mustache.render content, {star: star}
(state) -> state[node.attributes.name] = content

trim = (string) -> string.replace /^\s+|\s+$/g, ''

module.exports.parse = parse
Expand Down
37 changes: 32 additions & 5 deletions test/engine-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<pattern>how old are you</pattern>
<template><srai>you age</srai></template>
</category>
<category>
<pattern>lets chainge topic to Dev</pattern>
<template><set name=\"topic\">Development</set>ok</template>
</category>
<category>
<pattern>lets talk about *</pattern>
<template><set name=\"subject\"><star/></set></template>
<template><set name=\"subject\"><star/> stuff</set>ok</template>
</category>
<category>
<pattern>what the subject</pattern>
Expand Down Expand Up @@ -75,23 +79,46 @@ describe 'AIML engine', () ->
it 'should responce to exact message', (done) ->
engine.reply {name: 'Lisa'}, 'what is your name', (err, reply) ->
should.exist reply
reply.should.to.be.equal 'My name is Jonny'
reply.should.be.equal 'My name is Jonny'
done()

it 'should responce to not exact message', (done) ->
engine.reply {name: 'Lisa'}, 'Hey, what is your name?', (err, reply) ->
should.exist reply
reply.should.to.be.equal 'My name is Jonny'
reply.should.be.equal 'My name is Jonny'
done()

it 'should responce with context', (done) ->
engine.reply {name: 'Lisa'}, 'Dude, do you like bananas', (err, reply) ->
should.exist reply
reply.should.to.be.equal 'bananas? Maybe.'
reply.should.be.equal 'bananas? Maybe.'
done()

it 'should work with references', (done) ->
engine.reply {name: 'Lisa'}, 'how old are you?', (err, reply) ->
should.exist reply
reply.should.be.equal '21'
done()

it 'should work with references', (done) ->
engine.reply {name: 'Lisa'}, 'how old are you?', (err, reply) ->
should.exist reply
reply.should.to.be.equal '21'
reply.should.be.equal '21'
done()

it 'should work with setters ang getters', (done) ->
engine.reply {name: 'Lisa'}, 'lets chainge topic to Dev?', (err, reply) ->
should.exist reply
reply.should.be.equal 'ok'
should.exist engine.view.topic
engine.view.topic.should.be.equal 'Development'
done()

it 'should work with setters ang getters (with star)', (done) ->
engine.reply {name: 'Lisa'}, 'lets talk about js?', (err, reply) ->
should.exist reply
reply.should.be.equal 'ok'
engine.reply {name: 'Lisa'}, 'what the subject?', (err, reply) ->
should.exist reply
reply.should.be.equal 'Subject is js stuff'
done()
30 changes: 26 additions & 4 deletions test/parser-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<pattern>how old are you</pattern>
<template><srai>you age</srai></template>
</category>
<category>
<pattern>lets chainge topic to Dev</pattern>
<template><set name=\"topic\">Development</set>ok</template>
</category>
<category>
<pattern>lets talk about *</pattern>
<template><set name=\"subject\"><star/></set>ok</template>
<template><set name=\"subject\"><star/> stuff</set>ok</template>
</category>
<category>
<pattern>what the subject</pattern>
Expand All @@ -72,7 +76,7 @@ describe 'AIML parser', () ->
parse xml, (err, topics) ->
should.not.exist err
topics.should.have.length 2
topics[0].categories.should.have.length 6
topics[0].categories.should.have.length 7
should.not.exist topics[0].name
topics[1].name.should.equal 'Development'
done()
Expand Down Expand Up @@ -112,9 +116,27 @@ describe 'AIML parser', () ->
category.template.link.should.equal 'you age'
done()

it.skip 'should parse setters', (done) ->
it 'should parse setters', (done) ->
parse xml, (err, topics) ->
category = topics[0].categories[4]
category.pattern.should.equal 'lets chainge topic to Dev'
category.template.text.should.equal 'ok'
should.exist category.template.do
category.template.do.should.be.a 'function'
done()

it 'should parse setters with star', (done) ->
parse xml, (err, topics) ->
category = topics[0].categories[5]
category.pattern.should.equal 'lets talk about *'
category.template.text.should.equal '{{set(\'subject\', star)}}ok'
category.template.text.should.equal 'ok'
should.exist category.template.do
category.template.do.should.be.a 'function'
done()

it 'should parse getters', (done) ->
parse xml, (err, topics) ->
category = topics[0].categories[6]
category.pattern.should.equal 'what the subject'
category.template.text.should.equal 'Subject is {{subject}}'
done()

0 comments on commit 2ec2bd4

Please sign in to comment.