001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.krms.api.engine;
017    
018    import java.util.Map;
019    import java.util.Set;
020    
021    /**
022     * <p>An {@link TermResolver} implementor is a utility class used for resolution (reification) of {@link Term}s.  There are a
023     * 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 (which must not have any parameters)</b>.  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     */
045    public interface TermResolver <T> {
046            
047            /**
048             * @return the names of the terms that this resolver requires to resolve its output, or an empty set if it has no prereqs;
049             */
050            Set<String> getPrerequisites();
051            
052            /**
053             * @return the name of the term that the implementor resolves
054             */
055            String getOutput();
056            
057            /**
058             * 
059             * @return the names of any parameters that this {@link TermResolver} requires to churn out values for multiple {@link Term}s.  This may 
060             * be null if no parameters are required.  If this is non-null/non-empty, then this resolver can not be used as an intermediate
061             * in a term resolution chain.
062             */
063            Set<String> getParameterNames();
064            
065            /**
066             * @return an integer representing the cost of resolving the term. 1 is cheap, Integer.MAX_INT is expensive.
067             */
068            int getCost();
069            
070            /**
071             * @param resolvedPrereqs the resolved prereqs
072             * @param parameters any parameters on the {@link Term} to be resolved (which must match those declared via {@link #getParameterNames()} 
073             * @return the resolved fact value for the specified {@link Term}
074             * @throws {@link TermResolutionException} if something bad happens during the term resolution process
075             */
076            T resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) throws TermResolutionException;
077    }