Search in sources :

Example 11 with Project

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

the class RowTests method SetUp.

@BeforeMethod
public void SetUp() {
    writer = new StringWriter();
    project = new Project();
    options = mock(Properties.class);
}
Also used : Project(com.google.refine.model.Project) StringWriter(java.io.StringWriter) Properties(java.util.Properties) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 12 with Project

use of com.google.refine.model.Project in project polymap4-core by Polymap4.

the class ImportCSVTest method testSSV.

@Test
public void testSSV() throws Exception {
    // ; separated file
    File wohngebiete = new File(this.getClass().getResource("/data/wohngebiete_sachsen.csv").getFile());
    File tmp = File.createTempFile("foo", ".csv");
    com.google.common.io.Files.copy(wohngebiete, tmp);
    ImportResponse<CSVFormatAndOptions> response = service.importFile(tmp, CSVFormatAndOptions.createDefault(), null);
    assertEquals(";", response.options().separator());
    // get the loaded models
    ColumnModel columns = response.job().project.columnModel;
    assertEquals(12, columns.columns.size());
    List<Row> rows = response.job().project.rows;
    assertEquals("Baugenehmigungen: Neue Wohn-u.Nichtwohngeb. einschl. Wohnh.,", rows.get(0).cells.get(0).value);
    assertEquals(471, rows.size());
    assertEquals("neue Wohngeb. mit 1 od.2 Wohnungen, Räume u.Fläche d.Wohn.,", rows.get(1).cells.get(0).value);
    CSVFormatAndOptions options = response.options();
    options.setSeparator("\\t");
    service.updateOptions(response.job(), options, null);
    columns = response.job().project.columnModel;
    assertEquals(1, columns.columns.size());
    rows = response.job().project.rows;
    assertEquals("Baugenehmigungen: Neue Wohn-u.Nichtwohngeb. einschl. Wohnh.,;;;;;;;;;;;", rows.get(0).cells.get(0).value);
    assertEquals(471, rows.size());
    Project project = service.createProject(response.job(), options, null);
    rows = project.rows;
    assertEquals("Baugenehmigungen: Neue Wohn-u.Nichtwohngeb. einschl. Wohnh.,;;;;;;;;;;;", rows.get(0).cells.get(0).value);
    assertEquals(471, rows.size());
}
Also used : Project(com.google.refine.model.Project) ColumnModel(com.google.refine.model.ColumnModel) Row(com.google.refine.model.Row) File(java.io.File) CSVFormatAndOptions(org.polymap.core.data.refine.impl.CSVFormatAndOptions) Test(org.junit.Test)

Example 13 with Project

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

the class Cross method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (1 <= args.length && args.length <= 3) {
        // 1st argument can take either value or cell(for backward compatibility)
        Object v = args[0];
        // if 2nd argument is omitted or set to "", use the current project name
        Object targetProjectName = "";
        boolean isCurrentProject = false;
        if (args.length < 2 || args[1].equals("")) {
            isCurrentProject = true;
        } else {
            targetProjectName = args[1];
        }
        // if 3rd argument is omitted or set to "", use the index column
        Object targetColumnName = args.length < 3 || args[2].equals("") ? INDEX_COLUMN_NAME : args[2];
        long targetProjectID;
        ProjectLookup lookup;
        if (v != null && targetProjectName instanceof String && targetColumnName instanceof String) {
            try {
                targetProjectID = isCurrentProject ? ((Project) bindings.get("project")).id : ProjectManager.singleton.getProjectID((String) targetProjectName);
            } catch (GetProjectIDException e) {
                return new EvalError(e.getMessage());
            }
            try {
                lookup = ProjectManager.singleton.getLookupCacheManager().getLookup(targetProjectID, (String) targetColumnName);
            } catch (LookupException e) {
                return new EvalError(e.getMessage());
            }
            if (v instanceof WrappedCell) {
                return lookup.getRows(((WrappedCell) v).cell.value);
            } else {
                return lookup.getRows(v);
            }
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell or value, a project name to look up (optional), and a column name in that project (optional)");
}
Also used : Project(com.google.refine.model.Project) LookupException(com.google.refine.util.LookupException) ProjectLookup(com.google.refine.LookupCacheManager.ProjectLookup) WrappedCell(com.google.refine.expr.WrappedCell) GetProjectIDException(com.google.refine.util.GetProjectIDException) EvalError(com.google.refine.expr.EvalError)

Example 14 with Project

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

the class Reinterpret method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 2 || args.length == 3) {
        Object o1 = args[0];
        Object o2 = args[1];
        if (o1 != null && o2 != null && o2 instanceof String) {
            String str = (o1 instanceof String) ? (String) o1 : o1.toString();
            String decoder;
            String encoder;
            encoder = (String) o2;
            if (args.length == 2) {
                Project project = (Project) bindings.get("project");
                ProjectMetadata metadata = ProjectManager.singleton.getProjectMetadata(project.id);
                // can return "" for broken projects
                decoder = metadata.getEncoding();
            } else {
                decoder = (String) args[2];
            }
            return reinterpret(str, decoder, encoder);
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects String to reinterpret with a given target encoding and optional source encoding");
}
Also used : Project(com.google.refine.model.Project) ProjectMetadata(com.google.refine.ProjectMetadata) EvalError(com.google.refine.expr.EvalError)

Example 15 with Project

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

the class FacetCount method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 3 && args[1] instanceof String && args[2] instanceof String) {
        // choice value to look up
        Object choiceValue = args[0];
        String facetExpression = (String) args[1];
        String columnName = (String) args[2];
        Project project = (Project) bindings.get("project");
        Column column = project.columnModel.getColumnByName(columnName);
        if (column == null) {
            return new EvalError("No such column named " + columnName);
        }
        String key = "nominal-bin:" + facetExpression;
        ExpressionNominalValueGrouper grouper = (ExpressionNominalValueGrouper) column.getPrecompute(key);
        if (grouper == null) {
            try {
                Evaluable eval = MetaParser.parse(facetExpression);
                Engine engine = new Engine(project);
                grouper = new ExpressionNominalValueGrouper(eval, columnName, column.getCellIndex());
                engine.getAllRows().accept(project, grouper);
                column.setPrecompute(key, grouper);
            } catch (ParsingException e) {
                return new EvalError("Error parsing facet expression " + facetExpression);
            }
        }
        return grouper.getChoiceValueCountMultiple(choiceValue);
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a choice value, an expression as a string, and a column name");
}
Also used : Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) Column(com.google.refine.model.Column) ParsingException(com.google.refine.expr.ParsingException) EvalError(com.google.refine.expr.EvalError) Engine(com.google.refine.browsing.Engine) ExpressionNominalValueGrouper(com.google.refine.browsing.util.ExpressionNominalValueGrouper)

Aggregations

Project (com.google.refine.model.Project)146 Properties (java.util.Properties)46 IOException (java.io.IOException)40 Engine (com.google.refine.browsing.Engine)37 Test (org.testng.annotations.Test)37 ServletException (javax.servlet.ServletException)35 ProjectMetadata (com.google.refine.ProjectMetadata)33 BeforeMethod (org.testng.annotations.BeforeMethod)31 Cell (com.google.refine.model.Cell)29 Row (com.google.refine.model.Row)29 RefineTest (com.google.refine.RefineTest)25 Column (com.google.refine.model.Column)25 BeforeTest (org.testng.annotations.BeforeTest)18 Process (com.google.refine.process.Process)17 AbstractOperation (com.google.refine.model.AbstractOperation)14 StringWriter (java.io.StringWriter)13 RowVisitor (com.google.refine.browsing.RowVisitor)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 WrappedCell (com.google.refine.expr.WrappedCell)9