Search in sources :

Example 1 with ReconStats

use of com.google.refine.model.ReconStats in project OpenRefine by OpenRefine.

the class ReconChange method load.

public static Change load(LineNumberReader reader, Pool pool) throws Exception {
    ReconConfig newReconConfig = null;
    ReconStats newReconStats = null;
    ReconConfig oldReconConfig = null;
    ReconStats oldReconStats = null;
    String commonColumnName = null;
    CellChange[] cellChanges = null;
    String line;
    while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
        int equal = line.indexOf('=');
        CharSequence field = line.subSequence(0, equal);
        String value = line.substring(equal + 1);
        if ("newReconConfig".equals(field)) {
            if (value.length() > 0) {
                newReconConfig = ReconConfig.reconstruct(value);
            }
        } else if ("newReconStats".equals(field)) {
            if (value.length() > 0) {
                newReconStats = ParsingUtilities.mapper.readValue(value, ReconStats.class);
            }
        } else if ("oldReconConfig".equals(field)) {
            if (value.length() > 0) {
                oldReconConfig = ReconConfig.reconstruct(value);
            }
        } else if ("oldReconStats".equals(field)) {
            if (value.length() > 0) {
                oldReconStats = ParsingUtilities.mapper.readValue(value, ReconStats.class);
            }
        } else if ("commonColumnName".equals(field)) {
            commonColumnName = value;
        } else if ("cellChangeCount".equals(field)) {
            int cellChangeCount = Integer.parseInt(value);
            cellChanges = new CellChange[cellChangeCount];
            for (int i = 0; i < cellChangeCount; i++) {
                cellChanges[i] = CellChange.load(reader, pool);
            }
        }
    }
    ReconChange change = new ReconChange(cellChanges, commonColumnName, newReconConfig, newReconStats);
    change._oldReconConfig = oldReconConfig;
    change._oldReconStats = oldReconStats;
    return change;
}
Also used : ReconConfig(com.google.refine.model.recon.ReconConfig) ReconStats(com.google.refine.model.ReconStats)

Example 2 with ReconStats

use of com.google.refine.model.ReconStats in project OpenRefine by OpenRefine.

the class DataExtensionChange method apply.

@Override
public void apply(Project project) {
    synchronized (project) {
        if (_firstNewCellIndex < 0) {
            _firstNewCellIndex = project.columnModel.allocateNewCellIndex();
            for (int i = 1; i < _columnNames.size(); i++) {
                project.columnModel.allocateNewCellIndex();
            }
            _oldRows = new ArrayList<Row>(project.rows);
            _newRows = new ArrayList<Row>(project.rows.size());
            int cellIndex = project.columnModel.getColumnByName(_baseColumnName).getCellIndex();
            int keyCellIndex = project.columnModel.columns.get(project.columnModel.getKeyColumnIndex()).getCellIndex();
            int index = 0;
            int rowIndex = index < _rowIndices.size() ? _rowIndices.get(index) : _oldRows.size();
            DataExtension dataExtension = index < _rowIndices.size() ? _dataExtensions.get(index) : null;
            index++;
            Map<String, Recon> reconMap = new HashMap<String, Recon>();
            for (int r = 0; r < _oldRows.size(); r++) {
                Row oldRow = _oldRows.get(r);
                if (r < rowIndex) {
                    _newRows.add(oldRow.dup());
                    continue;
                }
                if (dataExtension == null || dataExtension.data.length == 0) {
                    _newRows.add(oldRow);
                } else {
                    Row firstNewRow = oldRow.dup();
                    extendRow(firstNewRow, dataExtension, 0, reconMap);
                    _newRows.add(firstNewRow);
                    int r2 = r + 1;
                    for (int subR = 1; subR < dataExtension.data.length; subR++) {
                        if (r2 < project.rows.size()) {
                            Row oldRow2 = project.rows.get(r2);
                            if (oldRow2.isCellBlank(cellIndex) && oldRow2.isCellBlank(keyCellIndex)) {
                                Row newRow = oldRow2.dup();
                                extendRow(newRow, dataExtension, subR, reconMap);
                                _newRows.add(newRow);
                                r2++;
                                continue;
                            }
                        }
                        Row newRow = new Row(cellIndex + _columnNames.size());
                        extendRow(newRow, dataExtension, subR, reconMap);
                        _newRows.add(newRow);
                    }
                    // r will be incremented by the for loop anyway
                    r = r2 - 1;
                }
                rowIndex = index < _rowIndices.size() ? _rowIndices.get(index) : _oldRows.size();
                dataExtension = index < _rowIndices.size() ? _dataExtensions.get(index) : null;
                index++;
            }
        }
        project.rows.clear();
        project.rows.addAll(_newRows);
        for (int i = 0; i < _columnNames.size(); i++) {
            String name = _columnNames.get(i);
            int cellIndex = _firstNewCellIndex + i;
            Column column = new Column(cellIndex, name);
            ReconType columnType = _columnTypes.get(i);
            column.setReconConfig(new DataExtensionReconConfig(_service, _identifierSpace, _schemaSpace, columnType));
            ReconStats reconStats = ReconStats.create(project, cellIndex);
            if (reconStats.matchedTopics > 0) {
                column.setReconStats(reconStats);
            }
            try {
                project.columnModel.addColumn(_columnInsertIndex + i, column, true);
                // the column might have been renamed to avoid collision
                _columnNames.set(i, column.getName());
            } catch (ModelException e) {
            // won't get here since we set the avoid collision flag
            }
        }
        project.update();
    }
}
Also used : ReconType(com.google.refine.model.ReconType) DataExtensionReconConfig(com.google.refine.model.recon.DataExtensionReconConfig) ModelException(com.google.refine.model.ModelException) HashMap(java.util.HashMap) DataExtension(com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtension) Column(com.google.refine.model.Column) ReconStats(com.google.refine.model.ReconStats) Row(com.google.refine.model.Row) Recon(com.google.refine.model.Recon)

Aggregations

ReconStats (com.google.refine.model.ReconStats)2 Column (com.google.refine.model.Column)1 ModelException (com.google.refine.model.ModelException)1 Recon (com.google.refine.model.Recon)1 ReconType (com.google.refine.model.ReconType)1 Row (com.google.refine.model.Row)1 DataExtensionReconConfig (com.google.refine.model.recon.DataExtensionReconConfig)1 ReconConfig (com.google.refine.model.recon.ReconConfig)1 DataExtension (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtension)1 HashMap (java.util.HashMap)1