Source code: org/maloi/evolvo/expressiontree/Variable.java
1 /* Evolvo - Image Generator
2 * Copyright (C) 2000 Andrew Molloy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /**
20 * $Id: Variable.java,v 1.1.1.1 2002/10/04 15:39:32 maloi Exp $
21 */
22
23 package org.maloi.evolvo.expressiontree;
24
25 import java.io.Serializable;
26
27 import org.maloi.evolvo.expressiontree.vm.Instruction;
28 import org.maloi.evolvo.expressiontree.vm.Machine;
29
30 /**
31 * Terminal node for expressionTree that stores a variable.
32 */
33 public class Variable extends ExpressionTree implements Cloneable, Serializable
34 {
35 /** The variable's value. */
36 Value v;
37 /** The variable's name. */
38 String name;
39
40 int reg;
41
42 /** Class constructor, sets the value (from a double) and name for this
43 * variable.
44 */
45 public Variable(double p, String n)
46 {
47 v = new Value(p);
48 setName(n);
49 }
50
51 /** Class constructor, sets the value (from an value object) and name for
52 * this variable.
53 */
54 public Variable(Value p, String n)
55 {
56 v = p;
57 setName(n);
58 }
59
60 public Variable()
61 {
62 v = new Value(0.0);
63 setName("");
64 }
65
66 public ExpressionTree getClone()
67 {
68 // We don't actually need to deep clone variable's, so just return this
69 return this;
70 }
71
72 /** Returns the variable's name as a String. */
73 public String toString()
74 {
75 return name;
76 }
77
78 /** Sets the variables value from a double. */
79 public void setVariable(double p)
80 {
81 v.setValue(p);
82 }
83
84 /** Returns the number of parameters this node expects. */
85 public int getNumParams()
86 {
87 return 0;
88 }
89
90 /** Returns the parameters this node is holding. */
91 public ExpressionTree[] getParams()
92 {
93 return null;
94 }
95
96 public void setParams(ExpressionTree[] dummy)
97 {
98 }
99
100 /** Builds a simple stack machine to evaluate this expression.
101 * Simply creates a new machine and passes it on to
102 * the buildMachine(machine) method.
103 */
104 public Machine getMachine()
105 {
106 Machine myMachine = new Machine();
107
108 buildMachine(myMachine);
109
110 return myMachine;
111 }
112
113 public void buildMachine(Machine myMachine)
114 {
115 Instruction inst = new Instruction();
116
117 inst.type = Instruction.TYPE_REGISTER;
118 inst.reg = reg;
119
120 myMachine.addInstruction(inst);
121 }
122
123 private void setName(String n)
124 {
125 name = n;
126
127 if (n.equals("x"))
128 {
129 reg = Machine.REGISTER_X;
130 }
131 else if (n.equals("y"))
132 {
133 reg = Machine.REGISTER_Y;
134 }
135 else if (n.equals("r"))
136 {
137 reg = Machine.REGISTER_R;
138 }
139 else if (n.equals("theta"))
140 {
141 reg = Machine.REGISTER_THETA;
142 }
143 else
144 {
145 System.err.println("Unimplemented variable: " + name);
146 reg = 0;
147 }
148 }
149 }