package org.semports.handshakes; import java.util.HashSet; import java.util.Iterator; import java.util.Random; import java.io.*; /** * This class contains the simulation functionality. The experiment series * have to be conigured in the source code of this class. * * @author Lutz Maicher */ public class Simulation { /** Current version of the semantic handshake simulator */ String version = "1.1"; // In the following you have to configure your // experiment. See the configuration guide at // the website for the Semantic Handshakes. /** Represents the path where the results of the experiment series are saved as [expname]+".dat" and [expname]+".plt" */ public static String path = "C:\\Dokumente und Einstellungen\\Lutz\\Eigene Dateien\\ARTIKEL und PRÄSENTATIONEN\\ARTIKEL TMRA 2006\\Ergebnisse\\"; /** Represents a header for the experiment series. It will be used for output purposes. **/ public static String expname = "exp01"; /** Represents a header for the experiment series. It will be used for output purposes.*/ public String header = "Iterating over nbrOfDifferentII"; /** Represents the label for the x-axis in the output diagram **/ public String xlabel = "nbrOfDifferentII"; /** Defines the range of the x-axis in gnuplot notation. See gnuplot documentation for further details.**/ public String xrange = "[2:100]"; /** Defines whether iteration over nbrOfDifferentII has to be done.*/ public boolean it_nbrOfDifferentII = true; /** Defines whether iteration over the distributionNbrOfII has to be done.*/ public boolean it_distribution_nbrOfII = false; /** Defines whether iteration over the distributionII has to be done.*/ public boolean it_distribution_II = false; /** Defines whether iteration over card(E) has to be done.*/ public boolean it_cardE = false; /** Represents of the number of different Identity Identifiers which are "known" in the world. If nbrOfDifferentII is 5, only five different Identity Identifier can be assigned to the Proxys. */ public int nbrOfDifferentII = 100; /** Represents the number of different Proxies which has to be created. */ public int cardE = 100; /** Represents the starting point of the iteration in executeExperimentSeries. */ public int i_start = 2; /** Represents the number of steps which have to be done in the iteration in executeExperimentSeries. */ public int i_loops = 50; /** Represents the width of a step in the iteration in executeExperimentSeries. */ public int i_step = 2; /** Represents D of the distribution of the number of initial Identity Identifier which should be assigned to a proxy. The upper limit b is distributionNbrOfII.length(). See the distribution definition schema described in the online documentation. */ public double distributionNbrOfII[] = {1.0}; /** Represents D of the distribution of the values of the Identity Identifiers of proxies. The upper limit b is nbrOfDifferentII. See the distribution definition schema described in the online documentation.*/ public double distributionII[] = {1.0}; // uniformly distributed /** Defines whether the second Identity Identifier is assigned according to a different distribution as the first Identity Identifier. **/ public boolean differentDistributionII2 = false; /** Represents D of the distribution of the values of the second Identiy Identifier. The upper limit b is nbrOfDifferentII. See the distribution definition schema described in the online documentation. */ public double distributionII2[] = {0.8, 0.9, 0.97, 1.0}; // heavily massed distributionNbrOfII ; /** Represents the number of merge roundtrips within a test. */ public int nbrOfMergeRoundtrips = 2; /** Represents the number of tests in an experiment.*/ public int nbrOfTests = 50; /** Defines whether the results of all tests should be printed to the output files. */ public boolean output_testresults = false; private Random myRandom; private HashSet myProxies; // Writes into the file [result]+[expname]+".dat" private PrintWriter myPrintWriterDat; // Writes into the file [result]+[expname]+".plt" private PrintWriter myPrintWriterPlt; private Helper myHelper; /** Creates a new instance of Simulation */ public Simulation(String pathName, String expName) { try { FileWriter fw = new FileWriter(pathName + expName + ".dat"); BufferedWriter bw = new BufferedWriter( fw ); myPrintWriterDat = new PrintWriter( bw ); FileWriter fw1 = new FileWriter(pathName + expName + ".plt"); BufferedWriter bw1 = new BufferedWriter( fw1 ); myPrintWriterPlt = new PrintWriter( bw1 ); } catch (Exception E) { } myHelper = new Helper(this.myPrintWriterDat); // Instantiating the plt-File for gnuplot myHelper.out("reset", myPrintWriterPlt); myHelper.out("set terminal windows",myPrintWriterPlt); myHelper.out("set title \""+expname + " " + this.header + "\"", this.myPrintWriterPlt); myHelper.out("set xlabel \""+this.xlabel+"\" 0.0, 0.0",myPrintWriterPlt); myHelper.out("set key right bottom", this.myPrintWriterPlt); myHelper.out("set ylabel \"average nbr of clusters\" textcolor lt 1",myPrintWriterPlt); myHelper.out("set y2label \"average card(T) of a cluster\" textcolor lt 2",myPrintWriterPlt); myHelper.out("set y2range [0.0 : ]",myPrintWriterPlt); myHelper.out("set xrange "+this.xrange,myPrintWriterPlt); myHelper.out("set ytics",myPrintWriterPlt); myHelper.out("set y2tics nomirror",myPrintWriterPlt); myHelper.out("plot \""+expName+".dat\" using 1:2 smooth sbezier ti \"cluster(E)\" lt 1 axis x1y1, \""+expName+".dat\" using 1:3 smooth sbezier ti \"card(T)\" with line axis x1y2, \""+expName+".dat\" using 1:2 notitle lt 3 axis x1y1, \""+expName+".dat\" using 1:3 notitle lt 4 axis x1y2",myPrintWriterPlt); } /** This function extracts the number of integration clouds * currently existing in the E. * * Two proxies from two different clusters will never * have identical IDs in their proxy repositories of * "identical" proxies. * * @return The number of clusters in E. */ public int getNbrOfClouds() { // Create an empty HashSet of IDs (workingCopy) HashSet WorkingCopy = new HashSet(); Iterator it_myProxys = myProxies.iterator(); int result = 0; // Iterate over all proxies in E while (it_myProxys.hasNext()) { // Get the current proxy from E Proxy cur_Proxy = (Proxy)it_myProxys.next(); // If the ID of the extracted Proxy is not in // the created empty hashSet of IDs (workingCopy) // a new cluster is detected. if (!WorkingCopy.contains(cur_Proxy.id)) { // Inrease the number of clusters (result) result = result + 1; // Add the ID of all identical proxies which are // part of the cur_Proxy's proxyRepository into // the HashSet of known IDs (workingCopy) Iterator it = cur_Proxy.proxyRepository.iterator(); while (it.hasNext()) WorkingCopy.add(it.next()); } } return result; } /** This function calculates the average card(T). * This value is the average size of an integration cloud in E. * * @return The average size of an integration cloud in E. */ public double getAverageCardT() { double result = 0.0; double i = myProxies.size(); Iterator it = myProxies.iterator(); while (it.hasNext()) { result = result + ((Proxy)it.next()).proxyRepository.size()/i; } return Helper.roundScale1(result); } /** This function has to be run after each iteration * to clean up all Proxies. */ private void prepareNextMergeRoundtrip(HashSet myProxies) { Iterator it_myProxies = myProxies.iterator(); while (it_myProxies.hasNext()) { Proxy cur_Proxy = (Proxy) it_myProxies.next(); cur_Proxy.prepareNextMergeRoundtrip(); } } /** This function iterates inside a global * iteration. (LocalIteration) * * @param myProxies - */ private void executeMergeRoundtrip(HashSet myProxies) { // Create an empty HashSet Proxies HashSet done = new HashSet(); // Doing a simple iteration Iterator it_myProxies = myProxies.iterator(); while (it_myProxies.hasNext()) { Proxy cur_Proxy = (Proxy) it_myProxies.next(); Iterator it2_myProxys = myProxies.iterator(); while (it2_myProxys.hasNext()) { Proxy cur2_Proxy = (Proxy) it2_myProxys.next(); if (!done.contains(cur2_Proxy)) if (!((cur2_Proxy).equals(cur_Proxy))) { if (cur_Proxy.equal(cur2_Proxy)) { cur_Proxy.merge(cur2_Proxy); } } } done.add(cur_Proxy); } // It is important that this function is called before the // analysis is done. this.prepareNextMergeRoundtrip(myProxies); } int getDistributionValue(double dis[] , int max) { // Example: (distribution definition according online documentation) // [{0.5 , 0.7 , 0.9 , 1.0} , 100] // Get a uniformly distrubuted value in [0,1[ // Example: 0.8 double val_result = myRandom.nextDouble(); // Intervall_size is the range of an intervall // Example: 100/4 = 25 int intervall_size = (int)(max / dis.length); // Get a uniformly distibuted value within the intervall // Example: 13 int intervall = myRandom.nextInt(intervall_size); int result = 0; boolean proceed = true; int i1 = 0; // Iterate through the definition of the distribution while (i10) otherDis=true; } myProxys.add(newProxy); } return myProxys; } /** This method executes the experiment series. Configuration of the experiment design might be realised in the source code of this method.**/ public void executeExperimentSeries() { myHelper.out("This experiment series \"" + this.header + "\" has the following paramters:"); myHelper.out("Its bases on the semantic handshakes version: " + this.version); myHelper.out(""); myHelper.out("card(E) - " + this.cardE); myHelper.out("distribution_nbrOfII - " + myHelper.outputDistribution(distributionNbrOfII) ); myHelper.out("maxII - " + this.nbrOfDifferentII ); myHelper.out("distribution_II - " + myHelper.outputDistribution(distributionII) ); myHelper.out(""); myHelper.out("NbrOfGlobalIteration - " + this.nbrOfTests ); myHelper.out("NbrOfLocalIteration - " + this.nbrOfMergeRoundtrips ); myRandom = new Random(); for (int i=0; i