Search in sources :

Example 11 with RowVisitor

use of com.google.refine.browsing.RowVisitor in project OpenRefine by OpenRefine.

the class RowReorderOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = new Engine(project);
    engine.setMode(_mode);
    List<Integer> rowIndices = new ArrayList<Integer>();
    if (_mode == Mode.RowBased) {
        RowVisitor visitor = new IndexingVisitor(rowIndices);
        if (_sorting != null) {
            SortingRowVisitor srv = new SortingRowVisitor(visitor);
            srv.initializeFromJSON(project, _sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        engine.getAllRows().accept(project, visitor);
    } else {
        RecordVisitor visitor = new IndexingVisitor(rowIndices);
        if (_sorting != null) {
            SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
            srv.initializeFromJSON(project, _sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        engine.getAllRecords().accept(project, visitor);
    }
    return new HistoryEntry(historyEntryID, project, "Reorder rows", this, new RowReorderChange(rowIndices));
}
Also used : RowReorderChange(com.google.refine.model.changes.RowReorderChange) ArrayList(java.util.ArrayList) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) HistoryEntry(com.google.refine.history.HistoryEntry) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) RecordVisitor(com.google.refine.browsing.RecordVisitor) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) Engine(com.google.refine.browsing.Engine) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor)

Example 12 with RowVisitor

use of com.google.refine.browsing.RowVisitor in project OpenRefine by OpenRefine.

the class ReconJudgeSimilarCellsOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    return new RowVisitor() {

        int _cellIndex;

        List<CellChange> _cellChanges;

        Recon _sharedNewRecon = null;

        Map<Long, Recon> _dupReconMap = new HashMap<Long, Recon>();

        long _historyEntryID;

        public RowVisitor init(int cellIndex, List<CellChange> cellChanges, long historyEntryID) {
            _cellIndex = cellIndex;
            _cellChanges = cellChanges;
            _historyEntryID = historyEntryID;
            return this;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(_cellIndex);
            if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) {
                String value = cell.value instanceof String ? ((String) cell.value) : cell.value.toString();
                if (_similarValue.equals(value)) {
                    Recon recon = null;
                    if (_judgment == Judgment.New && _shareNewTopics) {
                        if (_sharedNewRecon == null) {
                            _sharedNewRecon = new Recon(_historyEntryID, null, null);
                            _sharedNewRecon.judgment = Judgment.New;
                            _sharedNewRecon.judgmentBatchSize = 0;
                            _sharedNewRecon.judgmentAction = "similar";
                        }
                        _sharedNewRecon.judgmentBatchSize++;
                        recon = _sharedNewRecon;
                    } else {
                        if (_dupReconMap.containsKey(cell.recon.id)) {
                            recon = _dupReconMap.get(cell.recon.id);
                            recon.judgmentBatchSize++;
                        } else {
                            recon = cell.recon.dup(_historyEntryID);
                            recon.judgmentBatchSize = 1;
                            recon.matchRank = -1;
                            recon.judgmentAction = "similar";
                            if (_judgment == Judgment.Matched) {
                                recon.judgment = Recon.Judgment.Matched;
                                recon.match = _match;
                                if (recon.candidates != null) {
                                    for (int m = 0; m < recon.candidates.size(); m++) {
                                        if (recon.candidates.get(m).id.equals(_match.id)) {
                                            recon.matchRank = m;
                                            break;
                                        }
                                    }
                                }
                            } else if (_judgment == Judgment.New) {
                                recon.judgment = Recon.Judgment.New;
                                recon.match = null;
                            } else if (_judgment == Judgment.None) {
                                recon.judgment = Recon.Judgment.None;
                                recon.match = null;
                            }
                            _dupReconMap.put(cell.recon.id, recon);
                        }
                    }
                    Cell newCell = new Cell(cell.value, recon);
                    CellChange cellChange = new CellChange(rowIndex, _cellIndex, cell, newCell);
                    _cellChanges.add(cellChange);
                }
            }
            return false;
        }
    }.init(column.getCellIndex(), cellChanges, historyEntryID);
}
Also used : HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Recon(com.google.refine.model.Recon) Cell(com.google.refine.model.Cell)

Aggregations

RowVisitor (com.google.refine.browsing.RowVisitor)12 Column (com.google.refine.model.Column)9 Project (com.google.refine.model.Project)9 Row (com.google.refine.model.Row)8 JSONObject (org.json.JSONObject)8 Cell (com.google.refine.model.Cell)7 CellChange (com.google.refine.model.changes.CellChange)6 FilteredRows (com.google.refine.browsing.FilteredRows)5 JSONException (org.json.JSONException)5 Engine (com.google.refine.browsing.Engine)4 Serializable (java.io.Serializable)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 RecordVisitor (com.google.refine.browsing.RecordVisitor)3 Evaluable (com.google.refine.expr.Evaluable)3 HistoryEntry (com.google.refine.history.HistoryEntry)3 SortingRecordVisitor (com.google.refine.sorting.SortingRecordVisitor)3 SortingRowVisitor (com.google.refine.sorting.SortingRowVisitor)3 FilteredRecords (com.google.refine.browsing.FilteredRecords)2