View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, Ontotext Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2006</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.discovery.epfl;
26  
27  import ie.deri.wsmx.discovery.lightweight.LightweightDiscovery;
28  
29  import java.io.File;
30  import java.net.MalformedURLException;
31  import java.net.URL;
32  import java.util.*;
33  
34  import org.eclipse.core.runtime.*;
35  import org.eclipse.jface.dialogs.MessageDialog;
36  import org.eclipse.swt.widgets.Display;
37  import org.wsmo.common.IRI;
38  import org.wsmo.datastore.WsmoRepository;
39  import org.wsmo.service.Goal;
40  import org.wsmo.service.WebService;
41  import org.wsmostudio.discovery.Activator;
42  import org.wsmostudio.runtime.LogManager;
43  import org.wsmostudio.runtime.WSMORuntime;
44  import org.wsmostudio.runtime.cache.WSMOTopEntity;
45  
46  import ch.epfl.qosdisc.operators.Manager;
47  import ch.epfl.qosdisc.operators.TestFrame;
48  
49  
50  public class Discoverer {
51  	private static final String FILE_PROTO = "file://";
52      private static final String COMPARISON = "comparison";
53      private static final String RANKING = "ranking";
54      private static final String DIR_CONF = "conf";
55      private static final String DIR_DATA = "data";
56      private static final String FNAME_ONTO_RANKING = "QoSRankingOntology.wsml";
57      private static final String FNAME_ONTO_COMPARITION = "QoSUpperOntology.wsml";
58      
59      private TestFrame testFrame;
60      
61      public Discoverer() {
62          URL confURL = Activator.getDefault().getBundle().getEntry(DIR_CONF);
63          IPath confPath = null;
64          try {
65              confPath = new Path(FileLocator.toFileURL(confURL).getPath());
66          } catch(Exception ioe) {       
67              MessageDialog.openError(Display.getCurrent().getActiveShell(), 
68                      "Error", 
69                      "Error reading configuration file:\n" 
70                      + ioe.getMessage());
71              ioe.printStackTrace();
72          }
73          URL dataURL = Activator.getDefault().getBundle().getEntry(DIR_DATA);
74          IPath dataPath = null;
75          try {
76              dataPath = new Path(FileLocator.toFileURL(dataURL).getPath());
77          } catch(Exception ioe) {       
78              MessageDialog.openError(Display.getCurrent().getActiveShell(), 
79                      "Error", 
80                      "Error reading ranking and comparition ontologies:\n" 
81                      + ioe.getMessage());
82              ioe.printStackTrace();
83          }
84  
85          this.testFrame = new TestFrame(confPath.toString());
86          TestFrame.props.put(RANKING, FILE_PROTO+dataPath.toString()+FNAME_ONTO_RANKING);
87          TestFrame.props.put(COMPARISON, FILE_PROTO+dataPath.toString()+FNAME_ONTO_COMPARITION);
88      }
89  
90      public List<WebService> discover(Set<WebService> services, Goal goal, boolean functional) {
91  		if(functional) {
92              try {
93                  // Add the services to the functional discovery component.
94                  LightweightDiscovery lwd = new LightweightDiscovery();
95                  lwd.addWebService(services);
96                  
97                  // Perform the functional discovery.
98                  List<WebService> funcServices = lwd.discover(goal);
99             
100                 
101                 // Copy only the remaining services.
102                 services.clear();
103                 services.addAll(funcServices);
104             } 
105             catch(NoClassDefFoundError noClass) {
106                 MessageDialog.openError(Display.getCurrent().getActiveShell(), 
107                         "Missing KAON2 Libraries", 
108                         "Error loading KAON2 libraries:\n" 
109                         + noClass.getMessage());
110                 LogManager.logWarning(noClass);
111                 return null;
112             }
113             catch (Exception e) {
114                 MessageDialog.openError(Display.getCurrent().getActiveShell(), 
115                         "Functional Discovery Failed", 
116                         e.getMessage());
117                 LogManager.logError("org.wsmostudio.discovery",
118                                     "Functional discovery failed",
119                                     e);
120                 return null;
121             }
122 
123         }
124         
125         // Add the services to the test frame.
126         for(WebService s : services)
127             this.testFrame.addService(s);
128         
129         // And find the matching services.
130         List<WebService> result = this.testFrame.achieveGoal(goal);
131 		return result;
132 	}
133 	
134 	public List<WebService> discover(WsmoRepository repo, Goal goal, boolean functional) {
135 		List wsIriList = repo.listWebServices();
136         Set<WebService> services = new HashSet<WebService>();
137         
138         
139         for(Iterator it = wsIriList.iterator();it.hasNext();) {
140             IRI wsIri = (IRI)it.next();
141             WebService ws = repo.getWebService(wsIri);
142             services.add(ws);
143         }
144         
145         String goalFileLoc = WSMORuntime.getCache().getFileLocationForId((IRI)goal.getIdentifier(), WSMOTopEntity.GOAL);
146         Goal g = null;
147         try {
148             g = Manager.getGoal(new File(goalFileLoc).toURI().toURL().toString());
149         }
150         catch(MalformedURLException error) {
151             LogManager.logError(error);
152             return null;
153         }
154         return discover(services, g, functional);
155 	}
156 
157 }
158 
159 /*
160  * $Log$
161  * Revision 1.2  2007/04/17 14:28:02  alex_simov
162  * deprication fix
163  *
164  * Revision 1.1  2007/01/26 14:31:37  alex_simov
165  * discovery plug-in moved from EXT module
166  *
167  * Revision 1.7  2006/10/31 13:02:14  alex_simov
168  * no message
169  *
170  * Revision 1.6  2006/09/19 13:17:05  alex_simov
171  * no message
172  *
173  */