View Javadoc

1   package org.catacomb.numeric.data;
2   
3   import java.util.ArrayList;
4   
5   import org.catacomb.interlish.structure.Parent;
6   import org.catacomb.report.E;
7   
8   
9   public class NDSAccessor {
10  
11      NumDataStore store;
12  
13  
14      public NDSAccessor(NumDataStore ndstore) {
15          store = ndstore;
16      }
17  
18  
19      public NumDataItem getNumDataItem(String s) {
20  
21          NumDataItem numv = null;
22  
23          ArrayList<Object> op = getObjectPath(s);
24  
25          if (op != null && op.size() > 0) {
26              Object obj = op.get(op.size()-1);
27              if (obj instanceof NumDataItem) {
28                  numv = (NumDataItem)obj;
29  
30              } else {
31                  E.warning("wrong type " + obj + " when seeking " + s +
32                            " expecting NDI but got " +
33                            obj.getClass().getName());
34              }
35          }
36          return numv;
37      }
38  
39  
40  
41  
42  
43      public ArrayList<Object> getObjectPath(String s) {
44          ArrayList<Object> ret = new ArrayList<Object>();
45  
46          String[] sa = s.split("/");
47  
48          Object osf = store;
49          ret.add(osf);
50          int ioff = 0;
51          if (sa[0].equals(store.toString())) {
52              ioff = 1;
53          }
54  
55          for (int i = ioff; i < sa.length; i++) {
56              if (osf instanceof Parent) {
57                  Parent par = (Parent)osf;
58                  if (par.hasChild(sa[i])) {
59                      osf = par.getChild(sa[i]);
60                      ret.add(osf);
61  
62                  } else {
63                      E.possibleError(" no child \"" + sa[i] + "\" in parent " + "\"" + par + "\"");
64                      //     store.dumpChildren();
65                      ret = null;
66                      par = null;
67                      break;
68                  }
69              } else {
70                  E.error("need a parent object, not " + osf);
71              }
72          }
73  
74          return ret;
75      }
76  
77  
78  
79      public ArrayList<NumDataItem> getOrdinates(NumVector numV) {
80          ArrayList<NumDataItem> ret = new ArrayList<NumDataItem>();
81  
82  
83          if (numV instanceof DSlice) {
84              // just return the other slices? - too adhoc?
85              BlockStack bs = (BlockStack)(((DSlice)numV).getParent());
86  
87              addStackSlices(bs, ret, numV);
88  
89          } else if (numV instanceof DVector) {
90              int np = ((DVector)numV).getNPoint();
91  
92              for (DVector dv : store.getDVectors()) {
93                  if (dv.getNPoint() == np) {
94                      ret.add(dv);
95                  }
96              }
97  
98              for (BlockStack bs : store.getBlockStacks()) {
99                  if (bs.getSize() == np) {
100                     addStackSlices(bs, ret, null);
101 
102                 } else {
103                     if (np == 1 || bs.getSize() == 1) {
104                         // probably fine - don't report;
105                     } else {
106                         E.info("not adding a possible ordinate since it is a different size " +
107                                "(" + np + " compared with " + bs.getSize() + ") " + bs.getName());
108                     }
109                 }
110             }
111 
112         } else {
113             E.missing();
114         }
115         return ret;
116     }
117 
118 
119     private void addStackSlices(BlockStack bs, ArrayList<NumDataItem> ret,
120                                 Object excl) {
121         for (Object obj : bs.getSlices()) {
122             if (obj == excl) {
123                 // just leave it out;
124 
125             } else if (obj instanceof NumVector) {
126                 ret.add((NumDataItem)obj);
127 
128             } else if (obj instanceof NumVectorSet) {
129                 ret.add((NumDataItem)obj);
130 
131             } else if (obj instanceof AnimSlice) {
132                 // fine to ignore it;
133 
134             } else {
135                 E.info("ignoring " + obj);
136             }
137         }
138 
139     }
140 
141 
142     public NumDataItem getSibling(NumVector numV, String snm) {
143         NumDataItem ret = null;
144         ArrayList<NumDataItem> ndis = getOrdinates(numV);
145         for (NumDataItem ndi : ndis) {
146             if (snm.equals(ndi.getName())) {
147                 ret = ndi;
148                 break;
149             }
150         }
151 
152         return ret;
153     }
154 
155 
156 }