Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

getters and setters

  • Loading branch information...
commit 2ec2bd4a2b036efeebdcd315b401c48f49040f71 1 parent 2a83752
@dotcypress authored
View
15 lib/engine.coffee
@@ -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) ->
@@ -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
View
11 lib/parser.coffee
@@ -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) ->
@@ -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
View
37 test/engine-tests.coffee
@@ -22,8 +22,12 @@ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<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>
@@ -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()
View
30 test/parser-tests.coffee
@@ -47,8 +47,12 @@ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<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>
@@ -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()
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.