sav.z
Class Variable

java.lang.Object
  extended bysav.z.ClassNavigation
      extended bysav.z.Variable

public class Variable
extends ClassNavigation

The Variable class is main instrument for interface Java with Zigzag. Variable allows to manipulate and navigate through Zigzag objects, to convert their into Java String, array or Vectors. Variable is a simple mechanism to pass Zigzag objects, having "name", "name:value" or "name:...value" views from Java code into Zigzag scripts. To create Variable object, the var() method of Session is used.

Look at the following Java/Zigzag fragment, which creates a "table" with employee key column. It needs to understand next examples, primarily possible values of the Variables.

  String script =
    "$readTable() <" +
    "employee; name;           department; salary \n" +
    "101;      'Adams Smith';  1;          50000 \n" +
    "102;      'Clark White';  1;          30000 \n" +
    "103;      'Jones Hunter'; 2;          60000 \n" +
    "104;      'Blake Dull';   3;          65000 \n" +
    "105;      'Sylvia Smith'; 1;          25000 \n" +
    "108;      'Albert Green'; 3;          40000 \n" +
    "109;      'Clark Ringer'; 2;          50000 \n" +
    ">;"
  ;
  Session ss = new Session();
  ss.modifyBase("CompanyX");
  ss.z(script);
 
We know database contents and can form the $x variable by means of Zigzag query $x = employee:(salary:$z^);. Values of $x are employees with salary $z or more. First attempt to set $z the 70000 is not successful. So we try to find employee with salary 30000, that is with $z = 30000. To control a failure result, we will use compare ss.z(...) == 0 and even ZException via ss.setFailExcepted(true).
  ss.exploreBase("CompanyX");
  Variable varz = ss.var("z");
  varz.set("70000");
  script = "$x = employee:(salary:$z^);";
  if (ss.z(script) == 0) {
    varz.set("30000");
    ss.setFailExcepted(true);
    ss.z(script);
    ss.setFailExcepted(false);
  }
  Variable varx = ss.var("x");
 
To check $x result we could use the varx.isEmpty() method. Values of $x and correspondently varx are objects: employee:101, employee:102, employee:103 and so on except employee:105. To get employee values like 101, 102, ... we had to use Zigzag statement $x = employee/(salary:$z^);.

Now let's select the first three objects of the $x variable into the $y variable.

  Variable vary = ss.var("y");
  int max = 3;
  int n = 0;
  for (String emp = varx.first(); emp != null; emp = varx.next(emp)) {
    vary.add(emp);
    if (++n == max)
      break;
  }
 
We can execute some acts with $y via Zigzag or with vary via Java code. For example, to print table of $y employees we can use following.
  Printer prt = new WPrinter(new PrintWriter(System.out));
  ss.setReportPrinter(prt);
  prt.println("--- 3 potential workers  ---");
  ss.z("$printTable($y)");
  prt.flush();
 

Since:
7.0
See Also:
Session.var(String)

Method Summary
 void add(java.lang.String value)
          Adds the specified value to this Variable.
 void add(java.lang.String[] array)
          Adds the specified array values to this Variable.
 void add(java.lang.String zclass, java.lang.String value)
          Adds the specified value to the Zigzag class with name zclass inside this Variable.
 void add(java.lang.String zclass, java.lang.String[] array)
          Adds the specified array values to the Zigzag class with name zclass inside this Variable.
 java.lang.String[] array()
          Returns a string array of this Variable values.
 java.lang.String back()
          Returns back value before current.
 java.lang.String back(java.lang.String value)
          Returns back value before parameter value.
 void clear()
          Removes all of the values from this Variable.
 void clear(java.lang.String zclass)
          Removes all the values of zclass Zigzag class inside this Variable.
 java.lang.String first()
          Returns the first value of this Variable.
 void generalizeTo(Variable var)
          Copy general values to var Zigzag Variable.
 boolean has(java.lang.String value)
          Returns true if Variable has the specified value.
 boolean hasNumber()
          Returns true if Variable has value of number type.
 boolean isEmpty()
          Tests if Variable has no value.
 java.lang.String last()
          Returns the last value of this Variable.
 java.lang.String name()
          Returns a Zigzag name of this Variable, which begins with $ symbol.
 java.lang.String next()
          Returns next value after current.
 java.lang.String next(java.lang.String value)
          Returns next value after parameter value.
 java.lang.String[] nextArray(java.lang.String value, int size)
          Returns a next size array of values after the value.
 void remove(java.lang.String value)
          Removes a single value from this Variable, if it is present.
 void remove(java.lang.String zclass, java.lang.String value)
          Removes the value of zclass Zigzag class inside this Variable.
 void set(java.lang.String value)
          Sets up the specified value to this Variable.
 void set(java.lang.String[] array)
          Sets the specified array of values to this Variable.
 int size()
          Returns number of Variable values.
 java.lang.String term()
          Returns one (first) non-quoted string value of this Variable.
 java.lang.String value()
          Returns one (first) string value of this Variable.
 java.util.Vector values()
          Returns a Vector of this Variable values.
 VariableExtension ve()
          Returns VariableExtension representing functional extension of the Variable.
 
Methods inherited from class sav.z.ClassNavigation
array, back, first, generalizeTo, has, hasNumber, isEmpty, last, next, nextArray, size, term, value, values
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

ve

public VariableExtension ve()
Returns VariableExtension representing functional extension of the Variable. The methods of the VariableExtension are specific and are not recomended to usual developer.

See Also:
VariableExtension

name

public java.lang.String name()
Returns a Zigzag name of this Variable, which begins with $ symbol. For example, name is $x for var made with Variable var = session.var("x");

See Also:
Session.var(String)

array

public java.lang.String[] array()
                         throws java.io.IOException
Returns a string array of this Variable values. See example:
   session.z("$x = User/");
   Variable var = session.var("$x");
   String users[] = var.array();
 

Throws:
java.io.IOException
See Also:
set(String), set(String[]), add(String), add(String[])

value

public java.lang.String value()
                       throws java.io.IOException
Returns one (first) string value of this Variable. See example:
   session.z("$x = User/");
   Variable var = session.var("$x");
   String userName = var.value();
 

Throws:
java.io.IOException
See Also:
set(String), term()

term

public java.lang.String term()
                      throws java.io.IOException
Returns one (first) non-quoted string value of this Variable. See example, for which the term is John (no 'John').
   session.z("$x = 'John':'Smit'");
   Variable var = session.var("$x");
   String term = var.term();
 

Throws:
java.io.IOException
See Also:
Name.toTerm(String), value()

values

public java.util.Vector values()
                        throws java.io.IOException
Returns a Vector of this Variable values. See example:
   session.z("$x = User/");
   Variable var = session.var("$x");
   Vector values = var.values();
 

Throws:
java.io.IOException
See Also:
set(String), set(String[]), add(String), add(String[])

size

public int size()
         throws java.io.IOException
Returns number of Variable values.

Throws:
java.io.IOException
See Also:
array()

first

public java.lang.String first()
                       throws java.io.IOException
Returns the first value of this Variable. All the Variable values are ordered in ascendant sequence. This method is like value().

Throws:
java.io.IOException
See Also:
next(), last()

last

public java.lang.String last()
                      throws java.io.IOException
Returns the last value of this Variable. Remind that all the Variable values are ordered in ascendant sequence.

Throws:
java.io.IOException
See Also:
back(), first()

next

public java.lang.String next(java.lang.String value)
                      throws java.io.IOException
Returns next value after parameter value.
  Variable varx = session.var("x");
  for (String s = varx.first(); s != null; s = varx.next(s)) {
    ...
    int i = Integer.parseInt(s);
    s = String.valueOf(++i);
  }
 

Throws:
java.io.IOException
See Also:
first(), next()

back

public java.lang.String back(java.lang.String value)
                      throws java.io.IOException
Returns back value before parameter value.
  Variable varx = session.var("x");
  for (String s = varx.last(); s != null; s = varx.back(s)) {
    ...
    int i = Integer.parseInt(s);
    s = String.valueOf(++i);
  }
 

Throws:
java.io.IOException
See Also:
last(), back()

next

public java.lang.String next()
                      throws java.io.IOException
Returns next value after current.
  Variable varx = session.var("x");
  for (String s = varx.first(); s != null; s = varx.next()) {
    ...
  }
 

Throws:
java.io.IOException
See Also:
first()

back

public java.lang.String back()
                      throws java.io.IOException
Returns back value before current.
  Variable varx = session.var("x");
  for (String s = varx.last(); s != null; s = varx.back()) {
    ...
  }
 

Throws:
java.io.IOException
See Also:
last()

nextArray

public java.lang.String[] nextArray(java.lang.String value,
                                    int size)
                             throws java.io.IOException
Returns a next size array of values after the value.
  Variable varx = session.var("x");
  int size = 10;
  String current = null;
  while (true) {
    String array[] = varx.nextArray(current, size);
    ...
    if (array.length < size)
      break;
    current = array[size - 1];
  }
 

Throws:
java.io.IOException
See Also:
next(String)

generalizeTo

public void generalizeTo(Variable var)
                  throws java.io.IOException
Copy general values to var Zigzag Variable. For example, if values are programmer and programmer:'John', the var will contain only programmer.

Throws:
java.io.IOException
See Also:
values()

has

public boolean has(java.lang.String value)
            throws java.io.IOException
Returns true if Variable has the specified value.

Throws:
java.io.IOException
See Also:
ClassNavigation.value(String)

hasNumber

public boolean hasNumber()
                  throws java.io.IOException
Returns true if Variable has value of number type. Note that value of any type is a Java String, like number "123", quote "'Lennon'" or term "John Lennon".

Throws:
java.io.IOException
See Also:
value(), has(String)

isEmpty

public boolean isEmpty()
                throws java.io.IOException
Tests if Variable has no value.

Throws:
java.io.IOException
See Also:
size()

add

public void add(java.lang.String value)
         throws java.io.IOException
Adds the specified value to this Variable.

Throws:
java.io.IOException
See Also:
ClassNavigation.value(String), ClassNavigation.values(String), set(String)

add

public void add(java.lang.String[] array)
         throws java.io.IOException
Adds the specified array values to this Variable. The example makes "data1" resource for "ann1" and "dan" users.
   String users[] = { "ann1", "dan" }
   variable.add(users);
 

Throws:
java.io.IOException
See Also:
add(String)

clear

public void clear()
           throws java.io.IOException
Removes all of the values from this Variable. This Variable will be empty after this method.

Throws:
java.io.IOException
See Also:
add(String[]), set(String[])

remove

public void remove(java.lang.String value)
            throws java.io.IOException
Removes a single value from this Variable, if it is present.

Throws:
java.io.IOException
See Also:
clear()

set

public void set(java.lang.String value)
         throws java.io.IOException
Sets up the specified value to this Variable. The setting means assigning a new value with removing all the old values.

Throws:
java.io.IOException
See Also:
add(String)

set

public void set(java.lang.String[] array)
         throws java.io.IOException
Sets the specified array of values to this Variable. The old values are removed. The example below assumes the $x equal to user names 'ann1', 'dan'. Then via User:$x= the User:['ann1','dan'] users are assigned to the database and $y variable. The users array will be equal { "User:'ann1', User:'dan' }.
   Variable varx = session.var("$x");
   String userNames[] = { "'ann1'", "'dan'" }
   varx.set(users);
   session.z($y = User:$x=");
   Variable vary = session.var("$y");
   String users[] = vary.array();
 

Throws:
java.io.IOException
See Also:
set(String)

add

public void add(java.lang.String zclass,
                java.lang.String value)
         throws java.io.IOException
Adds the specified value to the Zigzag class with name zclass inside this Variable.

Throws:
java.io.IOException
See Also:
add(String)

add

public void add(java.lang.String zclass,
                java.lang.String[] array)
         throws java.io.IOException
Adds the specified array values to the Zigzag class with name zclass inside this Variable. Really following example adds { "User:'ann1'", "User:'dan'" } to the varx Variable.
   String userNames[] = { "'ann1'", "'dan'" }
   Variable varx = session.var("$x");
   varx.add("User", userNames);
 

Throws:
java.io.IOException
See Also:
add(String, String)

clear

public void clear(java.lang.String zclass)
           throws java.io.IOException
Removes all the values of zclass Zigzag class inside this Variable.

Throws:
java.io.IOException
See Also:
add(String, String), add(String, String[])

remove

public void remove(java.lang.String zclass,
                   java.lang.String value)
            throws java.io.IOException
Removes the value of zclass Zigzag class inside this Variable.

Throws:
java.io.IOException
See Also:
clear(String), add(String, String), add(String, String[])