View Javadoc

1   /***
2   *
3   * The owl-s matcher software is subject to the GNU Lesser General
4   * Public License Version 2.1 (the "License"). You may not copy or use this
5   * file, in either source code or executable form, except in compliance
6   * with the License. You may obtain a copy of the License at
7   * http://www.fsf.org/licenses/lgpl.txt or http://www.opensource.org/.
8   *
9   * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied without
11  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  * PURPOSE. See the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this distribution; if not, write to the
17  *
18  * Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330,
20  * Boston, MA  02111-1307 USA
21  *
22  * Copyright (C) 2003-2004
23  * TU Berlin, FG IVS
24  * Stefan Tang,
25  * Christoph Liebetruth,
26  * Michael C. Jaeger, 
27  *
28  * More information available at http://ivs.tu-berlin.de/
29  *
30  * $Id$
31  *
32  */
33  package de.tuberlin.ivs.owl.matching;
34  
35  import java.util.Vector;
36  import de.tuberlin.ivs.owl.service.Output;
37  import java.io.PrintStream;
38  
39  /***
40   * Implements the output parameter matching.
41   *
42   * @author Stefan Tang (steftang@stanford.edu), Christoph Liebetruth (christophl@voelcker.com)
43   * @version 1.1
44   */
45  public class OutputParameterMatching {
46  
47    /***
48     * The output stream that output information is written to.
49     */
50    private PrintStream outWriter;
51  
52    /***
53     * Every output of the requested service could be matched exactly with one
54     * output parameter of the advertised service. For every output pair the
55     * property-match degree and type-match degree is Equivalent.
56     */
57    public static int MATCH = 6;
58  
59    /***
60     * Every output of the requested service could be matched with one output of
61     * the advertised service. For every output pair the property-match degree is
62     * Equivalent but at least for one output pair the type-match degree is Subsumes.
63     */
64    public static int TYPE_SUBSUME = 5;
65  
66    /***
67     * Every output of the requested service could be matched with one output of
68     * the advertised service. For every output pair the property-match degree is
69     * Equivalent but at least for one output pair the type-match degree is Invert
70     * Subsumes.
71     */
72    public static int TYPE_INVERT = 4;
73  
74    /***
75     * Every output of the requested service could be matched with one output of
76     * the advertised service. For every output pair the property-match degree is
77     * either Equivalent or Subproperty, but we have at least one output pair
78     * matched with degree Subproperty, and the type-match degree is either
79     * Subsumes, Invert Subsumes or Equivalent.
80     */
81    public static int SUBPROPERTY = 3;
82  
83    /***
84     * Every output of the requested service has been matched with an input of
85     * the requested service. However, for at least one input pair the
86     * property-match degree is unclassified.
87     */
88    public static int UNCLASSIFIED = 2;
89  
90    /***
91     * At least one output of the requested service remains unmatched, but there
92     * is at least one output pair that has rank greater than zero.
93     */
94    public static int PARTIAL_FAIL = 1;
95  
96    /***
97     * Every output parameter of the requested service could not be matched.
98     */
99    public static int FAIL = 0;
100 
101 
102   /***
103    * Default constructor.
104    * @param out The output stream that output information is written to. Set to
105    * null if no output is desired.
106    */
107   public OutputParameterMatching(PrintStream out) {
108     outWriter = out;
109   }
110 
111   /***
112    * Matches all the outputs of the requested service against the outputs of
113    * the advertised service.
114    * @param reqOutputsList A list containing all outputs of the requested service.
115    * @param advOutputsList A list containing all outputs of the advertised service.
116    * @param reasoner An instance of the reasoner class that holds the current
117    * knowledge base.
118    * @return An integer denoting the matching result.
119    */
120   public int match(Vector reqOutputsList, Vector advOutputsList, Reasoner reasoner) {
121     if (Options.extendedResult==null) {
122       Options.extendedResult = new ExtendedMatchingResult();
123     }
124     if (reqOutputsList==null) {
125       Options.extendedResult.setOutputMatching(MATCH);
126       return MATCH;
127     } else {
128       if (advOutputsList==null) {
129         for (int i=0; i<reqOutputsList.size(); i++ ) {
130           Options.extendedResult.addUnmatchedReqOutput((Output)reqOutputsList.elementAt(i));
131         }
132         Options.extendedResult.setOutputMatching(PARTIAL_FAIL);
133         return PARTIAL_FAIL;
134       }
135     }
136     boolean atLeastOneFail = false;
137     int minOverallRank  = 9;
138     for (int i=0; i<reqOutputsList.size();i++) {
139       Output reqOutput = (Output)reqOutputsList.elementAt(i);
140       Output bestMatch = null;
141       int maxRank = 0;
142       for (int j=0; j<advOutputsList.size(); j++) {
143         Output tempAdvOutput = (Output)advOutputsList.elementAt(j);
144         int rank = reasoner.rankForParameters(tempAdvOutput, reqOutput);
145         if (rank>maxRank) {
146           maxRank = rank;
147           bestMatch = tempAdvOutput;
148         }
149       }
150       if (bestMatch==null) {
151         // reqOutput is not matched
152         Options.extendedResult.addUnmatchedReqOutput(reqOutput);
153         atLeastOneFail = true;
154       } else{
155         Options.extendedResult.addMatchedReqOutput(reqOutput,bestMatch);
156       }
157       if (maxRank<minOverallRank) {
158         minOverallRank = maxRank;
159       }
160     }
161     if (atLeastOneFail) {
162       Options.extendedResult.setOutputMatching(PARTIAL_FAIL);
163       return PARTIAL_FAIL;
164     }
165     if (minOverallRank==1 || minOverallRank==2 || minOverallRank==3) {
166       Options.extendedResult.setOutputMatching(UNCLASSIFIED);
167       return UNCLASSIFIED;
168     } else if (minOverallRank==4 || minOverallRank==5 || minOverallRank==6) {
169       Options.extendedResult.setOutputMatching(SUBPROPERTY);
170       return SUBPROPERTY;
171     } else if (minOverallRank==7) {
172       Options.extendedResult.setOutputMatching(TYPE_INVERT);
173       return TYPE_INVERT;
174     } else if (minOverallRank==8) {
175       Options.extendedResult.setOutputMatching(TYPE_SUBSUME);
176       return TYPE_SUBSUME;
177     } else if (minOverallRank==9) {
178       Options.extendedResult.setOutputMatching(MATCH);
179       return MATCH;
180     } else {
181       Options.extendedResult.setOutputMatching(FAIL);
182       return FAIL;
183     }
184   }
185 
186   /***
187    * Writes a message to the output stream if the stream is initialized.
188    * @param message The message to be written.
189    */
190   private void writeLog(String message) {
191     if (outWriter!=null) {
192       outWriter.println(message);
193     }
194   }
195 }