Skip to content

Commit

Permalink
Merge pull request #200 from stanford-oval/wip/post-merge-work
Browse files Browse the repository at this point in the history
First batch of work about inference mode
  • Loading branch information
gcampax authored Jul 10, 2020
2 parents 89afc47 + e57ccb2 commit 3a49577
Show file tree
Hide file tree
Showing 44 changed files with 1,791 additions and 2,998 deletions.
8 changes: 0 additions & 8 deletions data/builtins/matrix.tt

This file was deleted.

9 changes: 0 additions & 9 deletions data/builtins/thingengine.remote.tt

This file was deleted.

82 changes: 3 additions & 79 deletions languages/thingtalk/ast_manip.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ function checkValidQuery(table) {
return true;
}

function makeProgram(rule, principal = null) {
function makeProgram(rule) {
assert(rule instanceof Ast.Statement);

// FIXME: A hack for schema.org only to drop certain programs
Expand All @@ -515,7 +515,7 @@ function makeProgram(rule, principal = null) {
if (_loader.flags.nostream)
return null;
}
return new Ast.Program(null, [], [], [rule], principal);
return new Ast.Program(null, [], [], [rule], null);
}

function combineStreamCommand(stream, command) {
Expand Down Expand Up @@ -761,7 +761,7 @@ function addFilter(table, filter, forceAdd = false) {
// different story is when the filter being added is in the next sentence,
// because then we expect to paraphrase only the second filter, and hopefully not mess up
//
// hence, addFilterToProgram/addFilterToPolicy (which are contextual) pass forceAdd = true,
// hence, addFilterToProgram (which are contextual) pass forceAdd = true,
// which skips the 2 filter heuristic
if (!forceAdd && !_loader.flags.multifilters && table.isFilter && _loader.flags.turking)
return null;
Expand Down Expand Up @@ -840,30 +840,6 @@ function addFilterToProgram(program, filter) {
return clone;
}

function addFilterToPolicy(policy, filter) {
const clone = policy.clone();

if (clone.action.isSpecified) {
if (checkFilter(clone.action, filter)) {
clone.action.filter = new Ast.BooleanExpression.And(null, [clone.action.filter, filter]).optimize();
return clone;
}
}

if (clone.query.isSpecified) {
if (checkFilter(clone.query, filter)) {
clone.query.filter = new Ast.BooleanExpression.And(null, [clone.query.filter, filter]).optimize();
return clone;
}
}

if (!filter.isExternal)
return null;

clone.principal = new Ast.BooleanExpression.And(null, [clone.principal, filter]).optimize();
return clone;
}

function tableToStream(table, projArg) {
if (!table.schema.is_monitorable)
return null;
Expand All @@ -875,55 +851,6 @@ function tableToStream(table, projArg) {
return stream;
}

function inParamsToFilters(in_params) {
const operands = [];
for (let param of in_params) {
if (param.value.isUndefined)
continue;
operands.push(new Ast.BooleanExpression.Atom(null, param.name, '==', param.value));
}
return new Ast.BooleanExpression.And(null, operands);
}

function makePolicy(principal, table, action) {
if (action && action.invocation && action.invocation.selector.attributes.length)
return null;

const policyAction = action ?
new Ast.PermissionFunction.Specified(null, action.invocation.selector.kind, action.invocation.channel, inParamsToFilters(action.invocation.in_params), action.invocation.schema) :
Ast.PermissionFunction.Builtin;

let policyQuery = Ast.PermissionFunction.Builtin;
if (table) {
/*if (!table.schema.remote_confirmation || table.schema.remote_confirmation.indexOf('$__person') < 0)
return null;*/

if (table.isFilter && table.table.isInvocation) {
if (table.table.invocation.selector.attributes.length)
return null;

const queryfilter = new Ast.BooleanExpression.And(null, [inParamsToFilters(table.table.invocation.in_params), table.filter]);
policyQuery = new Ast.PermissionFunction.Specified(null, table.table.invocation.selector.kind, table.table.invocation.channel, queryfilter,
table.table.invocation.schema);
} else if (table.isInvocation) {
if (table.invocation.selector.attributes.length)
return null;

const queryfilter = inParamsToFilters(table.invocation.in_params);
policyQuery = new Ast.PermissionFunction.Specified(null, table.invocation.selector.kind, table.invocation.channel, queryfilter,
table.invocation.schema);
} else {
return null;
}
}

const sourcepredicate = principal ?
new Ast.BooleanExpression.Atom(null, 'source', '==', principal) :
Ast.BooleanExpression.True;

return new Ast.PermissionRule(null, sourcepredicate, policyQuery, policyAction);
}

function builtinSayAction(pname) {
if (!_loader.standardSchemas.say)
return null;
Expand Down Expand Up @@ -1808,8 +1735,6 @@ module.exports = {
timeGetPredicate,

makeProgram,
//combineRemoteProgram,
makePolicy,
combineStreamCommand,

checkNotSelfJoinStream,
Expand Down Expand Up @@ -1873,7 +1798,6 @@ module.exports = {
fillNextSlot,
addTimerToProgram,
addFilterToProgram,
addFilterToPolicy,
makeMonitor,

// joins
Expand Down
15 changes: 9 additions & 6 deletions languages/thingtalk/dialogue_acts/list-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {
proposalReply,
} = require('./refinement-helpers');

function checkListProposal(nameList, info) {
function checkListProposal(nameList, info, hasLearnMore) {
const { ctx, results } = nameList;
const resultType = results[0].value.id.getType();

Expand All @@ -55,7 +55,7 @@ function checkListProposal(nameList, info) {
}

const action = ctx.nextInfo && ctx.nextInfo.isAction ? getActionInvocation(ctx.next) : null;
return [results, info, action];
return [results, info, action, hasLearnMore];
}

function addActionToListProposal(nameList, action) {
Expand All @@ -67,16 +67,19 @@ function addActionToListProposal(nameList, action) {
if (ctxAction && !C.isSameFunction(ctxAction.schema, action.schema))
return null;

return [results, null, action];
return [results, null, action, false];
}

function makeListProposalReply(ctx, proposal) {
const [results, /*info*/, action] = proposal;
const [results, /*info*/, action, hasLearnMore] = proposal;
const options = {};
if (action || hasLearnMore)
options.end = false;
const dialogueAct = results.length === 2 ? 'sys_recommend_two' : 'sys_recommend_three';
if (action === null)
return makeAgentReply(ctx, makeSimpleState(ctx, dialogueAct, null), proposal);
return makeAgentReply(ctx, makeSimpleState(ctx, dialogueAct, null), proposal, null, options);
else
return makeAgentReply(ctx, addAction(ctx, dialogueAct, action, 'proposed'), proposal);
return makeAgentReply(ctx, addAction(ctx, dialogueAct, action, 'proposed'), proposal, null, options);
}

function positiveListProposalReply(ctx, [name, acceptedAction, mustHaveAction]) {
Expand Down
9 changes: 6 additions & 3 deletions languages/thingtalk/dialogue_acts/recommendation.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,18 @@ function makeAnswerStyleRecommendation({ topResult, ctx, action }, filter) {
}

function makeRecommendationReply(ctx, proposal) {
const { topResult, action } = proposal;
const { topResult, action, hasLearnMore } = proposal;
const options = {};
if (action || hasLearnMore)
options.end = false;
if (action === null) {
return makeAgentReply(ctx, makeSimpleState(ctx, 'sys_recommend_one', null), proposal);
return makeAgentReply(ctx, makeSimpleState(ctx, 'sys_recommend_one', null), proposal, null, options);
} else {
const chainParam = findChainParam(topResult, action);
if (!chainParam)
return null;
return makeAgentReply(ctx, addActionParam(ctx, 'sys_recommend_one', action, chainParam, topResult.value.id, 'proposed'),
proposal);
proposal, null, options);
}
}

Expand Down
78 changes: 0 additions & 78 deletions languages/thingtalk/en/commands.genie
Original file line number Diff line number Diff line change
Expand Up @@ -577,81 +577,3 @@ if (_tpLoader.standardSchemas.say) {
) => new Ast.Statement.Command(null, null, [C.builtinSayAction(c)]);
}
}

remote_command = {
!nofilter ('alert me' | 'tell me' | 'notify me' | 'let me know') ('if' | 'when') filter:if_filter 'in' table:complete_table ('for' | 'of') principal:constant_Entity__tt__username => {
if (C.hasGetPredicate(filter))
return null;
if (!table.schema.is_monitorable || !C.checkFilter(table, filter))
return null;
if (_tpLoader.flags.turking && table.schema.is_list)
return null;
table = C.addFilter(table, filter);
if (!table)
return null;
let stream = C.tableToStream(table, null);
if (!stream)
return null;
return C.makeProgram(new Ast.Statement.Rule(null, stream, [C.notifyAction()]), principal);
};
!nofilter ('if' | 'when') filter:if_filter 'in' table:complete_table ('for' | 'of') principal:constant_Entity__tt__username 'then' ('alert me' | 'tell me' | 'notify me' | 'let me know') => {
if (C.hasGetPredicate(filter))
return null;
if (!table.schema.is_monitorable || !C.checkFilter(table, filter))
return null;
if (_tpLoader.flags.turking && table.schema.is_list)
return null;
table = C.addFilter(table, filter);
if (!table)
return null;
let stream = C.tableToStream(table, null);
if (!stream)
return null;
return C.makeProgram(new Ast.Statement.Rule(null, stream, [C.notifyAction()]), principal);
};

(
('tell' | 'command' | 'order' | 'request' | 'ask') principal:constant_Entity__tt__username 'to' action:thingpedia_action
| ('tell' | 'command' | 'order' | 'request' | 'inform') principal:constant_Entity__tt__username 'that' ('he needs' | 'she needs' | 'i need him' | 'i need her') 'to' action:thingpedia_action
) if complete => C.makeProgram(new Ast.Statement.Command(null, null, [action]), principal);
(
('tell' | 'command' | 'order' | 'request' | 'ask') principal:constant_Entity__tt__username 'to get' table:complete_table 'and send it to me'
| ('request' | 'ask') principal:constant_Entity__tt__username 'to get' table:complete_table
| ('show me' | 'get') table:complete_table 'from' principal:constant_Entity__tt__username
| ('show me' | 'get' | 'what is') principal:constant_Entity__tt__username '\'s' table:complete_table
| ('tell' | 'command' | 'order' | 'request' | 'ask') principal:constant_Entity__tt__username 'to send me' table:complete_table
) => C.makeProgram(new Ast.Statement.Command(null, table, [C.notifyAction('return')]), principal);
('tell' | 'command' | 'order' | 'request' | 'ask') principal:constant_Entity__tt__username 'to' ('let me know' | 'inform me' | 'notify me' | 'alert me') stream:stream => C.makeProgram(new Ast.Statement.Rule(null, stream, [C.notifyAction('return')]), principal);

?extended_timers {
(
('tell' | 'command' | 'order' | 'request' | 'ask') principal:constant_Entity__tt__username 'to' action:thingpedia_action stream:timer
| ('tell' | 'command' | 'order' | 'request' | 'inform') principal:constant_Entity__tt__username 'that' ('he needs' | 'she needs' | 'i need him' | 'i need her') 'to' action:thingpedia_action stream:timer
) if complete => C.makeProgram(new Ast.Statement.Rule(null, stream, [action]), principal);
}
}

policy_command = {
('anyone' | 'anybody' | 'everyone' | 'everybody') ('can' | 'is allowed to' | 'is permitted to' | 'has permission to' | 'has my permission to') action:thingpedia_action if complete => C.makePolicy(null, null, action);
('anyone' | 'anybody' | 'everyone' | 'everybody') ('can' | 'is allowed to' | 'is permitted to' | 'has permission to' | 'has my permission to') action:thingpedia_action 'if' filter:get_predicate_filter if complete => {
if (!filter.isExternal)
return null;
let policy = C.makePolicy(null, null, action);
if (!policy)
return null;
policy.action.filter = new Ast.BooleanExpression.And(null, [policy.action.filter, filter]);
return policy;
};
('anyone' | 'anybody' | 'everyone' | 'everybody') ('can' | 'is allowed to' | 'is permitted to' | 'has permission to' | 'has my permission to') ('get' | 'see' | 'access' | 'monitor' | 'read') table:if_filtered_table => C.makePolicy(null, table, null);
source:constant_Entity__tt__username ('can' | 'is allowed to' | 'is permitted to' | 'has permission to' | 'has my permission to') action:thingpedia_action if complete => C.makePolicy(source, null, action);
source:constant_Entity__tt__username ('can' | 'is allowed to' | 'is permitted to' | 'has permission to' | 'has my permission to') action:thingpedia_action 'if' filter:get_predicate_filter if complete => {
if (!filter.isExternal)
return null;
let policy = C.makePolicy(source, null, action);
if (!policy)
return null;
policy.action.filter = new Ast.BooleanExpression.And(null, [policy.action.filter, filter]);
return policy;
};
source:constant_Entity__tt__username ('can' | 'is allowed to' | 'is permitted to' | 'has permission to' | 'has my permission to') ('get' | 'see' | 'access' | 'monitor' | 'read') table:if_filtered_table => C.makePolicy(source, table, null);
}
5 changes: 1 addition & 4 deletions languages/thingtalk/en/contextual.genie
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ $root = {
}
};

( q:complete_query_program 'how about' p:constant_Any '?'
( q:complete_query_program 'how about' p:constant_Any '?'
| q:complete_query_program 'show me' p:constant_Any 'instead'
) => C.replaceAnyParameterFromContext(q, p);

Expand Down Expand Up @@ -240,7 +240,4 @@ $root = {
return null;
return C.addFilterToProgram(prog, filter, _tpLoader);
};

prog:policy ('only if' | 'do this only if' | 'and only when' | 'only when') filter:atom_filter => C.addFilterToPolicy(prog, filter, _tpLoader);
prog:policy ('only if' | 'do this only if' | 'and only when' | 'only when') filter:get_predicate_filter => C.addFilterToPolicy(prog, filter, _tpLoader);
}
17 changes: 16 additions & 1 deletion languages/thingtalk/en/dialogue.genie
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ function context(state) {
return [tags, ctx];
}

// handle answers generated from the UI
function answer(state, answer) {
const ctx = S.getContextInfo(state);
switch (state.dialogueAct) {
case 'sys_slot_fill':
return D.impreciseSlotFillAnswer(ctx, answer);
case 'sys_search_question':
return D.impreciseSearchQuestionAnswer(ctx, answer);
case 'sys_confirm_action':
// TODO
return null;
default:
return null;
}
}

$agent = {
( ctx:ctx_greet ('hello' | 'hi') ('!' | ',') ('how can i help you' | 'what are you interested in' | 'what can i do for you') '?'
Expand All @@ -143,7 +158,7 @@ $agent = {
// action results
action_success_phrase;
action:action_success_phrase anything_else_phrase => action;
action:action_success_phrase anything_else_phrase => S.setEndBit(action, false);
action_error_phrase;
// confirming an action
Expand Down
6 changes: 3 additions & 3 deletions languages/thingtalk/en/dlg/list-proposal.genie
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ system_list_proposal = {
'would you like to learn more ?' | 'would you like to learn more about either of them ?' |
'would you like to know more about them ?' | 'would you like to hear more about them ?' |
'are you interested to hear more about any of them ?')
=> D.checkListProposal(name_list, null);
=> D.checkListProposal(name_list, null, true);

('i see' | 'i have' | 'i have found') name_list:result_name_list '.' ('they' | 'all of them') 'are' info:result_info_phrase '.'
=> D.checkListProposal(name_list, info);
=> D.checkListProposal(name_list, info, true);

('i see' | 'i have' | 'i have found') name_list:result_name_list '.' 'both are' info:result_info_phrase '.' => {
if (name_list.results.length !== 2)
return null;
return D.checkListProposal(name_list, info);
return D.checkListProposal(name_list, info, true);
};

('i see' | 'i have' | 'i have found') name_list:result_name_list '.' action:action_coref_list_proposal
Expand Down
3 changes: 2 additions & 1 deletion languages/thingtalk/en/dlg/search-questions.genie
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ disjunctive_search_question = {
return [pname];
};
// FIXME we need to check that the base table matches the current domain
( ('are you looking for' | 'would you like' | 'do you need') 'a' table:base_table ('with' | 'that has') question:question_npp_filter_pair '?'
| ('are you looking for' | 'would you like' | 'do you need') 'a' question:question_apv_filter_pair table:base_table '?'
| ('are you looking for' | 'would you like' | 'do you need') 'a' table:base_table question:question_pvp_filter_pair '?'
Expand All @@ -142,7 +143,7 @@ simple_search_question = {
one_param_direct_search_question;
two_param_direct_search_question;
disjunctive_search_question;
!inference disjunctive_search_question;
one_param_yesno_direct_search_question;
two_param_yesno_direct_search_question;
Expand Down
3 changes: 0 additions & 3 deletions languages/thingtalk/en/thingtalk.genie
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ import './stream_tables';
$root = {
thingpedia_program if complete;

?remote_commands remote_command [weight=0.1];
?policies policy_command [weight=0.2];

?timer stmt:timer_rule [weight=0.2] => C.makeProgram(stmt);
!nostream {
( stmt:monitor_command [weight=0.2]
Expand Down
Loading

0 comments on commit 3a49577

Please sign in to comment.