/*
 * Copyright 2017 The Johns Hopkins University Applied Physics Laboratory LLC
 * All rights reserved.
 *
 * 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.
 */
package edu.jhuapl.dorset.routing.tree;

import java.util.Arrays;

import edu.jhuapl.dorset.Request;
import edu.jhuapl.dorset.agents.Agent;
import edu.jhuapl.dorset.nlp.RuleBasedTokenizer;
import edu.jhuapl.dorset.nlp.Tokenizer;

/**
 * Use a keyword to determine which child node is returned.
 */
public class KeywordNode implements Node {
    private String keyword;
    private Node matchedNode;
    private Node nonMatchedNode;
    private Tokenizer tokenizer;

    /**
     * Create a keyword matching node
     *
     * @param keyword  keyword string to match to
     * @param matchNode  Node to return it keyword matched
     * @param nonMatchNode  Node to return if keyword not matched
     */
    public KeywordNode(String keyword, Node matchNode, Node nonMatchNode) {
        this.keyword = keyword.toLowerCase();
        this.matchedNode = matchNode;
        this.nonMatchedNode = nonMatchNode;
        this.tokenizer = new RuleBasedTokenizer();
    }

    @Override
    public Node selectChild(Request request) {
        String requestText = request.getText().toLowerCase();
        String[] tokens = tokenizer.tokenize(requestText);
        if (Arrays.asList(tokens).contains(keyword)) {
            return matchedNode;
        } else {
            return nonMatchedNode;
        }
    }

    @Override
    public Node[] getChildren() {
        return new Node[]{matchedNode, nonMatchedNode};
    }

    @Override
    public boolean isLeaf() {
        return false;
    }

    @Override
    public Agent[] getValue() {
        return null;
    }

}

@@ -901,25 +901,35 @@
 %0A * Use 
-a
+one or more
  keyword
  to dete
@@ -920,16 +920,17 @@
  keyword
+s
  to dete
@@ -1030,24 +1030,26 @@
 e String
+%5B%5D
  keyword
 ;%0A    pr
@@ -1040,16 +1040,17 @@
  keyword
+s
 ;%0A    pr
@@ -1284,17 +1284,17 @@
 return i
-t
+f
  keyword
@@ -1448,24 +1448,423 @@
 atchNode) %7B%0A
+        this(new String%5B%5D%7Bkeyword%7D, matchNode, nonMatchNode);%0A    %7D%0A%0A    /**%0A     * Create a keyword matching node%0A     *%0A     * @param keywords  array of keyword strings to match to%0A     * @param matchNode  Node to return if a keyword matched%0A     * @param nonMatchNode  Node to return if a keyword not matched%0A     */%0A    public KeywordNode(String%5B%5D keywords, Node matchNode, Node nonMatchNode) %7B%0A
         this
@@ -1875,18 +1875,135 @@
 word
+s
  = 
-keyword
+new String%5Bkeywords.length%5D;%0A        for (int i = 0; i %3C keywords.length; i++) %7B%0A            this.keywords%5Bi%5D = keywords%5Bi%5D
 .toL
@@ -2006,32 +2006,42 @@
 .toLowerCase();%0A
+        %7D%0A
         this.mat
@@ -2346,16 +2346,62 @@
 tText);%0A
+        for (String keyword : keywords) %7B%0A    
         
@@ -2455,24 +2455,28 @@
             
+    
 return match
@@ -2495,21 +2495,24 @@
     
-%7D else %7B%0A
+    %7D%0A    
     
+%7D%0A
     
@@ -2538,26 +2538,16 @@
 edNode;%0A
-        %7D%0A
     %7D%0A%0A 

support multiple keywords for tree routing