-
Notifications
You must be signed in to change notification settings - Fork 19
/
Operator.java
150 lines (135 loc) · 4.77 KB
/
Operator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* File : Operator.java
* Created : 08-mar-2004 11:24
* By : fbusquets
*
* JClic - Authoring and playing system for educational activities
*
* Copyright (C) 2000 - 2018 Francesc Busquets & Departament
* d'Educacio de la Generalitat de Catalunya
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details (see the LICENSE file).
*/
package edu.xtec.jclic.automation.arith;
import edu.xtec.util.Domable;
import edu.xtec.util.JDomUtility;
/**
* Utility class used by {@link edu.xtec.jclic.automation.arith.Arith} to
* encapsulate the properties and methods related to the members of the
* operations.
*
* @author Francesc Busquets (fbusquets@xtec.cat)
* @version 13.08.28
*/
public class Operator implements Domable {
protected static final int MAX_VALUE = 100000000;
protected static final int WZERO = 1, WONE = 2, WMINUSONE = 4;
protected static final int NLIMITS = 26;
protected static final int[] LIMITS = { 0, -9999, -1000, -999, -100, -99, -50, -25, -20, -10, -9, -5, -1, 0, 1, 5, 9,
10, 20, 25, 50, 99, 100, 999, 1000, 9999 };
protected static final int DEFAULT_LIMIT = 13;
protected static final int LIM0 = 13;
protected static final int LIM10 = 17;
protected static final int LIMI25 = 7;
protected static final int LIMS25 = 19;
protected static final int NOLIM = 25;
protected static final String[] LIM_CH = { "x", "-9999", "-1000", "-999", "-100", "-99", "-50", "-25", "-20", "-10",
"-9", "-5", "-1", "0", "1", "5", "9", "10", "20", "25", "50", "99", "100", "999", "1000", "9999" };
protected static final int NUMLST = 20;
int limInf;
int limSup;
int numDec;
boolean wZero, wOne, wMinusOne;
int fromList;
int[] lst = new int[NUMLST];
protected boolean fromBlank;
static final String ELEMENT_NAME = "operand", DECIMALS = "decimals", VALUES = "values", FROM = "from", TO = "to",
INCLUDE = "include", ZERO = "zero", ONE = "one", MINUSONE = "minusOne";
public Operator() {
limInf = LIM0;
limSup = LIM10;
numDec = 0;
wZero = false;
wOne = false;
wMinusOne = false;
fromList = 0;
}
public org.jdom.Element getJDomElement() {
org.jdom.Element e = new org.jdom.Element(ELEMENT_NAME);
if (numDec > 0)
e.setAttribute(DECIMALS, Integer.toString(numDec));
if (fromList > 0) {
e.setAttribute(VALUES, JDomUtility.intArrayToString(lst, fromList));
} else {
e.setAttribute(FROM, LIM_CH[limInf]);
e.setAttribute(TO, LIM_CH[limSup]);
if (wZero || wOne || wMinusOne) {
org.jdom.Element ei = new org.jdom.Element(INCLUDE);
ei.setAttribute(ZERO, JDomUtility.boolString(wZero));
ei.setAttribute(ONE, JDomUtility.boolString(wOne));
ei.setAttribute(MINUSONE, JDomUtility.boolString(wMinusOne));
e.addContent(ei);
}
}
return e;
}
public void setProperties(org.jdom.Element e, Object aux) throws Exception {
JDomUtility.checkName(e, ELEMENT_NAME);
numDec = JDomUtility.getIntAttr(e, DECIMALS, numDec);
String s = e.getAttributeValue(VALUES);
if (s != null) {
int[] v = JDomUtility.stringToIntArray(s);
fromList = v.length;
System.arraycopy(v, 0, lst, 0, fromList);
} else {
limInf = JDomUtility.getStrIndexAttr(e, FROM, LIM_CH, limInf);
limSup = JDomUtility.getStrIndexAttr(e, TO, LIM_CH, limSup);
org.jdom.Element child = e.getChild(INCLUDE);
if (child != null) {
wZero = JDomUtility.getBoolAttr(child, ZERO, wZero);
wOne = JDomUtility.getBoolAttr(child, ONE, wOne);
wMinusOne = JDomUtility.getBoolAttr(child, MINUSONE, wMinusOne);
}
}
}
public int setClic3Properties(byte[] ops, int p) {
int v;
int i, lb, hb;
int arithVer = 0;
if ((limInf = ops[p++] & 0x7F) == 0) {
fromBlank = true;
limInf = LIM0;
}
limSup = ((i = ops[p++] & 0x7F) == 0 ? LIM10 : i);
numDec = ops[p++] & 0x3;
v = ops[p++] & 0x7F;
wZero = ((v & WZERO) != 0);
wOne = ((v & WONE) != 0);
wMinusOne = ((v & WMINUSONE) != 0);
fromList = ops[p++] & 0x7F;
for (i = 0; i < NUMLST; i++) {
lb = ops[p++] & 0x7F;
v = ops[p++] & 0x7F;
hb = v & 0x3F;
lst[i] = lb + hb * 128;
if ((v & 0x40) != 0)
lst[i] *= -1;
}
return p;
}
protected static int adjustLimVer(int l) {
if (l >= LIMI25)
l++;
if (l >= LIMS25)
l++;
return l;
}
}