Skip to content

Commit 49c8b9d

Browse files
gaynor@illinois.edugaynor@illinois.edu
authored andcommitted
More improvements and documentation. Added lots of tests for new extraction operator.
1 parent 2c1e9a1 commit 49c8b9d

5 files changed

Lines changed: 174 additions & 163 deletions

File tree

ncsa-qdl/buildNumber.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#maven.buildNumber.plugin properties file
2-
#Wed Jul 13 17:11:18 CDT 2022
3-
buildNumber\\d*=9729
2+
#Tue Jul 19 12:43:10 CDT 2022
3+
buildNumber\\d*=9747

ncsa-qdl/src/main/java/edu/uiuc/ncsa/qdl/expressions/StemExtractionNode.java

Lines changed: 51 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import edu.uiuc.ncsa.qdl.statements.StatementWithResultInterface;
77
import edu.uiuc.ncsa.qdl.statements.TokenPosition;
88
import edu.uiuc.ncsa.qdl.variables.Constant;
9+
import edu.uiuc.ncsa.qdl.variables.QDLNull;
910
import edu.uiuc.ncsa.qdl.variables.QDLStem;
11+
import edu.uiuc.ncsa.security.core.exceptions.NFWException;
1012
import edu.uiuc.ncsa.security.core.exceptions.NotImplementedException;
1113

1214
import java.util.ArrayList;
@@ -76,11 +78,6 @@ protected IndexArgs normalize(IndexArgs args) {
7678

7779
@Override
7880
public Object evaluate(State state) {
79-
//return evaluateOLD(state);
80-
return evaluateNEW(state);
81-
}
82-
83-
protected Object evaluateNEW(State state) {
8481
IndexArgs args = new IndexArgs();
8582
args.addAll(linearize(state));
8683
indexArgs = normalize(args);
@@ -98,7 +95,6 @@ protected Object evaluateNEW(State state) {
9895
}
9996
}
10097
}
101-
// System.out.println(ia);
10298
}
10399
Object larg = args.get(0).swri.getResult();
104100
if (!(larg instanceof QDLStem)) {
@@ -112,9 +108,9 @@ protected Object evaluateNEW(State state) {
112108
setResult(inStem);
113109
return getResult();
114110
}
115-
QDLStem out = recurse(inStem, indexArgs);
111+
Object out = recurse(inStem, indexArgs);
116112
setResult(out);
117-
setResultType(Constant.STEM_TYPE);
113+
setResultType(Constant.getType(out));
118114
setEvaluated(true);
119115
return getResult();
120116
}
@@ -138,7 +134,6 @@ protected Object evaluateOLD(State state) {
138134
}
139135
}
140136
}
141-
// System.out.println(ia);
142137
}
143138
Object larg = args.get(0).swri.getResult();
144139
if (!(larg instanceof QDLStem)) {
@@ -156,7 +151,7 @@ protected Object evaluateOLD(State state) {
156151
ArrayList<IndexList> sourceIndices = null;
157152
ArrayList<IndexList> targetIndices = null;
158153
if (indexArgs.hasWildcard()) {
159-
QDLStem out = recurse(inStem, indexArgs);
154+
QDLStem out = (QDLStem) recurse(inStem, indexArgs);
160155
setResult(out);
161156
setResultType(Constant.STEM_TYPE);
162157
setEvaluated(true);
@@ -194,8 +189,6 @@ protected Object evaluateOLD(State state) {
194189
setEvaluated(true);
195190
setResult(out);
196191
return getResult();
197-
198-
199192
}
200193

201194
/*
@@ -229,64 +222,52 @@ protected Object evaluateOLD(State state) {
229222
* @param sourceIndices
230223
* @return
231224
*/
232-
protected QDLStem recurse(QDLStem in, IndexArgs sourceIndices) {
225+
protected Object recurse(QDLStem in, IndexArgs sourceIndices) {
233226
QDLStem out = new QDLStem();
227+
Object otherOut = null;
234228
int startIndex = 1;
235229
IndexArg root = indexArgs.get(startIndex);
236230
long autoIndex = 0L;
237231
for (Object key : root.createKeySet(in)) {
238232
Object value = in.get(key);
239233
if (value == null) {
234+
if (Constant.isScalar(root.swri.getResult()) && sourceIndices.size() == 2) {
235+
// edge case, they asked for a non-existent index
236+
// E.g. v.:=[;5]; v\42;
237+
return QDLNull.getInstance();
238+
}
239+
240240
continue;
241241
}
242242
if (value instanceof QDLStem) {
243243
IndexList indexList = new IndexList();
244244

245-
/* if (root.isWildcard()) {
245+
if (root.isWildcard()) {
246246
indexList.add(key);
247247
} else {
248-
if (Constant.isScalar(root.swri.getResult())) {
248+
if (!Constant.isScalar(root.swri.getResult())) {
249249
if (root.strictOrder || Constant.isString(key)) {
250250
indexList.add(key);
251251
} else {
252252
indexList.add(autoIndex++);
253253
}
254-
} else {
255-
System.out.println("1 " + "α=" + root.isWildcard() + ", σ=" + Constant.isScalar(root.swri.getResult()) + ", ω=" + root.strictOrder);
256254
}
257255
}
258-
recurse((QDLStem) value, out, indexList, sourceIndices, startIndex + 1, 0L);*/
259-
260-
261-
if ((root.swri instanceof AllIndices) || !Constant.isScalar(root.swri.getResult())) {
262-
if (root.isWildcard()) {
263-
// System.out.println("1 " + "α=" + root.isWildcard() + ", σ=" + Constant.isScalar(root.swri.getResult()) + ", ω=" + root.strictOrder);
264-
indexList.add(key);
265-
} else {
266-
if (root.strictOrder || Constant.isString(key)) {
267-
// System.out.println("2 " + "α=" + root.isWildcard() + ", σ=" + Constant.isScalar(root.swri.getResult()) + ", ω=" + root.strictOrder);
268-
indexList.add(key);
269-
} else {
270-
// System.out.println("3 " + "α=" + root.isWildcard() + ", σ=" + Constant.isScalar(root.swri.getResult()) + ", ω=" + root.strictOrder);
271-
272-
indexList.add(autoIndex++);
273-
}
274-
}
275-
}
276-
recurse((QDLStem) value, out, indexList, sourceIndices, startIndex + 1, 0L);
277-
256+
otherOut = recurse((QDLStem) value, out, indexList, sourceIndices, startIndex + 1, 0L);
278257
} else {
279258
if (indexArgs.size() - 1 == startIndex) {
280259
// set the value, but only if it is the end of an index list (so there
281260
// are no more indices to traverse.
282261
if (root.isWildcard()) {
283262
out.putLongOrString(key, value);
284263
} else {
285-
if (!Constant.isScalar(root.swri.getResult())) {
286-
if (root.strictOrder) {
264+
if (Constant.isScalar(root.swri.getResult())) {
265+
return value;
266+
} else {
267+
if (root.strictOrder || (key instanceof String)) {
287268
out.putLongOrString(key, value);
288269
} else {
289-
out.putLongOrString(autoIndex++, value);
270+
out.put(autoIndex++, value);
290271
}
291272

292273
}
@@ -295,31 +276,37 @@ protected QDLStem recurse(QDLStem in, IndexArgs sourceIndices) {
295276
}
296277
}
297278
}
298-
279+
if (otherOut != null && !(otherOut instanceof QDLStem)) {
280+
if (!out.isEmpty()) {
281+
throw new NFWException("both a scalar and stem were returned as values in " + getClass().getCanonicalName());
282+
}
283+
return otherOut;
284+
}
299285
return out;
300286
}
301287

302-
protected void recurseNEW(QDLStem in,
303-
QDLStem out,
304-
IndexList targetIndex,
305-
List<IndexArg> sourceIndices,
306-
int indexLocation,
307-
long strictIndex) {
288+
protected Object recurse(QDLStem in,
289+
QDLStem out,
290+
IndexList targetIndex,
291+
List<IndexArg> sourceIndices,
292+
int indexLocation,
293+
long strictIndex) {
308294

309295
if (sourceIndices.size() <= indexLocation) {
310-
// System.out.println("recurseNEW: targetIndex = " + targetIndex + ", strictIndex=" + strictIndex + ", loc=" + indexLocation + ", in=" + in + ", out=" + out);
296+
System.out.println("*** recurseNEW: targetIndex = " + targetIndex + ", strictIndex=" + strictIndex + ", loc=" + indexLocation + ", in=" + in + ", out=" + out);
311297
IndexArg lastIndex = sourceIndices.get(indexLocation - 1);
312298
if (Constant.isScalar(lastIndex.swri.getResult())) {
313299
out.addAll(in);
314300
} else {
315301
out.putLongOrString(strictIndex, in);
316302
}
317-
return;
303+
return out;
318304
}
319305
IndexArg indexArg = sourceIndices.get(indexLocation);
306+
Object otherOut = null;
320307
for (Object key : indexArg.createKeySet(in)) {
321308
Object value = in.get(key);
322-
// System.out.println("recurse 2 key =" + key + ", value=" + value);
309+
// System.out.println("recurse 2 key =" + key + ", value=" + value);
323310

324311
if (value == null) {
325312
continue;
@@ -331,16 +318,17 @@ protected void recurseNEW(QDLStem in,
331318
indexList.add(key);
332319
} else {
333320
if (Constant.isScalar(indexArg.swri.getResult())) {
334-
if(indexList.isEmpty()){
335-
if(value instanceof QDLStem){
321+
if (indexList.isEmpty()) {
322+
if (value instanceof QDLStem) {
336323
out.addAll((QDLStem) value);
337-
}else{
338-
out.put(0l, value);
324+
} else {
325+
// out.put(0l, value);
326+
return value;
339327
}
340-
}else {
328+
} else {
341329
out.set(indexList, value);
342330
}
343-
return;
331+
return out;
344332
} else {
345333
if (indexArg.strictOrder || (key instanceof String)) {
346334
indexList.add(key);
@@ -351,13 +339,13 @@ protected void recurseNEW(QDLStem in,
351339
}
352340
out.set(indexList, value);
353341
}
354-
// System.out.println("recurse: setting value key =" + indexList + ", value = " + value);
342+
// System.out.println("recurse: setting value key =" + indexList + ", value = " + value);
355343
}
356344

357345
if (value instanceof QDLStem) {
358346
IndexList indexList = targetIndex.clone();
359347
if (indexLocation + 1 < sourceIndices.size()) {
360-
// System.out.println("recurseNEW: INDEX CHECK, targetIndex = " + targetIndex + ", strictIndex=" + strictIndex + ", loc=" + indexLocation + ", in=" + in + ", out=" + out);
348+
// System.out.println("recurseNEW: INDEX CHECK, targetIndex = " + targetIndex + ", strictIndex=" + strictIndex + ", loc=" + indexLocation + ", in=" + in + ", out=" + out);
361349
}
362350

363351
if ((indexArg.swri instanceof AllIndices) || !Constant.isScalar(indexArg.swri.getResult())) {
@@ -376,77 +364,17 @@ protected void recurseNEW(QDLStem in,
376364
}
377365

378366
} else {
379-
recurse((QDLStem) value, out, indexList, sourceIndices, indexLocation + 1, 0L);
367+
otherOut = recurse((QDLStem) value, out, indexList, sourceIndices, indexLocation + 1, 0L);
380368
}
381369
}
382370
}
383-
}
384-
385-
protected void recurse(QDLStem in,
386-
QDLStem out,
387-
IndexList targetIndex,
388-
List<IndexArg> sourceIndices,
389-
int indexLocation,
390-
long strictIndex) {
391-
recurseNEW(in, out, targetIndex, sourceIndices, indexLocation, strictIndex);
392-
}
393-
394-
protected void recurseOLD(QDLStem in,
395-
QDLStem out,
396-
IndexList targetIndex,
397-
List<IndexArg> sourceIndices,
398-
int indexLocation,
399-
long strictIndex) {
400-
401-
if (sourceIndices.size() <= indexLocation) {
402-
return;
371+
if (!(otherOut instanceof QDLStem)) {
372+
return otherOut;
403373
}
404-
IndexArg indexArg = sourceIndices.get(indexLocation);
405-
for (Object key : indexArg.createKeySet(in)) {
406-
Object value = in.get(key);
407-
// System.out.println("recurse 2 key =" + key + ", value=" + value);
408-
409-
if (value == null) {
410-
continue;
411-
}
412-
if (value instanceof QDLStem) {
413-
IndexList indexList = targetIndex.clone();
414-
if (indexLocation + 1 < sourceIndices.size()) {
415-
}
416-
417-
if ((indexArg.swri instanceof AllIndices) || !Constant.isScalar(indexArg.swri.getResult())) {
418-
if (indexArg.strictOrder && indexArg.isWildcard()) {
419-
indexList.add(key);
420-
} else {
421-
indexList.add(strictIndex++);
422-
}
423-
}
424-
recurse((QDLStem) value, out, indexList, sourceIndices, indexLocation + 1, 0L);
425-
426-
427-
} else {
428-
if (sourceIndices.size() - 1 == indexLocation) {
429-
// Only set it if there are more indices. otherwise you get a ton of garbage
430-
IndexList indexList = targetIndex.clone();
431-
if (indexArg.isWildcard()) {
432-
indexList.add(key);
433-
} else {
434-
if (!Constant.isScalar(indexArg.swri.getResult())) {
435-
if (indexArg.strictOrder) {
436-
indexList.add(key);
437-
} else {
438-
indexList.add(strictIndex++);
439-
}
440-
}
374+
return out;
375+
}
441376

442-
}
443-
//System.out.println("recurse: setting value key =" + indexList + ", value = " + value);
444-
out.set(indexList, value);
445-
}
446377

447-
}
448-
}
449-
}
450378

451379
/*
452380
a. := n(3,4,5,n(3*4*5))

ncsa-qdl/src/main/java/edu/uiuc/ncsa/qdl/variables/QDLStem.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void setQDLMap(QDLMap qdlMap) {
5757

5858

5959
/* *******
60-
QDLMap specific
61-
****** */
60+
QDLMap specific
61+
****** */
6262
QDLMap qdlMap;
6363

6464
// Convenience methods.
6565
public Long getLong(String key) {
66-
if(isLongIndex(key)){
66+
if (isLongIndex(key)) {
6767
return getLong(Long.parseLong(key));
6868
}
6969
return (Long) getQDLMap().get(key);
@@ -426,23 +426,24 @@ public Object remove(Object key) {
426426
}
427427
return getQDLMap().remove(key);
428428
}
429-
public void addAll(QDLStem qdlStem){
429+
430+
public void addAll(QDLStem qdlStem) {
430431
getQDLMap().putAll(qdlStem.getQDLMap());
431-
getQDLList().addAll(qdlStem.getQDLList());
432+
getQDLList().addAll(qdlStem.getQDLList());
432433
}
433434

434435
/**
435436
* This does <b>not</b> add the list elements because that causes issues
436437
* with the contract for maps. If you are adding all the values of a stem
437438
* to this one, use {@link #addAll(QDLStem)}.
439+
*
438440
* @param m
439441
*/
440442
@Override
441443
public void putAll(Map<? extends String, ?> m) {
442-
if(m instanceof QDLStem){
444+
if (m instanceof QDLStem) {
443445
QDLStem qdlStem = (QDLStem) m;
444446
getQDLMap().putAll(qdlStem.getQDLMap());
445-
// getQDLList().addAll(qdlStem.getQDLList());
446447
return;
447448
}
448449
throw new IllegalArgumentException("Unknown map type");

0 commit comments

Comments
 (0)