View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.api.engine;
17  
18  import java.util.Map;
19  import java.util.Set;
20  
21  /**
22   * <p>An {@link TermResolver} implementor is a utility class used for resolution (reification) of fact values for one or
23   * more{@link Term}s.  There are a few key concepts to understand how {@link TermResolver}s function and work together.
24   * </p>
25   * <ul>
26   * <li><b>they can require prerequisite {@link Term}s</b> (which must not have any parameters).  If they do, when the {@link TermResolutionEngine} calls
27   * {@link #resolve(Map, Map)} it will pass the resolved prerequisites in the first argument.
28   * <li><b>they can be chained.</b>  This means that if the {@link TermResolutionEngine} has {@link TermResolver}s (a &lt;- b) and 
29   * (b &lt;- c), and you have the fact 'c', you can ask for term 'a' and the engine will chain the resolvers together
30   * to resolve it correctly.</li>
31   * <li><b>they can be parameterized.</b> If an TermResolver takes parameters, they must be declared via the
32   * {@link #getParameterNames()} method.  All declared parameters are considered to be required.  Parameters can be set
33   * on the {@link Term} (via the constructor) that you are asking to resolve. When the {@link TermResolutionEngine} calls 
34   * {@link #resolve(Map, Map)}, the parameters will be passed in to the second argument.</li>
35   * <li><b>Parameterized {@link TermResolver}s can not be intermediates in a resolution plan.</b>  Another way to say 
36   * this is that they can only be the last resolver in a chain.  For example, say
37   * the {@link TermResolutionEngine} has {@link TermResolver}s (a &lt;- b) which takes no parameters, 
38   * (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
39   * it because it would need to use a parameterized Term as an intermediate step in resolution, and that isn't allowed.</li>
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   * @param <T> the class of the resolved object 
44   */
45  public interface TermResolver <T> {
46  	
47  	/**
48       * Gets the names of the terms that this resolver requires in order to be able to resolve its output,
49       * or an empty set if it has no prereqs;
50       *
51  	 * @return the prerequisite term names
52  	 */
53  	Set<String> getPrerequisites();
54  	
55  	/**
56       * Gets the name of the term that this TermResolver resolves.
57       *
58  	 * @return the name of the term this TermResolver resolves.
59  	 */
60  	String getOutput();
61  	
62  	/**
63  	 * Gets the names of any parameters that this {@link TermResolver} requires to resolve {@link Term}s.
64       * This may be null if no parameters are required.  If this is non-null/non-empty, then this
65       * resolver can not be used as an intermediate in a term resolution chain.
66       *
67       * @return the names of parameters this TermResolver requires for resolution.
68  	 */
69  	Set<String> getParameterNames();
70  	
71  	/**
72  	 * Gets an int representing the cost of resolving the term. 1 is cheap, Integer.MAX_INT is expensive.
73       *
74       * @return the cost.
75  	 */
76  	int getCost();
77  	
78  	/**
79       * Resolves the output term's fact value given the resolved prerequisite terms and term parameters. The term
80       * resolution engine will call this method providing the following parameters to the resolver:
81       *
82  	 * @param resolvedPrereqs the resolved prereqs.  May be empty, but never null.
83  	 * @param parameters any parameters on the {@link Term} to be resolved (which must match those declared via
84       * {@link #getParameterNames()}.  May be empty, but never null.
85  	 * @return the resolved fact value for the specified {@link Term}.  May be null.
86  	 * @throws {@link TermResolutionException} if something bad happens during the term resolution process.
87  	 */
88  	T resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) throws TermResolutionException;
89  }