Révision 145
tmp/org.txm.r/src/org/txm/stat/package.html (revision 145) | ||
---|---|---|
1 |
<html> |
|
2 |
<body> |
|
3 |
<p>Root package of all statistical and numeric computing functions of the toolbox.</p> |
|
4 |
</body> |
|
5 |
</html> |
|
0 | 6 |
tmp/org.txm.r/src/org/txm/stat/utils/ArrayIndex.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.utils; |
|
29 |
|
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.Map; |
|
32 |
|
|
33 |
import org.txm.Messages; |
|
34 |
|
|
35 |
// TODO: Auto-generated Javadoc |
|
36 |
/** |
|
37 |
* Utilities for managing index of vector. |
|
38 |
* |
|
39 |
* @author sloiseau |
|
40 |
* |
|
41 |
*/ |
|
42 |
public final class ArrayIndex { |
|
43 |
|
|
44 |
/** |
|
45 |
* Convert an array of zero-based index to an array of one-base index, that |
|
46 |
* is, add <code>1</code> to all elements. |
|
47 |
* |
|
48 |
* This method check every index (see exception thrown). |
|
49 |
* |
|
50 |
* @param zero_based the zero based index. |
|
51 |
* @param length the number of elements that can be indexed. |
|
52 |
* @return the one base index. |
|
53 |
*/ |
|
54 |
public static final int[] zeroToOneBasedIndex(int[] zero_based, int length) { |
|
55 |
if (zero_based == null || zero_based.length == 0) { |
|
56 |
throw new IllegalArgumentException(Messages.ArrayIndex_0); |
|
57 |
} |
|
58 |
int[] one_based = new int[zero_based.length]; |
|
59 |
for (int i = 0; i < zero_based.length; i++) { |
|
60 |
if (zero_based[i] < 0 || zero_based[i] >= length) { |
|
61 |
throw new IllegalArgumentException(Messages.ArrayIndex_1); |
|
62 |
} |
|
63 |
one_based[i] = zero_based[i] + 1; |
|
64 |
} |
|
65 |
return one_based; |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* Zero to one based index. |
|
70 |
* |
|
71 |
* @param zero_based the zero_based |
|
72 |
* @return the int |
|
73 |
*/ |
|
74 |
public static final int zeroToOneBasedIndex(int zero_based) { |
|
75 |
return zero_based + 1; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Get the index (0-based) of the element of subvector into vector. Return |
|
80 |
* <code>-1</code> when no element in vector correspond to the element in |
|
81 |
* subvector. |
|
82 |
* |
|
83 |
* @param vector the vector |
|
84 |
* @param subvector the subvector |
|
85 |
* @return the index |
|
86 |
*/ |
|
87 |
public static int[] getIndex(String[] vector, String[] subvector) { |
|
88 |
int[] result = new int[subvector.length]; |
|
89 |
Map<String, Integer> index = new HashMap<String, Integer>(); |
|
90 |
for (int i = 0; i < subvector.length; i++) { |
|
91 |
index.put(subvector[i], i); |
|
92 |
result[i] = -1; |
|
93 |
} |
|
94 |
for (int i = 0; i < vector.length; i++) { |
|
95 |
if (index.containsKey(vector[i])) { |
|
96 |
result[index.get(vector[i])] = i; |
|
97 |
} |
|
98 |
} |
|
99 |
return result; |
|
100 |
} |
|
101 |
} |
|
0 | 102 |
tmp/org.txm.r/src/org/txm/stat/utils/ArrayEquals.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.utils; |
|
29 |
|
|
30 |
import java.util.Arrays; |
|
31 |
|
|
32 |
// TODO: Auto-generated Javadoc |
|
33 |
/** |
|
34 |
* Utility methods for testing for equality of arrays. |
|
35 |
* |
|
36 |
* @author sloiseau |
|
37 |
* |
|
38 |
*/ |
|
39 |
public final class ArrayEquals { |
|
40 |
|
|
41 |
/** |
|
42 |
* Test for equality of both array. For consistency with Arrays.equals |
|
43 |
* contract, the two arrays are equals if both are <code>null</code>. |
|
44 |
* |
|
45 |
* @param array1 the array1 |
|
46 |
* @param array2 the array2 |
|
47 |
* @return <code>true</code> if (1) both arrays are <code>null</code> or if |
|
48 |
* (2) both arrays have the same length and contain pair of arrays |
|
49 |
* equals to each other according to |
|
50 |
* {@link Arrays#equals(int[], int[])}. |
|
51 |
*/ |
|
52 |
public static final boolean deepEquals(int[][] array1, int[][] array2) { |
|
53 |
if (array1 == null && array2 == null) |
|
54 |
return true; |
|
55 |
if (array1 == null || array2 == null) |
|
56 |
return false; |
|
57 |
if (array1.length != array2.length) |
|
58 |
return false; |
|
59 |
for (int i = 0; i < array2.length; i++) { |
|
60 |
if (!Arrays.equals(array1[i], array2[i])) |
|
61 |
return false; |
|
62 |
} |
|
63 |
return true; |
|
64 |
} |
|
65 |
|
|
66 |
} |
|
0 | 67 |
tmp/org.txm.r/src/org/txm/stat/utils/VectorizeArray.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.utils; |
|
29 |
|
|
30 |
import org.txm.Messages; |
|
31 |
|
|
32 |
// TODO: Auto-generated Javadoc |
|
33 |
/** |
|
34 |
* Static methods for dealing vectorization of Arrays. |
|
35 |
* |
|
36 |
* @author sloiseau |
|
37 |
* |
|
38 |
*/ |
|
39 |
public final class VectorizeArray { |
|
40 |
|
|
41 |
/** |
|
42 |
* Convert a <code>double * double</code> array in a <code>double</code> |
|
43 |
* array. Each inner array of the argument is added one after the other into |
|
44 |
* the result array. |
|
45 |
* |
|
46 |
* <pre> |
|
47 |
* [ |
|
48 |
* [1, 2, 3], |
|
49 |
* [4, 5, 6] |
|
50 |
* ] |
|
51 |
* </pre> |
|
52 |
* |
|
53 |
* result in : |
|
54 |
* |
|
55 |
* <pre> |
|
56 |
* [1, 2, 3, 4, 5, 6] |
|
57 |
* </pre> |
|
58 |
* |
|
59 |
* that is, "row first". |
|
60 |
* |
|
61 |
* @param matrix a <code>double * double</code> array where all inner arrays |
|
62 |
* have the same length. |
|
63 |
* @return an array of <code>double</code> produced by vectorization of the |
|
64 |
* matrix. |
|
65 |
*/ |
|
66 |
public final static double[] vectorizeByInner(double[][] matrix) { |
|
67 |
if (matrix == null || matrix.length == 0) { |
|
68 |
throw new IllegalArgumentException(Messages.VectorizeArray_0); |
|
69 |
} |
|
70 |
int inner_length = matrix[0].length; |
|
71 |
if (inner_length < 1) { |
|
72 |
throw new IllegalArgumentException(Messages.VectorizeArray_1); |
|
73 |
} |
|
74 |
double[] vector = new double[matrix.length * matrix[0].length]; |
|
75 |
for (int i = 0; i < matrix.length; i++) { |
|
76 |
if (matrix[i] == null) { |
|
77 |
throw new IllegalArgumentException(Messages.VectorizeArray_10); |
|
78 |
} |
|
79 |
if (inner_length != matrix[i].length) { |
|
80 |
throw new IllegalArgumentException(Messages.VectorizeArray_3); |
|
81 |
} |
|
82 |
System.arraycopy(matrix[i], 0, vector, i * inner_length, |
|
83 |
inner_length); |
|
84 |
} |
|
85 |
return vector; |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* Convert a <code>int * int</code> array in a <code>double</code> array. |
|
90 |
* Each inner array of the argument is added one after the other into the |
|
91 |
* result array. |
|
92 |
* |
|
93 |
* <pre> |
|
94 |
* [ |
|
95 |
* [1, 2, 3], |
|
96 |
* [4, 5, 6] |
|
97 |
* ] |
|
98 |
* </pre> |
|
99 |
* |
|
100 |
* result in : |
|
101 |
* |
|
102 |
* <pre> |
|
103 |
* [1, 2, 3, 4, 5, 6] |
|
104 |
* </pre> |
|
105 |
* |
|
106 |
* that is, "row first". |
|
107 |
* |
|
108 |
* @param matrix a <code>int * int</code> array where all inner arrays have the |
|
109 |
* same length. |
|
110 |
* @return an array of <code>int</code> produced by vectorization of the |
|
111 |
* matrix. |
|
112 |
*/ |
|
113 |
public final static int[] vectorizeByInner(int[][] matrix) { |
|
114 |
if (matrix == null || matrix.length == 0) { |
|
115 |
throw new IllegalArgumentException(Messages.VectorizeArray_4); |
|
116 |
} |
|
117 |
int inner_length = matrix[0].length; |
|
118 |
if (inner_length < 1) { |
|
119 |
throw new IllegalArgumentException(Messages.VectorizeArray_5); |
|
120 |
} |
|
121 |
int[] vector = new int[matrix.length * matrix[0].length]; |
|
122 |
for (int i = 0; i < matrix.length; i++) { |
|
123 |
if (matrix[i] == null) { |
|
124 |
throw new IllegalArgumentException(Messages.VectorizeArray_10); |
|
125 |
} |
|
126 |
if (inner_length != matrix[i].length) { |
|
127 |
throw new IllegalArgumentException(Messages.VectorizeArray_7); |
|
128 |
} |
|
129 |
System.arraycopy(matrix[i], 0, vector, i * inner_length, |
|
130 |
inner_length); |
|
131 |
} |
|
132 |
return vector; |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* Convert a <code>double * double</code> array in a <code>double</code> |
|
137 |
* array. The elements of the input array are assembled in the output array |
|
138 |
* so that elements of same index in the outer array are consecutive. So : |
|
139 |
* |
|
140 |
* <pre> |
|
141 |
* [ |
|
142 |
* [1, 2, 3], |
|
143 |
* [4, 5, 6] |
|
144 |
* ] |
|
145 |
* </pre> |
|
146 |
* |
|
147 |
* result in : |
|
148 |
* |
|
149 |
* <pre> |
|
150 |
* [1, 4, 2, 5, 3, 6] |
|
151 |
* </pre> |
|
152 |
* |
|
153 |
* that is, "column first". |
|
154 |
* |
|
155 |
* @param matrix a <code>double * double</code> array where all inner arrays |
|
156 |
* have the same length. |
|
157 |
* @return an array of <code>double</code> produced by vectorization of the |
|
158 |
* matrix. |
|
159 |
*/ |
|
160 |
public final static double[] vectorizeByOuter(double[][] matrix) { |
|
161 |
if (matrix == null || matrix.length == 0) { |
|
162 |
throw new IllegalArgumentException(Messages.VectorizeArray_8); |
|
163 |
} |
|
164 |
int inner_length = matrix[0].length; |
|
165 |
int outer_length = matrix.length; |
|
166 |
if (inner_length < 1) { |
|
167 |
throw new IllegalArgumentException(Messages.VectorizeArray_9); |
|
168 |
} |
|
169 |
double[] vector = new double[matrix.length * matrix[0].length]; |
|
170 |
for (int i = 0; i < matrix.length; i++) { |
|
171 |
if (matrix[i] == null) { |
|
172 |
throw new IllegalArgumentException(Messages.VectorizeArray_10); |
|
173 |
} |
|
174 |
if (inner_length != matrix[i].length) { |
|
175 |
throw new IllegalArgumentException(Messages.VectorizeArray_11); |
|
176 |
} |
|
177 |
for (int j = 0; j < matrix[i].length; j++) { |
|
178 |
vector[(j * outer_length) + i] = matrix[i][j]; |
|
179 |
} |
|
180 |
|
|
181 |
} |
|
182 |
return vector; |
|
183 |
} |
|
184 |
|
|
185 |
} |
|
0 | 186 |
tmp/org.txm.r/src/org/txm/stat/utils/PrintArray.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.utils; |
|
29 |
|
|
30 |
import java.util.Arrays; |
|
31 |
|
|
32 |
// TODO: Auto-generated Javadoc |
|
33 |
/** |
|
34 |
* Various utilities method for printing arrays. |
|
35 |
* |
|
36 |
* @author sloiseau |
|
37 |
* |
|
38 |
*/ |
|
39 |
public final class PrintArray { |
|
40 |
|
|
41 |
/** |
|
42 |
* Double array. |
|
43 |
* |
|
44 |
* @param matrix the matrix |
|
45 |
* @return the string |
|
46 |
*/ |
|
47 |
public final static String doubleArray(double[][] matrix) { |
|
48 |
// DoubleMatrix2D m = new SparseDoubleMatrix2D(matrix); |
|
49 |
// return m.toString(); |
|
50 |
StringBuffer sb = new StringBuffer(); |
|
51 |
for (int i = 0; i < matrix.length; i++) { |
|
52 |
sb.append(Arrays.toString(matrix[i])); |
|
53 |
} |
|
54 |
return sb.toString(); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Int array. |
|
59 |
* |
|
60 |
* @param matrix the matrix |
|
61 |
* @return the string |
|
62 |
*/ |
|
63 |
public final static String intArray(int[][] matrix) { |
|
64 |
double[][] doublematrix = new double[matrix.length][matrix[0].length]; |
|
65 |
for (int i = 0; i < matrix.length; i++) { |
|
66 |
for (int j = 0; j < doublematrix[i].length; j++) { |
|
67 |
doublematrix[i][j] = matrix[i][j]; |
|
68 |
} |
|
69 |
} |
|
70 |
return doubleArray(doublematrix); |
|
71 |
} |
|
72 |
|
|
73 |
} |
|
0 | 74 |
tmp/org.txm.r/src/org/txm/stat/utils/CheckArray.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.utils; |
|
29 |
|
|
30 |
// TODO: Auto-generated Javadoc |
|
31 |
/** |
|
32 |
* Various utility methods for checking consistency of matrix as java array of |
|
33 |
* arrays. |
|
34 |
* |
|
35 |
* @author sloiseau |
|
36 |
* |
|
37 |
*/ |
|
38 |
public final class CheckArray { |
|
39 |
|
|
40 |
/** |
|
41 |
* Check if an array is not null (or 0-length), does not include null inner |
|
42 |
* array (or 0 length), and if all inner arrays have the same length. |
|
43 |
* |
|
44 |
* @param array the array |
|
45 |
* @return <code>true</code> if the condition are satisfied, |
|
46 |
* <code>false</code> otherwise. |
|
47 |
*/ |
|
48 |
public final static boolean checkMatrixRepresentation(int[][] array) { |
|
49 |
// same code as in #checkMatrixRepresentation(double[][]) |
|
50 |
if (array == null) |
|
51 |
return false; |
|
52 |
int length = array.length; |
|
53 |
if (length < 1) |
|
54 |
return false; |
|
55 |
if (array[0] == null) |
|
56 |
return false; |
|
57 |
int firstElementLength = array[0].length; |
|
58 |
if (firstElementLength == 0) |
|
59 |
return false; |
|
60 |
for (int i = 0; i < array.length; i++) { |
|
61 |
if (array[i] == null) |
|
62 |
return false; |
|
63 |
if (array[i].length != firstElementLength) |
|
64 |
return false; |
|
65 |
} |
|
66 |
return true; |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* Check if an array is not null (or 0-length), does not include null inner |
|
71 |
* array (or 0 length), and if all inner arrays have the same length. |
|
72 |
* |
|
73 |
* @param array the array |
|
74 |
* @return <code>true</code> if the condition are satisfied, |
|
75 |
* <code>false</code> otherwise. |
|
76 |
*/ |
|
77 |
public final static boolean checkMatrixRepresentation(double[][] array) { |
|
78 |
// same code as in #checkMatrixRepresentation(int[][]) |
|
79 |
if (array == null) |
|
80 |
return false; |
|
81 |
int length = array.length; |
|
82 |
if (length < 1) |
|
83 |
return false; |
|
84 |
if (array[0] == null) |
|
85 |
return false; |
|
86 |
int firstElementLength = array[0].length; |
|
87 |
if (firstElementLength == 0) |
|
88 |
return false; |
|
89 |
for (int i = 0; i < array.length; i++) { |
|
90 |
if (array[i] == null) |
|
91 |
return false; |
|
92 |
if (array[i].length != firstElementLength) |
|
93 |
return false; |
|
94 |
} |
|
95 |
return true; |
|
96 |
} |
|
97 |
|
|
98 |
} |
|
0 | 99 |
tmp/org.txm.r/src/org/txm/stat/utils/package.html (revision 145) | ||
---|---|---|
1 |
<html> |
|
2 |
<body> |
|
3 |
<p>Utility functions for dealing with array (vectorizing, indexing, checking consistency...).</p> |
|
4 |
</body> |
|
5 |
</html> |
|
0 | 6 |
tmp/org.txm.r/src/org/txm/stat/ConnectToRserve.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate:$ |
|
25 |
// $LastChangedRevision:$ |
|
26 |
// $LastChangedBy:$ |
|
27 |
// |
|
28 |
package org.txm.stat; |
|
29 |
import org.rosuda.REngine.REXP; |
|
30 |
import org.rosuda.REngine.RList; |
|
31 |
import org.rosuda.REngine.Rserve.RConnection; |
|
32 |
|
|
33 |
// TODO: Auto-generated Javadoc |
|
34 |
/** |
|
35 |
* The Class ConnectToRserve. |
|
36 |
*/ |
|
37 |
class ConnectToRserve { |
|
38 |
|
|
39 |
/** |
|
40 |
* The main method. |
|
41 |
* |
|
42 |
* @param args the arguments |
|
43 |
*/ |
|
44 |
public static void main(String args[]) { |
|
45 |
try { |
|
46 |
RConnection c = new RConnection(); |
|
47 |
String userhome = System.getProperty("user.home"); //$NON-NLS-1$ |
|
48 |
RList ret = c.eval("source('"+userhome+"/Bureau/R/Rdata')").asList(); //$NON-NLS-1$ //$NON-NLS-2$ |
|
49 |
|
|
50 |
System.out.println("source ret : "+ret.get(1)); //$NON-NLS-1$ |
|
51 |
System.out.println(ret); |
|
52 |
|
|
53 |
REXP ret2 = c.eval("print(lexicaltable)"); //$NON-NLS-1$ |
|
54 |
System.out.println("lexicaltable :"+ret2); //$NON-NLS-1$ |
|
55 |
} |
|
56 |
catch(Exception e) { |
|
57 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
0 | 61 |
tmp/org.txm.r/src/org/txm/stat/engine/r/RConstant.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (Mon, 06 May 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.engine.r; |
|
29 |
|
|
30 |
// TODO: Auto-generated Javadoc |
|
31 |
/** |
|
32 |
* The Class RConstant. |
|
33 |
*/ |
|
34 |
public class RConstant { |
|
35 |
|
|
36 |
/** The Constant LIST_EXTRACTOR. */ |
|
37 |
public final static String LIST_EXTRACTOR = "$"; //$NON-NLS-1$ |
|
38 |
|
|
39 |
/** The Constant OPEN_PAREN. */ |
|
40 |
public final static String OPEN_PAREN = "("; //$NON-NLS-1$ |
|
41 |
|
|
42 |
/** The Constant CLOSE_PAREN. */ |
|
43 |
public final static String CLOSE_PAREN = ")"; //$NON-NLS-1$ |
|
44 |
|
|
45 |
/** The Constant AFFECTATION. */ |
|
46 |
public final static String AFFECTATION = "<-"; //$NON-NLS-1$ |
|
47 |
} |
|
0 | 48 |
tmp/org.txm.r/src/org/txm/stat/engine/r/package.html (revision 145) | ||
---|---|---|
1 |
<html> |
|
2 |
<body> |
|
3 |
<p>Root package of the implementation of a stat engine using the R piece of software (<a href="http://www.r-project.org">http://www.r-project.org</a>.</p> |
|
4 |
</body> |
|
5 |
</html> |
|
0 | 6 |
tmp/org.txm.r/src/org/txm/stat/engine/r/RWorkspaceException.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (Mon, 06 May 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.engine.r; |
|
29 |
|
|
30 |
import org.txm.stat.StatException; |
|
31 |
|
|
32 |
// TODO: Auto-generated Javadoc |
|
33 |
/** |
|
34 |
* Super-class of all exception related to the R engine. |
|
35 |
* |
|
36 |
* @author sloiseau |
|
37 |
*/ |
|
38 |
public class RWorkspaceException extends StatException { |
|
39 |
|
|
40 |
/** The Constant serialVersionUID. */ |
|
41 |
private static final long serialVersionUID = 1630828823146927953L; |
|
42 |
|
|
43 |
/** |
|
44 |
* Instantiates a new r workspace exception. |
|
45 |
*/ |
|
46 |
public RWorkspaceException() { |
|
47 |
super(); |
|
48 |
// TODO Auto-generated constructor stub |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Instantiates a new r workspace exception. |
|
53 |
* |
|
54 |
* @param arg0 the arg0 |
|
55 |
* @param arg1 the arg1 |
|
56 |
*/ |
|
57 |
public RWorkspaceException(String arg0, Throwable arg1) { |
|
58 |
super(arg0, arg1); |
|
59 |
// TODO Auto-generated constructor stub |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* Instantiates a new r workspace exception. |
|
64 |
* |
|
65 |
* @param arg0 the arg0 |
|
66 |
*/ |
|
67 |
public RWorkspaceException(String arg0) { |
|
68 |
super(arg0); |
|
69 |
// TODO Auto-generated constructor stub |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* Instantiates a new r workspace exception. |
|
74 |
* |
|
75 |
* @param arg0 the arg0 |
|
76 |
*/ |
|
77 |
public RWorkspaceException(Throwable arg0) { |
|
78 |
super(arg0); |
|
79 |
// TODO Auto-generated constructor stub |
|
80 |
} |
|
81 |
|
|
82 |
} |
|
0 | 83 |
tmp/org.txm.r/src/org/txm/stat/engine/r/RException.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (Mon, 06 May 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.engine.r; |
|
29 |
|
|
30 |
import org.txm.Messages; |
|
31 |
|
|
32 |
// TODO: Auto-generated Javadoc |
|
33 |
/** |
|
34 |
* An exception indicating an error during the evaluation of an expression by |
|
35 |
* the R engine. |
|
36 |
* |
|
37 |
* @author sloiseau |
|
38 |
*/ |
|
39 |
public class RException extends RWorkspaceException { |
|
40 |
|
|
41 |
/** The Constant serialVersionUID. */ |
|
42 |
private static final long serialVersionUID = -3459835604570896478L; |
|
43 |
|
|
44 |
/** |
|
45 |
* Instantiates a new r exception. |
|
46 |
*/ |
|
47 |
public RException() { |
|
48 |
super(); |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Instantiates a new r exception. |
|
53 |
* |
|
54 |
* @param msg the msg |
|
55 |
*/ |
|
56 |
public RException(String msg) { |
|
57 |
super(msg); |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* Instantiates a new r exception. |
|
62 |
* |
|
63 |
* @param expr the expr |
|
64 |
* @param msg the msg |
|
65 |
*/ |
|
66 |
public RException(String expr, String msg) { |
|
67 |
super(Messages.RException_0 + msg + Messages.RException_1 + Messages.RException_2 + expr); |
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* Instantiates a new r exception. |
|
72 |
* |
|
73 |
* @param arg0 the arg0 |
|
74 |
* @param arg1 the arg1 |
|
75 |
*/ |
|
76 |
public RException(String arg0, Throwable arg1) { |
|
77 |
super(arg0, arg1); |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Instantiates a new r exception. |
|
82 |
* |
|
83 |
* @param arg0 the arg0 |
|
84 |
*/ |
|
85 |
public RException(Throwable arg0) { |
|
86 |
super(arg0); |
|
87 |
} |
|
88 |
|
|
89 |
} |
|
0 | 90 |
tmp/org.txm.r/src/org/txm/stat/engine/r/RWorkspace.java (revision 145) | ||
---|---|---|
1 |
// Copyright © 2010-2013 ENS de Lyon. |
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of |
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice |
|
4 |
// Sophia Antipolis, University of Paris 3. |
|
5 |
// |
|
6 |
// The TXM platform is free software: you can redistribute it |
|
7 |
// and/or modify it under the terms of the GNU General Public |
|
8 |
// License as published by the Free Software Foundation, |
|
9 |
// either version 2 of the License, or (at your option) any |
|
10 |
// later version. |
|
11 |
// |
|
12 |
// The TXM platform is distributed in the hope that it will be |
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied |
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
15 |
// PURPOSE. See the GNU General Public License for more |
|
16 |
// details. |
|
17 |
// |
|
18 |
// You should have received a copy of the GNU General |
|
19 |
// Public License along with the TXM platform. If not, see |
|
20 |
// http://www.gnu.org/licenses. |
|
21 |
// |
|
22 |
// |
|
23 |
// |
|
24 |
// $LastChangedDate: 2015-06-15 15:29:16 +0200 (Mon, 15 Jun 2015) $ |
|
25 |
// $LastChangedRevision: 2989 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.stat.engine.r; |
|
29 |
|
|
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.File; |
|
32 |
import java.io.FileReader; |
|
33 |
import java.io.FileWriter; |
|
34 |
import java.io.IOException; |
|
35 |
import java.sql.Connection; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.Arrays; |
|
38 |
import java.util.UUID; |
|
39 |
|
|
40 |
import org.apache.commons.lang.StringEscapeUtils; |
|
41 |
import org.apache.commons.lang.StringUtils; |
|
42 |
import org.eclipse.core.runtime.Platform; |
|
43 |
import org.eclipse.osgi.util.NLS; |
|
44 |
import org.eclipse.ui.internal.util.BundleUtility; |
|
45 |
import org.osgi.framework.Bundle; |
|
46 |
import org.rosuda.REngine.REXP; |
|
47 |
import org.rosuda.REngine.REXPLogical; |
|
48 |
import org.rosuda.REngine.REXPMismatchException; |
|
49 |
import org.rosuda.REngine.REngineException; |
|
50 |
import org.rosuda.REngine.Rserve.RConnection; |
|
51 |
import org.rosuda.REngine.Rserve.RserveException; |
|
52 |
import org.txm.Messages; |
|
53 |
import org.txm.stat.StatException; |
|
54 |
import org.txm.stat.data.QuantitativeDataStructure; |
|
55 |
import org.txm.stat.engine.r.data.RObjectAlreadyExist; |
|
56 |
import org.txm.stat.engine.r.rcolt.RColt; |
|
57 |
import org.txm.stat.utils.VectorizeArray; |
|
58 |
import org.txm.utils.OSDetector; |
|
59 |
import org.txm.utils.StreamHog; |
|
60 |
import org.txm.utils.SystemProxyDetector; |
|
61 |
import org.txm.utils.logger.Log; |
|
62 |
|
|
63 |
import cern.colt.list.DoubleArrayList; |
|
64 |
import cern.colt.list.IntArrayList; |
|
65 |
import cern.colt.matrix.DoubleMatrix2D; |
|
66 |
|
|
67 |
// TODO: Auto-generated Javadoc |
|
68 |
/** |
|
69 |
* RWorkspace is the wrapper on top of the R Engine. All data sent to and |
|
70 |
* retreive from R are using this class. One instance of this class must be |
|
71 |
* created in a program. |
|
72 |
* |
|
73 |
* This class throw exception RWorkspace exception, encapsulating lower level |
|
74 |
* exception such as <code>RserveException</code>, |
|
75 |
* <code>REXPMismatchException</code> or <code>REngineException</code>. |
|
76 |
* |
|
77 |
* This class offers many methods avoiding other classes in the toolbox to |
|
78 |
* contain R code. The goal is to concentrate R code, as much as possible, here. |
|
79 |
* See {@link #extractItemFromListByName(String, String)} for instance, and |
|
80 |
* other "extract..." methods. |
|
81 |
* |
|
82 |
* methods in this class may be organized in the following group: |
|
83 |
* <ul> |
|
84 |
* <li>1/ initialization, connection to R, shutdown, etc.</li> |
|
85 |
* <li>2/ adding object to the R workspace (methods prefixed with "add").</li> |
|
86 |
* <li>3/ methods encapsulating R code for common fonctions: methods prefixed |
|
87 |
* with "assign...", "extract..." for instance.</li> |
|
88 |
* <li>4/ methods for evaluating R expression ({@link #safeEval(String)}, |
|
89 |
* {@link #eval(String)}, {@link #evalToDouble(String)}, etc.)</li> |
|
90 |
* <li>5/ Static methods for converting result of evaluation in type ( |
|
91 |
* {@link #toDouble(REXP)}, etc.)</li> |
|
92 |
* <li>6/ methods for checking existence of R object in the R workspace, |
|
93 |
* removing object from the R workspace, etc. |
|
94 |
* {@link #checkForDuplicateVariable(String)}, {@link #clearWorkspace()}, |
|
95 |
* {@link #containsVariable(String)}, |
|
96 |
* {@link #removeVariableFromWorkspace(String)}, etc.)</li> |
|
97 |
* <li>7/ methods for plotting.</li> |
|
98 |
* </ul> |
|
99 |
* |
|
100 |
* TODO : assign/remove... in a textometrie environment, when Rengine.assign() |
|
101 |
* will be able to deal with R environment. |
|
102 |
* |
|
103 |
* TODO : remove unused variables from workspace. |
|
104 |
* |
|
105 |
* @author Sylvain Loiseau <sloiseau@ens-lsh.fr> |
|
106 |
*/ |
|
107 |
|
|
108 |
public final class RWorkspace { |
|
109 |
|
|
110 |
/** The comm. */ |
|
111 |
private static RFileCommunication comm; |
|
112 |
|
|
113 |
/** The connection. */ |
|
114 |
private static RConnection connection = null; |
|
115 |
|
|
116 |
/** The eval logs. */ |
|
117 |
static ArrayList<String> evalLogs = new ArrayList<String>(); |
|
118 |
|
|
119 |
/** The filecommunication. */ |
|
120 |
private static boolean filecommunication = false; |
|
121 |
|
|
122 |
// -------------------------------------- |
|
123 |
/** The last safeeval expr. */ |
|
124 |
static String lastSafeevalExpr = ""; //$NON-NLS-1$ |
|
125 |
|
|
126 |
/** The Rserve process. */ |
|
127 |
private static Process RserveProcess = null; |
|
128 |
|
|
129 |
/** The workspace. */ |
|
130 |
private static RWorkspace workspace = null; |
|
131 |
|
|
132 |
/** The error logger. */ |
|
133 |
private StreamHog errorLogger; |
|
134 |
|
|
135 |
/** The input logger. */ |
|
136 |
private StreamHog inputLogger; |
|
137 |
|
|
138 |
/** The loggin. */ |
|
139 |
private boolean logging; |
|
140 |
|
|
141 |
/** The DEFAUL t_ symbol. */ |
|
142 |
//private String DEFAULT_SYMBOL = "txmresult"; //$NON-NLS-1$ |
|
143 |
|
|
144 |
/** |
|
145 |
* Folder where to create the R working directory |
|
146 |
*/ |
|
147 |
protected File userdir = null; |
|
148 |
|
|
149 |
/** |
|
150 |
* Appendto eval log. |
|
151 |
* |
|
152 |
* @param cmd the cmd |
|
153 |
*/ |
|
154 |
public static void appendtoEvalLog(String cmd) |
|
155 |
{ |
|
156 |
evalLogs.add(cmd); |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* Close the connection. |
|
161 |
* |
|
162 |
* @throws RWorkspaceException the r workspace exception |
|
163 |
*/ |
|
164 |
public static final void closeConnection() throws RWorkspaceException { |
|
165 |
// If no R object have been created during the lif ecycle of the |
|
166 |
// application, |
|
167 |
// the connection has never been initialized. |
|
168 |
if (connection != null) |
|
169 |
connection.close(); |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* Connect to Rserve. |
|
174 |
* |
|
175 |
* @param host the host |
|
176 |
* @param port the port |
|
177 |
* @return true if success |
|
178 |
* @throws RWorkspaceException the r workspace exception |
|
179 |
*/ |
|
180 |
public static boolean connect(String host, int port) |
|
181 |
throws RWorkspaceException { |
|
182 |
try { |
|
183 |
Log.info(Messages.RWorkspace_2+host+":"+port); //$NON-NLS-1$ |
|
184 |
connection = new RConnection(host, port); |
|
185 |
//connection.setStringEncoding("UTF-8"); |
|
186 |
|
|
187 |
return isConnected(); |
|
188 |
} catch (RserveException x) { |
|
189 |
throw new RWorkspaceException(x); |
|
190 |
} |
|
191 |
|
|
192 |
} |
|
193 |
|
|
194 |
/** |
|
195 |
* Connect to Rserve. |
|
196 |
* |
|
197 |
* @param host the host |
|
198 |
* @param port the port |
|
199 |
* @param user the user |
|
200 |
* @param password the password |
|
201 |
* @return true, if successful |
|
202 |
*/ |
|
203 |
public static boolean connect(String host, int port, String user, |
|
204 |
String password) { |
|
205 |
try { |
|
206 |
Log.info(Messages.RWorkspace_2+host+":"+port+" user :"+user); //$NON-NLS-2$ //$NON-NLS-1$ |
|
207 |
connection = new RConnection(host, port); |
|
208 |
connection.login(user, password); |
|
209 |
return isConnected(); |
|
210 |
|
|
211 |
} catch (RserveException x) { |
|
212 |
System.out.println(Messages.RWorkspace_0 + host+ ", " + port + ", " + user); //$NON-NLS-1$//$NON-NLS-2$ |
|
213 |
Log.severe(Messages.RWorkspace_0 + host+ ", " + port + ", " + user); //$NON-NLS-1$//$NON-NLS-2$ |
|
214 |
} |
|
215 |
return false; |
|
216 |
} |
|
217 |
|
|
218 |
/** |
|
219 |
* Gets the last safe eval expr. |
|
220 |
* |
|
221 |
* @return the last safe eval expr |
|
222 |
*/ |
|
223 |
public static String getLastSafeEvalExpr() |
|
224 |
{ |
|
225 |
return lastSafeevalExpr; |
|
226 |
} |
|
227 |
|
|
228 |
|
|
229 |
public static String getRandomSymbol() { |
|
230 |
String symbol = "Random"+UUID.randomUUID(); //$NON-NLS-1$ |
|
231 |
return symbol; |
|
232 |
} |
|
233 |
|
|
234 |
/** |
|
235 |
* Entering the R workspace. |
|
236 |
* |
|
237 |
* @param |
|
238 |
* @return the r workspace instance |
|
239 |
* @throws RWorkspaceException the r workspace exception |
|
240 |
*/ |
|
241 |
public static final RWorkspace getRWorkspaceInstance() |
|
242 |
throws RWorkspaceException { |
|
243 |
Bundle bundle = Platform.getBundle("org.txm.r"); |
|
244 |
File userdir = new File(bundle.getLocation()); |
|
245 |
System.out.println("BUNDLE LOCATION OF 'org.txm.r': "+userdir); |
|
246 |
if (workspace == null) { |
|
247 |
workspace = new RWorkspace(userdir); |
|
248 |
} |
|
249 |
return workspace; |
|
250 |
} |
|
251 |
|
|
252 |
/** |
|
253 |
* Post initialisation of Rserve : loads libraries. |
|
254 |
* @deprecated The extensions will load themself the packages they need |
|
255 |
* @throws RWorkspaceException the r workspace exception |
|
256 |
*/ |
|
257 |
private static void init() throws RWorkspaceException { |
|
258 |
String[] packagesToLoad = { }; //$NON-NLS-1$ |
|
259 |
for (int i = 0; i < packagesToLoad.length; i++) { |
|
260 |
String name = packagesToLoad[i]; |
|
261 |
try { |
|
262 |
// System.out.println("load R lib : " + name); |
|
263 |
connection.eval(("library(" + name + ")")); //$NON-NLS-1$ //$NON-NLS-2$ |
|
264 |
// System.out.println("Done : load R lib : " + name); |
|
265 |
} catch (RserveException e) { |
|
266 |
throw new RWorkspaceException(NLS.bind(Messages.LIBBRARY_NOT_LOADED, name, e)); |
|
267 |
} catch (Exception e) { |
|
268 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
|
|
273 |
/** |
|
274 |
* Default initialisation of RWorkspace with defautl parameters : |
|
275 |
* address = 127.0.0.1 |
|
276 |
* port = 6311 |
|
277 |
* debug = false. |
|
278 |
* |
|
279 |
* @param pathToRExecutalbe the path to r executalbe |
|
280 |
* @throws RWorkspaceException the r workspace exception |
|
281 |
*/ |
|
282 |
public static final void initializeRWorkspace(String pathToRExecutalbe) |
|
283 |
throws RWorkspaceException { |
|
284 |
workspace = new RWorkspace(pathToRExecutalbe, "127.0.0.1", 6311, false); //$NON-NLS-1$ |
|
285 |
} |
|
286 |
|
|
287 |
/** |
|
288 |
* Initialize r workspace. |
|
289 |
* debug = false |
|
290 |
* |
|
291 |
* @param pathToRExecutalbe the path to r executalbe |
|
292 |
* @param host the host |
|
293 |
* @param port the port |
|
294 |
* @throws RWorkspaceException the r workspace exception |
|
295 |
*/ |
|
296 |
public static final void initializeRWorkspace(String pathToRExecutalbe, |
|
297 |
String host, int port) throws RWorkspaceException { |
|
298 |
workspace = new RWorkspace(pathToRExecutalbe, host, port, false); |
|
299 |
} |
|
300 |
|
|
301 |
// -------------------------------------- |
|
302 |
/** |
|
303 |
* check R path |
|
304 |
* check if Rserve has already been launched |
|
305 |
* if not try to launch it. |
|
306 |
* |
|
307 |
* @param pathToRExecutable the path to r executable |
|
308 |
* @param options |
|
309 |
* @param rServeArgs |
|
310 |
* @return true if Rserve is launched |
|
311 |
* @throws RWorkspaceException the r workspace exception |
|
312 |
*/ |
|
313 |
private static boolean initRserve(String pathToRExecutable, int port, boolean debug, String rargs, String rServeArgs) |
|
314 |
throws RWorkspaceException { |
|
315 |
boolean isRServerOk; |
|
316 |
if (pathToRExecutable == null || pathToRExecutable.length() == 0) { |
|
317 |
isRServerOk = StartRserve.checkLocalRserve(port, debug, rargs, rServeArgs); |
|
318 |
if (!isRServerOk) { |
|
319 |
throw new RWorkspaceException( |
|
320 |
Messages.RSERVE_PATH_NOT_SET_ERROR); |
|
321 |
} |
|
322 |
} else { |
|
323 |
Log.info(Messages.RWorkspace_10+pathToRExecutable); |
|
324 |
isRServerOk = StartRserve.launchRserve(pathToRExecutable, port, debug, rargs, rServeArgs); |
|
325 |
// System.out.println("ap launchRserve"); |
|
326 |
if (!isRServerOk) { |
|
327 |
throw new RWorkspaceException(Messages.RSERVE_PATH_SET_ERROR+ " : "+pathToRExecutable); //$NON-NLS-1$ |
|
328 |
} |
|
329 |
} |
|
330 |
|
|
331 |
if (isRServerOk) { |
|
332 |
Log.finest("RSERVE_ACTIVATED"); //$NON-NLS-1$ |
|
333 |
System.out.println(Messages.CONNECTED_TO_STATS_MODULE); |
|
334 |
RserveProcess = StartRserve.Rserveprocess; |
|
335 |
} |
|
336 |
return isRServerOk; |
|
337 |
} |
|
338 |
|
|
339 |
/** |
|
340 |
* Checks if is connected. |
|
341 |
* |
|
342 |
* @return true if the RWorkspace is connected to RServe |
|
343 |
*/ |
|
344 |
private static boolean isConnected() { |
|
345 |
if (!connection.isConnected()) { |
|
346 |
return false; |
|
347 |
} else { |
|
348 |
return true; |
|
349 |
} |
|
350 |
} |
|
351 |
|
|
352 |
|
|
353 |
/** |
|
354 |
* Checks if is file tranfert. |
|
355 |
* |
|
356 |
* @return true, if is file tranfert |
|
357 |
*/ |
|
358 |
public static boolean isFileTranfert() { |
|
359 |
return filecommunication; |
|
360 |
} |
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
/** |
|
365 |
* Load eval log. |
|
366 |
* |
|
367 |
* @param file the file |
|
368 |
* @throws IOException Signals that an I/O exception has occurred. |
|
369 |
*/ |
|
370 |
public static void loadEvalLog(File file) throws IOException |
|
371 |
{ |
|
372 |
BufferedReader reader = new BufferedReader(new FileReader(file)); |
|
373 |
String cmd = reader.readLine(); |
|
374 |
while(cmd != null) |
|
375 |
{ |
|
376 |
evalLogs.add(cmd); |
|
377 |
cmd = reader.readLine(); |
|
378 |
} |
|
379 |
reader.close(); |
|
380 |
} |
|
381 |
|
|
382 |
/** |
|
383 |
* Prints the last safe eval expr. |
|
384 |
*/ |
|
385 |
public static void printLastSafeEvalExpr() { |
|
386 |
System.out.println(Messages.RWorkspace_11+lastSafeevalExpr); |
|
387 |
} |
|
388 |
|
|
389 |
/** |
|
390 |
* Save eval log. |
|
391 |
* |
|
392 |
* @param file the file |
|
393 |
* @throws IOException Signals that an I/O exception has occurred. |
|
394 |
*/ |
|
395 |
public static void saveEvalLog(File file) throws IOException { |
|
396 |
FileWriter writer = new FileWriter(file); |
|
397 |
for (String cmd : evalLogs) |
|
398 |
writer.write(cmd+"\n"); //$NON-NLS-1$ |
|
399 |
writer.flush(); |
|
400 |
writer.close(); |
|
401 |
} |
|
402 |
|
|
403 |
/** |
|
404 |
* if state is true, the matrix will be send to R with a File. |
|
405 |
* |
|
406 |
* @param state the state |
|
407 |
* @return true if success |
|
408 |
*/ |
|
409 |
public static boolean setUseFileCommunication(boolean state) { |
|
410 |
filecommunication = state; |
|
411 |
if (filecommunication) { |
|
412 |
try { |
|
413 |
comm = new RFileCommunication(); |
|
414 |
} catch (IOException e) { |
|
415 |
filecommunication = false; |
|
416 |
Log.severe(Messages.RWorkspace_6+Log.toString(e)); |
|
417 |
return false; |
|
418 |
} |
|
419 |
} |
|
420 |
return true; |
|
421 |
} |
|
422 |
|
|
423 |
/** |
|
424 |
* Close the connection and destroy the Rserve process. |
|
425 |
* |
|
426 |
* @throws RWorkspaceException the r workspace exception |
|
427 |
*/ |
|
428 |
public static final void shutdown() throws RWorkspaceException { |
|
429 |
try { |
|
430 |
// If no R object have been created during the lif ecycle of the |
|
431 |
// application, |
|
432 |
// the connection has never been initialized. |
|
433 |
|
|
434 |
if (connection != null) { |
|
435 |
if (connection.isConnected()) { |
|
436 |
// connection.close(); // No: invoquing "close()" make |
|
437 |
// "shutdown()" to throw a "not connected" exception. |
|
438 |
connection.shutdown(); |
|
439 |
connection = null; // warning, after shuting down the |
|
440 |
// server, the connection appears still |
|
441 |
// ok but using it yield to a |
|
442 |
// "Broken pipe" exception. |
|
443 |
} |
|
444 |
} |
|
445 |
if (RserveProcess != null) RserveProcess.destroy(); |
|
446 |
} catch (RserveException e) { |
|
447 |
throw new RWorkspaceException(e); |
|
448 |
} |
|
449 |
} |
|
450 |
|
|
451 |
/** |
|
452 |
* Start exec. |
|
453 |
* |
|
454 |
* @param pathToRExecutable the path to r executable |
|
455 |
* @param string |
|
456 |
* @param string |
|
457 |
* @return true, if successful |
|
458 |
*/ |
|
459 |
public static boolean startExec(String pathToRExecutable, int port, boolean debug, String rArgs, String rServeArgs) { |
|
460 |
// System.out.println("Start Exec"); |
|
461 |
try { |
|
462 |
initRserve(pathToRExecutable, port, debug, rArgs, rServeArgs); |
|
463 |
return true; |
|
464 |
} catch (RWorkspaceException e) { |
|
465 |
System.out.println(Messages.RWorkspace_8+e); |
|
466 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
467 |
} |
|
468 |
return false; |
|
469 |
} |
|
470 |
|
|
471 |
/** |
|
472 |
* To double. |
|
473 |
* |
|
474 |
* @param rexp the rexp |
|
475 |
* @return the double[] |
|
476 |
* @throws RWorkspaceException the r workspace exception |
|
477 |
*/ |
|
478 |
public static final double[] toDouble(REXP rexp) throws RWorkspaceException { |
|
479 |
try { |
|
480 |
return rexp.asDoubles(); |
|
481 |
} catch (REXPMismatchException e) { |
|
482 |
throw new RWorkspaceException(e); |
|
483 |
} |
|
484 |
} |
|
485 |
|
|
486 |
/** |
|
487 |
* protected on purpose. See {@link #getRWorkspaceInstance()}. |
|
488 |
* |
|
489 |
* @throws RWorkspaceException the r workspace exception |
|
490 |
*/ |
|
491 |
public RWorkspace(File userdir) throws RWorkspaceException { |
|
492 |
this.userdir = userdir; |
|
493 |
} |
|
494 |
|
|
495 |
// -------------------------------------- |
|
496 |
|
|
497 |
// /** |
|
498 |
// * protected on purpose. See {@link #getRWorkspaceInstance()}. |
|
499 |
// * |
|
500 |
// * @param pathToRExecutable the path to r executable |
|
501 |
// * @throws RWorkspaceException the r workspace exception |
|
502 |
// */ |
|
503 |
// protected RWorkspace(String pathToRExecutable, int port, boolean debug) throws RWorkspaceException { |
|
504 |
// if (initRserve(pathToRExecutable, port, debug, "","")) { |
|
505 |
// connect(); |
|
506 |
// init(); |
|
507 |
// } |
|
508 |
// } |
|
509 |
|
|
510 |
/** |
|
511 |
* protected on purpose. See {@link #getRWorkspaceInstance()}. |
|
512 |
* |
|
513 |
* @param pathToRExecutable the path to r executable |
|
514 |
* @param host the host |
|
515 |
* @param port the port |
|
516 |
* @throws RWorkspaceException the r workspace exception |
|
517 |
*/ |
|
518 |
protected RWorkspace(String pathToRExecutable, String host, int port, boolean debug) |
|
519 |
throws RWorkspaceException { |
|
520 |
if (initRserve(pathToRExecutable, port, debug, "", "")) { |
|
521 |
connect(host, port); |
|
522 |
init(); |
|
523 |
} |
|
524 |
} |
|
525 |
|
|
526 |
/** |
|
527 |
* Add a matrix into the workspace and link it to a name. The inner arrays |
|
528 |
* of the <code>matrix</code> parameters are the <strong>row</strong> of the |
|
529 |
* resulting R matrix. |
|
530 |
* |
|
531 |
* @param variableName the name to be used in the R workspace. |
|
532 |
* @param matrix the data to be bound to the name. In the form |
|
533 |
* <code>matrix[row][column]</code>, with exactly the same number |
|
534 |
* of column in every row. |
Formats disponibles : Unified diff