Search in sources :

Example 1 with WikibaseSchema

use of org.openrefine.wikidata.schema.WikibaseSchema in project OpenRefine by OpenRefine.

the class PreviewWikibaseSchemaCommand method doPost.

/**
 * This command uses POST but is left CSRF-unprotected since it does not
 * incur a side effect or state change in the backend.
 * The reason why it uses POST is to make sure large schemas and engines
 * can be passed as parameters.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Project project = getProject(request);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        String schemaJson = request.getParameter("schema");
        WikibaseSchema schema = null;
        if (schemaJson != null) {
            try {
                schema = WikibaseSchema.reconstruct(schemaJson);
            } catch (IOException e) {
                respondError(response, "Wikibase schema could not be parsed. Error message: " + e.getMessage());
                return;
            }
        } else {
            schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema");
        }
        if (schema == null) {
            respondError(response, "No Wikibase schema provided.");
            return;
        }
        Manifest manifest = null;
        String manifestJson = request.getParameter("manifest");
        if (manifestJson != null) {
            try {
                manifest = ManifestParser.parse(manifestJson);
            } catch (ManifestException e) {
                respondError(response, "Wikibase manifest could not be parsed. Error message: " + e.getMessage());
                return;
            }
        }
        if (manifest == null) {
            respondError(response, "No Wikibase manifest provided.");
            return;
        }
        QAWarningStore warningStore = new QAWarningStore();
        // Evaluate project
        Engine engine = getEngine(request, project);
        List<TermedStatementEntityEdit> editBatch = schema.evaluate(project, engine, warningStore);
        // Inspect the edits and generate warnings
        EditInspector inspector = new EditInspector(warningStore, manifest);
        inspector.inspect(editBatch, schema);
        // Dump the first 10 edits, scheduled with the default scheduler
        WikibaseAPIUpdateScheduler scheduler = new WikibaseAPIUpdateScheduler();
        List<TermedStatementEntityEdit> nonNullEdits = scheduler.schedule(editBatch).stream().filter(e -> !e.isNull()).collect(Collectors.toList());
        List<TermedStatementEntityEdit> firstEdits = nonNullEdits.stream().limit(10).collect(Collectors.toList());
        PreviewResults previewResults = new PreviewResults(warningStore.getWarnings(), warningStore.getMaxSeverity(), warningStore.getNbWarnings(), nonNullEdits.size(), firstEdits);
        respondJSON(response, previewResults);
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) ServletException(javax.servlet.ServletException) ManifestException(org.openrefine.wikidata.manifests.ManifestException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EditInspector(org.openrefine.wikidata.qa.EditInspector) Collectors(java.util.stream.Collectors) ManifestParser(org.openrefine.wikidata.manifests.ManifestParser) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) CommandUtilities.respondError(org.openrefine.wikidata.commands.CommandUtilities.respondError) QAWarningStore(org.openrefine.wikidata.qa.QAWarningStore) Command(com.google.refine.commands.Command) Engine(com.google.refine.browsing.Engine) Manifest(org.openrefine.wikidata.manifests.Manifest) IOException(java.io.IOException) Manifest(org.openrefine.wikidata.manifests.Manifest) ManifestException(org.openrefine.wikidata.manifests.ManifestException) ServletException(javax.servlet.ServletException) ManifestException(org.openrefine.wikidata.manifests.ManifestException) IOException(java.io.IOException) Project(com.google.refine.model.Project) EditInspector(org.openrefine.wikidata.qa.EditInspector) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) QAWarningStore(org.openrefine.wikidata.qa.QAWarningStore) Engine(com.google.refine.browsing.Engine)

Example 2 with WikibaseSchema

use of org.openrefine.wikidata.schema.WikibaseSchema in project OpenRefine by OpenRefine.

the class QuickStatementsExporterTest method testSimpleProject.

@Test
public void testSimpleProject() throws IOException {
    Project project = this.createCSVProject(TestingData.inceptionWithNewCsv);
    TestingData.reconcileInceptionCells(project);
    String serialized = TestingData.jsonFromFile("schema/inception.json");
    WikibaseSchema schema = WikibaseSchema.reconstruct(serialized);
    project.overlayModels.put("wikibaseSchema", schema);
    Engine engine = new Engine(project);
    StringWriter writer = new StringWriter();
    Properties properties = new Properties();
    exporter.export(project, properties, engine, writer);
    assertEquals(TestingData.inceptionWithNewQS, writer.toString());
}
Also used : Project(com.google.refine.model.Project) StringWriter(java.io.StringWriter) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) Properties(java.util.Properties) Engine(com.google.refine.browsing.Engine) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Example 3 with WikibaseSchema

use of org.openrefine.wikidata.schema.WikibaseSchema in project OpenRefine by OpenRefine.

the class SchemaPropertyExtractorTest method testNoProperties.

@Test
public void testNoProperties() {
    schema = new WikibaseSchema();
    SchemaPropertyExtractor extractor = new SchemaPropertyExtractor();
    Set<PropertyIdValue> propertyIdValues = extractor.getAllProperties(schema);
    Assert.assertEquals(propertyIdValues, new HashSet<>());
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test) CommandTest(org.openrefine.wikidata.commands.CommandTest)

Example 4 with WikibaseSchema

use of org.openrefine.wikidata.schema.WikibaseSchema in project OpenRefine by OpenRefine.

the class SaveWikibaseSchemaOperationTest method testLoadChange.

@Test
public void testLoadChange() throws Exception {
    String schemaJson = TestingData.jsonFromFile("schema/inception.json");
    String changeString = "newSchema=" + schemaJson + "\n" + "oldSchema=\n" + "/ec/";
    WikibaseSchema schema = WikibaseSchema.reconstruct(schemaJson);
    LineNumberReader reader = makeReader(changeString);
    Change change = SaveWikibaseSchemaOperation.WikibaseSchemaChange.load(reader, pool);
    change.apply(project);
    assertEquals(schema, project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey));
    change.revert(project);
    assertNull(project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey));
    // not checking for equality because JSON serialization varies
    saveChange(change);
}
Also used : WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) Change(com.google.refine.history.Change) LineNumberReader(java.io.LineNumberReader) Test(org.testng.annotations.Test)

Example 5 with WikibaseSchema

use of org.openrefine.wikidata.schema.WikibaseSchema in project OpenRefine by OpenRefine.

the class SchemaExporter method export.

@Override
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
    WikibaseSchema schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema");
    if (schema == null) {
        schema = new WikibaseSchema();
    }
    ParsingUtilities.mapper.writeValue(writer, schema);
}
Also used : WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema)

Aggregations

WikibaseSchema (org.openrefine.wikidata.schema.WikibaseSchema)6 Project (com.google.refine.model.Project)3 Test (org.testng.annotations.Test)3 Engine (com.google.refine.browsing.Engine)2 IOException (java.io.IOException)2 Properties (java.util.Properties)2 ServletException (javax.servlet.ServletException)2 Command (com.google.refine.commands.Command)1 Change (com.google.refine.history.Change)1 AbstractOperation (com.google.refine.model.AbstractOperation)1 Process (com.google.refine.process.Process)1 LineNumberReader (java.io.LineNumberReader)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CommandTest (org.openrefine.wikidata.commands.CommandTest)1 CommandUtilities.respondError (org.openrefine.wikidata.commands.CommandUtilities.respondError)1 Manifest (org.openrefine.wikidata.manifests.Manifest)1