View Javadoc
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   * Small utility functions that can be used with JavaOctave.
13   * 
14   * Holder class for static functions.
15   * @deprecated
16   */
17  public final class OctaveUtils {
18  
19  
20      private OctaveUtils() {
21          throw new UnsupportedOperationException("Do not instantiate");
22      }
23  
24      /**
25       * Returns a collection of variables defined 
26       * excluding variables like {@link OctaveEngine#NARGIN} and {@link OctaveEngine#ANS} 
27       * but also those that are most likely to be created by this software. TBD: clarification 
28       * 
29       * @param octave
30       *    some octave engine. 
31       * @return collection of variables
32       * @deprecated
33       * use {@link OctaveEngine#getVarNames()} instead. 
34       */
35      public static Collection<String> listVars(final OctaveEngine octave) {
36  	// Justification: 3.0 are the earliest versions. 
37  	// all later ones don't use '-v' any more 
38  	String script = octave.getOctaveVersion().startsWith("3.0.") ? "ans=whos -v()" : "ans=whos()";
39  	octave.eval(script);
40  	// TBD: clarify: if we use who instead of whos, this can be simplified.  
41  	octave.eval("{ans.name}");
42  	OctaveCell cell = octave.get(OctaveCell.class, "ans");
43  	// TBD: this can be unified with OctaveEngine.getNamesOfPackagesInstalled()
44  	int len = cell.dataSize();
45  	Collection<String> collection = new HashSet<String>();
46  	//cell.getSize(i)
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  	// TBD: eliminate magic literal 
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  }