2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.vector;
21 import peersim.core.*;
24 * Observes the cosine angle between two vectors. The number which is output is
25 * the inner product divided by the product of the length of the vectors.
26 * All values are converted to double before processing.
28 * This observer class can observe any protocol field containing a
29 * primitive value, provided that the field is associated with a getter method
31 * The methods to be used are specified through parameter {@value #PAR_METHOD1}
32 * and {@value #PAR_METHOD2}.
34 * Please refer to package {@link peersim.vector} for a detailed description of
37 public class VectAngle implements Control
40 // --------------------------------------------------------------------------
42 // --------------------------------------------------------------------------
45 * The first protocol to be observed.
48 private static final String PAR_PROT1 = "protocol1";
51 * The second protocol to be observed.
54 private static final String PAR_PROT2 = "protocol2";
57 * The getter method used to obtain the values of the first protocol.
58 * Defaults to <code>getValue</code> (for backward compatibility with previous
59 * implementation of this class, that were based on the
60 * {@link SingleValue} interface).
61 * Refer to the {@linkplain peersim.vector vector package description} for more
62 * information about getters and setters.
65 private static final String PAR_METHOD1 = "getter1";
68 * The getter method used to obtain the values of the second protocol.
69 * Defaults to <code>getValue</code> (for backward compatibility with previous
70 * implementation of this class, that were based on the
71 * {@link SingleValue} interface).
72 * Refer to the {@linkplain peersim.vector vector package description} for more
73 * information about getters and setters.
76 private static final String PAR_METHOD2 = "getter2";
78 // --------------------------------------------------------------------------
80 // --------------------------------------------------------------------------
82 /** The prefix for this observer*/
83 private final String name;
85 private final Getter getter1;
87 private final Getter getter2;
89 // --------------------------------------------------------------------------
91 // --------------------------------------------------------------------------
94 * Standard constructor that reads the configuration parameters.
95 * Invoked by the simulation engine.
96 * @param prefix the configuration prefix for this class
98 public VectAngle(String prefix)
101 getter1 = new Getter(prefix,PAR_PROT1,PAR_METHOD1);
102 getter2 = new Getter(prefix,PAR_PROT2,PAR_METHOD2);
105 // --------------------------------------------------------------------------
107 // --------------------------------------------------------------------------
110 * Observes the cosine angle between two vectors. The printed values
111 * are: cosine, Eucledian norm of vect 1, Eucledian norm of vector 2,
113 * @return always false
115 public boolean execute() {
117 double sqrsum1 = 0, sqrsum2 = 0, prod = 0;
118 for (int i = 0; i < Network.size(); ++i)
120 double v1= getter1.get(i).doubleValue();
121 double v2= getter2.get(i).doubleValue();
127 double cos = prod / Math.sqrt(sqrsum1) / Math.sqrt(sqrsum2);
129 // deal with numeric errors
131 if( cos < -1 ) cos = -1;
133 System.out.println(name+": " + cos + " "
134 + Math.sqrt(sqrsum1) + " " + Math.sqrt(sqrsum2) + " "
139 //--------------------------------------------------------------------------