Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
wcs.ml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661(* * This file is part of the Watson Conversation Service OCaml API project. * * Copyright 2016-2017 IBM Corporation * * 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. *) open Wcs_t let omap f o = begin match o with | None -> None | Some x -> Some (f x) end let new_id = let cpt = ref 0 in fun () -> incr cpt; "node_"^(string_of_int !cpt) let list_workspaces_request (* ?version *) ?page_limit ?include_count ?sort ?cursor () : list_workspaces_request = { (* list_ws_req_version = version; *) list_ws_req_page_limit = page_limit; list_ws_req_include_count = include_count; list_ws_req_sort = sort; list_ws_req_cursor = cursor; } let get_workspace_request ?export workspace_id = { get_ws_req_workspace_id = workspace_id; get_ws_req_export = export; } let example example ?created ?updated () : intent_example = { ex_text = example; ex_created = created; ex_updated = updated; } let intent intent ?description ?(examples=[]) ?created ?updated () : intent_def = { i_def_intent = intent; i_def_description = description; i_def_examples = List.map (fun s -> example s ()) examples; i_def_created = created; i_def_updated = updated; } let value value ?metadata ?(synonyms=[]) ?created ?updated () : entity_value = { e_val_value = value; e_val_metadata = metadata; e_val_synonyms = synonyms; e_val_created = created; e_val_updated = updated; } let entity entity ?description ?metadata ?source ?open_list ?(values=[]) ?created ?updated ?fuzzy_match () : entity_def = { e_def_entity = entity; e_def_description = description; e_def_metadata = metadata; e_def_source = source; e_def_open_list = open_list; e_def_values = List.map (fun (v, syn) -> value v ~synonyms:syn ()) values; e_def_created = created; e_def_updated = updated; e_def_fuzzy_match = fuzzy_match; } let next_step node ~selector () : next_step = { next_behavior = "jump_to"; next_selector = selector; next_dialog_node = node.node_dialog_node; } let mk_next_step = next_step (* alias to avoid hiding *) let next_step_id node_id ~selector () : next_step = { next_behavior = "jump_to"; next_selector = selector; next_dialog_node = node_id; } let mk_next_step_id = next_step_id (* alias to avoid hiding *) let output (* XXX TODO : handle multiple outputs XXX *) text : output_def = Json_spel.of_json (`Assoc [ "text", `String text ]) let mk_output = output (* alias to avoid hiding *) let dialog_node dialog_node ?description ?type_ ?conditions ?conditions_spel ?parent ?previous_sibling ?text ?text_spel ?output ?output_spel ?context ?context_spel ?metadata ?next_step ?next_step_id ?created ?updated ?event_name ?variable () : dialog_node = let parent_id = omap (fun node -> node.node_dialog_node) parent in let previous_sibling_id = omap (fun node -> node.node_dialog_node) previous_sibling in let text = begin match text, text_spel with | None, None -> None | Some text, None -> Some (mk_output text) | None, Some expr -> Some (mk_output (Spel_print.to_text expr)) | Some _, Some _ -> Log.error "Wcs" (Some None) "dialog_node: ~text and ~text_spel cannot be present simultanously" end in let output = begin match output, output_spel with | None, None -> None | Some output, None -> Some (Json_spel.of_json output) | None, Some json_expr -> Some json_expr | Some _, Some _ -> Log.error "Wcs" (Some None) "dialog_node: ~output and ~output_spel cannot be present simultanously" end in let output = begin match text, output with | None, None -> None | Some text, None -> Some text | None, Some output -> Some output | Some _, Some _ -> Log.error "Wcs" (Some None) "dialog_node: ~text and ~output cannot be present simultanously" end in let context = begin match context, context_spel with | None, None -> None | Some context, None -> Some (Json_spel.of_json context) | None, Some json_expr -> Some json_expr | Some _, Some _ -> Log.error "Wcs" (Some None) "dialog_node: ~context and ~context_spel cannot be present simultanously" end in let next_step = begin match next_step, next_step_id with | None, None -> None | Some (node, selector), None -> Some (mk_next_step node ~selector ()) | None, Some (node_id, selector) -> Some (mk_next_step_id node_id ~selector ()) | Some _, Some _ -> Log.error "Wcs" (Some None) "dialog_node: ~next_step and ~next_step_id cannot be present simultanously" end in let conditions = begin match conditions, conditions_spel with | None,None -> Some (Spel.bool true) | Some text, None -> Some (Spel.of_string text) | None, Some expr -> Some expr | Some _, Some _ -> Log.error "Wcs" (Some None) "dialog_node: ~conditions and ~conditions_spel cannot be present simultanously" end in { node_dialog_node = dialog_node; node_description = description; node_type_ = type_; node_conditions = conditions; node_parent = parent_id; node_previous_sibling = previous_sibling_id; node_output = output; node_context = context; node_metadata = metadata; node_next_step = next_step; node_created = created; node_updated = updated; node_child_input_kind = None; node_event_name = event_name; node_variable = variable; } let response_condition ~parent = dialog_node (new_id ()) ~type_: Node_response_condition ~parent:parent ?next_step: None (* Not yet implemented*) ?next_step_id: None (* Not yet implemented*) ?event_name: None ?variable: None let fix_links nodes = let parent_child_tbl = Hashtbl.create 7 in let node_tbl = Hashtbl.create 7 in List.map (fun node -> let node = begin match node.node_parent, node.node_previous_sibling with | Some _, Some _ -> node | Some _, None | None, None -> begin try let previous_sibling = Hashtbl.find parent_child_tbl node.node_parent in { node with node_previous_sibling = Some previous_sibling.node_dialog_node } with Not_found -> node end | None, Some previous_sibling_name -> begin try let previous_sibling = Hashtbl.find node_tbl previous_sibling_name in { node with node_parent = previous_sibling.node_parent } with Not_found -> node end end in Hashtbl.add parent_child_tbl node.node_parent node; Hashtbl.add node_tbl node.node_dialog_node node; node) nodes let workspace name ?description ?language ?metadata ?(counterexamples=[]) ?(dialog_nodes=[]) ?(entities=[]) ?(intents=[]) ?created ?updated ?modified ?created_by ?modified_by ?workspace_id ?status () : workspace = let counterexamples = List.map (fun s -> example s ()) counterexamples in { ws_name = Some name; ws_description = description; ws_language = language; ws_metadata = metadata; ws_counterexamples = counterexamples; ws_dialog_nodes = fix_links dialog_nodes; ws_entities = entities; ws_intents = intents; ws_created = created; ws_updated = updated; ws_modified = modified; ws_created_by = created_by; ws_modified_by = modified_by; ws_workspace_id = workspace_id; ws_status = status; } let logs_request (* ?version *) ?filter ?sort ?page_limit ?cursor () : logs_request = { logs_filter = filter; logs_sort = sort; logs_page_limit = page_limit; logs_cursor = cursor; } let action_def name ?(agent="client") ?(type_="conversation") ?(parameters=Json_spel.null) ?result_variable () : action_def = { act_def_name = name; act_def_agent = agent; act_def_type_ = type_; act_def_parameters = parameters; act_def_result_variable = result_variable; } let action name ?(agent="client") ?(type_="conversation") ?(parameters=Json.null) ?result_variable () : action = { act_name = name; act_agent = agent; act_type_ = type_; act_parameters = parameters; act_result_variable = result_variable; } let sys_number : entity_def = entity "sys-number" ~source: "system.entities" () (** {6. Message} *) let message_request ?text ?input ?alternate_intents ?context ?entities ?intents ?output () : message_request = let input = begin match text, input with | Some text, None -> { in_text = text } | None, Some input -> input | Some text, Some input -> Log.error "Wcs" (Some input) "message_request: ~text and ~input cannot be present simultanously" | None, None -> Log.error "Wcs" (Some { in_text = "" }) "message_request: ~text or ~input must be present" end in let alternate_intents = begin match alternate_intents with | Some b -> b | None -> false end in { msg_req_input = input; msg_req_alternate_intents = alternate_intents; msg_req_context = context; msg_req_entities = entities; msg_req_intents = intents; msg_req_output = output; } (** {6 Tree manipulation} *) let get_root tree : dialog_node option = begin try Some (List.find (fun x -> x.node_parent == None && x.node_previous_sibling == None) tree) with Not_found -> None end let get_name = omap (fun x -> x.node_dialog_node) let add_node dialog_nodes dialog_node parent previous_sibling : dialog_node list = let node = { dialog_node with node_parent = get_name parent; node_previous_sibling = get_name previous_sibling; } in let dialog_nodes = List.map (fun n -> if n.node_previous_sibling = get_name previous_sibling then { n with node_previous_sibling = Some node.node_dialog_node; } else n) dialog_nodes in node :: dialog_nodes let last_sibling dialog_node dialog_nodes = let is_last_sibling x = let x_is_previous_sibling = List.exists (fun y -> Some x.node_dialog_node = y.node_previous_sibling) dialog_nodes in x.node_parent == dialog_node.node_parent && not x_is_previous_sibling in begin try Some (List.find is_last_sibling dialog_nodes) with Not_found -> None end let add_tree dialog_nodes tree parent previous_sibling : dialog_node list = let root = begin match get_root tree with | Some root -> root | None -> Log.error "Wcs" None "add_tree: tree has no root" end in let last = last_sibling root tree in let dialog_nodes = List.map (fun n -> if n.node_previous_sibling = get_name previous_sibling then { n with node_previous_sibling = get_name last; } else n) dialog_nodes in let tree = List.map (fun n -> begin match n.node_parent, n.node_previous_sibling with | None, None -> {n with node_parent = get_name parent; node_previous_sibling = get_name previous_sibling;} | None, Some _ -> {n with node_parent = get_name parent; } | _ -> n end) tree in dialog_nodes @ tree (** {6 Json conversion} *) (** {8 Conversion of Wcs data structures to JSON} *) let json_of_workspace_response rsp = Yojson.Basic.from_string (Wcs_j.string_of_workspace_response rsp) let json_of_pagination_response rsp = Yojson.Basic.from_string (Wcs_j.string_of_pagination_response rsp) let json_of_list_workspaces_request req = Yojson.Basic.from_string (Wcs_j.string_of_list_workspaces_request req) let json_of_list_workspaces_response rsp = Yojson.Basic.from_string (Wcs_j.string_of_list_workspaces_response rsp) let json_of_intent_example x = Yojson.Basic.from_string (Wcs_j.string_of_intent_example x) let json_of_intent_def x = Yojson.Basic.from_string (Wcs_j.string_of_intent_def x) let json_of_entity_value x = Yojson.Basic.from_string (Wcs_j.string_of_entity_value x) let json_of_entity_def x = Yojson.Basic.from_string (Wcs_j.string_of_entity_def x) let json_of_next_step x = Yojson.Basic.from_string (Wcs_j.string_of_next_step x) let json_of_output_def x = Yojson.Basic.from_string (Wcs_j.string_of_output_def x) let json_of_dialog_node x = Yojson.Basic.from_string (Wcs_j.string_of_dialog_node x) let json_of_workspace x = Yojson.Basic.from_string (Wcs_j.string_of_workspace x) let json_of_input x = Yojson.Basic.from_string (Wcs_j.string_of_input x) let json_of_entity x = Yojson.Basic.from_string (Wcs_j.string_of_entity x) let json_of_output x = Yojson.Basic.from_string (Wcs_j.string_of_output x) let json_of_message_request x = Yojson.Basic.from_string (Wcs_j.string_of_message_request x) let json_of_message_response x = Yojson.Basic.from_string (Wcs_j.string_of_message_response x) let json_of_create_response x = Yojson.Basic.from_string (Wcs_j.string_of_create_response x) let json_of_get_workspace_request x = Yojson.Basic.from_string (Wcs_j.string_of_get_workspace_request x) let json_of_log_entry x = Yojson.Basic.from_string (Wcs_j.string_of_log_entry x) let json_of_action x = Yojson.Basic.from_string (Wcs_j.string_of_action x) let json_of_action_def x = Yojson.Basic.from_string (Wcs_j.string_of_action_def x) let json_of_logs_request x = Yojson.Basic.from_string (Wcs_j.string_of_logs_request x) let json_of_logs_response x = Yojson.Basic.from_string (Wcs_j.string_of_logs_response x) (** {8 Conversion of Wcs data structures to JSON with embedded Spel} *) let json_spel_of_workspace_response rsp = Json_spel.of_json (json_of_workspace_response rsp) let json_spel_of_pagination_response rsp = Json_spel.of_json (json_of_pagination_response rsp) let json_spel_of_list_workspaces_request req = Json_spel.of_json (json_of_list_workspaces_request req) let json_spel_of_list_workspaces_response rsp = Json_spel.of_json (json_of_list_workspaces_response rsp) let json_spel_of_intent_example x = Json_spel.of_json (json_of_intent_example x) let json_spel_of_intent_def x = Json_spel.of_json (json_of_intent_def x) let json_spel_of_entity_value x = Json_spel.of_json (json_of_entity_value x) let json_spel_of_entity_def x = Json_spel.of_json (json_of_entity_def x) let json_spel_of_next_step x = Json_spel.of_json (json_of_next_step x) let json_spel_of_output_def x = Json_spel.of_json (json_of_output_def x) let json_spel_of_dialog_node x = Json_spel.of_json (json_of_dialog_node x) let json_spel_of_workspace x = Json_spel.of_json (json_of_workspace x) let json_spel_of_input x = Json_spel.of_json (json_of_input x) let json_spel_of_entity x = Json_spel.of_json (json_of_entity x) let json_spel_of_output x = Json_spel.of_json (json_of_output x) let json_spel_of_message_request x = Json_spel.of_json (json_of_message_request x) let json_spel_of_message_response x = Json_spel.of_json (json_of_message_response x) let json_spel_of_create_response x = Json_spel.of_json (json_of_create_response x) let json_spel_of_get_workspace_request x = Json_spel.of_json (json_of_get_workspace_request x) let json_spel_of_log_entry x = Json_spel.of_json (json_of_log_entry x) let json_spel_of_action x = Json_spel.of_json (json_of_action x) let json_spel_of_action_def x = Json_spel.of_json (json_of_action_def x) let json_spel_of_logs_request x = Json_spel.of_json (json_of_logs_request x) let json_spel_of_logs_response x = Json_spel.of_json (json_of_logs_response x)