⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcollection.java

📁 实现对owl-s描述服务的检索
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * OWL-S Matchmaker
 * 
 * COPYRIGHT NOTICE
 * 
 * Copyright (C) 2005 DFKI GmbH, Germany
 * Developed by Benedikt Fries, Matthias Klusch
 * 
 * The code is free for non-commercial use only.
 * You can redistribute it and/or modify it under the terms
 * of the Mozilla Public License version 1.1  as
 * published by the Mozilla Foundation at
 * http://www.mozilla.org/MPL/MPL-1.1.txt
 * 
 */
package owlsmx.gui.data;



import java.awt.Component;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
//import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.DefaultListModel;
import owlsmx.gui.util.GUIState;
import owlsmx.gui.util.MatchmakerInterface;
import owlsmx.gui.util.UpdateDataListener;
import owlsmx.gui.util.UpdateDataListenerRegistry;



/**
 * Testcollection is a singleton that stores services, queries and the answersets for the queries.
 * In addition it connects the GUI to the OWLS-MX Matchmaker and stores the datamodels for
 * services and queries. 
 * 
 * @author Ben
 *
 */
public class TestCollection implements java.io.Serializable, UpdateDataListenerRegistry, UpdateDataListener{

	private static final long serialVersionUID = -6345043491596293272L;
	private Map services = new HashMap();
	private Map queries = new HashMap();
	private Map answerset = new HashMap();
	private Map matchmakerAnswerset = new HashMap();
	private Map result = new HashMap();
	private String name = "";
	private String version = "";
	private String comment = "";
    private DefaultListModel serviceDataModel = new DefaultListModel();
    private DefaultListModel queryDataModel = new DefaultListModel();
    private DefaultListModel answersDataModel = new DefaultListModel();
    private Query activeQuery;
    private Set registry = new HashSet();
    private static TestCollection instance = new TestCollection();    
    
    /**
     * Empty Constructor
     */
    private TestCollection() {		 
    }
   
    /**
     * @return single instance of the TestCollection
     */
    public static TestCollection getInstance() {
    	return instance;
    }
	
	/**
	 * Clear the testcollection, resets all variables
	 */
	public void clearTC() {
		services = new HashMap();
		queries = new HashMap();
		answerset = new HashMap();
		matchmakerAnswerset = new HashMap();
		result = new HashMap();
		name = "";
		version = "";
		comment = "";
	    serviceDataModel = new DefaultListModel();
	    queryDataModel = new DefaultListModel();
	    answersDataModel = new DefaultListModel();
	    updateData();
	}
    
	/**
	 * Adds the services with the given URI
	 * Take care as the services will be added with the URI stored IN the service
	 * (to avoid unecessary duplicates if the same service is added from different
	 * locations)	 * 
	 * 
	 * @param uri	URI of the service to be added
	 * @return		the loaded Service
	 * @throws FileNotFoundException	thrown if the service can't be loaded from the given URI
	 */
	public ServiceItem addService(URI uri) throws FileNotFoundException {
		uri = GUIState.getInstance().replaceString(uri);		
		if (services.containsKey(uri)) {
			owlsmx.io.ErrorLog.instanceOf().report(
        			"Service " + uri + " is already in the set of registered services");
			return (ServiceItem)services.get(uri);
		}
		ServiceItem item = new ServiceItem(uri);
		uri = item.getURI();
		if (services.containsKey(uri)) {
			owlsmx.io.ErrorLog.instanceOf().report(
        			"Service with real URI " + uri + " is already in the set of registered services");
			return (ServiceItem)services.get(uri);
		}
		services.put(uri, item);
		serviceDataModel.addElement(item);
		return item;
	}
	
	/**
	 * Removes the service with the given URI from the test collection
	 * (If no services is stored with the given URI it will try to load
	 * the service from this URI and remove that one)
	 * 
	 * @param uri		uri of the service that should be removed
	 * @param update	if the underlying data models should be updated
	 */
	private void removeService(URI uri, boolean update){		
		if (!services.containsKey(uri)) {
			ServiceItem item = new ServiceItem(uri);
			uri = item.getURI();
		}
		services.remove(uri);		
		Iterator iter = answerset.keySet().iterator();
		while(iter.hasNext()) {
			removeServiceFromAnswerset((URI) iter.next(), uri);
		}
		if (update)
			updateDataModels();
		if (activeQuery!=null)
			updateAnswerset(activeQuery);
	}
	
	
	/**
	 * Removes the service with the given URI from the test collection
	 * (If no services is stored with the given URI it will try to load
	 * the service from this URI and remove that one)
	 * This function will also trigger an update the underlying data models
	 * 
	 * @param uri		uri of the service that should be removed
	 */
	public void removeService(URI uri) {
		removeService(uri, true);
	}
	
	/**
	 * Removes multiple services from the test collection
	 * 
	 * @param services Vector of ServiceItems that should be removed
	 */
	public void removeServices(Object[] services) {
		for (int i = 0; i<services.length;i++) {
			removeService(((ServiceItem)services[i]).getURI(),false);			
		}
		updateDataModels();
		if (activeQuery!=null)
			updateAnswerset(activeQuery);
	}
	
	/**
	 * Removes multiple services from the test collection
	 * 
	 * @param services Set of ServiceItems that should be removed
	 */
	public void removeServices(Set services) {
		removeServices(services.toArray());
	}
	
	/**
	 * returns the service of the given URI
	 * (If no services is stored with the given URI it will try to load
	 * the service from this URI and remove that one)
	 * 
	 * @param uri	URI of the service
	 * @return		ServiceItem of the service
	 */
	public ServiceItem getService(URI uri) {		
		if (!services.containsKey(uri)) {
			ServiceItem item = new ServiceItem(uri);
			uri = item.getURI();
		}
		return (ServiceItem)services.get(uri);
	}
	
	/**
	 * Returns all services in the test collection
	 * 
	 * @return Set of Services
	 */
	public Set getServices() {
		return new HashSet(services.values());
	}
	
	
	/**
	 * Adds the query with the given URI
	 * Take care as the query will be added with the URI stored IN the query
	 * (to avoid unecessary duplicates if the same service is added from different
	 * locations)
	 * 
	 * @param uri	URI of the query to be added
	 * @return		the loaded Query
	 * @throws FileNotFoundException	thrown if the query can't be loaded from the given URI
	 */
	public Query addQuery(URI uri) {
		try {
			uri = GUIState.getInstance().replaceString(uri);			
			if (queries.containsKey(uri))
				return (Query)queries.get(uri);
			Query item = new Query(uri);
			if (queries.containsKey(item.getURI()))
				return (Query)queries.get(uri);
			queries.put(item.getURI(), item);
			queryDataModel.addElement(item);
			answerset.put(item.getURI(),new TreeSet());
			matchmakerAnswerset.put(item.getURI(),new TreeSet());		
			return item;
		}
		catch(Exception e) {
			e.printStackTrace();
			return new Query(null);
		}
	}
	
	/**
	 * Removes the query with the given URI from the test collection
	 * (If no services is stored with the given URI it will try to load
	 * the service from this URI and remove that one)
	 * 
	 * @param uri		uri of the query that should be removed
	 * @param update	if the underlying data models should be updated
	 */
	public void removeQuery(URI uri, boolean update) {
		answerset.remove(uri);
		matchmakerAnswerset.remove(uri);		
		if (!queries.containsKey(uri)) {
			Query item = new Query(uri);
			uri = item.getURI();
		}		
		queries.remove(uri);
		answerset.remove(uri);
		matchmakerAnswerset.remove(uri);
		if (update)
			updateDataModels();
		if( (activeQuery!=null) && (activeQuery.getURI().equals(uri)) )
			updateAnswerset(activeQuery);
	}
	
	/**
	 * Removes the query with the given URI from the test collection
	 * (If no services is stored with the given URI it will try to load
	 * the service from this URI and remove that one)
	 * This function will also trigger an update the underlying data models
	 * 
	 * @param uri		uri of the query that should be removed
	 */
	public void removeQuery(URI uri) {		
		removeQuery(uri,true);
	}
	
	/**
	 * Removes multiple queries from the test collection
	 * 
	 * @param queries Vector of queries that should be removed
	 */
	public void removeQueries(Object[] queries) {
		for (int i = 0; i<queries.length;i++) {
			removeQuery(((Query)queries[i]).getURI(),false);			
		}
		updateDataModels();
		if (activeQuery!=null)
			updateAnswerset(activeQuery);
	}
	
	
	/**
	 * Removes multiple queries from the test collection
	 * 
	 * @param queries Set of queries that should be removed
	 */
	public void removeQueries(Set queries) {
		removeQueries(queries.toArray());
	}
	
	public SortedSet getQueries() {
		return new TreeSet(queries.values());
	}
	
	/**
	 * Adds a service to the answerset of a given query
	 * Assumes that both service and query where already added to the collection
	 * Assumes that the URI equals the URI specified in the service or query, 
	 * but loads the query or service if not.
	 * 
	 * @param query		URI of the query to whose answerset the service should be added
	 * @param service	URI of the service to be added to the answerset
	 */
	public void addServiceToAnswerset(URI query, URI service) {
		try {
		URI localQuery = GUIState.getInstance().replaceString(query);
		URI localService = GUIState.getInstance().replaceString(service);
		if (!services.containsKey(localService)) {
			ServiceItem item = new ServiceItem(localService);
			GUIState.displayWarning(this.getClass().toString(), "Tried "+ item.getURI().toString() + 
					"\n instead of " + localService.toString());
			if (services.containsKey(item.getURI())) {				
				localService = item.getURI();			
			}
		}
		
		if (!queries.containsKey(localQuery)){
			Query queryItem = new Query(localQuery);
			GUIState.displayWarning(this.getClass().toString(), "Tried "+ queryItem.getURI().toString() + 
					"\n instead of " + localQuery.toString());
			if (queries.containsKey(queryItem.getURI()))
				localQuery = queryItem.getURI();
		}
			
		if ( (!queries.containsKey(localQuery))  || (!services.containsKey(localService)) 
											|| (!answerset.containsKey(localQuery)) ){			
			GUIState.displayWarning(this.getClass().toString() +"-addServiceToAnswerset:", 
    	    		"Didn't add " + localService.toString() + "\n to Answerset of query " + 
    	    		localQuery.toString() + "\n" + "(Query " + (queries.containsKey(localQuery)) + ")" +    	    		
    	    		"(Service " + (services.containsKey(localQuery)) + ")" +
    	    		"(AnswerQuery " + (answerset.containsKey(localQuery)) + ")"	);	  
			return;
		} 
		
		if (((TreeSet)answerset.get(query)).contains(service))
			return;
		((TreeSet)answerset.get(query)).add(service);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -