// Path: core/src/main/java/edu/jhuapl/dorset/Request.java
// public class Request {
//     /** Maximum length of the identifier string ({@value #MAX_ID_LENGTH}) */
//     public static final int MAX_ID_LENGTH = 36;
// 
//     private String text;
//     private final String id;
//     private final User user;
// 
//     /**
//      * Create a request
//      * <p>
//      * Automatically sets the identifier of the request
//      *
//      * @param text  the text of the request
//      */
//     public Request(String text) {
//         this(text, null, "");
//     }
//     
//     /**
//      * Create a request
//      * <p>
//      * Automatically sets the identifier of the request
//      *
//      * @param text  the text of the request
//      * @param user  the user of the request
//      * */
//     public Request(String text, User user) {
//         this(text, user, "");
//     }
// 
//     /**
//      * Create a request
//      *
//      * @param text  the text of the request
//      * @param id  the identifier of the request (cannot be longer then MAX_ID_LENGTH)
//      *            The identifier must be unique.
//      */
//     public Request(String text, String id) {
//         this(text, null, id);
//     }
// 
//     /**
//      * Create a request
//      *
//      * @param text  the text of the request
//      * @param user  the user of the request
//      * @param id  the identifier of the request (cannot be longer then MAX_ID_LENGTH)
//      *            The identifier must be unique.
//      */
//     public Request(String text, User user, String id) {
//         if (id.length() > MAX_ID_LENGTH) {
//             throw new IllegalArgumentException(
//                             "Request id cannot be longer than " + String.valueOf(MAX_ID_LENGTH));
//         }
//         
//         if (id.length() == 0) {
//             this.id = UUID.randomUUID().toString();
//         } else {
//             this.id = id;
//         }
//         
//         this.text = text;
//         this.user = user;
//     }
//     
//     /**
//      * Get the text of the request
//      *
//      * @return the text of the request
//      */
//     public String getText() {
//         return text;
//     }
// 
//     /**
//      * Set the text of the request
//      *
//      * @param text  the text of the request
//      */
//     public void setText(String text) {
//         this.text = text;
//     }
// 
//     /**
//      * Get the identifier of the request
//      *
//      * @return identifier
//      */
//     public String getId() {
//         return id;
//     }
//     
//     /**
//      * Get the User of the request
//      *
//      * @return user
//      */
//     public User getUser() {
//         return user;
//     }
//  
// }
// 
// Path: core/src/main/java/edu/jhuapl/dorset/agents/Agent.java
// public interface Agent {
// 
//     /**
//      * Get the name of the agent
//      *
//      * @return name
//      */
//     public String getName();
// 
//     /**
//      * Override the default name of the agent
//      *
//      * @param name  New name for the agent
//      */
//     public void setName(String name);
// 
//     /**
//      * Get the description of the agent for human consumption
//      *
//      * @return Description of the agent
//      */
//     public Description getDescription();
// 
//     /**
//      * Override the default description
//      *
//      * @param description  Description of the agent
//      * @see Description
//      */
//     public void setDescription(Description description);
//     
//     /**
//      * Process a request
//      *
//      * @param request  Request object
//      * @return response to the request
//      */
//     public AgentResponse process(AgentRequest request);
// }


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import edu.jhuapl.dorset.Request;
import edu.jhuapl.dorset.agents.Agent;
/*
 * Copyright 2016 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;



/**
 * This router uses regular expressions to direct requests.
 * <p>
 * Each agent can have one or more regular expressions associated with it.
 * The regular expressions are treated as case insensitive.
 */
public class RegexRouter implements Router {
    public static final String REGEX = "regex";

    private HashMap<Pattern, Agent> agentMap;
    private HashSet<Agent> agents;

    /**
     * Create the router
     *
     * @param agentsConfig  agents and routing configuration for those agents
     */
    public RegexRouter(RouterAgentConfig agentsConfig) {
        agentMap = new HashMap<Pattern, Agent>();
        agents = new HashSet<Agent>();
        for (RouterAgentConfigEntry entry : agentsConfig) {
            String[] regexes = entry.getParams().getStrings(REGEX);
            if (regexes == null) {
                continue;
            }
            agents.add(entry.getAgent());
            for (String regex : regexes) {
                agentMap.put(Pattern.compile(regex, Pattern.CASE_INSENSITIVE), entry.getAgent());
            }
        }
    }

    @Override
// Path: core/src/main/java/edu/jhuapl/dorset/Request.java
// public class Request {
//     /** Maximum length of the identifier string ({@value #MAX_ID_LENGTH}) */
//     public static final int MAX_ID_LENGTH = 36;
// 
//     private String text;
//     private final String id;
//     private final User user;
// 
//     /**
//      * Create a request
//      * <p>
//      * Automatically sets the identifier of the request
//      *
//      * @param text  the text of the request
//      */
//     public Request(String text) {
//         this(text, null, "");
//     }
//     
//     /**
//      * Create a request
//      * <p>
//      * Automatically sets the identifier of the request
//      *
//      * @param text  the text of the request
//      * @param user  the user of the request
//      * */
//     public Request(String text, User user) {
//         this(text, user, "");
//     }
// 
//     /**
//      * Create a request
//      *
//      * @param text  the text of the request
//      * @param id  the identifier of the request (cannot be longer then MAX_ID_LENGTH)
//      *            The identifier must be unique.
//      */
//     public Request(String text, String id) {
//         this(text, null, id);
//     }
// 
//     /**
//      * Create a request
//      *
//      * @param text  the text of the request
//      * @param user  the user of the request
//      * @param id  the identifier of the request (cannot be longer then MAX_ID_LENGTH)
//      *            The identifier must be unique.
//      */
//     public Request(String text, User user, String id) {
//         if (id.length() > MAX_ID_LENGTH) {
//             throw new IllegalArgumentException(
//                             "Request id cannot be longer than " + String.valueOf(MAX_ID_LENGTH));
//         }
//         
//         if (id.length() == 0) {
//             this.id = UUID.randomUUID().toString();
//         } else {
//             this.id = id;
//         }
//         
//         this.text = text;
//         this.user = user;
//     }
//     
//     /**
//      * Get the text of the request
//      *
//      * @return the text of the request
//      */
//     public String getText() {
//         return text;
//     }
// 
//     /**
//      * Set the text of the request
//      *
//      * @param text  the text of the request
//      */
//     public void setText(String text) {
//         this.text = text;
//     }
// 
//     /**
//      * Get the identifier of the request
//      *
//      * @return identifier
//      */
//     public String getId() {
//         return id;
//     }
//     
//     /**
//      * Get the User of the request
//      *
//      * @return user
//      */
//     public User getUser() {
//         return user;
//     }
//  
// }
// 
// Path: core/src/main/java/edu/jhuapl/dorset/agents/Agent.java
// public interface Agent {
// 
//     /**
//      * Get the name of the agent
//      *
//      * @return name
//      */
//     public String getName();
// 
//     /**
//      * Override the default name of the agent
//      *
//      * @param name  New name for the agent
//      */
//     public void setName(String name);
// 
//     /**
//      * Get the description of the agent for human consumption
//      *
//      * @return Description of the agent
//      */
//     public Description getDescription();
// 
//     /**
//      * Override the default description
//      *
//      * @param description  Description of the agent
//      * @see Description
//      */
//     public void setDescription(Description description);
//     
//     /**
//      * Process a request
//      *
//      * @param request  Request object
//      * @return response to the request
//      */
//     public AgentResponse process(AgentRequest request);
// }


// Path: core/src/main/java/edu/jhuapl/dorset/routing/RegexRouter.java
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import edu.jhuapl.dorset.Request;
import edu.jhuapl.dorset.agents.Agent;

/*
 * Copyright 2016 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;



/**
 * This router uses regular expressions to direct requests.
 * <p>
 * Each agent can have one or more regular expressions associated with it.
 * The regular expressions are treated as case insensitive.
 */
public class RegexRouter implements Router {
    public static final String REGEX = "regex";

    private HashMap<Pattern, Agent> agentMap;
    private HashSet<Agent> agents;

    /**
     * Create the router
     *
     * @param agentsConfig  agents and routing configuration for those agents
     */
    public RegexRouter(RouterAgentConfig agentsConfig) {
        agentMap = new HashMap<Pattern, Agent>();
        agents = new HashSet<Agent>();
        for (RouterAgentConfigEntry entry : agentsConfig) {
            String[] regexes = entry.getParams().getStrings(REGEX);
            if (regexes == null) {
                continue;
            }
            agents.add(entry.getAgent());
            for (String regex : regexes) {
                agentMap.put(Pattern.compile(regex, Pattern.CASE_INSENSITIVE), entry.getAgent());
            }
        }
    }

    @Override
    public Agent[] route(Request request) {