/** * Constructor. * @param inNodeSchema original schema for nodes * @param inEdgeSchema original schema for edges */ public IvtkObviousTree(Schema inNodeSchema, Schema inEdgeSchema) { this.nodeSchema = inNodeSchema; this.edgeSchema = inEdgeSchema; if (!edgeSchema.hasColumn("parent")) { edgeSchema.addColumn("parent", Integer.class, 0); } if (!edgeSchema.hasColumn("child")) { edgeSchema.addColumn("child", Integer.class, 0); } edgeTable = new IvtkObviousTable(edgeSchema); tree = new DefaultTree(); ColumnFactory factory = ColumnFactory.getInstance(); for (int i = 0; i < nodeSchema.getColumnCount(); i++) { Column col = factory.create(nodeSchema.getColumnType(i).getSimpleName(), nodeSchema.getColumnName(i)); tree.addColumn(col); } if (this.forest == null) { this.forest = new ArrayList<Tree<Node, Edge>>(); } if (!this.forest.contains(this)) { this.forest.add(this); } }
/** * Constructor that used Obvious Schema to wrap Prefuse Tables. * @param schema original obvious schema for the table */ public PrefuseObviousTable(Schema schema) { this.table = new prefuse.data.Table(0, schema.getColumnCount()); this.listener = new ArrayList<TableListener>(); for (int i = 0; i < schema.getColumnCount(); i++) { this.table.addColumn(schema.getColumnName(i), schema.getColumnType(i), schema.getColumnDefault(i)); } }
/** * Gets a specific value. * @param rowId row index * @param field column name * @return value for this couple */ public Object getValue(int rowId, String field) { return getValue(rowId, schema.getColumnIndex(field)); }
@Override public Object getDefault(String field) { return schema.getColumnDefault(schema.getColumnIndex(field)); }
@Override public boolean canGet(String field, Class<?> type) { if (!schema.hasColumn(field)) { return false; } return schema.getColumnType(field).equals(type); }
protected Instances createInstances() { FastVector attributes = new FastVector(); for (int i = 0; i < schema.getColumnCount(); i++) { Attribute attribute = createAttribute(schema.getColumnName(i), schema.getColumnType(i)); attributes.addElement(attribute); } return new Instances("test", attributes, 1); }
/** * Gets the name of a column. * @param columnIndex index of the column * @return name of the column */ public String getColumnName(int columnIndex) { return table.getSchema().getColumnName(columnIndex); }
/** * Indicates if a given value is correct. * @param rowId row index * @param col column index * @return true if the coordinates are valid */ public boolean isValueValid(int rowId, int col) { return isValidRow(rowId) && (col < schema.getColumnCount()); }
@Override public Class<?> getColumnType(String field) { return schema.getColumnType(field); }
/** * Adds a row with default value. * @return number of rows */ public int addRow() { Object defaultValue = "n/a"; try { if (canAddRow()) { int rowId = table.addRow(); for (int i = 0; i < schema.getColumnCount(); i++) { TypedFormat format = formatFactory.getFormat( schema.getColumnType(i).getSimpleName()); defaultValue = schema.getColumnDefault(i); if (schema.getColumnDefault(i) == null) { defaultValue = setDefaultValue(format); } StringBuffer val = format.format(defaultValue, new StringBuffer(), new FieldPosition(0)); table.setValueAt(val.toString(), rowId, i); //table.getColumnAt(i).setValueAt(table.getColumnAt(i).size(), // val.toString()); } this.fireTableEvent(table.getLastRow(), table.getLastRow(), TableListener.ALL_COLUMN, TableListener.INSERT); } return this.getRowCount(); } catch (Exception e) { throw new ObviousRuntimeException(e); } }
@SuppressWarnings("unchecked") @Override public void addColumn(String arg0, Class arg1, Object arg2) { if (!m_names.contains(arg0)) { if (!table.getSchema().hasColumn(arg0)) { table.getSchema().addColumn(arg0, arg1, arg2); } Column col = ColumnFactory.getColumn(arg1, getRowCount(), arg2); int colIndex = table.getSchema().getColumnIndex(arg0); this.m_lastCol = colIndex; this.m_columns.add(col); this.m_names.add(arg0); ColumnEntry entry = new ColumnEntry(colIndex, col, new ColumnMetadata(this, arg0)); ColumnEntry oldEntry = (ColumnEntry) this.m_entries.put(arg0, entry); if (oldEntry != null) { oldEntry.dispose(); } invalidateSchema(); // listen to what the column has to say col.addColumnListener(this); fireTableEvent(0, getRowCount(), m_lastCol, TableModelEvent.INSERT); } }
@Override public Node addNode() { Object[] values = new Object[nodeSchema.getColumnCount()]; for (int i = 0; i < nodeSchema.getColumnCount(); i++) { values[i] = nodeSchema.getColumnDefault(i); } NodeImpl node = new NodeImpl(nodeSchema, values); network.addNode(node); return new WrapToPrefNode(network, node, getNodeCount()); }
@Override public void add(Instance instance) { double[] values = instance.toDoubleArray(); Object[] obvValues = new Object[table.getSchema().getColumnCount()]; for (int i = 0; i < table.getSchema().getColumnCount(); i++) { Attribute att = instance.attribute(i); Class<?> c = table.getSchema().getColumnType(i); if(att.isString() && ObviousWekaUtils.isString(c)) { obvValues[i] = instance.attribute(i).value(i); } else if (att.isNumeric() && ObviousWekaUtils.isNumeric(c)) { obvValues[i] = instance.value(att); } else if (att.isDate() && ObviousWekaUtils.isDate(c)) { obvValues[i] = new Date((long) values[i]); } else { obvValues[i] = null; } } Tuple tuple = new TupleImpl(table.getSchema(), obvValues); m_Instances.addElement(new ObviousWekaInstance(tuple, this)); table.addRow(tuple); }
@Override protected boolean hasColumn(String arg0) { return table.getSchema().hasColumn(arg0); }
@Override protected Column removeColumn(int arg0) { table.getSchema().removeColumn(arg0); return super.removeColumn(arg0); }
/** * Creates attributes vector from an obvious table. * @return vector from attributes */ protected FastVector createAttributes() { FastVector attributes = new FastVector(); for (int i = 0; i < table.getSchema().getColumnCount(); i++) { attributes.addElement(createAttribute(table.getSchema().getColumnName(i), table.getSchema().getColumnType(i))); } return attributes; }
/** * Returns the name of the column at columnIndex. * This is used to initialize the table's column header name. * Note: this name does not need to be unique; two columns * in a table can have the same name. * @param columnIndex the index of the column * @return the name of the column */ public String getColumnName(int columnIndex) { return this.table.getSchema().getColumnName(columnIndex); }
@Override public int numValues() { return tuple.getSchema().getColumnCount(); }