Comparing version 0.5.3 to 0.5.4
@@ -1,2 +0,2 @@ | ||
export interface ChatState<BOT, USER, CHANNEL, CONVERSATION, USERINCONVERSATION> { | ||
export interface ChatState<BOT = undefined, USER = undefined, CHANNEL = undefined, CONVERSATION = undefined, USERINCONVERSATION = undefined> { | ||
bot?: BOT; | ||
@@ -3,0 +3,0 @@ user?: USER; |
@@ -19,3 +19,5 @@ import { Observable } from 'rxjs'; | ||
export declare const observize: <T>(t: Observizeable<T>) => Observable<T>; | ||
export declare function isRule<M>(r: IRule<M> | Handler<M>): r is IRule<M>; | ||
export declare const ruleize: <M extends Match>(r: IRule<M> | Handler<M>) => IRule<M>; | ||
export declare const matchize: <M extends Match>(matcher: Matcher<M, Match>, match: M) => Observable<Match>; | ||
export declare abstract class BaseRule<M extends Match> implements IRule<M> { | ||
@@ -34,3 +36,3 @@ abstract tryMatch(match: M): Observable<RuleResult>; | ||
private handler; | ||
constructor(...args: (Matcher | Handler)[]); | ||
constructor(...args: (Matcher | Predicate | Handler)[]); | ||
tryMatch(match: M): Observable<RuleResult>; | ||
@@ -55,30 +57,23 @@ prependMatcher<L extends Match>(matcher: Matcher<L, M>): SimpleRule<L>; | ||
} | ||
export interface Predicate<M extends Match> { | ||
export interface Predicate<M extends Match = M> { | ||
(match: M): Observizeable<boolean>; | ||
} | ||
export interface Predicates<M extends Match> { | ||
[name: string]: Predicate<M>; | ||
} | ||
export declare const Helpers: <M extends Match>() => { | ||
rule: { | ||
(handler: Handler<M>): any; | ||
<Z extends Match>(m1: Matcher<M, Z>, handler: Handler<Z>): any; | ||
<N extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, Z>, handler: Handler<Z>): any; | ||
<N extends Match, O extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, handler: Handler<Z>): any; | ||
<N extends Match, O extends Match, P extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, m4: Matcher<P, Z>, handler: Handler<Z>): any; | ||
<N extends Match, O extends Match, P extends Match, Q extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, m4: Matcher<P, Q>, m5: Matcher<Q, Z>, handler: Handler<Z>): any; | ||
(handler: IRule<M> | Handler<M>): any; | ||
(p1: Predicate<M>, handler: IRule<M> | Handler<M>): any; | ||
<Z extends Match>(m1: Matcher<M, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<Z extends Match>(p1: Predicate<M>, m2: Matcher<M, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<Z extends Match>(m1: Matcher<M, Z>, p2: Predicate<Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<N extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<Z extends Match>(m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<Z extends Match>(p1: Matcher<M, Match>, m2: Matcher<M, Z>, p3: Predicate<Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<Z extends Match>(p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<N extends Match, Z extends Match>(p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<N extends Match, Z extends Match>(m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<N extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, handler: Handler<Z> | IRule<Z>): any; | ||
<N extends Match, O extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, handler: Handler<Z> | IRule<Z>): any; | ||
}; | ||
first: (...rules: (IRule<M> | Handler<M>)[]) => IRule<M>; | ||
run: (handler: Handler<M>) => IRule<M>; | ||
matchAlways: (match: M) => M; | ||
matchPredicate: (predicate: Predicate<M>) => (match: any) => Observable<any>; | ||
filter: (predicate: Predicate<M>, rule: IRule<M> | Handler<M>) => IRule<M>; | ||
prepend: { | ||
<L extends Match>(m1: Matcher<L, M>, rule: IRule<M>): IRule<L>; | ||
<K extends Match, L extends Match>(m1: Matcher<K, L>, m2: Matcher<L, M>, rule: IRule<M>): IRule<K>; | ||
<J extends Match, K extends Match, L extends Match>(m1: Matcher<J, K>, m2: Matcher<K, L>, m3: Matcher<L, M>, rule: IRule<M>): IRule<J>; | ||
<I extends Match, J extends Match, K extends Match, L extends Match>(m1: Matcher<I, J>, m2: Matcher<J, K>, m3: Matcher<K, L>, m4: Matcher<L, M>, rule: IRule<M>): IRule<I>; | ||
<H extends Match, I extends Match, J extends Match, K extends Match, L extends Match>(m1: Matcher<H, I>, m2: Matcher<I, J>, m3: Matcher<J, K>, m4: Matcher<K, L>, m5: Matcher<L, M>, rule: IRule<M>): IRule<H>; | ||
<L extends Match>(m1: Matcher<L, Match>, ...matchers: (Matcher<Match, Match> | IRule<Match>)[]): IRule<L>; | ||
}; | ||
}; |
@@ -27,5 +27,16 @@ "use strict"; | ||
}; | ||
function isRule(r) { | ||
return (r.tryMatch !== undefined); | ||
} | ||
exports.isRule = isRule; | ||
exports.ruleize = function (r) { | ||
return ((r.tryMatch) ? r : new SimpleRule(r)); | ||
return isRule(r) ? r : new SimpleRule(r); | ||
}; | ||
exports.matchize = function (matcher, match) { | ||
// we want to allow any matcher to be a predicate (return a boolean) | ||
// if so, the 'falsey' case will be filtered out by observize, | ||
// so we just need to catch the case where it is precisely true | ||
return exports.observize(matcher(match)) | ||
.map(function (m) { return typeof (m) === 'boolean' ? match : m; }); | ||
}; | ||
var BaseRule = (function () { | ||
@@ -58,3 +69,4 @@ function BaseRule() { | ||
console.log("calling matcher #" + i, currentMatcher); | ||
return exports.observize(currentMatcher(prevMatch)).do(function (result) { return console.log("result", result); }); | ||
return exports.matchize(currentMatcher, prevMatch) | ||
.do(function (result) { return console.log("result", result); }); | ||
}); | ||
@@ -87,13 +99,15 @@ }, rxjs_1.Observable.of(match)) | ||
console.log("SimpleRule.tryMatch", this.matchers); | ||
return this.matchers.length | ||
? combineMatchers.apply(void 0, this.matchers)(match) | ||
.do(function (m) { return console.log("match", m); }) | ||
.map(function (m) { return ({ | ||
score: m.score, | ||
action: function () { return _this.handler(m); } | ||
}); }) | ||
: rxjs_1.Observable.of({ | ||
if (this.matchers.length === 0) | ||
return rxjs_1.Observable.of({ | ||
score: match.score, | ||
action: function () { return _this.handler(match); } | ||
}); | ||
return (this.matchers.length === 1 | ||
? exports.matchize(this.matchers[0], match) | ||
: combineMatchers.apply(void 0, this.matchers)(match)) | ||
.do(function (m) { return console.log("match", m); }) | ||
.map(function (m) { return ({ | ||
score: m.score, | ||
action: function () { return _this.handler(m); } | ||
}); }); | ||
}; | ||
@@ -116,3 +130,5 @@ SimpleRule.prototype.prependMatcher = function (matcher) { | ||
console.log("FirstMatchingRule.constructor: rules", rules); | ||
_this.rule$ = rxjs_1.Observable.from(rules).filter(function (rule) { return !!rule; }).map(function (rule) { return exports.ruleize(rule); }); | ||
_this.rule$ = rxjs_1.Observable.from(rules) | ||
.filter(function (rule) { return !!rule; }) | ||
.map(function (rule) { return exports.ruleize(rule); }); | ||
return _this; | ||
@@ -144,3 +160,3 @@ } | ||
var _this = this; | ||
return exports.observize(this.matcher(match)) | ||
return exports.matchize(this.matcher, match) | ||
.flatMap(function (m) { return _this.rule.tryMatch(m); }); | ||
@@ -172,2 +188,13 @@ }; | ||
} | ||
var ruleOrHandler = args[args.length - 1]; | ||
if (isRule(ruleOrHandler)) { | ||
switch (args.length) { | ||
case 1: | ||
return ruleOrHandler; | ||
case 2: | ||
return ruleOrHandler.prependMatcher(args[0]); | ||
default: | ||
return ruleOrHandler.prependMatcher(combineMatchers.apply(void 0, args.slice(0, args.length - 1))); | ||
} | ||
} | ||
return new (SimpleRule.bind.apply(SimpleRule, [void 0].concat(args)))(); | ||
@@ -183,29 +210,8 @@ } | ||
var run = function (handler) { return new RunRule(handler); }; | ||
var matchAlways = function (match) { return match; }; | ||
var matchPredicate = function (predicate) { | ||
return function (match) { | ||
return exports.observize(predicate(match)) | ||
.map(function (_) { return match; }); | ||
}; | ||
}; | ||
var filter = function (predicate, rule) { | ||
return exports.ruleize(rule).prependMatcher(matchPredicate(predicate)); | ||
}; | ||
function prepend() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return args[args.length - 1].prependMatcher(combineMatchers.apply(void 0, args.slice(0, args.length - 1))); | ||
} | ||
return { | ||
rule: rule, | ||
first: first, | ||
run: run, | ||
matchAlways: matchAlways, | ||
matchPredicate: matchPredicate, | ||
filter: filter, | ||
prepend: prepend, | ||
run: run | ||
}; | ||
}; | ||
//# sourceMappingURL=Rules.js.map |
@@ -159,3 +159,3 @@ "use strict"; | ||
// luis.rule('intent2', handler2) | ||
// ).prepend(luis.model()) | ||
// ).prependMatcher(luis.model()) | ||
LuisModel.prototype.best = function () { | ||
@@ -162,0 +162,0 @@ var _this = this; |
{ | ||
"name": "prague", | ||
"version": "0.5.3", | ||
"description": "experimental Rx-based bot technology", | ||
"version": "0.5.4", | ||
"description": "rules-based app engine", | ||
"main": "dist/prague.js", | ||
@@ -6,0 +6,0 @@ "types": "dist/prague.d.ts", |
@@ -1,2 +0,2 @@ | ||
export interface ChatState<BOT, USER, CHANNEL, CONVERSATION, USERINCONVERSATION> { | ||
export interface ChatState<BOT = undefined, USER = undefined, CHANNEL = undefined, CONVERSATION = undefined, USERINCONVERSATION = undefined> { | ||
bot?: BOT | ||
@@ -3,0 +3,0 @@ user?: USER, |
115
src/Rules.ts
@@ -40,6 +40,18 @@ import { Observable } from 'rxjs'; | ||
export function isRule<M>(r: IRule<M> | Handler<M>): r is IRule<M> { | ||
return ((r as any).tryMatch !== undefined); | ||
} | ||
export const ruleize = <M extends Match>(r: IRule<M> | Handler<M>) => { | ||
return (((r as IRule<M>).tryMatch) ? r : new SimpleRule(r as Handler<M>)) as IRule<M>; | ||
return isRule(r) ? r : new SimpleRule(r) as IRule<M>; | ||
} | ||
export const matchize = <M extends Match>(matcher: Matcher<M>, match: M) => { | ||
// we want to allow any matcher to be a predicate (return a boolean) | ||
// if so, the 'falsey' case will be filtered out by observize, | ||
// so we just need to catch the case where it is precisely true | ||
return observize(matcher(match)) | ||
.map(m => typeof(m) === 'boolean' ? match : m); | ||
} | ||
export abstract class BaseRule<M extends Match> implements IRule<M> { | ||
@@ -74,3 +86,4 @@ abstract tryMatch(match: M): Observable<RuleResult>; | ||
console.log(`calling matcher #${i}`, currentMatcher); | ||
return observize(currentMatcher(prevMatch)).do(result => console.log("result", result)); | ||
return matchize(currentMatcher, prevMatch) | ||
.do(result => console.log("result", result)); | ||
}), | ||
@@ -86,3 +99,3 @@ Observable.of(match) | ||
constructor(... args: (Matcher | Handler)[]) { | ||
constructor(... args: (Matcher | Predicate | Handler)[]) { | ||
super(); | ||
@@ -100,13 +113,19 @@ if (args.length < 1) { | ||
console.log("SimpleRule.tryMatch", this.matchers); | ||
return this.matchers.length | ||
? (combineMatchers<M>(... this.matchers)(match) as Observable<Match>) | ||
.do(m => console.log("match", m)) | ||
.map(m => ({ | ||
score: m.score, | ||
action: () => this.handler(m) | ||
} as RuleResult)) | ||
: Observable.of({ | ||
if (this.matchers.length === 0) | ||
return Observable.of({ | ||
score: match.score, | ||
action: () => this.handler(match) | ||
} as RuleResult); | ||
return ( | ||
this.matchers.length === 1 | ||
? matchize(this.matchers[0], match) | ||
: combineMatchers<M>(... this.matchers)(match) as Observable<Match> | ||
) | ||
.do(m => console.log("match", m)) | ||
.map(m => ({ | ||
score: m.score, | ||
action: () => this.handler(m) | ||
} as RuleResult)); | ||
} | ||
@@ -130,3 +149,5 @@ | ||
console.log("FirstMatchingRule.constructor: rules", rules); | ||
this.rule$ = Observable.from(rules).filter(rule => !!rule).map(rule => ruleize(rule)); | ||
this.rule$ = Observable.from(rules) | ||
.filter(rule => !!rule) | ||
.map(rule => ruleize(rule)); | ||
} | ||
@@ -157,3 +178,3 @@ | ||
tryMatch(match: L): Observable<RuleResult> { | ||
return observize(this.matcher(match)) | ||
return matchize(this.matcher, match) | ||
.flatMap(m => this.rule.tryMatch(m)); | ||
@@ -210,46 +231,44 @@ } | ||
export interface Predicate<M extends Match> { | ||
export interface Predicate<M extends Match = M> { | ||
(match: M): Observizeable<boolean>; | ||
} | ||
export interface Predicates<M extends Match> { | ||
[name: string]: Predicate<M> | ||
} | ||
export const Helpers = <M extends Match>() => { | ||
// helpers are first defined as local variables so that they can call each other if necessary | ||
function rule(handler: Handler<M>) | ||
function rule<Z extends Match>(m1: Matcher<M, Z>, handler: Handler<Z>) | ||
function rule<N extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, Z>, handler: Handler<Z>) | ||
function rule<N extends Match, O extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, handler: Handler<Z>) | ||
function rule<N extends Match, O extends Match, P extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, m4: Matcher<P, Z>, handler: Handler<Z>) | ||
function rule<N extends Match, O extends Match, P extends Match, Q extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, m4: Matcher<P, Q>, m5: Matcher<Q, Z>, handler: Handler<Z>) | ||
function rule<A>(... args: (Matcher | Handler)[]) { | ||
return new SimpleRule(... args) as IRule<M>; | ||
} | ||
function rule(handler: Handler<M> | IRule<M>) | ||
const first = (... rules: (IRule<M> | Handler<M>)[]) => new FirstMatchingRule(... rules) as IRule<M>; | ||
function rule(p1: Predicate<M>, handler: Handler<M> | IRule<M>) | ||
function rule<Z extends Match>(m1: Matcher<M, Z>, handler: Handler<Z> | IRule<Z>) | ||
const run = (handler: Handler<M>) => new RunRule<M>(handler) as IRule<M>; | ||
function rule<Z extends Match>(p1: Predicate<M>, m2: Matcher<M, Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<Z extends Match>(m1: Matcher<M, Z>, p2: Predicate<Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<N extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, Z>, handler: Handler<Z> | IRule<Z>) | ||
const matchAlways = (match: M) => match; | ||
function rule<Z extends Match>(m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<Z extends Match>(p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<Z extends Match>(p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<N extends Match, Z extends Match>(p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<N extends Match, Z extends Match>(m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<N extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, handler: Handler<Z> | IRule<Z>) | ||
function rule<N extends Match, O extends Match, Z extends Match>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, handler: Handler<Z> | IRule<Z>) | ||
const matchPredicate = (predicate: Predicate<M>) => | ||
match => | ||
observize(predicate(match)) | ||
.map(_ => match); | ||
function rule(... args: (Predicate | Matcher | Handler | IRule)[]) { | ||
const ruleOrHandler = args[args.length - 1] as Handler | IRule; | ||
if (isRule(ruleOrHandler)) { | ||
switch (args.length) { | ||
case 1: | ||
return ruleOrHandler; | ||
case 2: | ||
return ruleOrHandler.prependMatcher(args[0] as Matcher); | ||
default: | ||
return ruleOrHandler.prependMatcher(combineMatchers(... args.slice(0, args.length - 1) as Matcher[])); | ||
} | ||
} | ||
return new SimpleRule(... args as Matcher[]) as IRule<M>; | ||
} | ||
const filter = (predicate: Predicate<M>, rule: IRule<M> | Handler<M>) => | ||
ruleize(rule).prependMatcher<M>(matchPredicate(predicate)); | ||
const first = (... rules: (IRule<M> | Handler<M>)[]) => new FirstMatchingRule(... rules) as IRule<M>; | ||
function prepend<L extends Match>(m1: Matcher<L, M>, rule: IRule<M>): IRule<L> | ||
function prepend<K extends Match, L extends Match>(m1: Matcher<K, L>, m2: Matcher<L, M>, rule: IRule<M>): IRule<K> | ||
function prepend<J extends Match, K extends Match, L extends Match>(m1: Matcher<J, K>, m2: Matcher<K, L>, m3: Matcher<L, M>, rule: IRule<M>): IRule<J> | ||
function prepend<I extends Match, J extends Match, K extends Match, L extends Match>(m1: Matcher<I, J>, m2: Matcher<J, K>, m3: Matcher<K, L>, m4: Matcher<L, M>, rule: IRule<M>): IRule<I> | ||
function prepend<H extends Match, I extends Match, J extends Match, K extends Match, L extends Match>(m1: Matcher<H, I>, m2: Matcher<I, J>, m3: Matcher<J, K>, m4: Matcher<K, L>, m5: Matcher<L, M>, rule: IRule<M>): IRule<H> | ||
function prepend<L extends Match>(m1: Matcher<L>, ... matchers: (Matcher | IRule)[]): IRule<L> | ||
function prepend<L extends Match>(... args: (Matcher | IRule)[]): IRule<L> { | ||
return (args[args.length - 1] as IRule).prependMatcher(combineMatchers(... args.slice(0, args.length - 1) as Matcher[])); | ||
} | ||
const run = (handler: Handler<M>) => new RunRule<M>(handler) as IRule<M>; | ||
@@ -259,8 +278,4 @@ return { | ||
first, | ||
run, | ||
matchAlways, | ||
matchPredicate, | ||
filter, | ||
prepend, | ||
run | ||
} | ||
} |
@@ -194,3 +194,3 @@ import { Observable } from 'rxjs'; | ||
// luis.rule('intent2', handler2) | ||
// ).prepend(luis.model()) | ||
// ).prependMatcher(luis.model()) | ||
@@ -197,0 +197,0 @@ best(... luisRules: LuisRule<M>[]): IRule<M> { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
160651
2604