import { FileUtil } from "../core/file-util" ; export class IntentSchema { public constructor ( public schemaJSON ) { } public static fromFile ( file , callback ) { FileUtil . readFile ( file , function ( data ) { if ( data !== null ) { let json = null ; try { json = JSON . parse ( data . toString ( ) ) ; let schema = new IntentSchema ( json ) ; callback ( schema ) ; } catch ( e ) { callback ( null , "Bad JSON: " + e . message ) ; } } else { let error = "File not found: " + file ; callback ( null , error ) ; } } ) ; } public static fromJSON ( schemaJSON ) { return new IntentSchema ( schemaJSON ) ; } public intents ( ) { let intentArray = [ ] ; for ( let intentJSON of this . schemaJSON . intents ) { let intent = new Intent ( intentJSON . intent ) ; if ( intentJSON . slots !== undefined && intentJSON . slots !== null ) { for ( let slotJSON of intentJSON . slots ) { intent . addSlot ( new IntentSlot ( slotJSON . name , slotJSON . type ) ) ; } } intentArray . push ( intent ) ; } return intentArray ; } public intent ( intentString ) { let intent = null ; for ( let o of this . intents ( ) ) { if ( o . name === intentString ) { intent = o ; break ; } } return intent ; } public hasIntent ( intentString ) { return this . intent ( intentString ) !== null ; } } export class Intent { public builtin : boolean = false ; public slots : Array < IntentSlot > = null ; public constructor ( public name ) { if ( this . name . indexOf ( "AMAZON" ) !== - 1 ) { this . builtin = true ; } } public addSlot ( slot ) { if ( this . slots === null ) { this . slots = [ ] ; } this . slots . push ( slot ) ; } } export class IntentSlot { public constructor ( public name , public type ) { } }