001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.api.engine;
017
018import java.util.Map;
019import java.util.Set;
020
021/**
022 * <p>An {@link TermResolver} implementor is a utility class used for resolution (reification) of fact values for one or
023 * more{@link Term}s.  There are a few key concepts to understand how {@link TermResolver}s function and work together.
024 * </p>
025 * <ul>
026 * <li><b>they can require prerequisite {@link Term}s</b> (which must not have any parameters).  If they do, when the {@link TermResolutionEngine} calls
027 * {@link #resolve(Map, Map)} it will pass the resolved prerequisites in the first argument.
028 * <li><b>they can be chained.</b>  This means that if the {@link TermResolutionEngine} has {@link TermResolver}s (a &lt;- b) and 
029 * (b &lt;- c), and you have the fact 'c', you can ask for term 'a' and the engine will chain the resolvers together
030 * to resolve it correctly.</li>
031 * <li><b>they can be parameterized.</b> If an TermResolver takes parameters, they must be declared via the
032 * {@link #getParameterNames()} method.  All declared parameters are considered to be required.  Parameters can be set
033 * on the {@link Term} (via the constructor) that you are asking to resolve. When the {@link TermResolutionEngine} calls 
034 * {@link #resolve(Map, Map)}, the parameters will be passed in to the second argument.</li>
035 * <li><b>Parameterized {@link TermResolver}s can not be intermediates in a resolution plan.</b>  Another way to say 
036 * this is that they can only be the last resolver in a chain.  For example, say
037 * the {@link TermResolutionEngine} has {@link TermResolver}s (a &lt;- b) which takes no parameters, 
038 * (b &lt;- c) which takes a parameter "foo", and you have the fact 'c'.  If you ask for Term 'a', the engine will not be able to resolve
039 * it because it would need to use a parameterized Term as an intermediate step in resolution, and that isn't allowed.</li>
040 * 
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 *
043 * @param <T> the class of the resolved object 
044 */
045public interface TermResolver <T> {
046        
047        /**
048     * Gets the names of the terms that this resolver requires in order to be able to resolve its output,
049     * or an empty set if it has no prereqs;
050     *
051         * @return the prerequisite term names
052         */
053        Set<String> getPrerequisites();
054        
055        /**
056     * Gets the name of the term that this TermResolver resolves.
057     *
058         * @return the name of the term this TermResolver resolves.
059         */
060        String getOutput();
061        
062        /**
063         * Gets the names of any parameters that this {@link TermResolver} requires to resolve {@link Term}s.
064     * This may be null if no parameters are required.  If this is non-null/non-empty, then this
065     * resolver can not be used as an intermediate in a term resolution chain.
066     *
067     * @return the names of parameters this TermResolver requires for resolution.
068         */
069        Set<String> getParameterNames();
070        
071        /**
072         * Gets an int representing the cost of resolving the term. 1 is cheap, Integer.MAX_INT is expensive.
073     *
074     * @return the cost.
075         */
076        int getCost();
077        
078        /**
079     * Resolves the output term's fact value given the resolved prerequisite terms and term parameters. The term
080     * resolution engine will call this method providing the following parameters to the resolver:
081     *
082         * @param resolvedPrereqs the resolved prereqs.  May be empty, but never null.
083         * @param parameters any parameters on the {@link Term} to be resolved (which must match those declared via
084     * {@link #getParameterNames()}.  May be empty, but never null.
085         * @return the resolved fact value for the specified {@link Term}.  May be null.
086         * @throws {@link TermResolutionException} if something bad happens during the term resolution process.
087         */
088        T resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) throws TermResolutionException;
089}