private static Entity getEntity(String[] names, double simTime, Entity thisEnt, Entity objEnt) throws Error { Entity ent; if (names[0].equals("this")) ent = thisEnt; else if (names[0].equals("obj")) ent = objEnt; else ent = Entity.getNamedEntity(names[0]); if (ent == null) { throw new Error(String.format("Could not find entity: %s", names[0])); } // Run the output chain up to the second last name for (int i = 1; i < names.length - 1; ++i) { String outputName = names[i]; OutputHandle oh = ent.getOutputHandle(outputName); if (oh == null) { throw new Error( String.format("Output '%s' not found on entity '%s'", outputName, ent.getInputName())); } if (!Entity.class.isAssignableFrom(oh.getReturnType())) { throw new Error(String.format("Output '%s' is not an entity output", outputName)); } ent = oh.getValue(simTime, Entity.class); } return ent; }
public static void runAssignment( ExpParser.Assignment assign, double simTime, Entity thisEnt, Entity objEnt) throws Error { Entity assignmentEnt = getEntity(assign.destination, simTime, thisEnt, objEnt); ExpResult result = evaluateExpression(assign.value, simTime, thisEnt, objEnt); String attribName = assign.destination[assign.destination.length - 1]; if (!assignmentEnt.hasAttribute(attribName)) { throw new Error( String.format("Entity '%s' does not have attribute '%s'", assignmentEnt, attribName)); } assignmentEnt.setAttribute(attribName, result.value); }
@Override public ExpResult getVariableValue(String[] names) { try { errorString = null; Entity ent = getEntity(names, simTime, thisEnt, objEnt); String outputName = names[names.length - 1]; OutputHandle oh = ent.getOutputHandle(outputName); if (oh == null) { errorString = String.format( "Could not find output '%s' on entity '%s'", outputName, ent.getInputName()); return null; } return new ExpResult(oh.getValueAsDouble(simTime, 0)); } catch (Exception e) { errorString = e.getMessage(); } return null; }
public class InstanceIterable<T extends Entity> implements Iterable<T>, Iterator<T> { private final ArrayList<? extends Entity> allInstances = Entity.getAll(); private final Class<T> entClass; private int curPos; private int nextPos; public InstanceIterable(Class<T> aClass) { entClass = aClass; curPos = -1; nextPos = -1; } private void updatePos() { if (nextPos >= allInstances.size()) return; while (++nextPos < allInstances.size()) { // If we find a match, break out if (allInstances.get(nextPos).getClass() == entClass) break; } } @Override public boolean hasNext() { if (curPos == nextPos) updatePos(); if (nextPos < allInstances.size()) return true; else return false; } @Override public T next() { if (curPos == nextPos) updatePos(); if (nextPos < allInstances.size()) { curPos = nextPos; return entClass.cast(allInstances.get(curPos)); } else { throw new NoSuchElementException(); } } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public Iterator<T> iterator() { return this; } }
@Override public void setEntity(Entity entity) { jTabbedFrame.removeAll(); currentEntity = entity; if (currentEntity == null) { setTitle("Property Viewer"); return; } setTitle("Property Viewer - " + currentEntity.getInputName()); ArrayList<ClassFields> cFields = getFields(entity); for (int i = 0; i < cFields.size(); i++) { // The properties in the current page ClassFields cf = cFields.get(i); PropertyTableModel mod = new PropertyTableModel(entity, cf.fields); PropertyTable tab = new PropertyTable(mod); JScrollPane scroll = new JScrollPane(tab); jTabbedFrame.addTab(cf.klass.getSimpleName(), scroll); } }
private static ArrayList<ClassFields> getFields(Entity object) { if (object == null) return new ArrayList<ClassFields>(0); return getFields(object.getClass()); }