"use strict";
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const thingtalk_1 = require("thingtalk");
const C = __importStar(require("../ast_manip"));
async function default_1($runtime, $ttUtils, $options, $locale, $grammar, $loader) {
    const HERE = new thingtalk_1.Ast.Value.Location(new thingtalk_1.Ast.Location.Relative('current_location'));
    $grammar.declareSymbol("projection_Measure_m");
    $grammar.declareSymbol("compute_question");
    $grammar.declareSymbol("with_arg_min_max_table");
    $grammar.declareSymbol("projection_Measure_m");
    if (!$options.flags.turking) {
        $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["from here", ""])], (($0, $1, table, $2) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), new $runtime.Choice(["from here", ""])], (($0, $1, $2, table, $3) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), "is", new $runtime.Choice(["from here", ""])], (($0, $1, table, $2, $3) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), "is", new $runtime.Choice(["from here", ""])], (($0, $1, $2, table, $3, $4) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), "from", new $runtime.NonTerminal("constant_Location", undefined)], (($0, $1, table, $2, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["the distance from", new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["of", "to"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)])], (($0, place, $1, table) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["the distance", new $runtime.Choice(["of", "to"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), "from", new $runtime.NonTerminal("constant_Location", undefined)], (($0, $1, $2, table, $3, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["the distance from", new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["of", "to"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)])], (($0, place, $1, $2, table) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "from", new $runtime.NonTerminal("constant_Location", undefined), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), "is"], (($0, $1, $2, place, table, $3) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), "is from", new $runtime.NonTerminal("constant_Location", undefined)], (($0, $1, table, $2, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "from", new $runtime.NonTerminal("constant_Location", undefined), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), "is"], (($0, $1, $2, place, $3, table, $4) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Measure_m", ["how", new $runtime.Choice(["far", "near", "close"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), "is from", new $runtime.NonTerminal("constant_Location", undefined)], (($0, $1, $2, table, $3, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
    }
    $grammar.declareSymbol("projection_Number");
    if (!$options.flags.turking) {
        $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["of", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"])], (($0, p, $1, table) => {
            if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["of", "in"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"])], (($0, p, $1, $2, table) => {
            if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), new $runtime.Choice(["of", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"])], (($0, p, $1, filter, $2, table) => {
            const ptype = table.schema.out[p.name];
            if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem;
            if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), new $runtime.Choice(["of", "in"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"])], (($0, p, $1, filter, $2, $3, table) => {
            const ptype = table.schema.out[p.name];
            if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                return null;
            const elementType = ptype.elem;
            if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                return null;
            const list = C.makeListExpression(p, filter);
            if (!list)
                return null;
            return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["of", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"])], (($0, p, $1, table) => {
            if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumber)
                return null;
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("projection_Number", ["the number of", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["of", "in"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"])], (($0, p, $1, $2, table) => {
            if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumber)
                return null;
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
    }
    $grammar.declareSymbol("compute_question");
    if (!$options.flags.turking) {
        $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["from here", ""]), new $runtime.Choice(["?", ""])], (($0, $1, $2, table, $3, $4) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), new $runtime.Choice(["from here", ""]), new $runtime.Choice(["?", ""])], (($0, $1, $2, $3, table, $4, $5) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["from", "to"]), new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["?", ""])], (($0, $1, $2, table, $3, place, $4) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how", new $runtime.Choice(["far", "near", "close"]), new $runtime.Choice(["is", "are"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["has_geo", (true)]), new $runtime.Choice(["from", "to"]), new $runtime.NonTerminal("constant_Location", undefined), new $runtime.Choice(["?", ""])], (($0, $1, $2, $3, table, $4, place, $5) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'));
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["does", "do"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), "are there", new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, $2, table, $3) => {
            assert_1.default(table.schema.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), new $runtime.Choice(["does", "do"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, $2, table, $3) => {
            assert_1.default(table.schema.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), "in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_ArrayCount", undefined), "are there in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isNumber);
            return C.makeProjection(table, p.name);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["does", "do"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "are there", new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, $2, table, $3) => {
            assert_1.default(table.schema.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), new $runtime.Choice(["does", "do"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, $2, table, $3) => {
            assert_1.default(table.schema.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "are there in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, table, $2) => {
            assert_1.default(table.schema.out[p.name].isArray);
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeProjection(table, conflictParam);
            return C.makeAggComputeExpression(table, 'count', null, p, thingtalk_1.Type.Number);
        }), C.expressionKeyFn, {});
        if (!$options.flags.dialogues) {
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), new $runtime.Choice(["does", "do"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, filter, $2, table, $3) => {
                const ptype = table.schema.out[p.name];
                if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                    return null;
                const elementType = ptype.elem;
                if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                    return null;
                const list = C.makeListExpression(p, filter);
                if (!list)
                    return null;
                return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
            }), C.expressionKeyFn, {});
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, filter, $2, table, $3) => {
                const ptype = table.schema.out[p.name];
                if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                    return null;
                const elementType = ptype.elem;
                if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                    return null;
                const list = C.makeListExpression(p, filter);
                if (!list)
                    return null;
                return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
            }), C.expressionKeyFn, {});
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), "are there", new $runtime.Choice(["in a", "in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, filter, $2, $3, table, $4) => {
                const ptype = table.schema.out[p.name];
                if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                    return null;
                const elementType = ptype.elem;
                if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                    return null;
                const list = C.makeListExpression(p, filter);
                if (!list)
                    return null;
                return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
            }), C.expressionKeyFn, {});
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), new $runtime.Choice(["does", "do"]), "the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, filter, $2, $3, table, $4) => {
                const ptype = table.schema.out[p.name];
                if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                    return null;
                const elementType = ptype.elem;
                if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                    return null;
                const list = C.makeListExpression(p, filter);
                if (!list)
                    return null;
                return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
            }), C.expressionKeyFn, {});
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), "in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, filter, $2, table, $3) => {
                const ptype = table.schema.out[p.name];
                if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                    return null;
                const elementType = ptype.elem;
                if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                    return null;
                const list = C.makeListExpression(p, filter);
                if (!list)
                    return null;
                return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
            }), C.expressionKeyFn, {});
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Array__Any", undefined), "with", new $runtime.NonTerminal("with_filter", ["functionName", 1, "functionName"]), "are there in the", new $runtime.NonTerminal("with_arg_min_max_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, filter, $2, table, $3) => {
                const ptype = table.schema.out[p.name];
                if (!ptype || !(ptype instanceof thingtalk_1.Type.Array) || !(filter instanceof thingtalk_1.Ast.AtomBooleanExpression))
                    return null;
                const elementType = ptype.elem;
                if (!(elementType instanceof thingtalk_1.Type.Compound) || !elementType.fields[filter.name])
                    return null;
                const list = C.makeListExpression(p, filter);
                if (!list)
                    return null;
                return C.makeAggComputeExpression(table, 'count', null, list, thingtalk_1.Type.Number);
            }), C.expressionKeyFn, {});
        }
        if ($options.flags.aggregation) {
            $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("with_filtered_table", ["is_list", (true)]), "are there ?"], (($0, table, $1) => {
                assert_1.default(table.schema.is_list);
                const newSchema = table.schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT)
                    .addArguments([new thingtalk_1.Ast.ArgumentDef(null, thingtalk_1.Ast.ArgDirection.OUT, 'count', thingtalk_1.Type.Number)]);
                newSchema.is_list = false;
                const agg = new thingtalk_1.Ast.AggregationExpression(null, table, '*', 'count', newSchema);
                return new thingtalk_1.Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
            }), C.expressionKeyFn, {});
            $grammar.addRule("compute_question", [new $runtime.Choice(["please", ""]), "count the", new $runtime.NonTerminal("with_filtered_table", ["is_list", (true)])], (($0, $1, table) => {
                assert_1.default(table.schema.is_list);
                const newSchema = table.schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT)
                    .addArguments([new thingtalk_1.Ast.ArgumentDef(null, thingtalk_1.Ast.ArgDirection.OUT, 'count', thingtalk_1.Type.Number)]);
                newSchema.is_list = false;
                const agg = new thingtalk_1.Ast.AggregationExpression(null, table, '*', 'count', newSchema);
                return new thingtalk_1.Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
            }), C.expressionKeyFn, {});
            if (!$options.flags.schema_org) {
                $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), "are", new $runtime.Choice(["in", "in the"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "?"], (($0, p, $1, $2, table, $3) => {
                    if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumeric())
                        return null;
                    if (!table.schema.is_list)
                        return null;
                    const newSchema = table.schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT || arg.name === p.name);
                    newSchema.is_list = false;
                    const agg = new thingtalk_1.Ast.AggregationExpression(null, table, p.name, 'sum', newSchema);
                    return new thingtalk_1.Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
                }), C.expressionKeyFn, {});
            }
            if (!$options.flags.schema_org) {
                $grammar.addRule("compute_question", ["how many", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), new $runtime.Choice(["do", "does"]), new $runtime.NonTerminal("with_filtered_table", ["functionName", 1, "functionName"]), "have ?"], (($0, p, $1, table, $2) => {
                    if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumeric())
                        return null;
                    if (!table.schema.is_list)
                        return null;
                    const newSchema = table.schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT || arg.name === p.name);
                    newSchema.is_list = false;
                    const agg = new thingtalk_1.Ast.AggregationExpression(null, table, p.name, 'sum', newSchema);
                    return new thingtalk_1.Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
                }), C.expressionKeyFn, {});
            }
        }
    }
    $grammar.declareSymbol("with_arg_min_max_table");
    if (!$options.flags.turking) {
        $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("complete_table", ["has_geo", (true)]), new $runtime.Choice(["", "from here", "to here", "to me"])], (($0, table, $1) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)])], (($0, table) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["farthest", "most far away"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)])], (($0, table) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), HERE], new thingtalk_1.Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("complete_table", ["has_geo", (true)]), "to", new $runtime.NonTerminal("constant_Location", undefined)], (($0, table, $1, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("complete_table", ["has_geo", (true)]), new $runtime.Choice(["that is", "which is"]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table, $0, $1, $2, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("complete_table", ["has_geo", (true)]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table, $0, $1, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["nearest", "closest"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), "to", new $runtime.NonTerminal("constant_Location", undefined)], (($0, table, $1, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["that is", "which is"]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table, $0, $1, $2, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["nearest", "closest"]), "to", new $runtime.NonTerminal("constant_Location", undefined)], ((table, $0, $1, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'asc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.Choice(["farthest", "most far away"]), new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), "from", new $runtime.NonTerminal("constant_Location", undefined)], (($0, table, $1, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["that is", "which is"]), new $runtime.Choice(["farthest", "most far away"]), "from", new $runtime.NonTerminal("constant_Location", undefined)], ((table, $0, $1, $2, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", ["has_geo", (true)]), new $runtime.Choice(["farthest", "most far away"]), "from", new $runtime.NonTerminal("constant_Location", undefined)], ((table, $0, $1, place) => {
            assert_1.default(table.schema.out.geo.isLocation);
            return C.makeComputeArgMinMaxExpression(table, 'distance', [new thingtalk_1.Ast.Value.VarRef('geo'), place], new thingtalk_1.Type.Measure('m'), 'desc');
        }), C.expressionKeyFn, {});
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["most", "highest number of", "largest number of", "largest count of"]), new $runtime.NonTerminal("out_param_Array__Any", ["functionName", 0, "functionName"])], ((table, $0, $1, p) => {
            if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
                return null;
            const conflictParam = C.hasConflictParam(table, p.name, 'count');
            if (conflictParam)
                return C.makeArgMaxMinTable(table, conflictParam, 'desc');
            return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'desc');
        }), C.expressionKeyFn, { weight: (0.33) });
        if (!$options.flags.turking) {
            $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["least", "fewest", "smallest number of", "tiniest number of", "smallest count of"]), new $runtime.NonTerminal("out_param_Array__Any", ["functionName", 0, "functionName"])], ((table, $0, $1, p) => {
                if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
                    return null;
                const conflictParam = C.hasConflictParam(table, p.name, 'count');
                if (conflictParam)
                    return C.makeArgMaxMinTable(table, conflictParam, 'asc');
                return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'asc');
            }), C.expressionKeyFn, { weight: (0.33) });
        }
        if ($options.flags.turking) {
            $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["fewest", "smallest number of"]), new $runtime.NonTerminal("out_param_Array__Any", ["functionName", 0, "functionName"])], ((table, $0, $1, p) => {
                if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
                    return null;
                const conflictParam = C.hasConflictParam(table, p.name, 'count');
                if (conflictParam)
                    return C.makeArgMaxMinTable(table, conflictParam, 'asc');
                return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'asc');
            }), C.expressionKeyFn, { weight: (0.33) });
        }
        $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["most", "highest number of", "largest number of", "largest count of"]), new $runtime.NonTerminal("out_param_ArrayCount", ["functionName", 0, "functionName"])], ((table, $0, $1, p) => {
            if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumber)
                return null;
            return C.makeArgMaxMinTable(table, p.name, 'desc');
        }), C.expressionKeyFn, { weight: (0.33) });
        if (!$options.flags.turking) {
            $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["least", "fewest", "smallest number of", "tiniest number of", "smallest count of"]), new $runtime.NonTerminal("out_param_ArrayCount", ["functionName", 0, "functionName"])], ((table, $0, $1, p) => {
                if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumber)
                    return null;
                return C.makeArgMaxMinTable(table, p.name, 'asc');
            }), C.expressionKeyFn, { weight: (0.33) });
        }
        if ($options.flags.turking) {
            $grammar.addRule("with_arg_min_max_table", [new $runtime.NonTerminal("with_filtered_table", undefined), "with the", new $runtime.Choice(["fewest", "smallest number of"]), new $runtime.NonTerminal("out_param_ArrayCount", ["functionName", 0, "functionName"])], ((table, $0, $1, p) => {
                if (!table.schema.out[p.name] || !table.schema.out[p.name].isNumber)
                    return null;
                return C.makeArgMaxMinTable(table, p.name, 'asc');
            }), C.expressionKeyFn, { weight: (0.33) });
        }
    }
}
exports.default = default_1;
;
//# sourceMappingURL=computation.genie.js.map