1 package eu.simuline.octave;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5
6 import java.util.regex.Pattern;
7
8 import eu.simuline.octave.type.OctaveCell;
9 import eu.simuline.octave.type.OctaveString;
10
11
12
13
14
15
16
17 public final class OctaveUtils {
18
19
20 private OctaveUtils() {
21 throw new UnsupportedOperationException("Do not instantiate");
22 }
23
24
25
26
27
28
29
30
31
32
33
34
35 public static Collection<String> listVars(final OctaveEngine octave) {
36
37
38 String script = octave.getOctaveVersion().startsWith("3.0.") ? "ans=whos -v()" : "ans=whos()";
39 octave.eval(script);
40
41 octave.eval("{ans.name}");
42 OctaveCell cell = octave.get(OctaveCell.class, "ans");
43
44 int len = cell.dataSize();
45 Collection<String> collection = new HashSet<String>();
46
47 for (int idx = 0; idx < len; idx++) {
48 collection.add(cell.get(OctaveString.class, 1, idx+1).getString());
49 }
50 collection.removeIf(p -> "__nargin__".equals(p));
51 collection.removeIf(p -> "ans".equals(p));
52
53 Pattern pattern = Pattern.compile("javaoctave_[0-9a-f]{12}_eval");
54 collection.removeIf(p -> pattern.matcher(p).matches());
55 return collection;
56 }
57
58 }