Add license headers, general cleanups on Java files

This commit is contained in:
Antonin Delpeuch 2018-03-04 00:14:59 +00:00
parent d6b229f25e
commit 0b14a1a627
140 changed files with 5494 additions and 2592 deletions

View File

@ -1,10 +1,32 @@
#-------------------------------------------------------------------------------
# Copyright (C) 2018 Antonin Delpeuch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-------------------------------------------------------------------------------
Sources for the images:
- Information.svg by User:Bobarino: The following images were obtained from Wikimedia Commons:
- Information.svg by User:Bobarino: (CC BY-SA)
https://commons.wikimedia.org/wiki/File:Information.svg https://commons.wikimedia.org/wiki/File:Information.svg
- Warning.svg by User:Ezekiel63745 - Warning.svg by User:Ezekiel63745 (CC BY-SA)
https://commons.wikimedia.org/wiki/File:Information_orange.svg https://commons.wikimedia.org/wiki/File:Information_orange.svg
- Important.svg originally from David Vignoni - Important.svg originally from David Vignoni (GNU LGPL)
https://commons.wikimedia.org/wiki/File:Nuvola_apps_important.svg https://commons.wikimedia.org/wiki/File:Nuvola_apps_important.svg
- Critical.svg by User:Stannered - Critical.svg by User:Stannered (CC BY-SA)
https://commons.wikimedia.org/wiki/File:Stop_x_nuvola.svg https://commons.wikimedia.org/wiki/File:Stop_x_nuvola.svg

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import java.io.IOException; import java.io.IOException;
@ -14,13 +37,13 @@ import org.openrefine.wikidata.editing.ConnectionManager;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
public class LoginCommand extends Command { public class LoginCommand extends Command {
@Override @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
String username = request.getParameter("wb-username"); String username = request.getParameter("wb-username");
String password = request.getParameter("wb-password"); String password = request.getParameter("wb-password");
String remember = request.getParameter("remember-credentials"); String remember = request.getParameter("remember-credentials");
System.out.println(remember);
ConnectionManager manager = ConnectionManager.getInstance(); ConnectionManager manager = ConnectionManager.getInstance();
if (username != null && password != null) { if (username != null && password != null) {
manager.login(username, password, "on".equals(remember)); manager.login(username, password, "on".equals(remember));

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -15,8 +38,7 @@ public class PerformWikibaseEditsCommand extends EngineDependentCommand {
protected AbstractOperation createOperation(Project project, HttpServletRequest request, JSONObject engineConfig) protected AbstractOperation createOperation(Project project, HttpServletRequest request, JSONObject engineConfig)
throws Exception { throws Exception {
String summary = request.getParameter("summary"); String summary = request.getParameter("summary");
return new PerformWikibaseEditsOperation(engineConfig, return new PerformWikibaseEditsOperation(engineConfig, summary);
summary);
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
/* /*
Copyright 2010, Google Inc. Copyright 2010, Google Inc.
@ -34,8 +57,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -63,6 +84,7 @@ import com.google.refine.model.Project;
import com.google.refine.util.ParsingUtilities; import com.google.refine.util.ParsingUtilities;
public class PreviewWikibaseSchemaCommand extends Command { public class PreviewWikibaseSchemaCommand extends Command {
@Override @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
@ -75,7 +97,6 @@ public class PreviewWikibaseSchemaCommand extends Command {
String jsonString = request.getParameter("schema"); String jsonString = request.getParameter("schema");
WikibaseSchema schema = null; WikibaseSchema schema = null;
if (jsonString != null) { if (jsonString != null) {
try { try {

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import java.io.IOException; import java.io.IOException;

View File

@ -1,41 +1,61 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.wikibaseapi.ApiConnection; import org.wikidata.wdtk.wikibaseapi.ApiConnection;
import org.wikidata.wdtk.wikibaseapi.LoginFailedException; import org.wikidata.wdtk.wikibaseapi.LoginFailedException;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.preference.PreferenceStore; import com.google.refine.preference.PreferenceStore;
/** /**
* Manages a connection to Wikidata, with login credentials stored * Manages a connection to Wikidata, with login credentials stored in the
* in the preferences. * preferences.
* *
* Ideally, we should store only the cookies and not the password. * Ideally, we should store only the cookies and not the password. But
* But Wikidata-Toolkit does not allow for that as cookies are kept * Wikidata-Toolkit does not allow for that as cookies are kept private.
* private.
* *
* This class is also hard-coded for Wikidata: generalization to other * This class is also hard-coded for Wikidata: generalization to other Wikibase
* Wikibase instances should be feasible though. * instances should be feasible though.
* *
* @author antonin * @author Antonin Delpeuch
*/ */
public class ConnectionManager { public class ConnectionManager {
public static final String PREFERENCE_STORE_KEY = "wikidata_credentials"; public static final String PREFERENCE_STORE_KEY = "wikidata_credentials";
private PreferenceStore prefStore; private PreferenceStore prefStore;
private ApiConnection connection; private ApiConnection connection;
private static class ConnectionManagerHolder { private static class ConnectionManagerHolder {
private static final ConnectionManager instance = new ConnectionManager(); private static final ConnectionManager instance = new ConnectionManager();
} }
@ -77,8 +97,7 @@ public class ConnectionManager {
if (savedCredentials != null) { if (savedCredentials != null) {
connection = ApiConnection.getWikidataApiConnection(); connection = ApiConnection.getWikidataApiConnection();
try { try {
connection.login(savedCredentials.getString("username"), connection.login(savedCredentials.getString("username"), savedCredentials.getString("password"));
savedCredentials.getString("password"));
} catch (LoginFailedException e) { } catch (LoginFailedException e) {
connection = null; connection = null;
} catch (JSONException e) { } catch (JSONException e) {

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import java.io.IOException; import java.io.IOException;
@ -28,8 +51,7 @@ import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;
*/ */
public class EditBatchProcessor { public class EditBatchProcessor {
static final Logger logger = LoggerFactory static final Logger logger = LoggerFactory.getLogger(EditBatchProcessor.class);
.getLogger(EditBatchProcessor.class);
private WikibaseDataFetcher fetcher; private WikibaseDataFetcher fetcher;
private WikibaseDataEditor editor; private WikibaseDataEditor editor;
@ -44,11 +66,10 @@ public class EditBatchProcessor {
private Map<String, EntityDocument> currentDocs; private Map<String, EntityDocument> currentDocs;
private int batchSize; private int batchSize;
/** /**
* Initiates the process of pushing a batch of updates * Initiates the process of pushing a batch of updates to Wikibase. This
* to Wikibase. This schedules the updates and is a * schedules the updates and is a prerequisite for calling
* prerequisite for calling {@link performOneEdit}. * {@link performOneEdit}.
* *
* @param fetcher * @param fetcher
* the fetcher to use to retrieve the current state of items * the fetcher to use to retrieve the current state of items
@ -61,14 +82,11 @@ public class EditBatchProcessor {
* @param summary * @param summary
* the summary to append to all edits * the summary to append to all edits
* @param batchSize * @param batchSize
* the number of items that should be retrieved in one go from the API * the number of items that should be retrieved in one go from the
* API
*/ */
public EditBatchProcessor(WikibaseDataFetcher fetcher, public EditBatchProcessor(WikibaseDataFetcher fetcher, WikibaseDataEditor editor, List<ItemUpdate> updates,
WikibaseDataEditor editor, NewItemLibrary library, String summary, int batchSize) {
List<ItemUpdate> updates,
NewItemLibrary library,
String summary,
int batchSize) {
this.fetcher = fetcher; this.fetcher = fetcher;
this.editor = editor; this.editor = editor;
editor.setEditAsBot(true); // this will not do anything if the user does not editor.setEditAsBot(true); // this will not do anything if the user does not
@ -88,13 +106,13 @@ public class EditBatchProcessor {
this.currentDocs = Collections.emptyMap(); this.currentDocs = Collections.emptyMap();
} }
/** /**
* Performs the next edit in the batch. * Performs the next edit in the batch.
* *
* @throws InterruptedException * @throws InterruptedException
*/ */
public void performEdit() throws InterruptedException { public void performEdit()
throws InterruptedException {
if (remainingEdits() == 0) { if (remainingEdits() == 0) {
return; return;
} }
@ -116,8 +134,7 @@ public class EditBatchProcessor {
ItemDocument itemDocument = Datamodel.makeItemDocument(update.getItemId(), ItemDocument itemDocument = Datamodel.makeItemDocument(update.getItemId(),
update.getLabels().stream().collect(Collectors.toList()), update.getLabels().stream().collect(Collectors.toList()),
update.getDescriptions().stream().collect(Collectors.toList()), update.getDescriptions().stream().collect(Collectors.toList()),
update.getAliases().stream().collect(Collectors.toList()), update.getAliases().stream().collect(Collectors.toList()), update.getAddedStatementGroups(),
update.getAddedStatementGroups(),
Collections.emptyMap()); Collections.emptyMap());
ItemDocument createdDoc = editor.createItemDocument(itemDocument, summary); ItemDocument createdDoc = editor.createItemDocument(itemDocument, summary);
@ -125,28 +142,12 @@ public class EditBatchProcessor {
} else { } else {
// Existing item // Existing item
ItemDocument currentDocument = (ItemDocument) currentDocs.get(update.getItemId().getId()); ItemDocument currentDocument = (ItemDocument) currentDocs.get(update.getItemId().getId());
/* editor.updateTermsStatements(currentDocument, update.getLabels().stream().collect(Collectors.toList()),
TermStatementUpdate tsUpdate = new TermStatementUpdate(
currentDocument,
update.getAddedStatements().stream().collect(Collectors.toList()),
update.getDeletedStatements().stream().collect(Collectors.toList()),
update.getLabels().stream().collect(Collectors.toList()),
update.getDescriptions().stream().collect(Collectors.toList()),
update.getAliases().stream().collect(Collectors.toList()),
new ArrayList<MonolingualTextValue>()
);
ObjectMapper mapper = new ObjectMapper();
logger.info(mapper.writeValueAsString(update));
logger.info(update.toString());
logger.info(tsUpdate.getJsonUpdateString()); */
editor.updateTermsStatements(currentDocument,
update.getLabels().stream().collect(Collectors.toList()),
update.getDescriptions().stream().collect(Collectors.toList()), update.getDescriptions().stream().collect(Collectors.toList()),
update.getAliases().stream().collect(Collectors.toList()), update.getAliases().stream().collect(Collectors.toList()),
new ArrayList<MonolingualTextValue>(), new ArrayList<MonolingualTextValue>(),
update.getAddedStatements().stream().collect(Collectors.toList()), update.getAddedStatements().stream().collect(Collectors.toList()),
update.getDeletedStatements().stream().collect(Collectors.toList()), update.getDeletedStatements().stream().collect(Collectors.toList()), summary);
summary);
} }
} catch (MediaWikiApiErrorException e) { } catch (MediaWikiApiErrorException e) {
// TODO find a way to report these errors to the user in a nice way // TODO find a way to report these errors to the user in a nice way
@ -172,7 +173,8 @@ public class EditBatchProcessor {
return (100 * (globalCursor + batchCursor)) / scheduled.size(); return (100 * (globalCursor + batchCursor)) / scheduled.size();
} }
protected void prepareNewBatch() throws InterruptedException { protected void prepareNewBatch()
throws InterruptedException {
// remove the previous batch from the remainingUpdates // remove the previous batch from the remainingUpdates
globalCursor += currentBatch.size(); globalCursor += currentBatch.size();
currentBatch.clear(); currentBatch.clear();
@ -183,9 +185,7 @@ public class EditBatchProcessor {
} else { } else {
currentBatch = remainingUpdates.subList(0, batchSize); currentBatch = remainingUpdates.subList(0, batchSize);
} }
List<String> qidsToFetch = currentBatch.stream() List<String> qidsToFetch = currentBatch.stream().filter(u -> !u.isNew()).map(u -> u.getItemId().getId())
.filter(u -> !u.isNew())
.map(u -> u.getItemId().getId())
.collect(Collectors.toList()); .collect(Collectors.toList());
// Get the current documents for this batch of updates // Get the current documents for this batch of updates

View File

@ -1,28 +1,49 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import com.google.refine.model.Project; import java.util.Set;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
import com.google.refine.model.Column; import com.google.refine.model.Column;
import com.google.refine.model.Project;
import com.google.refine.model.Recon; import com.google.refine.model.Recon;
import com.google.refine.model.ReconCandidate; import com.google.refine.model.ReconCandidate;
import com.google.refine.model.ReconStats; import com.google.refine.model.ReconStats;
import com.google.refine.model.Row; import com.google.refine.model.Row;
/** /**
* This keeps track of the new items that we * This keeps track of the new items that we have created for each internal
* have created for each internal reconciliation id. * reconciliation id.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public class NewItemLibrary { public class NewItemLibrary {
@ -40,7 +61,9 @@ public class NewItemLibrary {
/** /**
* Retrieves the Qid allocated to a given new cell * Retrieves the Qid allocated to a given new cell
* @param id: the fake ItemId generated by the cell *
* @param id:
* the fake ItemId generated by the cell
* @return the qid (or null if unallocated yet) * @return the qid (or null if unallocated yet)
*/ */
public String getQid(long id) { public String getQid(long id) {
@ -49,30 +72,32 @@ public class NewItemLibrary {
/** /**
* Stores a Qid associated to a new cell * Stores a Qid associated to a new cell
* @param id : the internal reconciliation id of the new cell *
* @param qid : the associated Qid returned by Wikibase * @param id
* : the internal reconciliation id of the new cell
* @param qid
* : the associated Qid returned by Wikibase
*/ */
public void setQid(long id, String qid) { public void setQid(long id, String qid) {
map.put(id, qid); map.put(id, qid);
} }
/** /**
* Changes the "new" reconciled cells to their allocated * Changes the "new" reconciled cells to their allocated qids for later use.
* qids for later use.
* *
* @param reset: set to true to revert the operation (set cells to "new") * @param reset:
* set to true to revert the operation (set cells to "new")
*/ */
public void updateReconciledCells(Project project, boolean reset) { public void updateReconciledCells(Project project, boolean reset) {
Set<Integer> impactedColumns = new HashSet<>(); Set<Integer> impactedColumns = new HashSet<>();
/* /*
* Note that there is a slight violation of OpenRefine's model here: * Note that there is a slight violation of OpenRefine's model here: if we
* if we reconcile multiple cells to the same new item, and then * reconcile multiple cells to the same new item, and then perform this
* perform this operation on a subset of the corresponding rows, * operation on a subset of the corresponding rows, we are going to modify cells
* we are going to modify cells that are outside the facet (because * that are outside the facet (because they are reconciled to the same cell).
* they are reconciled to the same cell). But I think this is the * But I think this is the right thing to do.
* right thing to do.
*/ */
for (Row row : project.rows) { for (Row row : project.rows) {
@ -82,17 +107,14 @@ public class NewItemLibrary {
continue; continue;
} }
Recon recon = cell.recon; Recon recon = cell.recon;
if (Recon.Judgment.New.equals(recon.judgment) && !reset && if (Recon.Judgment.New.equals(recon.judgment) && !reset
map.containsKey(recon.judgmentHistoryEntry)) { && map.containsKey(recon.judgmentHistoryEntry)) {
recon.judgment = Recon.Judgment.Matched; recon.judgment = Recon.Judgment.Matched;
recon.match = new ReconCandidate( recon.match = new ReconCandidate(map.get(recon.judgmentHistoryEntry), cell.value.toString(),
map.get(recon.judgmentHistoryEntry), new String[0], 100);
cell.value.toString(),
new String[0],
100);
impactedColumns.add(i); impactedColumns.add(i);
} else if (Recon.Judgment.Matched.equals(recon.judgment) && reset && } else if (Recon.Judgment.Matched.equals(recon.judgment) && reset
map.containsKey(recon.judgmentHistoryEntry)) { && map.containsKey(recon.judgmentHistoryEntry)) {
recon.judgment = Recon.Judgment.New; recon.judgment = Recon.Judgment.New;
recon.match = null; recon.match = null;
impactedColumns.add(i); impactedColumns.add(i);
@ -108,6 +130,7 @@ public class NewItemLibrary {
/** /**
* Getter, only meant to be used by Jackson * Getter, only meant to be used by Jackson
*
* @return the underlying map * @return the underlying map
*/ */
@JsonProperty("qidMap") @JsonProperty("qidMap")

View File

@ -1,33 +1,52 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue;
import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue; import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.helpers.DatamodelConverter; import org.wikidata.wdtk.datamodel.helpers.DatamodelConverter;
import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl; import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value;
/** /**
* A class that rewrites an {@link ItemUpdate}, * A class that rewrites an {@link ItemUpdate}, replacing reconciled entity id
* replacing reconciled entity id values by their concrete * values by their concrete values after creation of all the new items involved.
* values after creation of all the new items involved.
* *
* If an item has not been created yet, an {@link IllegalArgumentException} * If an item has not been created yet, an {@link IllegalArgumentException} will
* will be raised. * be raised.
* *
* The subject is treated as a special case: it is returned unchanged. * The subject is treated as a special case: it is returned unchanged. This is
* This is because it is guaranteed not to appear in the update (but * because it is guaranteed not to appear in the update (but it does appear in
* it does appear in the datamodel representation as the subject is passed around * the datamodel representation as the subject is passed around to the Claim
* to the Claim objects its document contains). * objects its document contains).
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -38,9 +57,8 @@ public class ReconEntityRewriter extends DatamodelConverter {
private ItemIdValue subject; private ItemIdValue subject;
/** /**
* Constructor. Sets up a rewriter which uses the provided library * Constructor. Sets up a rewriter which uses the provided library to look up
* to look up qids of new items, and the subject (which should not be * qids of new items, and the subject (which should not be rewritten).
* rewritten).
* *
* @param library * @param library
* @param subject * @param subject
@ -64,25 +82,21 @@ public class ReconEntityRewriter extends DatamodelConverter {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Trying to rewrite an update where a new item was not created yet."); "Trying to rewrite an update where a new item was not created yet.");
} }
return Datamodel.makeItemIdValue(newId, return Datamodel.makeItemIdValue(newId, recon.getRecon().identifierSpace);
recon.getRecon().identifierSpace);
} }
} }
return super.copy(value); return super.copy(value);
} }
public ItemUpdate rewrite(ItemUpdate update) { public ItemUpdate rewrite(ItemUpdate update) {
Set<MonolingualTextValue> labels = update.getLabels().stream() Set<MonolingualTextValue> labels = update.getLabels().stream().map(l -> copy(l)).collect(Collectors.toSet());
.map(l -> copy(l)).collect(Collectors.toSet()); Set<MonolingualTextValue> descriptions = update.getDescriptions().stream().map(l -> copy(l))
Set<MonolingualTextValue> descriptions = update.getDescriptions().stream() .collect(Collectors.toSet());
.map(l -> copy(l)).collect(Collectors.toSet()); Set<MonolingualTextValue> aliases = update.getAliases().stream().map(l -> copy(l)).collect(Collectors.toSet());
Set<MonolingualTextValue> aliases = update.getAliases().stream() List<Statement> addedStatements = update.getAddedStatements().stream().map(l -> copy(l))
.map(l -> copy(l)).collect(Collectors.toSet()); .collect(Collectors.toList());
List<Statement> addedStatements = update.getAddedStatements().stream() Set<Statement> deletedStatements = update.getDeletedStatements().stream().map(l -> copy(l))
.map(l -> copy(l)).collect(Collectors.toList()); .collect(Collectors.toSet());
Set<Statement> deletedStatements = update.getDeletedStatements().stream() return new ItemUpdate(update.getItemId(), addedStatements, deletedStatements, labels, descriptions, aliases);
.map(l -> copy(l)).collect(Collectors.toSet());
return new ItemUpdate(update.getItemId(), addedStatements,
deletedStatements, labels, descriptions, aliases);
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import java.util.Properties; import java.util.Properties;
@ -9,10 +32,10 @@ import org.json.JSONWriter;
import com.google.refine.Jsonizable; import com.google.refine.Jsonizable;
/** /**
* This is just the necessary bits to store Wikidata credentials * This is just the necessary bits to store Wikidata credentials in OpenRefine's
* in OpenRefine's preference store. * preference store.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
class WikibaseCredentials implements Jsonizable { class WikibaseCredentials implements Jsonizable {
@ -55,11 +78,9 @@ class WikibaseCredentials implements Jsonizable {
writer.endObject(); writer.endObject();
} }
public static WikibaseCredentials load(JSONObject obj) throws JSONException { public static WikibaseCredentials load(JSONObject obj)
return new WikibaseCredentials( throws JSONException {
obj.getString("username"), return new WikibaseCredentials(obj.getString("username"), obj.getString("password"));
obj.getString("password"));
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.exporters; package org.openrefine.wikidata.exporters;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -14,14 +37,12 @@ import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor; import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
/** /**
* Prints a Wikibase value as a string as required by QuickStatements. * Prints a Wikibase value as a string as required by QuickStatements. Format
* Format documentation: * documentation: https://www.wikidata.org/wiki/Help:QuickStatements
* https://www.wikidata.org/wiki/Help:QuickStatements
* *
* Any new entity id will be * Any new entity id will be assumed to be the last one created, represented
* assumed to be the last one created, represented with "LAST". It is * with "LAST". It is fine to do this assumption because we are working on edit
* fine to do this assumption because we are working on edit batches * batches previously scheduled by {@link QuickStatementsUpdateScheduler}.
* previously scheduled by {@link QuickStatementsUpdateScheduler}.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -45,19 +66,12 @@ public class QSValuePrinter implements ValueVisitor<String> {
@Override @Override
public String visit(GlobeCoordinatesValue value) { public String visit(GlobeCoordinatesValue value) {
return String.format( return String.format(Locale.US, "@%f/%f", value.getLatitude(), value.getLongitude());
Locale.US,
"@%f/%f",
value.getLatitude(),
value.getLongitude());
} }
@Override @Override
public String visit(MonolingualTextValue value) { public String visit(MonolingualTextValue value) {
return String.format( return String.format("%s:\"%s\"", value.getLanguageCode(), value.getText());
"%s:\"%s\"",
value.getLanguageCode(),
value.getText());
} }
@Override @Override
@ -66,22 +80,16 @@ public class QSValuePrinter implements ValueVisitor<String> {
String unitIri = value.getUnit(); String unitIri = value.getUnit();
String unitRepresentation = "", boundsRepresentation = ""; String unitRepresentation = "", boundsRepresentation = "";
if (!unitIri.isEmpty()) { if (!unitIri.isEmpty()) {
if (!unitIri.startsWith(unitPrefix)) if (!unitIri.startsWith(unitPrefix)) return null; // QuickStatements only accepts Qids as units
return null; // QuickStatements only accepts Qids as units
unitRepresentation = "U" + unitIri.substring(unitPrefix.length()); unitRepresentation = "U" + unitIri.substring(unitPrefix.length());
} }
if (value.getLowerBound() != null) { if (value.getLowerBound() != null) {
// bounds are always null at the same time so we know they are both not null // bounds are always null at the same time so we know they are both not null
BigDecimal lowerBound = value.getLowerBound(); BigDecimal lowerBound = value.getLowerBound();
BigDecimal upperBound = value.getUpperBound(); BigDecimal upperBound = value.getUpperBound();
boundsRepresentation = String.format(Locale.US, "[%s,%s]", boundsRepresentation = String.format(Locale.US, "[%s,%s]", lowerBound.toString(), upperBound.toString());
lowerBound.toString(), upperBound.toString());
} }
return String.format( return String.format(Locale.US, "%s%s%s", value.getNumericValue().toString(), boundsRepresentation,
Locale.US,
"%s%s%s",
value.getNumericValue().toString(),
boundsRepresentation,
unitRepresentation); unitRepresentation);
} }
@ -92,14 +100,7 @@ public class QSValuePrinter implements ValueVisitor<String> {
@Override @Override
public String visit(TimeValue value) { public String visit(TimeValue value) {
return String.format( return String.format("+%04d-%02d-%02dT%02d:%02d:%02dZ/%d", value.getYear(), value.getMonth(), value.getDay(),
"+%04d-%02d-%02dT%02d:%02d:%02dZ/%d", value.getHour(), value.getMinute(), value.getSecond(), value.getPrecision());
value.getYear(),
value.getMonth(),
value.getDay(),
value.getHour(),
value.getMinute(),
value.getSecond(),
value.getPrecision());
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.exporters; package org.openrefine.wikidata.exporters;
import java.io.IOException; import java.io.IOException;
@ -30,10 +53,8 @@ public class QuickStatementsExporter implements WriterExporter {
final static Logger logger = LoggerFactory.getLogger("QuickStatementsExporter"); final static Logger logger = LoggerFactory.getLogger("QuickStatementsExporter");
public static final String impossibleSchedulingErrorMessage = public static final String impossibleSchedulingErrorMessage = "This edit batch cannot be performed with QuickStatements due to the structure of its new items.";
"This edit batch cannot be performed with QuickStatements due to the structure of its new items."; public static final String noSchemaErrorMessage = "No schema was provided. You need to align your project with Wikidata first.";
public static final String noSchemaErrorMessage =
"No schema was provided. You need to align your project with Wikidata first.";
public QuickStatementsExporter() { public QuickStatementsExporter() {
} }
@ -67,12 +88,14 @@ public class QuickStatementsExporter implements WriterExporter {
* the writer to which the QS should be written * the writer to which the QS should be written
* @throws IOException * @throws IOException
*/ */
public void translateSchema(Project project, Engine engine, WikibaseSchema schema, Writer writer) throws IOException { public void translateSchema(Project project, Engine engine, WikibaseSchema schema, Writer writer)
throws IOException {
List<ItemUpdate> items = schema.evaluate(project, engine); List<ItemUpdate> items = schema.evaluate(project, engine);
translateItemList(items, writer); translateItemList(items, writer);
} }
public void translateItemList(List<ItemUpdate> updates, Writer writer) throws IOException { public void translateItemList(List<ItemUpdate> updates, Writer writer)
throws IOException {
QuickStatementsUpdateScheduler scheduler = new QuickStatementsUpdateScheduler(); QuickStatementsUpdateScheduler scheduler = new QuickStatementsUpdateScheduler();
try { try {
List<ItemUpdate> scheduled = scheduler.schedule(updates); List<ItemUpdate> scheduled = scheduler.schedule(updates);
@ -85,7 +108,9 @@ public class QuickStatementsExporter implements WriterExporter {
} }
protected void translateNameDescr(String qid, Set<MonolingualTextValue> values, String prefix, ItemIdValue id, Writer writer) throws IOException { protected void translateNameDescr(String qid, Set<MonolingualTextValue> values, String prefix, ItemIdValue id,
Writer writer)
throws IOException {
for (MonolingualTextValue value : values) { for (MonolingualTextValue value : values) {
writer.write(qid + "\t"); writer.write(qid + "\t");
writer.write(prefix); writer.write(prefix);
@ -96,7 +121,8 @@ public class QuickStatementsExporter implements WriterExporter {
} }
} }
protected void translateItem(ItemUpdate item, Writer writer) throws IOException { protected void translateItem(ItemUpdate item, Writer writer)
throws IOException {
String qid = item.getItemId().getId(); String qid = item.getItemId().getId();
if (item.isNew()) { if (item.isNew()) {
writer.write("CREATE\n"); writer.write("CREATE\n");
@ -116,7 +142,8 @@ public class QuickStatementsExporter implements WriterExporter {
} }
} }
protected void translateStatement(String qid, Statement statement, String pid, boolean add, Writer writer) throws IOException { protected void translateStatement(String qid, Statement statement, String pid, boolean add, Writer writer)
throws IOException {
Claim claim = statement.getClaim(); Claim claim = statement.getClaim();
Value val = claim.getValue(); Value val = claim.getValue();
@ -140,13 +167,15 @@ public class QuickStatementsExporter implements WriterExporter {
} }
} }
protected void translateSnakGroup(SnakGroup sg, boolean reference, Writer writer) throws IOException { protected void translateSnakGroup(SnakGroup sg, boolean reference, Writer writer)
throws IOException {
for (Snak s : sg.getSnaks()) { for (Snak s : sg.getSnaks()) {
translateSnak(s, reference, writer); translateSnak(s, reference, writer);
} }
} }
protected void translateSnak(Snak s, boolean reference, Writer writer) throws IOException { protected void translateSnak(Snak s, boolean reference, Writer writer)
throws IOException {
String pid = s.getPropertyId().getId(); String pid = s.getPropertyId().getId();
if (reference) { if (reference) {
pid = pid.replace('P', 'S'); pid = pid.replace('P', 'S');
@ -159,5 +188,4 @@ public class QuickStatementsExporter implements WriterExporter {
} }
} }
} }

View File

@ -1,44 +1,48 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.operations; package org.openrefine.wikidata.operations;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.Writer; import java.io.Writer;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.openrefine.wikidata.editing.ConnectionManager; import org.openrefine.wikidata.editing.ConnectionManager;
import org.openrefine.wikidata.editing.EditBatchProcessor; import org.openrefine.wikidata.editing.EditBatchProcessor;
import org.openrefine.wikidata.editing.NewItemLibrary; import org.openrefine.wikidata.editing.NewItemLibrary;
import org.openrefine.wikidata.editing.ReconEntityRewriter;
import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.scheduler.ImpossibleSchedulingException;
import org.openrefine.wikidata.updates.scheduler.UpdateScheduler;
import org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
import org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue; import org.openrefine.wikidata.updates.ItemUpdate;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl;
import org.wikidata.wdtk.datamodel.interfaces.DataObjectFactory;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.util.WebResourceFetcherImpl; import org.wikidata.wdtk.util.WebResourceFetcherImpl;
import org.wikidata.wdtk.wikibaseapi.ApiConnection; import org.wikidata.wdtk.wikibaseapi.ApiConnection;
import org.wikidata.wdtk.wikibaseapi.TermStatementUpdate;
import org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor; import org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor;
import org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher; import org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;
import org.wikidata.wdtk.datamodel.interfaces.SiteLink;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -53,16 +57,13 @@ import com.google.refine.process.LongRunningProcess;
import com.google.refine.process.Process; import com.google.refine.process.Process;
import com.google.refine.util.Pool; import com.google.refine.util.Pool;
public class PerformWikibaseEditsOperation extends EngineDependentOperation { public class PerformWikibaseEditsOperation extends EngineDependentOperation {
static final Logger logger = LoggerFactory
.getLogger(PerformWikibaseEditsOperation.class); static final Logger logger = LoggerFactory.getLogger(PerformWikibaseEditsOperation.class);
private String summary; private String summary;
public PerformWikibaseEditsOperation( public PerformWikibaseEditsOperation(JSONObject engineConfig, String summary) {
JSONObject engineConfig,
String summary) {
super(engineConfig); super(engineConfig);
this.summary = summary; this.summary = summary;
@ -76,12 +77,9 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
if (obj.has("summary")) { if (obj.has("summary")) {
summary = obj.getString("summary"); summary = obj.getString("summary");
} }
return new PerformWikibaseEditsOperation( return new PerformWikibaseEditsOperation(engineConfig, summary);
engineConfig,
summary);
} }
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
@ -103,13 +101,9 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
} }
@Override @Override
public Process createProcess(Project project, Properties options) throws Exception { public Process createProcess(Project project, Properties options)
return new PerformEditsProcess( throws Exception {
project, return new PerformEditsProcess(project, createEngine(project), getBriefDescription(project), summary);
createEngine(project),
getBriefDescription(project),
summary
);
} }
static public class PerformWikibaseEditsChange implements Change { static public class PerformWikibaseEditsChange implements Change {
@ -171,8 +165,7 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
protected String _summary; protected String _summary;
protected final long _historyEntryID; protected final long _historyEntryID;
protected PerformEditsProcess(Project project, protected PerformEditsProcess(Project project, Engine engine, String description, String summary) {
Engine engine, String description, String summary) {
super(description); super(description);
this._project = project; this._project = project;
this._engine = engine; this._engine = engine;
@ -199,8 +192,8 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
// Prepare the edits // Prepare the edits
NewItemLibrary newItemLibrary = new NewItemLibrary(); NewItemLibrary newItemLibrary = new NewItemLibrary();
EditBatchProcessor processor = new EditBatchProcessor(wbdf, EditBatchProcessor processor = new EditBatchProcessor(wbdf, wbde, itemDocuments, newItemLibrary, _summary,
wbde, itemDocuments, newItemLibrary, _summary, 50); 50);
// Perform edits // Perform edits
logger.info("Performing edits"); logger.info("Performing edits");
@ -221,13 +214,8 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
if (!_canceled) { if (!_canceled) {
Change change = new PerformWikibaseEditsChange(newItemLibrary); Change change = new PerformWikibaseEditsChange(newItemLibrary);
HistoryEntry historyEntry = new HistoryEntry( HistoryEntry historyEntry = new HistoryEntry(_historyEntryID, _project, _description,
_historyEntryID, PerformWikibaseEditsOperation.this, change);
_project,
_description,
PerformWikibaseEditsOperation.this,
change
);
_project.history.addEntry(historyEntry); _project.history.addEntry(historyEntry);
_project.processManager.onDoneProcess(this); _project.processManager.onDoneProcess(this);

View File

@ -1,24 +1,38 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.operations; package org.openrefine.wikidata.operations;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.openrefine.wikidata.schema.WbItemConstant;
import org.openrefine.wikidata.schema.WbItemDocumentExpr;
import org.openrefine.wikidata.schema.WbNameDescExpr;
import org.openrefine.wikidata.schema.WbStatementGroupExpr;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.refine.history.Change; import com.google.refine.history.Change;
import com.google.refine.history.HistoryEntry; import com.google.refine.history.HistoryEntry;
import com.google.refine.model.AbstractOperation; import com.google.refine.model.AbstractOperation;
@ -34,13 +48,11 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
public SaveWikibaseSchemaOperation(WikibaseSchema schema) { public SaveWikibaseSchemaOperation(WikibaseSchema schema) {
this._schema = schema; this._schema = schema;
} }
static public AbstractOperation reconstruct(Project project, JSONObject obj) static public AbstractOperation reconstruct(Project project, JSONObject obj)
throws Exception { throws Exception {
return new SaveWikibaseSchemaOperation(WikibaseSchema.reconstruct(obj return new SaveWikibaseSchemaOperation(WikibaseSchema.reconstruct(obj.getJSONObject("schema")));
.getJSONObject("schema")));
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
@ -62,17 +74,17 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
} }
@Override @Override
protected HistoryEntry createHistoryEntry(Project project, protected HistoryEntry createHistoryEntry(Project project, long historyEntryID)
long historyEntryID) throws Exception { throws Exception {
String description = "Save Wikibase schema skeleton"; String description = "Save Wikibase schema skeleton";
Change change = new WikibaseSchemaChange(_schema); Change change = new WikibaseSchemaChange(_schema);
return new HistoryEntry(historyEntryID, project, description, return new HistoryEntry(historyEntryID, project, description, SaveWikibaseSchemaOperation.this, change);
SaveWikibaseSchemaOperation.this, change);
} }
static public class WikibaseSchemaChange implements Change { static public class WikibaseSchemaChange implements Change {
final protected WikibaseSchema _newSchema; final protected WikibaseSchema _newSchema;
protected WikibaseSchema _oldSchema = null; protected WikibaseSchema _oldSchema = null;
public final static String overlayModelKey = "wikibaseSchema"; public final static String overlayModelKey = "wikibaseSchema";
@ -98,7 +110,8 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
} }
} }
public void save(Writer writer, Properties options) throws IOException { public void save(Writer writer, Properties options)
throws IOException {
writer.write("newSchema="); writer.write("newSchema=");
writeWikibaseSchema(_newSchema, writer); writeWikibaseSchema(_newSchema, writer);
writer.write('\n'); writer.write('\n');
@ -120,11 +133,9 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
String value = line.substring(equal + 1); String value = line.substring(equal + 1);
if ("oldSchema".equals(field) && value.length() > 0) { if ("oldSchema".equals(field) && value.length() > 0) {
oldSchema = WikibaseSchema.reconstruct(ParsingUtilities oldSchema = WikibaseSchema.reconstruct(ParsingUtilities.evaluateJsonStringToObject(value));
.evaluateJsonStringToObject(value));
} else if ("newSchema".equals(field) && value.length() > 0) { } else if ("newSchema".equals(field) && value.length() > 0) {
newSchema = WikibaseSchema.reconstruct(ParsingUtilities newSchema = WikibaseSchema.reconstruct(ParsingUtilities.evaluateJsonStringToObject(value));
.evaluateJsonStringToObject(value));
} }
} }

View File

@ -1,4 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import java.util.Set; import java.util.Set;
@ -14,16 +36,20 @@ import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
public interface ConstraintFetcher { public interface ConstraintFetcher {
/** /**
* Retrieves the regular expression for formatting a property, or null if * Retrieves the regular expression for formatting a property, or null if there
* there is no such constraint * is no such constraint
*
* @param pid * @param pid
* @return the expression of a regular expression which should be compatible with java.util.regex * @return the expression of a regular expression which should be compatible
* with java.util.regex
*/ */
String getFormatRegex(PropertyIdValue pid); String getFormatRegex(PropertyIdValue pid);
/** /**
* Retrieves the property that is the inverse of a given property * Retrieves the property that is the inverse of a given property
* @param pid: the property to retrieve the inverse for *
* @param pid:
* the property to retrieve the inverse for
* @return the pid of the inverse property * @return the pid of the inverse property
*/ */
PropertyIdValue getInversePid(PropertyIdValue pid); PropertyIdValue getInversePid(PropertyIdValue pid);
@ -44,12 +70,14 @@ public interface ConstraintFetcher {
boolean isForReferencesOnly(PropertyIdValue pid); boolean isForReferencesOnly(PropertyIdValue pid);
/** /**
* Get the list of allowed qualifiers (as property ids) for this property (null if any) * Get the list of allowed qualifiers (as property ids) for this property (null
* if any)
*/ */
Set<PropertyIdValue> allowedQualifiers(PropertyIdValue pid); Set<PropertyIdValue> allowedQualifiers(PropertyIdValue pid);
/** /**
* Get the list of mandatory qualifiers (as property ids) for this property (null if any) * Get the list of mandatory qualifiers (as property ids) for this property
* (null if any)
*/ */
Set<PropertyIdValue> mandatoryQualifiers(PropertyIdValue pid); Set<PropertyIdValue> mandatoryQualifiers(PropertyIdValue pid);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import java.util.HashMap; import java.util.HashMap;
@ -18,8 +41,6 @@ import org.openrefine.wikidata.qa.scrutinizers.SingleValueScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.UnsourcedScrutinizer; import org.openrefine.wikidata.qa.scrutinizers.UnsourcedScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.WhitespaceScrutinizer; import org.openrefine.wikidata.qa.scrutinizers.WhitespaceScrutinizer;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.scheduler.ImpossibleSchedulingException;
import org.openrefine.wikidata.updates.scheduler.UpdateScheduler;
import org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler; import org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
@ -30,6 +51,7 @@ import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
* *
*/ */
public class EditInspector { public class EditInspector {
private Map<String, EditScrutinizer> scrutinizers; private Map<String, EditScrutinizer> scrutinizers;
private QAWarningStore warningStore; private QAWarningStore warningStore;
private ConstraintFetcher fetcher; private ConstraintFetcher fetcher;
@ -55,6 +77,7 @@ public class EditInspector {
/** /**
* Adds a new scrutinizer to the inspector * Adds a new scrutinizer to the inspector
*
* @param scrutinizer * @param scrutinizer
*/ */
public void register(EditScrutinizer scrutinizer) { public void register(EditScrutinizer scrutinizer) {
@ -64,9 +87,9 @@ public class EditInspector {
scrutinizer.setFetcher(fetcher); scrutinizer.setFetcher(fetcher);
} }
/** /**
* Inspect a batch of edits with the registered scrutinizers * Inspect a batch of edits with the registered scrutinizers
*
* @param editBatch * @param editBatch
*/ */
public void inspect(List<ItemUpdate> editBatch) { public void inspect(List<ItemUpdate> editBatch) {
@ -81,10 +104,8 @@ public class EditInspector {
scrutinizer.scrutinize(mergedUpdates); scrutinizer.scrutinize(mergedUpdates);
} }
if (warningStore.getNbWarnings() == 0) { if (warningStore.getNbWarnings() == 0) {
warningStore.addWarning(new QAWarning( warningStore.addWarning(new QAWarning("no-issue-detected", null, QAWarning.Severity.INFO, 0));
"no-issue-detected", null, QAWarning.Severity.INFO, 0));
} }
} }
} }

View File

@ -1,23 +1,43 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties;
import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
import org.openrefine.wikidata.utils.JacksonJsonizable; import org.openrefine.wikidata.utils.JacksonJsonizable;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* A class to represent a QA warning emitted by the Wikidata schema * A class to represent a QA warning emitted by the Wikidata schema This could
* This could probably be reused at a broader scale, for instance for * probably be reused at a broader scale, for instance for Data Package
* Data Package validation. * validation.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -27,13 +47,15 @@ public class QAWarning extends JacksonJsonizable implements Comparable<QAWarning
public enum Severity { public enum Severity {
INFO, // We just report something to the user but it is probably fine INFO, // We just report something to the user but it is probably fine
WARNING, // Edits that look wrong but in some cases they are actually fine WARNING, // Edits that look wrong but in some cases they are actually fine
IMPORTANT, // There is almost surely something wrong about the edit but in rare cases we might want to allow it IMPORTANT, // There is almost surely something wrong about the edit but in rare cases we
// might want to allow it
CRITICAL, // We should never edit if there is a critical issue CRITICAL, // We should never edit if there is a critical issue
} }
/// The type of QA warning emitted /// The type of QA warning emitted
private final String type; private final String type;
// The key for aggregation of other QA warnings together - this specializes the id // The key for aggregation of other QA warnings together - this specializes the
// id
private final String bucketId; private final String bucketId;
// The severity of the issue // The severity of the issue
private final Severity severity; private final Severity severity;
@ -66,6 +88,7 @@ public class QAWarning extends JacksonJsonizable implements Comparable<QAWarning
/** /**
* Aggregates another QA warning of the same aggregation id. * Aggregates another QA warning of the same aggregation id.
*
* @param other * @param other
*/ */
public QAWarning aggregate(QAWarning other) { public QAWarning aggregate(QAWarning other) {
@ -75,8 +98,7 @@ public class QAWarning extends JacksonJsonizable implements Comparable<QAWarning
if (other.getSeverity().compareTo(severity) > 0) { if (other.getSeverity().compareTo(severity) > 0) {
newSeverity = other.getSeverity(); newSeverity = other.getSeverity();
} }
QAWarning merged = new QAWarning(getType(), getBucketId(), newSeverity, QAWarning merged = new QAWarning(getType(), getBucketId(), newSeverity, newCount);
newCount);
for (Entry<String, Object> entry : properties.entrySet()) { for (Entry<String, Object> entry : properties.entrySet()) {
merged.setProperty(entry.getKey(), entry.getValue()); merged.setProperty(entry.getKey(), entry.getValue());
} }
@ -87,10 +109,12 @@ public class QAWarning extends JacksonJsonizable implements Comparable<QAWarning
} }
/** /**
* Sets a property of the QA warning, to be used by the front-end * Sets a property of the QA warning, to be used by the front-end for display.
* for display. *
* @param key: the name of the property * @param key:
* @param value should be Jackson-serializable * the name of the property
* @param value
* should be Jackson-serializable
*/ */
public void setProperty(String key, Object value) { public void setProperty(String key, Object value) {
this.properties.put(key, value); this.properties.put(key, value);
@ -136,10 +160,8 @@ public class QAWarning extends JacksonJsonizable implements Comparable<QAWarning
return false; return false;
} }
QAWarning otherWarning = (QAWarning) other; QAWarning otherWarning = (QAWarning) other;
return type.equals(otherWarning.getType()) && return type.equals(otherWarning.getType()) && bucketId.equals(otherWarning.getBucketId())
bucketId.equals(otherWarning.getBucketId()) && && severity.equals(otherWarning.getSeverity()) && count == otherWarning.getCount()
severity.equals(otherWarning.getSeverity()) && && properties.equals(otherWarning.getProperties());
count == otherWarning.getCount() &&
properties.equals(otherWarning.getProperties());
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,6 +53,7 @@ public class QAWarningStore {
/** /**
* Stores a warning, aggregating it with any existing * Stores a warning, aggregating it with any existing
*
* @param warning * @param warning
*/ */
public void addWarning(QAWarning warning) { public void addWarning(QAWarning warning) {

View File

@ -1,16 +1,36 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.openrefine.wikidata.utils.EntityCache; import org.openrefine.wikidata.utils.EntityCache;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument; import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
@ -21,13 +41,14 @@ import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
/** /**
* This class provides an abstraction over the way constraint * This class provides an abstraction over the way constraint definitions are
* definitions are stored in Wikidata. * stored in Wikidata.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public class WikidataConstraintFetcher implements ConstraintFetcher { public class WikidataConstraintFetcher implements ConstraintFetcher {
public static String WIKIDATA_CONSTRAINT_PID = "P2302"; public static String WIKIDATA_CONSTRAINT_PID = "P2302";
public static String FORMAT_CONSTRAINT_QID = "Q21502404"; public static String FORMAT_CONSTRAINT_QID = "Q21502404";
@ -55,7 +76,6 @@ public class WikidataConstraintFetcher implements ConstraintFetcher {
public static String TYPE_CONSTRAINT_QID = "Q21503250"; public static String TYPE_CONSTRAINT_QID = "Q21503250";
@Override @Override
public String getFormatRegex(PropertyIdValue pid) { public String getFormatRegex(PropertyIdValue pid) {
List<SnakGroup> specs = getSingleConstraint(pid, FORMAT_CONSTRAINT_QID); List<SnakGroup> specs = getSingleConstraint(pid, FORMAT_CONSTRAINT_QID);
@ -129,11 +149,15 @@ public class WikidataConstraintFetcher implements ConstraintFetcher {
} }
/** /**
* Returns a single constraint for a particular type and a property, or null * Returns a single constraint for a particular type and a property, or null if
* if there is no such constraint * there is no such constraint
* @param pid: the property to retrieve the constraints for *
* @param qid: the type of the constraints * @param pid:
* @return the list of qualifiers for the constraint, or null if it does not exist * the property to retrieve the constraints for
* @param qid:
* the type of the constraints
* @return the list of qualifiers for the constraint, or null if it does not
* exist
*/ */
protected List<SnakGroup> getSingleConstraint(PropertyIdValue pid, String qid) { protected List<SnakGroup> getSingleConstraint(PropertyIdValue pid, String qid) {
Statement statement = getConstraintsByType(pid, qid).findFirst().orElse(null); Statement statement = getConstraintsByType(pid, qid).findFirst().orElse(null);
@ -145,20 +169,24 @@ public class WikidataConstraintFetcher implements ConstraintFetcher {
/** /**
* Gets the list of constraints of a particular type for a property * Gets the list of constraints of a particular type for a property
* @param pid: the property to retrieve the constraints for *
* @param qid: the type of the constraints * @param pid:
* the property to retrieve the constraints for
* @param qid:
* the type of the constraints
* @return the stream of matching constraint statements * @return the stream of matching constraint statements
*/ */
protected Stream<Statement> getConstraintsByType(PropertyIdValue pid, String qid) { protected Stream<Statement> getConstraintsByType(PropertyIdValue pid, String qid) {
Stream<Statement> allConstraints = getConstraintStatements(pid) Stream<Statement> allConstraints = getConstraintStatements(pid).stream()
.stream()
.filter(s -> ((EntityIdValue) s.getValue()).getId().equals(qid)); .filter(s -> ((EntityIdValue) s.getValue()).getId().equals(qid));
return allConstraints; return allConstraints;
} }
/** /**
* Gets all the constraint statements for a given property * Gets all the constraint statements for a given property
* @param pid : the id of the property to retrieve the constraints for *
* @param pid
* : the id of the property to retrieve the constraints for
* @return the list of constraint statements * @return the list of constraint statements
*/ */
protected List<Statement> getConstraintStatements(PropertyIdValue pid) { protected List<Statement> getConstraintStatements(PropertyIdValue pid) {
@ -173,8 +201,11 @@ public class WikidataConstraintFetcher implements ConstraintFetcher {
/** /**
* Returns the values of a given property in qualifiers * Returns the values of a given property in qualifiers
* @param groups: the qualifiers *
* @param pid: the property to filter on * @param groups:
* the qualifiers
* @param pid:
* the property to filter on
* @return * @return
*/ */
protected List<Value> findValues(List<SnakGroup> groups, String pid) { protected List<Value> findValues(List<SnakGroup> groups, String pid) {

View File

@ -1,9 +1,30 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
@ -12,8 +33,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
/** /**
* A scrutinizer that checks for properties using the same value * A scrutinizer that checks for properties using the same value on different
* on different items. * items.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -40,11 +61,7 @@ public class DistinctValuesScrutinizer extends StatementScrutinizer {
} }
if (seen.containsKey(mainSnakValue)) { if (seen.containsKey(mainSnakValue)) {
EntityIdValue otherId = seen.get(mainSnakValue); EntityIdValue otherId = seen.get(mainSnakValue);
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
type,
pid.getId(),
QAWarning.Severity.IMPORTANT,
1);
issue.setProperty("property_entity", pid); issue.setProperty("property_entity", pid);
issue.setProperty("item1_entity", entityId); issue.setProperty("item1_entity", entityId);
issue.setProperty("item2_entity", otherId); issue.setProperty("item2_entity", otherId);

View File

@ -1,8 +1,30 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.List; import java.util.List;
import org.openrefine.wikidata.qa.WikidataConstraintFetcher;
import org.openrefine.wikidata.qa.ConstraintFetcher; import org.openrefine.wikidata.qa.ConstraintFetcher;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
import org.openrefine.wikidata.qa.QAWarning.Severity; import org.openrefine.wikidata.qa.QAWarning.Severity;
@ -34,7 +56,9 @@ public abstract class EditScrutinizer {
/** /**
* Reads the candidate edits and emits warnings in the store * Reads the candidate edits and emits warnings in the store
* @param edit: the list of ItemUpdates to scrutinize *
* @param edit:
* the list of ItemUpdates to scrutinize
*/ */
public abstract void scrutinize(List<ItemUpdate> edit); public abstract void scrutinize(List<ItemUpdate> edit);
@ -48,6 +72,7 @@ public abstract class EditScrutinizer {
/** /**
* Helper to be used by subclasses to emit simple INFO warnings * Helper to be used by subclasses to emit simple INFO warnings
*
* @param warning * @param warning
*/ */
protected void info(String type) { protected void info(String type) {
@ -57,6 +82,7 @@ public abstract class EditScrutinizer {
/** /**
* Helper to be used by subclasses to emit simple warnings * Helper to be used by subclasses to emit simple warnings
*
* @param warning * @param warning
*/ */
protected void warning(String type) { protected void warning(String type) {
@ -65,6 +91,7 @@ public abstract class EditScrutinizer {
/** /**
* Helper to be used by subclasses to emit simple important warnings * Helper to be used by subclasses to emit simple important warnings
*
* @param warning * @param warning
*/ */
protected void important(String type) { protected void important(String type) {
@ -73,6 +100,7 @@ public abstract class EditScrutinizer {
/** /**
* Helper to be used by subclasses to emit simple critical warnings * Helper to be used by subclasses to emit simple critical warnings
*
* @param warning * @param warning
*/ */
protected void critical(String type) { protected void critical(String type) {

View File

@ -1,8 +1,30 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
@ -12,8 +34,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.StringValue; import org.wikidata.wdtk.datamodel.interfaces.StringValue;
/** /**
* A scrutinizer that detects incorrect formats in text values * A scrutinizer that detects incorrect formats in text values (mostly
* (mostly identifiers). * identifiers).
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -29,10 +51,11 @@ public class FormatScrutinizer extends SnakScrutinizer {
} }
/** /**
* Loads the regex for a property and compiles it to a pattern * Loads the regex for a property and compiles it to a pattern (this is cached
* (this is cached upstream, plus we are doing it only once per * upstream, plus we are doing it only once per property and batch).
* property and batch). *
* @param pid the id of the property to fetch the constraints for * @param pid
* the id of the property to fetch the constraints for
* @return * @return
*/ */
protected Pattern getPattern(PropertyIdValue pid) { protected Pattern getPattern(PropertyIdValue pid) {
@ -60,11 +83,7 @@ public class FormatScrutinizer extends SnakScrutinizer {
} }
if (!pattern.matcher(value).matches()) { if (!pattern.matcher(value).matches()) {
if (added) { if (added) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
type,
pid.getId(),
QAWarning.Severity.IMPORTANT,
1);
issue.setProperty("property_entity", pid); issue.setProperty("property_entity", pid);
issue.setProperty("regex", pattern.toString()); issue.setProperty("regex", pattern.toString());
issue.setProperty("example_value", value); issue.setProperty("example_value", value);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashMap; import java.util.HashMap;
@ -14,8 +37,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
/** /**
* A scrutinizer that checks for missing inverse statements in * A scrutinizer that checks for missing inverse statements in edit batches.
* edit batches.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -85,10 +107,7 @@ public class InverseConstraintScrutinizer extends StatementScrutinizer {
PropertyIdValue missingProperty = propertyPair.getValue(); PropertyIdValue missingProperty = propertyPair.getValue();
Set<EntityIdValue> reciprocalLinks = _statements.get(missingProperty).get(idValue); Set<EntityIdValue> reciprocalLinks = _statements.get(missingProperty).get(idValue);
if (reciprocalLinks == null || !reciprocalLinks.contains(itemLinks.getKey())) { if (reciprocalLinks == null || !reciprocalLinks.contains(itemLinks.getKey())) {
QAWarning issue = new QAWarning(type, QAWarning issue = new QAWarning(type, ourProperty.getId(), QAWarning.Severity.IMPORTANT, 1);
ourProperty.getId(),
QAWarning.Severity.IMPORTANT,
1);
issue.setProperty("added_property_entity", ourProperty); issue.setProperty("added_property_entity", ourProperty);
issue.setProperty("inverse_property_entity", missingProperty); issue.setProperty("inverse_property_entity", missingProperty);
issue.setProperty("source_entity", itemLinks.getKey()); issue.setProperty("source_entity", itemLinks.getKey());

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.List; import java.util.List;
@ -17,15 +40,16 @@ public abstract class ItemUpdateScrutinizer extends EditScrutinizer {
} }
/** /**
* Method to be overridden by subclasses to scrutinize * Method to be overridden by subclasses to scrutinize an individual item
* an individual item update. * update.
*
* @param update * @param update
*/ */
public abstract void scrutinize(ItemUpdate update); public abstract void scrutinize(ItemUpdate update);
/** /**
* Method to be overridden by subclasses to emit warnings * Method to be overridden by subclasses to emit warnings once a batch has been
* once a batch has been completely analyzed. * completely analyzed.
*/ */
public void batchIsFinished() { public void batchIsFinished() {
; ;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
@ -23,31 +46,19 @@ public class NewItemScrutinizer extends ItemUpdateScrutinizer {
info(newItemType); info(newItemType);
if (update.getLabels().isEmpty() && update.getAliases().isEmpty()) { if (update.getLabels().isEmpty() && update.getAliases().isEmpty()) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(noLabelType, null, QAWarning.Severity.CRITICAL, 1);
noLabelType,
null,
QAWarning.Severity.CRITICAL,
1);
issue.setProperty("example_entity", update.getItemId()); issue.setProperty("example_entity", update.getItemId());
addIssue(issue); addIssue(issue);
} }
if (update.getDescriptions().isEmpty()) { if (update.getDescriptions().isEmpty()) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(noDescType, null, QAWarning.Severity.WARNING, 1);
noDescType,
null,
QAWarning.Severity.WARNING,
1);
issue.setProperty("example_entity", update.getItemId()); issue.setProperty("example_entity", update.getItemId());
addIssue(issue); addIssue(issue);
} }
if (!update.getDeletedStatements().isEmpty()) { if (!update.getDeletedStatements().isEmpty()) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(deletedStatementsType, null, QAWarning.Severity.WARNING, 1);
deletedStatementsType,
null,
QAWarning.Severity.WARNING,
1);
issue.setProperty("example_entity", update.getItemId()); issue.setProperty("example_entity", update.getItemId());
addIssue(issue); addIssue(issue);
} }
@ -62,11 +73,7 @@ public class NewItemScrutinizer extends ItemUpdateScrutinizer {
} }
} }
if (!typeFound) { if (!typeFound) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(noTypeType, null, QAWarning.Severity.WARNING, 1);
noTypeType,
null,
QAWarning.Severity.WARNING,
1);
issue.setProperty("example_entity", update.getItemId()); issue.setProperty("example_entity", update.getItemId());
addIssue(issue); addIssue(issue);
} }

View File

@ -1,10 +1,32 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.List; import java.util.List;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
public class NoEditsMadeScrutinizer extends EditScrutinizer { public class NoEditsMadeScrutinizer extends EditScrutinizer {
public static final String type = "no-edit-generated"; public static final String type = "no-edit-generated";

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashMap; import java.util.HashMap;
@ -12,8 +35,8 @@ import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
/** /**
* A scrutinizer that checks the compatibility of the qualifiers * A scrutinizer that checks the compatibility of the qualifiers and the
* and the property of a statement, and looks for mandatory qualifiers. * property of a statement, and looks for mandatory qualifiers.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
*/ */
@ -58,31 +81,25 @@ public class QualifierCompatibilityScrutinizer extends StatementScrutinizer {
@Override @Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) { public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
PropertyIdValue statementProperty = statement.getClaim().getMainSnak().getPropertyId(); PropertyIdValue statementProperty = statement.getClaim().getMainSnak().getPropertyId();
Set<PropertyIdValue> qualifiers = statement.getClaim().getQualifiers(). Set<PropertyIdValue> qualifiers = statement.getClaim().getQualifiers().stream().map(e -> e.getProperty())
stream().map(e -> e.getProperty()).collect(Collectors.toSet()); .collect(Collectors.toSet());
Set<PropertyIdValue> missingQualifiers = mandatoryQualifiers(statementProperty) Set<PropertyIdValue> missingQualifiers = mandatoryQualifiers(statementProperty).stream()
.stream().filter(p -> !qualifiers.contains(p)).collect(Collectors.toSet()); .filter(p -> !qualifiers.contains(p)).collect(Collectors.toSet());
Set<PropertyIdValue> disallowedQualifiers = qualifiers Set<PropertyIdValue> disallowedQualifiers = qualifiers.stream()
.stream().filter(p -> !qualifierIsAllowed(statementProperty, p)).collect(Collectors.toSet()); .filter(p -> !qualifierIsAllowed(statementProperty, p)).collect(Collectors.toSet());
for (PropertyIdValue missing : missingQualifiers) { for (PropertyIdValue missing : missingQualifiers) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(missingMandatoryQualifiersType,
missingMandatoryQualifiersType, statementProperty.getId() + "-" + missing.getId(), QAWarning.Severity.WARNING, 1);
statementProperty.getId()+"-"+missing.getId(),
QAWarning.Severity.WARNING,
1);
issue.setProperty("statement_property_entity", statementProperty); issue.setProperty("statement_property_entity", statementProperty);
issue.setProperty("missing_property_entity", missing); issue.setProperty("missing_property_entity", missing);
issue.setProperty("example_item_entity", entityId); issue.setProperty("example_item_entity", entityId);
addIssue(issue); addIssue(issue);
} }
for (PropertyIdValue disallowed : disallowedQualifiers) { for (PropertyIdValue disallowed : disallowedQualifiers) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(disallowedQualifiersType,
disallowedQualifiersType, statementProperty.getId() + "-" + disallowed.getId(), QAWarning.Severity.WARNING, 1);
statementProperty.getId()+"-"+disallowed.getId(),
QAWarning.Severity.WARNING,
1);
issue.setProperty("statement_property_entity", statementProperty); issue.setProperty("statement_property_entity", statementProperty);
issue.setProperty("disallowed_property_entity", disallowed); issue.setProperty("disallowed_property_entity", disallowed);
issue.setProperty("example_item_entity", entityId); issue.setProperty("example_item_entity", entityId);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashMap; import java.util.HashMap;
@ -13,13 +36,10 @@ import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
public class RestrictedPositionScrutinizer extends StatementScrutinizer { public class RestrictedPositionScrutinizer extends StatementScrutinizer {
protected enum SnakPosition { protected enum SnakPosition {
MAINSNAK, MAINSNAK, QUALIFIER, REFERENCE
QUALIFIER,
REFERENCE
} }
private Map<PropertyIdValue, SnakPosition> _restrictedPids; private Map<PropertyIdValue, SnakPosition> _restrictedPids;
@ -70,7 +90,8 @@ public class RestrictedPositionScrutinizer extends StatementScrutinizer {
} }
} }
protected void scrutinizeSnakSet(Iterator<Snak> snaks, EntityIdValue entityId, SnakPosition position, boolean added) { protected void scrutinizeSnakSet(Iterator<Snak> snaks, EntityIdValue entityId, SnakPosition position,
boolean added) {
while (snaks.hasNext()) { while (snaks.hasNext()) {
Snak snak = snaks.next(); Snak snak = snaks.next();
scrutinize(snak, entityId, position, added); scrutinize(snak, entityId, position, added);
@ -83,11 +104,8 @@ public class RestrictedPositionScrutinizer extends StatementScrutinizer {
String positionStr = position.toString().toLowerCase(); String positionStr = position.toString().toLowerCase();
String restrictionStr = restriction.toString().toLowerCase(); String restrictionStr = restriction.toString().toLowerCase();
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning("property-restricted-to-" + restrictionStr + "-found-in-" + positionStr,
"property-restricted-to-"+restrictionStr+"-found-in-"+positionStr, snak.getPropertyId().getId(), QAWarning.Severity.IMPORTANT, 1);
snak.getPropertyId().getId(),
QAWarning.Severity.IMPORTANT,
1);
issue.setProperty("property_entity", snak.getPropertyId()); issue.setProperty("property_entity", snak.getPropertyId());
addIssue(issue); addIssue(issue);
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
@ -5,10 +28,10 @@ import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
/** /**
* A scrutinizer that checks for self-referential statements. * A scrutinizer that checks for self-referential statements. These statements
* These statements are flagged by Wikibase as suspicious. * are flagged by Wikibase as suspicious.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public class SelfReferentialScrutinizer extends SnakScrutinizer { public class SelfReferentialScrutinizer extends SnakScrutinizer {
@ -18,9 +41,7 @@ public class SelfReferentialScrutinizer extends SnakScrutinizer {
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (entityId.equals(snak.getValue())) { if (entityId.equals(snak.getValue())) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(type, null, QAWarning.Severity.WARNING, 1);
type, null,
QAWarning.Severity.WARNING, 1);
issue.setProperty("example_entity", entityId); issue.setProperty("example_entity", entityId);
addIssue(issue); addIssue(issue);
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashSet; import java.util.HashSet;
@ -9,8 +32,8 @@ import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
/** /**
* For now this scrutinizer only checks for uniqueness at * For now this scrutinizer only checks for uniqueness at the item level (it
* the item level (it ignores qualifiers and references). * ignores qualifiers and references).
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -27,9 +50,7 @@ public class SingleValueScrutinizer extends ItemUpdateScrutinizer {
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
if (seenSingleProperties.contains(pid)) { if (seenSingleProperties.contains(pid)) {
QAWarning issue = new QAWarning( QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.WARNING, 1);
type, pid.getId(),
QAWarning.Severity.WARNING, 1);
issue.setProperty("property_entity", pid); issue.setProperty("property_entity", pid);
issue.setProperty("example_entity", update.getItemId()); issue.setProperty("example_entity", update.getItemId());
addIssue(issue); addIssue(issue);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.Iterator; import java.util.Iterator;
@ -8,8 +31,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
/** /**
* A scrutinizer that inspects snaks individually, no matter whether they * A scrutinizer that inspects snaks individually, no matter whether they appear
* appear as main snaks, qualifiers or references. * as main snaks, qualifiers or references.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -18,9 +41,13 @@ public abstract class SnakScrutinizer extends StatementScrutinizer {
/** /**
* This is the method that subclasses should override to implement their checks. * This is the method that subclasses should override to implement their checks.
* @param snak: the snak to inspect *
* @param entityId: the item on which it is going to (dis)appear * @param snak:
* @param added: whether this snak is going to be added or deleted * the snak to inspect
* @param entityId:
* the item on which it is going to (dis)appear
* @param added:
* whether this snak is going to be added or deleted
*/ */
public abstract void scrutinize(Snak snak, EntityIdValue entityId, boolean added); public abstract void scrutinize(Snak snak, EntityIdValue entityId, boolean added);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
@ -18,11 +41,15 @@ public abstract class StatementScrutinizer extends ItemUpdateScrutinizer {
} }
/** /**
* The method that should be overridden by subclasses, implementing * The method that should be overridden by subclasses, implementing the checks
* the checks on one statement * on one statement
* @param statement: the statement to scrutinize *
* @param entityId: the id of the entity on which this statement is made or removed * @param statement:
* @param added: whether this statement was added or deleted * the statement to scrutinize
* @param entityId:
* the id of the entity on which this statement is made or removed
* @param added:
* whether this statement was added or deleted
*/ */
public abstract void scrutinize(Statement statement, EntityIdValue entityId, boolean added); public abstract void scrutinize(Statement statement, EntityIdValue entityId, boolean added);
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
@ -6,7 +29,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
/** /**
* A scrutinizer checking for unsourced statements * A scrutinizer checking for unsourced statements
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public class UnsourcedScrutinizer extends StatementScrutinizer { public class UnsourcedScrutinizer extends StatementScrutinizer {

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
@ -8,7 +31,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Value;
/** /**
* A scrutinizer that inspects the values of snaks and terms * A scrutinizer that inspects the values of snaks and terms
* @author antonin *
* @author Antonin Delpeuch
* *
*/ */
public abstract class ValueScrutinizer extends SnakScrutinizer { public abstract class ValueScrutinizer extends SnakScrutinizer {

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashMap; import java.util.HashMap;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
@ -10,13 +33,14 @@ import com.google.refine.model.ColumnModel;
import com.google.refine.model.Row; import com.google.refine.model.Row;
/** /**
* A class holding all the necessary information about * A class holding all the necessary information about the context in which a
* the context in which a schema expression is evaluated. * schema expression is evaluated.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
*/ */
public class ExpressionContext { public class ExpressionContext {
private String baseIRI; private String baseIRI;
private int rowId; private int rowId;
private Row row; private Row row;
@ -25,6 +49,7 @@ public class ExpressionContext {
/** /**
* Builds an expression context to evaluate a schema on a row * Builds an expression context to evaluate a schema on a row
*
* @param baseIRI * @param baseIRI
* the siteIRI of the schema * the siteIRI of the schema
* @param rowId * @param rowId
@ -34,15 +59,10 @@ public class ExpressionContext {
* @param columnModel * @param columnModel
* lets us access cells by column name * lets us access cells by column name
* @param warningStore * @param warningStore
* where to store the issues encountered when * where to store the issues encountered when evaluating (can be set
* evaluating (can be set to null if these issues should be ignored) * to null if these issues should be ignored)
*/ */
public ExpressionContext( public ExpressionContext(String baseIRI, int rowId, Row row, ColumnModel columnModel, QAWarningStore warningStore) {
String baseIRI,
int rowId,
Row row,
ColumnModel columnModel,
QAWarningStore warningStore) {
Validate.notNull(baseIRI); Validate.notNull(baseIRI);
this.baseIRI = baseIRI; this.baseIRI = baseIRI;
this.rowId = rowId; this.rowId = rowId;
@ -56,14 +76,14 @@ public class ExpressionContext {
public String getBaseIRI() { public String getBaseIRI() {
return baseIRI; return baseIRI;
} }
/** /**
* Retrieves a cell in the current row, by column name. * Retrieves a cell in the current row, by column name. If the column does not
* If the column does not exist, null is returned. * exist, null is returned.
* *
* @param name * @param name
* the name of the column to retrieve the cell from * the name of the column to retrieve the cell from
* @return * @return the cell
* the cell
*/ */
public Cell getCellByName(String name) { public Cell getCellByName(String name) {
Column column = columnModel.getColumnByName(name); Column column = columnModel.getColumnByName(name);

View File

@ -1,12 +1,35 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
@ -19,8 +42,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
/** /**
* A constant for a time value, accepting a number of formats * A constant for a time value, accepting a number of formats which determine
* which determine the precision of the parsed value. * the precision of the parsed value.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -28,32 +51,28 @@ import com.google.common.collect.ImmutableMap;
public class WbDateConstant implements WbExpression<TimeValue> { public class WbDateConstant implements WbExpression<TimeValue> {
/** /**
* Map of formats accepted by the parser. Each format is associated * Map of formats accepted by the parser. Each format is associated to the time
* to the time precision it induces (an integer according to Wikibase's data model). * precision it induces (an integer according to Wikibase's data model).
*/ */
public static Map<SimpleDateFormat, Integer> acceptedFormats = ImmutableMap.<SimpleDateFormat, Integer> builder() public static Map<SimpleDateFormat, Integer> acceptedFormats = ImmutableMap.<SimpleDateFormat, Integer> builder()
.put(new SimpleDateFormat("yyyy"), 9) .put(new SimpleDateFormat("yyyy"), 9).put(new SimpleDateFormat("yyyy-MM"), 10)
.put(new SimpleDateFormat("yyyy-MM"), 10) .put(new SimpleDateFormat("yyyy-MM-dd"), 11).put(new SimpleDateFormat("yyyy-MM-dd'T'HH"), 12)
.put(new SimpleDateFormat("yyyy-MM-dd"), 11)
.put(new SimpleDateFormat("yyyy-MM-dd'T'HH"), 12)
.put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"), 13) .put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"), 13)
.put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), 14) .put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), 14).build();
.build();
private TimeValue parsed; private TimeValue parsed;
private String origDatestamp; private String origDatestamp;
/** /**
* Constructor. Used for deserialization from JSON. * Constructor. Used for deserialization from JSON. The object will be
* The object will be constructed even if the time cannot * constructed even if the time cannot be parsed (it will evaluate to null) in
* be parsed (it will evaluate to null) in {@link evaluate}. * {@link evaluate}.
* *
* @param origDatestamp * @param origDatestamp
* the date value as a string * the date value as a string
*/ */
@JsonCreator @JsonCreator
public WbDateConstant( public WbDateConstant(@JsonProperty("value") String origDatestamp) {
@JsonProperty("value") String origDatestamp) {
Validate.notNull(origDatestamp); Validate.notNull(origDatestamp);
this.setOrigDatestamp(origDatestamp); this.setOrigDatestamp(origDatestamp);
} }
@ -65,8 +84,8 @@ public class WbDateConstant implements WbExpression<TimeValue> {
} }
/** /**
* Parses a timestamp into a Wikibase {@link TimeValue}. The * Parses a timestamp into a Wikibase {@link TimeValue}. The precision is
* precision is automatically inferred from the format. * automatically inferred from the format.
* *
* @param datestamp * @param datestamp
* the time to parse * the time to parse
@ -74,7 +93,8 @@ public class WbDateConstant implements WbExpression<TimeValue> {
* @throws ParseException * @throws ParseException
* if the time cannot be parsed * if the time cannot be parsed
*/ */
public static TimeValue parse(String datestamp) throws ParseException { public static TimeValue parse(String datestamp)
throws ParseException {
Date date = null; Date date = null;
int precision = 9; // default precision (will be overridden) int precision = 9; // default precision (will be overridden)
for (Entry<SimpleDateFormat, Integer> entry : acceptedFormats.entrySet()) { for (Entry<SimpleDateFormat, Integer> entry : acceptedFormats.entrySet()) {
@ -94,18 +114,13 @@ public class WbDateConstant implements WbExpression<TimeValue> {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar = Calendar.getInstance(); calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
return Datamodel.makeTimeValue( return Datamodel.makeTimeValue(calendar.get(Calendar.YEAR), (byte) (calendar.get(Calendar.MONTH) + 1), // java
calendar.get(Calendar.YEAR), // starts
(byte) (calendar.get(Calendar.MONTH)+1), // java starts at 0 // at
(byte) calendar.get(Calendar.DAY_OF_MONTH), // 0
(byte) calendar.get(Calendar.HOUR_OF_DAY), (byte) calendar.get(Calendar.DAY_OF_MONTH), (byte) calendar.get(Calendar.HOUR_OF_DAY),
(byte) calendar.get(Calendar.MINUTE), (byte) calendar.get(Calendar.MINUTE), (byte) calendar.get(Calendar.SECOND), (byte) precision, 0, 1,
(byte) calendar.get(Calendar.SECOND), calendar.getTimeZone().getRawOffset() / 3600000, TimeValue.CM_GREGORIAN_PRO);
(byte) precision,
0,
1,
calendar.getTimeZone().getRawOffset()/3600000,
TimeValue.CM_GREGORIAN_PRO);
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.text.ParseException; import java.text.ParseException;
@ -10,8 +33,8 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
/** /**
* An expression that represents a time value, extracted from a string. * An expression that represents a time value, extracted from a string. A number
* A number of formats are recognized, see {@link WbDateConstant} for details. * of formats are recognized, see {@link WbDateConstant} for details.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *

View File

@ -1,20 +1,40 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
/** /**
* The base interface for all expressions, which evaluate to a * The base interface for all expressions, which evaluate to a particular type T
* particular type T in an ExpressionContext. * in an ExpressionContext.
*/ */
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
include=JsonTypeInfo.As.PROPERTY, @JsonSubTypes({ @Type(value = WbStringConstant.class, name = "wbstringconstant"),
property="type")
@JsonSubTypes({
@Type(value = WbStringConstant.class, name = "wbstringconstant"),
@Type(value = WbStringVariable.class, name = "wbstringvariable"), @Type(value = WbStringVariable.class, name = "wbstringvariable"),
@Type(value = WbLocationConstant.class, name = "wblocationconstant"), @Type(value = WbLocationConstant.class, name = "wblocationconstant"),
@Type(value = WbLocationVariable.class, name = "wblocationvariable"), @Type(value = WbLocationVariable.class, name = "wblocationvariable"),
@ -28,13 +48,13 @@ import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
@Type(value = WbPropConstant.class, name = "wbpropconstant"), @Type(value = WbPropConstant.class, name = "wbpropconstant"),
@Type(value = WbLanguageConstant.class, name = "wblanguageconstant"), @Type(value = WbLanguageConstant.class, name = "wblanguageconstant"),
@Type(value = WbLanguageVariable.class, name = "wblanguagevariable"), @Type(value = WbLanguageVariable.class, name = "wblanguagevariable"),
@Type(value = WbQuantityExpr.class, name="wbquantityexpr"), @Type(value = WbQuantityExpr.class, name = "wbquantityexpr"), })
})
public interface WbExpression<T> { public interface WbExpression<T> {
/** /**
* Evaluates the value expression in a given context, * Evaluates the value expression in a given context, returns a Wikibase value
* returns a Wikibase value suitable to be the target of a claim. * suitable to be the target of a claim.
*/ */
public T evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException; public T evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException;
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
@ -8,8 +31,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* Represents an item that does not vary, * Represents an item that does not vary, it is independent of the row.
* it is independent of the row.
*/ */
public class WbItemConstant implements WbExpression<ItemIdValue> { public class WbItemConstant implements WbExpression<ItemIdValue> {
@ -17,9 +39,7 @@ public class WbItemConstant implements WbExpression<ItemIdValue> {
private String label; private String label;
@JsonCreator @JsonCreator
public WbItemConstant( public WbItemConstant(@JsonProperty("qid") String qid, @JsonProperty("label") String label) {
@JsonProperty("qid") String qid,
@JsonProperty("label") String label) {
Validate.notNull(qid); Validate.notNull(qid);
this.qid = qid; this.qid = qid;
Validate.notNull(label); Validate.notNull(label);
@ -28,10 +48,7 @@ public class WbItemConstant implements WbExpression<ItemIdValue> {
@Override @Override
public ItemIdValue evaluate(ExpressionContext ctxt) { public ItemIdValue evaluate(ExpressionContext ctxt) {
return new SuggestedItemIdValue( return new SuggestedItemIdValue(qid, ctxt.getBaseIRI(), label);
qid,
ctxt.getBaseIRI(),
label);
} }
@JsonProperty("qid") @JsonProperty("qid")

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.Collections; import java.util.Collections;
@ -17,8 +40,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
/** /**
* The representation of an item document, which can contain * The representation of an item document, which can contain variables both for
* variables both for its own id and in its contents. * its own id and in its contents.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -32,8 +55,7 @@ public class WbItemDocumentExpr extends JacksonJsonizable implements WbExpressio
private List<WbStatementGroupExpr> statementGroups; private List<WbStatementGroupExpr> statementGroups;
@JsonCreator @JsonCreator
public WbItemDocumentExpr( public WbItemDocumentExpr(@JsonProperty("subject") WbExpression<? extends ItemIdValue> subjectExpr,
@JsonProperty("subject") WbExpression<? extends ItemIdValue> subjectExpr,
@JsonProperty("nameDescs") List<WbNameDescExpr> nameDescExprs, @JsonProperty("nameDescs") List<WbNameDescExpr> nameDescExprs,
@JsonProperty("statementGroups") List<WbStatementGroupExpr> statementGroupExprs) { @JsonProperty("statementGroups") List<WbStatementGroupExpr> statementGroupExprs) {
Validate.notNull(subjectExpr); Validate.notNull(subjectExpr);
@ -49,7 +71,8 @@ public class WbItemDocumentExpr extends JacksonJsonizable implements WbExpressio
} }
@Override @Override
public ItemUpdate evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException { public ItemUpdate evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException {
ItemIdValue subjectId = getSubject().evaluate(ctxt); ItemIdValue subjectId = getSubject().evaluate(ctxt);
ItemUpdateBuilder update = new ItemUpdateBuilder(subjectId); ItemUpdateBuilder update = new ItemUpdateBuilder(subjectId);
for (WbStatementGroupExpr expr : getStatementGroups()) { for (WbStatementGroupExpr expr : getStatementGroups()) {
@ -88,9 +111,8 @@ public class WbItemDocumentExpr extends JacksonJsonizable implements WbExpressio
return false; return false;
} }
WbItemDocumentExpr otherExpr = (WbItemDocumentExpr) other; WbItemDocumentExpr otherExpr = (WbItemDocumentExpr) other;
return subject.equals(otherExpr.getSubject()) && return subject.equals(otherExpr.getSubject()) && nameDescs.equals(otherExpr.getNameDescs())
nameDescs.equals(otherExpr.getNameDescs()) && && statementGroups.equals(otherExpr.getStatementGroups());
statementGroups.equals(otherExpr.getStatementGroups());
} }
@Override @Override

View File

@ -1,6 +1,28 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue; import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
@ -24,8 +46,8 @@ public class WbItemVariable extends WbVariableExpr<ItemIdValue> {
} }
/** /**
* Constructs a variable and sets the column it is bound to. Mostly * Constructs a variable and sets the column it is bound to. Mostly used as a
* used as a convenience method for testing. * convenience method for testing.
* *
* @param columnName * @param columnName
* the name of the column the expression should draw its value from * the name of the column the expression should draw its value from
@ -35,10 +57,10 @@ public class WbItemVariable extends WbVariableExpr<ItemIdValue> {
} }
@Override @Override
public ItemIdValue fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException { public ItemIdValue fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException {
if (cell.recon != null if (cell.recon != null
&& (Judgment.Matched.equals(cell.recon.judgment) || && (Judgment.Matched.equals(cell.recon.judgment) || Judgment.New.equals(cell.recon.judgment))) {
Judgment.New.equals(cell.recon.judgment))) {
return new ReconItemIdValue(cell.recon, cell.value.toString()); return new ReconItemIdValue(cell.recon, cell.value.toString());
} }
throw new SkipSchemaExpressionException(); throw new SkipSchemaExpressionException();

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
@ -19,9 +42,7 @@ public class WbLanguageConstant implements WbExpression<String> {
protected String _langLabel; protected String _langLabel;
@JsonCreator @JsonCreator
public WbLanguageConstant( public WbLanguageConstant(@JsonProperty("id") String langId, @JsonProperty("label") String langLabel) {
@JsonProperty("id") String langId,
@JsonProperty("label") String langLabel) {
_langId = normalizeLanguageCode(langId); _langId = normalizeLanguageCode(langId);
Validate.notNull(_langId, "A valid language code must be provided."); Validate.notNull(_langId, "A valid language code must be provided.");
Validate.notNull(langLabel); Validate.notNull(langLabel);
@ -29,13 +50,12 @@ public class WbLanguageConstant implements WbExpression<String> {
} }
/** /**
* Checks that a language code is valid and returns its preferred * Checks that a language code is valid and returns its preferred version
* version (converting deprecated language codes to their better values). * (converting deprecated language codes to their better values).
* *
* @param lang * @param lang
* a Wikimedia language code * a Wikimedia language code
* @return * @return the normalized code, or null if the code is invalid.
* the normalized code, or null if the code is invalid.
*/ */
public static String normalizeLanguageCode(String lang) { public static String normalizeLanguageCode(String lang) {
try { try {
@ -47,7 +67,8 @@ public class WbLanguageConstant implements WbExpression<String> {
} }
@Override @Override
public String evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException { public String evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException {
return _langId; return _langId;
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
@ -7,9 +30,9 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
/** /**
* A language variable generates a language code from a cell. * A language variable generates a language code from a cell. It checks its
* It checks its values against a known list of valid language codes * values against a known list of valid language codes and fixes on the fly the
* and fixes on the fly the deprecated ones (see {@link WbLanguageConstant}). * deprecated ones (see {@link WbLanguageConstant}).
*/ */
public class WbLanguageVariable extends WbVariableExpr<String> { public class WbLanguageVariable extends WbVariableExpr<String> {
@ -18,8 +41,8 @@ public class WbLanguageVariable extends WbVariableExpr<String> {
} }
/** /**
* Constructs a variable and sets the column it is bound to. Mostly * Constructs a variable and sets the column it is bound to. Mostly used as a
* used as a convenience method for testing. * convenience method for testing.
* *
* @param columnName * @param columnName
* the name of the column the expression should draw its value from * the name of the column the expression should draw its value from

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.text.ParseException; import java.text.ParseException;
@ -11,7 +34,8 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* A constant for a geographical location. The accepted format is lat,lng or lat/lng. * A constant for a geographical location. The accepted format is lat,lng or
* lat/lng.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -24,8 +48,7 @@ public class WbLocationConstant implements WbExpression<GlobeCoordinatesValue> {
private GlobeCoordinatesValue parsed; private GlobeCoordinatesValue parsed;
@JsonCreator @JsonCreator
public WbLocationConstant( public WbLocationConstant(@JsonProperty("value") String origValue) throws ParseException {
@JsonProperty("value") String origValue) throws ParseException {
this.value = origValue; this.value = origValue;
Validate.notNull(origValue); Validate.notNull(origValue);
this.parsed = parse(origValue); this.parsed = parse(origValue);
@ -37,11 +60,11 @@ public class WbLocationConstant implements WbExpression<GlobeCoordinatesValue> {
* *
* @param expr * @param expr
* the string to parse * the string to parse
* @return * @return the parsed location
* the parsed location
* @throws ParseException * @throws ParseException
*/ */
public static GlobeCoordinatesValue parse(String expr) throws ParseException { public static GlobeCoordinatesValue parse(String expr)
throws ParseException {
double lat = 0; double lat = 0;
double lng = 0; double lng = 0;
double precision = defaultPrecision; double precision = defaultPrecision;
@ -53,8 +76,7 @@ public class WbLocationConstant implements WbExpression<GlobeCoordinatesValue> {
if (parts.length == 3) { if (parts.length == 3) {
precision = Double.parseDouble(parts[2]); precision = Double.parseDouble(parts[2]);
} }
return Datamodel.makeGlobeCoordinatesValue(lat, lng, precision, return Datamodel.makeGlobeCoordinatesValue(lat, lng, precision, GlobeCoordinatesValue.GLOBE_EARTH);
GlobeCoordinatesValue.GLOBE_EARTH);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
; ;
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.text.ParseException; import java.text.ParseException;
@ -9,7 +32,6 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
public class WbLocationVariable extends WbVariableExpr<GlobeCoordinatesValue> { public class WbLocationVariable extends WbVariableExpr<GlobeCoordinatesValue> {
@JsonCreator @JsonCreator

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
@ -10,15 +33,13 @@ import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class WbMonolingualExpr implements WbExpression<MonolingualTextValue> { public class WbMonolingualExpr implements WbExpression<MonolingualTextValue> {
private WbExpression<? extends String> languageExpr; private WbExpression<? extends String> languageExpr;
private WbExpression<? extends StringValue> valueExpr; private WbExpression<? extends StringValue> valueExpr;
@JsonCreator @JsonCreator
public WbMonolingualExpr( public WbMonolingualExpr(@JsonProperty("language") WbExpression<? extends String> languageExpr,
@JsonProperty("language") WbExpression<? extends String> languageExpr,
@JsonProperty("value") WbExpression<? extends StringValue> valueExpr) { @JsonProperty("value") WbExpression<? extends StringValue> valueExpr) {
Validate.notNull(languageExpr); Validate.notNull(languageExpr);
this.languageExpr = languageExpr; this.languageExpr = languageExpr;
@ -35,11 +56,7 @@ public class WbMonolingualExpr implements WbExpression<MonolingualTextValue> {
return Datamodel.makeMonolingualTextValue(text, lang); return Datamodel.makeMonolingualTextValue(text, lang);
} catch (SkipSchemaExpressionException e) { } catch (SkipSchemaExpressionException e) {
QAWarning warning = new QAWarning( QAWarning warning = new QAWarning("monolingual-text-without-language", null, QAWarning.Severity.WARNING, 1);
"monolingual-text-without-language",
null,
QAWarning.Severity.WARNING,
1);
warning.setProperty("example_text", text); warning.setProperty("example_text", text);
ctxt.addWarning(warning); ctxt.addWarning(warning);
throw new SkipSchemaExpressionException(); throw new SkipSchemaExpressionException();
@ -62,8 +79,7 @@ public class WbMonolingualExpr implements WbExpression<MonolingualTextValue> {
return false; return false;
} }
WbMonolingualExpr otherExpr = (WbMonolingualExpr) other; WbMonolingualExpr otherExpr = (WbMonolingualExpr) other;
return languageExpr.equals(otherExpr.getLanguageExpr()) && return languageExpr.equals(otherExpr.getLanguageExpr()) && valueExpr.equals(otherExpr.getValueExpr());
valueExpr.equals(otherExpr.getValueExpr());
} }
@Override @Override

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
@ -10,9 +33,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* An expression that represent a term (label, description or alias). * An expression that represent a term (label, description or alias). The
* The structure is slightly different from other expressions because * structure is slightly different from other expressions because we need to
* we need to call different methods on {@link ItemUpdateBuilder}. * call different methods on {@link ItemUpdateBuilder}.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -21,17 +44,14 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class WbNameDescExpr { public class WbNameDescExpr {
enum NameDescrType { enum NameDescrType {
LABEL, LABEL, DESCRIPTION, ALIAS,
DESCRIPTION,
ALIAS,
} }
private NameDescrType type; private NameDescrType type;
private WbMonolingualExpr value; private WbMonolingualExpr value;
@JsonCreator @JsonCreator
public WbNameDescExpr( public WbNameDescExpr(@JsonProperty("name_type") NameDescrType type,
@JsonProperty("name_type") NameDescrType type,
@JsonProperty("value") WbMonolingualExpr value) { @JsonProperty("value") WbMonolingualExpr value) {
Validate.notNull(type); Validate.notNull(type);
this.type = type; this.type = type;
@ -82,8 +102,7 @@ public class WbNameDescExpr {
return false; return false;
} }
WbNameDescExpr otherExpr = (WbNameDescExpr) other; WbNameDescExpr otherExpr = (WbNameDescExpr) other;
return type.equals(otherExpr.getType()) && return type.equals(otherExpr.getType()) && value.equals(otherExpr.getValue());
value.equals(otherExpr.getValue());
} }
@Override @Override

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
@ -20,9 +43,7 @@ public class WbPropConstant implements WbExpression<PropertyIdValue> {
private String datatype; private String datatype;
@JsonCreator @JsonCreator
public WbPropConstant( public WbPropConstant(@JsonProperty("pid") String pid, @JsonProperty("label") String label,
@JsonProperty("pid") String pid,
@JsonProperty("label") String label,
@JsonProperty("datatype") String datatype) { @JsonProperty("datatype") String datatype) {
Validate.notNull(pid); Validate.notNull(pid);
this.pid = pid; this.pid = pid;
@ -57,7 +78,8 @@ public class WbPropConstant implements WbExpression<PropertyIdValue> {
return false; return false;
} }
WbPropConstant otherConstant = (WbPropConstant) other; WbPropConstant otherConstant = (WbPropConstant) other;
return pid.equals(otherConstant.getPid()) && label.equals(otherConstant.getLabel()) && datatype.equals(otherConstant.getDatatype()); return pid.equals(otherConstant.getPid()) && label.equals(otherConstant.getLabel())
&& datatype.equals(otherConstant.getDatatype());
} }
@Override @Override

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -18,17 +41,16 @@ public class WbQuantityExpr implements WbExpression<QuantityValue> {
private final WbExpression<? extends ItemIdValue> unitExpr; private final WbExpression<? extends ItemIdValue> unitExpr;
/** /**
* Creates an expression for a quantity, which * Creates an expression for a quantity, which contains two sub-expressions: one
* contains two sub-expressions: one for the amount (a string with * for the amount (a string with a particular format) and one for the unit,
* a particular format) and one for the unit, which is optional. * which is optional.
* *
* Setting unitExpr to null will give quantities without units. Setting * Setting unitExpr to null will give quantities without units. Setting it to a
* it to a non-null value will make the unit mandatory: if the unit expression * non-null value will make the unit mandatory: if the unit expression fails to
* fails to evaluate, the whole quantity expression will fail too. * evaluate, the whole quantity expression will fail too.
*/ */
@JsonCreator @JsonCreator
public WbQuantityExpr( public WbQuantityExpr(@JsonProperty("amount") WbExpression<? extends StringValue> amountExpr,
@JsonProperty("amount") WbExpression<? extends StringValue> amountExpr,
@JsonProperty("unit") WbExpression<? extends ItemIdValue> unitExpr) { @JsonProperty("unit") WbExpression<? extends ItemIdValue> unitExpr) {
Validate.notNull(amountExpr); Validate.notNull(amountExpr);
this.amountExpr = amountExpr; this.amountExpr = amountExpr;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.ArrayList; import java.util.ArrayList;
@ -24,17 +47,18 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE) @JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
public class WbReferenceExpr implements WbExpression<Reference> { public class WbReferenceExpr implements WbExpression<Reference> {
private List<WbSnakExpr> snakExprs; private List<WbSnakExpr> snakExprs;
@JsonCreator @JsonCreator
public WbReferenceExpr( public WbReferenceExpr(@JsonProperty("snaks") List<WbSnakExpr> snakExprs) {
@JsonProperty("snaks") List<WbSnakExpr> snakExprs) {
Validate.notNull(snakExprs); Validate.notNull(snakExprs);
this.snakExprs = snakExprs; this.snakExprs = snakExprs;
} }
@Override @Override
public Reference evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException { public Reference evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException {
List<SnakGroup> snakGroups = new ArrayList<SnakGroup>(); List<SnakGroup> snakGroups = new ArrayList<SnakGroup>();
for (WbSnakExpr expr : getSnaks()) { for (WbSnakExpr expr : getSnaks()) {
List<Snak> snakList = new ArrayList<Snak>(1); List<Snak> snakList = new ArrayList<Snak>(1);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
@ -26,8 +49,7 @@ public class WbSnakExpr implements WbExpression<Snak> {
private WbExpression<? extends Value> value; private WbExpression<? extends Value> value;
@JsonCreator @JsonCreator
public WbSnakExpr( public WbSnakExpr(@JsonProperty("prop") WbExpression<? extends PropertyIdValue> propExpr,
@JsonProperty("prop") WbExpression<? extends PropertyIdValue> propExpr,
@JsonProperty("value") WbExpression<? extends Value> valueExpr) { @JsonProperty("value") WbExpression<? extends Value> valueExpr) {
Validate.notNull(propExpr); Validate.notNull(propExpr);
this.prop = propExpr; this.prop = propExpr;
@ -36,7 +58,8 @@ public class WbSnakExpr implements WbExpression<Snak> {
} }
@Override @Override
public Snak evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException { public Snak evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException {
PropertyIdValue propertyId = getProp().evaluate(ctxt); PropertyIdValue propertyId = getProp().evaluate(ctxt);
Value evaluatedValue = value.evaluate(ctxt); Value evaluatedValue = value.evaluate(ctxt);
return Datamodel.makeValueSnak(propertyId, evaluatedValue); return Datamodel.makeValueSnak(propertyId, evaluatedValue);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.ArrayList; import java.util.ArrayList;
@ -10,10 +33,10 @@ import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim; import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Reference; import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank; import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
@ -30,8 +53,7 @@ public class WbStatementExpr {
private List<WbReferenceExpr> referenceExprs; private List<WbReferenceExpr> referenceExprs;
@JsonCreator @JsonCreator
public WbStatementExpr( public WbStatementExpr(@JsonProperty("value") WbExpression<? extends Value> mainSnakValueExpr,
@JsonProperty("value") WbExpression<? extends Value> mainSnakValueExpr,
@JsonProperty("qualifiers") List<WbSnakExpr> qualifierExprs, @JsonProperty("qualifiers") List<WbSnakExpr> qualifierExprs,
@JsonProperty("references") List<WbReferenceExpr> referenceExprs) { @JsonProperty("references") List<WbReferenceExpr> referenceExprs) {
Validate.notNull(mainSnakValueExpr); Validate.notNull(mainSnakValueExpr);
@ -67,11 +89,7 @@ public class WbStatementExpr {
try { try {
qualifiers.add(qExpr.evaluate(ctxt)); qualifiers.add(qExpr.evaluate(ctxt));
} catch (SkipSchemaExpressionException e) { } catch (SkipSchemaExpressionException e) {
QAWarning warning = new QAWarning( QAWarning warning = new QAWarning("ignored-qualifiers", null, QAWarning.Severity.INFO, 1);
"ignored-qualifiers",
null,
QAWarning.Severity.INFO,
1);
warning.setProperty("example_entity", subject); warning.setProperty("example_entity", subject);
warning.setProperty("example_property_entity", mainSnak.getPropertyId()); warning.setProperty("example_property_entity", mainSnak.getPropertyId());
ctxt.addWarning(warning); ctxt.addWarning(warning);
@ -86,11 +104,7 @@ public class WbStatementExpr {
try { try {
references.add(rExpr.evaluate(ctxt)); references.add(rExpr.evaluate(ctxt));
} catch (SkipSchemaExpressionException e) { } catch (SkipSchemaExpressionException e) {
QAWarning warning = new QAWarning( QAWarning warning = new QAWarning("ignored-references", null, QAWarning.Severity.INFO, 1);
"ignored-references",
null,
QAWarning.Severity.INFO,
1);
warning.setProperty("example_entity", subject); warning.setProperty("example_entity", subject);
warning.setProperty("example_property_entity", mainSnak.getPropertyId()); warning.setProperty("example_property_entity", mainSnak.getPropertyId());
ctxt.addWarning(warning); ctxt.addWarning(warning);
@ -122,9 +136,8 @@ public class WbStatementExpr {
return false; return false;
} }
WbStatementExpr otherExpr = (WbStatementExpr) other; WbStatementExpr otherExpr = (WbStatementExpr) other;
return mainSnakValueExpr.equals(otherExpr.getMainsnak()) && return mainSnakValueExpr.equals(otherExpr.getMainsnak()) && qualifierExprs.equals(otherExpr.getQualifiers())
qualifierExprs.equals(otherExpr.getQualifiers()) && && referenceExprs.equals(otherExpr.getReferences());
referenceExprs.equals(otherExpr.getReferences());
} }
@Override @Override

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,7 +37,6 @@ import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class WbStatementGroupExpr { public class WbStatementGroupExpr {
@ -23,8 +45,7 @@ public class WbStatementGroupExpr {
private List<WbStatementExpr> statementExprs; private List<WbStatementExpr> statementExprs;
@JsonCreator @JsonCreator
public WbStatementGroupExpr( public WbStatementGroupExpr(@JsonProperty("property") WbExpression<? extends PropertyIdValue> propertyExpr,
@JsonProperty("property") WbExpression<? extends PropertyIdValue> propertyExpr,
@JsonProperty("statements") List<WbStatementExpr> claimExprs) { @JsonProperty("statements") List<WbStatementExpr> claimExprs) {
Validate.notNull(propertyExpr); Validate.notNull(propertyExpr);
this.propertyExpr = propertyExpr; this.propertyExpr = propertyExpr;
@ -33,7 +54,8 @@ public class WbStatementGroupExpr {
this.statementExprs = claimExprs; this.statementExprs = claimExprs;
} }
public StatementGroup evaluate(ExpressionContext ctxt, ItemIdValue subject) throws SkipSchemaExpressionException { public StatementGroup evaluate(ExpressionContext ctxt, ItemIdValue subject)
throws SkipSchemaExpressionException {
PropertyIdValue propertyId = propertyExpr.evaluate(ctxt); PropertyIdValue propertyId = propertyExpr.evaluate(ctxt);
List<Statement> statements = new ArrayList<Statement>(statementExprs.size()); List<Statement> statements = new ArrayList<Statement>(statementExprs.size());
for (WbStatementExpr expr : statementExprs) { for (WbStatementExpr expr : statementExprs) {
@ -66,8 +88,7 @@ public class WbStatementGroupExpr {
return false; return false;
} }
WbStatementGroupExpr otherExpr = (WbStatementGroupExpr) other; WbStatementGroupExpr otherExpr = (WbStatementGroupExpr) other;
return propertyExpr.equals(otherExpr.getProperty()) && return propertyExpr.equals(otherExpr.getProperty()) && statementExprs.equals(otherExpr.getStatements());
statementExprs.equals(otherExpr.getStatements());
} }
@Override @Override

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
@ -7,7 +30,6 @@ import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStringConstant implements WbExpression<StringValue> { public class WbStringConstant implements WbExpression<StringValue> {
private String value; private String value;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
@ -21,8 +44,8 @@ public class WbStringVariable extends WbVariableExpr<StringValue> {
} }
/** /**
* Constructs a variable and sets the column it is bound to. Mostly * Constructs a variable and sets the column it is bound to. Mostly used as a
* used as a convenience method for testing. * convenience method for testing.
* *
* @param columnName * @param columnName
* the name of the column the expression should draw its value from * the name of the column the expression should draw its value from

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
@ -8,10 +31,10 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
/** /**
* A base class for expressions which draw their values * A base class for expressions which draw their values from a particular
* from a particular column. * column.
* *
* @author antonin * @author Antonin Delpeuch
* *
* @param <T> * @param <T>
* the type of Wikibase value returned by the expression. * the type of Wikibase value returned by the expression.
@ -30,8 +53,8 @@ public abstract class WbVariableExpr<T> implements WbExpression<T> {
/** /**
* Returns the column name used by the variable. * Returns the column name used by the variable.
* @return *
* the OpenRefine column name * @return the OpenRefine column name
*/ */
@JsonProperty("columnName") @JsonProperty("columnName")
public String getColumnName() { public String getColumnName() {
@ -39,9 +62,8 @@ public abstract class WbVariableExpr<T> implements WbExpression<T> {
} }
/** /**
* Changes the column name used by the variable. * Changes the column name used by the variable. This is useful for
* This is useful for deserialization, as well as updates when * deserialization, as well as updates when column names change.
* column names change.
*/ */
@JsonProperty("columnName") @JsonProperty("columnName")
public void setColumnName(String columnName) { public void setColumnName(String columnName) {
@ -62,19 +84,18 @@ public abstract class WbVariableExpr<T> implements WbExpression<T> {
} }
/** /**
* Method that should be implemented by subclasses, * Method that should be implemented by subclasses, converting an OpenRefine
* converting an OpenRefine cell to a Wikibase value. * cell to a Wikibase value. Access to other values and emiting warnings is
* Access to other values and emiting warnings is possible via * possible via the supplied EvaluationContext object.
* the supplied EvaluationContext object.
* *
* @param cell * @param cell
* the cell to convert * the cell to convert
* @param ctxt * @param ctxt
* the evaluation context * the evaluation context
* @return * @return the corresponding Wikibase value
* the corresponding Wikibase value
*/ */
public abstract T fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException; public abstract T fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException;
/** /**
* Helper for equality methods of subclasses. * Helper for equality methods of subclasses.

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.ArrayList; import java.util.ArrayList;
@ -8,6 +31,10 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.openrefine.wikidata.qa.QAWarningStore;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -17,17 +44,10 @@ import com.google.refine.browsing.RowVisitor;
import com.google.refine.model.OverlayModel; import com.google.refine.model.OverlayModel;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.model.Row; import com.google.refine.model.Row;
import org.openrefine.wikidata.schema.WbItemDocumentExpr;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.ItemUpdateBuilder;
import org.openrefine.wikidata.qa.QAWarningStore;
import org.openrefine.wikidata.schema.ExpressionContext;
import org.openrefine.wikidata.utils.JacksonJsonizable;
/** /**
* Main class representing a skeleton of Wikibase edits with * Main class representing a skeleton of Wikibase edits with OpenRefine columns
* OpenRefine columns as variables. * as variables.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -66,9 +86,9 @@ public class WikibaseSchema implements OverlayModel {
} }
/** /**
* Evaluates all item documents in a particular expression context. * Evaluates all item documents in a particular expression context. This
* This specifies, among others, a row where the values of the variables * specifies, among others, a row where the values of the variables will be
* will be read. * read.
* *
* @param ctxt * @param ctxt
* the context in which the schema should be evaluated. * the context in which the schema should be evaluated.
@ -88,13 +108,13 @@ public class WikibaseSchema implements OverlayModel {
} }
/** /**
* Evaluates the schema on a project, returning a list of ItemUpdates * Evaluates the schema on a project, returning a list of ItemUpdates generated
* generated by the schema. * by the schema.
* *
* Some warnings will be emitted in the warning store: those are only * Some warnings will be emitted in the warning store: those are only the ones
* the ones that are generated at evaluation time (such as invalid formats * that are generated at evaluation time (such as invalid formats for dates).
* for dates). Issues detected on candidate statements (such as constraint * Issues detected on candidate statements (such as constraint violations) are
* violations) are not included at this stage. * not included at this stage.
* *
* @param project * @param project
* the project on which the schema should be evaluated * the project on which the schema should be evaluated
@ -102,8 +122,7 @@ public class WikibaseSchema implements OverlayModel {
* the engine, which gives access to the current facets * the engine, which gives access to the current facets
* @param warningStore * @param warningStore
* a store in which issues will be emitted * a store in which issues will be emitted
* @return item updates are stored in their * @return item updates are stored in their generating order (not merged yet).
* generating order (not merged yet).
*/ */
public List<ItemUpdate> evaluate(Project project, Engine engine, QAWarningStore warningStore) { public List<ItemUpdate> evaluate(Project project, Engine engine, QAWarningStore warningStore) {
List<ItemUpdate> result = new ArrayList<>(); List<ItemUpdate> result = new ArrayList<>();
@ -120,8 +139,10 @@ public class WikibaseSchema implements OverlayModel {
} }
protected class EvaluatingRowVisitor implements RowVisitor { protected class EvaluatingRowVisitor implements RowVisitor {
private List<ItemUpdate> result; private List<ItemUpdate> result;
private QAWarningStore warningStore; private QAWarningStore warningStore;
public EvaluatingRowVisitor(List<ItemUpdate> result, QAWarningStore warningStore) { public EvaluatingRowVisitor(List<ItemUpdate> result, QAWarningStore warningStore) {
this.result = result; this.result = result;
this.warningStore = warningStore; this.warningStore = warningStore;
@ -134,12 +155,7 @@ public class WikibaseSchema implements OverlayModel {
@Override @Override
public boolean visit(Project project, int rowIndex, Row row) { public boolean visit(Project project, int rowIndex, Row row) {
ExpressionContext ctxt = new ExpressionContext( ExpressionContext ctxt = new ExpressionContext(baseIri, rowIndex, row, project.columnModel, warningStore);
baseIri,
rowIndex,
row,
project.columnModel,
warningStore);
result.addAll(evaluateItemDocuments(ctxt)); result.addAll(evaluateItemDocuments(ctxt));
return false; return false;
} }
@ -150,12 +166,14 @@ public class WikibaseSchema implements OverlayModel {
} }
} }
static public WikibaseSchema reconstruct(JSONObject o) throws JSONException { static public WikibaseSchema reconstruct(JSONObject o)
throws JSONException {
JSONArray changeArr = o.getJSONArray("itemDocuments"); JSONArray changeArr = o.getJSONArray("itemDocuments");
WikibaseSchema schema = new WikibaseSchema(); WikibaseSchema schema = new WikibaseSchema();
for (int i = 0; i != changeArr.length(); i++) { for (int i = 0; i != changeArr.length(); i++) {
WbItemDocumentExpr changeExpr = JacksonJsonizable.fromJSONClass(changeArr.getJSONObject(i), WbItemDocumentExpr.class); WbItemDocumentExpr changeExpr = JacksonJsonizable.fromJSONClass(changeArr.getJSONObject(i),
WbItemDocumentExpr.class);
schema.itemDocumentExprs.add(changeExpr); schema.itemDocumentExprs.add(changeExpr);
} }
return schema; return schema;
@ -174,7 +192,8 @@ public class WikibaseSchema implements OverlayModel {
writer.endObject(); writer.endObject();
} }
static public WikibaseSchema load(Project project, JSONObject obj) throws Exception { static public WikibaseSchema load(Project project, JSONObject obj)
throws Exception {
return reconstruct(obj); return reconstruct(obj);
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import java.util.List; import java.util.List;
@ -5,34 +28,31 @@ import java.util.List;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
/** /**
* An entity id value that also comes with * An entity id value that also comes with a label and possibly types.
* a label and possibly types.
* *
* The rationale behind this classes is that OpenRefine * The rationale behind this classes is that OpenRefine already stores labels
* already stores labels and types for the Wikidata items * and types for the Wikidata items it knows about (in the reconciliation data),
* it knows about (in the reconciliation data), so it is * so it is worth keeping this data to avoid re-fetching it when we need it.
* worth keeping this data to avoid re-fetching it when
* we need it.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public interface PrefetchedEntityIdValue extends EntityIdValue { public interface PrefetchedEntityIdValue extends EntityIdValue {
/** /**
* This should return the label "as we got it", with no guarantee * This should return the label "as we got it", with no guarantee that it is
* that it is current or that its language matches that of the user. * current or that its language matches that of the user. In general though,
* In general though, that should be the case if the user always uses * that should be the case if the user always uses OpenRefine with the same
* OpenRefine with the same language settings. * language settings.
* *
* @return the preferred label of the entity * @return the preferred label of the entity
*/ */
public String getLabel(); public String getLabel();
/** /**
* Returns a list of types for this item. Again these are the types * Returns a list of types for this item. Again these are the types as they were
* as they were originally fetched from the reconciliation interface: * originally fetched from the reconciliation interface: they can diverge from
* they can diverge from what is currently on the item. * what is currently on the item.
* *
* Empty lists should be returned for * Empty lists should be returned for
*/ */

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,19 +37,18 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.refine.model.Recon; import com.google.refine.model.Recon;
/** /**
* An EntityIdValue that holds not just the id but also * An EntityIdValue that holds not just the id but also the label as fetched by
* the label as fetched by either the reconciliation interface * either the reconciliation interface or the suggester and its type, both
* or the suggester and its type, both stored as reconciliation * stored as reconciliation candidates.
* candidates.
* *
* This label will be localized depending on the language chosen * This label will be localized depending on the language chosen by the user for
* by the user for OpenRefine's interface. Storing it lets us * OpenRefine's interface. Storing it lets us reuse it later on without having
* reuse it later on without having to re-fetch it. * to re-fetch it.
* *
* Storing the types also lets us perform some constraint checks * Storing the types also lets us perform some constraint checks without
* without re-fetching the types of many items. * re-fetching the types of many items.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue { public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue {
@ -37,8 +59,7 @@ public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue {
public ReconEntityIdValue(Recon match, String cellValue) { public ReconEntityIdValue(Recon match, String cellValue) {
_recon = match; _recon = match;
_cellValue = cellValue; _cellValue = cellValue;
assert (Recon.Judgment.Matched.equals(_recon.judgment) || assert (Recon.Judgment.Matched.equals(_recon.judgment) || Recon.Judgment.New.equals(_recon.judgment));
Recon.Judgment.New.equals(_recon.judgment));
} }
@JsonIgnore @JsonIgnore
@ -71,22 +92,18 @@ public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue {
public abstract String getEntityType(); public abstract String getEntityType();
/** /**
* Returns the integer used internally in OpenRefine to identify the new * Returns the integer used internally in OpenRefine to identify the new item.
* item.
* *
* @return * @return the judgment history entry id of the reconciled cell
* the judgment history entry id of the reconciled cell
*/ */
public long getReconInternalId() { public long getReconInternalId() {
return getRecon().judgmentHistoryEntry; return getRecon().judgmentHistoryEntry;
} }
/** /**
* Returns the reconciliation object corresponding to this entity. * Returns the reconciliation object corresponding to this entity.
* *
* @return * @return the full reconciliation metadata of the corresponding cell
* the full reconciliation metadata of the corresponding cell
*/ */
public Recon getRecon() { public Recon getRecon() {
return _recon; return _recon;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;

View File

@ -1,18 +1,40 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.wikidata.wdtk.datamodel.helpers.Hash; import org.wikidata.wdtk.datamodel.helpers.Hash;
import org.wikidata.wdtk.datamodel.helpers.ToString;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor; import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
/** /**
* An EntityIdValue that we have obtained from a suggest widget * An EntityIdValue that we have obtained from a suggest widget in the schema
* in the schema alignment dialog. * alignment dialog.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public abstract class SuggestedEntityIdValue implements PrefetchedEntityIdValue { public abstract class SuggestedEntityIdValue implements PrefetchedEntityIdValue {
@ -59,8 +81,7 @@ public abstract class SuggestedEntityIdValue implements PrefetchedEntityIdValue
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other == null || if (other == null || !EntityIdValue.class.isInstance(other)) {
!EntityIdValue.class.isInstance(other)) {
return false; return false;
} }
final EntityIdValue otherNew = (EntityIdValue) other; final EntityIdValue otherNew = (EntityIdValue) other;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.entityvalues; package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.helpers.ToString; import org.wikidata.wdtk.datamodel.helpers.ToString;

View File

@ -1,6 +1,29 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.exceptions; package org.openrefine.wikidata.schema.exceptions;
public class InvalidSchemaException extends Exception { public class InvalidSchemaException extends Exception {
static final long serialVersionUID = 494837587034L; static final long serialVersionUID = 494837587034L;
} }

View File

@ -1,6 +1,29 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.schema.exceptions; package org.openrefine.wikidata.schema.exceptions;
public class SkipSchemaExpressionException extends Exception { public class SkipSchemaExpressionException extends Exception {
static final long serialVersionUID = 738592057L; static final long serialVersionUID = 738592057L;
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates; package org.openrefine.wikidata.updates;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,13 +46,14 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* A class to plan an update of an item, after evaluating the statements * A class to plan an update of an item, after evaluating the statements but
* but before fetching the current content of the item (this is why it does not * before fetching the current content of the item (this is why it does not
* extend StatementsUpdate). * extend StatementsUpdate).
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
*/ */
public class ItemUpdate { public class ItemUpdate {
private final ItemIdValue qid; private final ItemIdValue qid;
private final List<Statement> addedStatements; private final List<Statement> addedStatements;
private final Set<Statement> deletedStatements; private final Set<Statement> deletedStatements;
@ -41,7 +65,8 @@ public class ItemUpdate {
* Constructor. * Constructor.
* *
* @param qid * @param qid
* the subject of the document. It can be a reconciled item value for new items. * the subject of the document. It can be a reconciled item value for
* new items.
* @param addedStatements * @param addedStatements
* the statements to add on the item. They should be distinct. They * the statements to add on the item. They should be distinct. They
* are modelled as a list because their insertion order matters. * are modelled as a list because their insertion order matters.
@ -52,13 +77,12 @@ public class ItemUpdate {
* @param descriptions * @param descriptions
* the descriptions to add on the item * the descriptions to add on the item
* @param aliases * @param aliases
* the aliases to add on the item. In theory their order should matter * the aliases to add on the item. In theory their order should
* but in practice people rarely rely on the order of aliases so this * matter but in practice people rarely rely on the order of aliases
* is just kept as a set for simplicity. * so this is just kept as a set for simplicity.
*/ */
@JsonCreator @JsonCreator
public ItemUpdate( public ItemUpdate(@JsonProperty("subject") ItemIdValue qid,
@JsonProperty("subject") ItemIdValue qid,
@JsonProperty("addedStatements") List<Statement> addedStatements, @JsonProperty("addedStatements") List<Statement> addedStatements,
@JsonProperty("deletedStatements") Set<Statement> deletedStatements, @JsonProperty("deletedStatements") Set<Statement> deletedStatements,
@JsonProperty("labels") Set<MonolingualTextValue> labels, @JsonProperty("labels") Set<MonolingualTextValue> labels,
@ -97,8 +121,8 @@ public class ItemUpdate {
} }
/** /**
* Added statements are recorded as a list because * Added statements are recorded as a list because their order of insertion
* their order of insertion matters. * matters.
* *
* @return the list of all added statements * @return the list of all added statements
*/ */
@ -152,16 +176,13 @@ public class ItemUpdate {
*/ */
@JsonIgnore @JsonIgnore
public boolean isEmpty() { public boolean isEmpty() {
return (addedStatements.isEmpty() return (addedStatements.isEmpty() && deletedStatements.isEmpty() && labels.isEmpty() && descriptions.isEmpty()
&& deletedStatements.isEmpty()
&& labels.isEmpty()
&& descriptions.isEmpty()
&& aliases.isEmpty()); && aliases.isEmpty());
} }
/** /**
* Merges all the changes in other into this instance. * Merges all the changes in other into this instance. Both updates should have
* Both updates should have the same subject. * the same subject.
* *
* @param other * @param other
* the other change that should be merged * the other change that should be merged
@ -182,14 +203,11 @@ public class ItemUpdate {
newDescriptions.addAll(other.getDescriptions()); newDescriptions.addAll(other.getDescriptions());
Set<MonolingualTextValue> newAliases = new HashSet<>(aliases); Set<MonolingualTextValue> newAliases = new HashSet<>(aliases);
newAliases.addAll(other.getAliases()); newAliases.addAll(other.getAliases());
return new ItemUpdate( return new ItemUpdate(qid, newAddedStatements, newDeletedStatements, newLabels, newDescriptions, newAliases);
qid, newAddedStatements, newDeletedStatements,
newLabels, newDescriptions, newAliases);
} }
/** /**
* Group added statements in StatementGroups: useful if the * Group added statements in StatementGroups: useful if the item is new.
* item is new.
* *
* @return a grouped version of getAddedStatements() * @return a grouped version of getAddedStatements()
*/ */
@ -210,8 +228,8 @@ public class ItemUpdate {
} }
/** /**
* Group a list of ItemUpdates by subject: this is useful to make one single edit * Group a list of ItemUpdates by subject: this is useful to make one single
* per item. * edit per item.
* *
* @param itemDocuments * @param itemDocuments
* @return a map from item ids to merged ItemUpdate for that id * @return a map from item ids to merged ItemUpdate for that id
@ -242,15 +260,12 @@ public class ItemUpdate {
} }
/** /**
* This should only be used when creating a new item. * This should only be used when creating a new item. This ensures that we never
* This ensures that we never add an alias without adding * add an alias without adding a label in the same language.
* a label in the same language.
*/ */
public ItemUpdate normalizeLabelsAndAliases() { public ItemUpdate normalizeLabelsAndAliases() {
// Ensure that we are only adding aliases with labels // Ensure that we are only adding aliases with labels
Set<String> labelLanguages = labels.stream() Set<String> labelLanguages = labels.stream().map(l -> l.getLanguageCode()).collect(Collectors.toSet());
.map(l -> l.getLanguageCode())
.collect(Collectors.toSet());
Set<MonolingualTextValue> filteredAliases = new HashSet<>(); Set<MonolingualTextValue> filteredAliases = new HashSet<>();
Set<MonolingualTextValue> newLabels = new HashSet<>(labels); Set<MonolingualTextValue> newLabels = new HashSet<>(labels);
@ -262,8 +277,7 @@ public class ItemUpdate {
filteredAliases.add(alias); filteredAliases.add(alias);
} }
} }
return new ItemUpdate(qid, addedStatements, deletedStatements, return new ItemUpdate(qid, addedStatements, deletedStatements, newLabels, descriptions, filteredAliases);
newLabels, descriptions, filteredAliases);
} }
@Override @Override
@ -272,18 +286,16 @@ public class ItemUpdate {
return false; return false;
} }
ItemUpdate otherUpdate = (ItemUpdate) other; ItemUpdate otherUpdate = (ItemUpdate) other;
return qid.equals(otherUpdate.getItemId())&& return qid.equals(otherUpdate.getItemId()) && addedStatements.equals(otherUpdate.getAddedStatements())
addedStatements.equals(otherUpdate.getAddedStatements()) && && deletedStatements.equals(otherUpdate.getDeletedStatements())
deletedStatements.equals(otherUpdate.getDeletedStatements()) && && labels.equals(otherUpdate.getLabels()) && descriptions.equals(otherUpdate.getDescriptions())
labels.equals(otherUpdate.getLabels()) && && aliases.equals(otherUpdate.getAliases());
descriptions.equals(otherUpdate.getDescriptions()) &&
aliases.equals(otherUpdate.getAliases());
} }
@Override @Override
public int hashCode() { public int hashCode() {
return qid.hashCode() + addedStatements.hashCode() + deletedStatements.hashCode() + return qid.hashCode() + addedStatements.hashCode() + deletedStatements.hashCode() + labels.hashCode()
labels.hashCode() + descriptions.hashCode() + aliases.hashCode(); + descriptions.hashCode() + aliases.hashCode();
} }
@Override @Override

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates; package org.openrefine.wikidata.updates;
import java.util.Set; import java.util.Set;
@ -10,7 +33,6 @@ import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
/** /**
* Constructs a {@link ItemUpdate} incrementally. * Constructs a {@link ItemUpdate} incrementally.
* *
@ -18,6 +40,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
* *
*/ */
public class ItemUpdateBuilder { public class ItemUpdateBuilder {
private ItemIdValue qid; private ItemIdValue qid;
private List<Statement> addedStatements; private List<Statement> addedStatements;
private Set<Statement> deletedStatements; private Set<Statement> deletedStatements;
@ -30,7 +53,8 @@ public class ItemUpdateBuilder {
* Constructor. * Constructor.
* *
* @param qid * @param qid
* the subject of the document. It can be a reconciled item value for new items. * the subject of the document. It can be a reconciled item value for
* new items.
*/ */
public ItemUpdateBuilder(ItemIdValue qid) { public ItemUpdateBuilder(ItemIdValue qid) {
Validate.notNull(qid); Validate.notNull(qid);
@ -44,8 +68,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Mark a statement for insertion. If it matches an existing * Mark a statement for insertion. If it matches an existing statement, it will
* statement, it will update the statement instead. * update the statement instead.
* *
* @param statement * @param statement
* the statement to add or update * the statement to add or update
@ -57,8 +81,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Mark a statement for deletion. If no such statement exists, * Mark a statement for deletion. If no such statement exists, nothing will be
* nothing will be deleted. * deleted.
* *
* @param statement * @param statement
* the statement to delete * the statement to delete
@ -94,8 +118,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Adds a label to the item. It will override any * Adds a label to the item. It will override any existing label in this
* existing label in this language. * language.
* *
* @param label * @param label
* the label to add * the label to add
@ -107,8 +131,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Adds a list of labels to the item. It will override any * Adds a list of labels to the item. It will override any existing label in
* existing label in each language. * each language.
* *
* @param labels * @param labels
* the labels to add * the labels to add
@ -120,8 +144,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Adds a description to the item. It will override any existing * Adds a description to the item. It will override any existing description in
* description in this language. * this language.
* *
* @param description * @param description
* the description to add * the description to add
@ -133,8 +157,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Adds a list of descriptions to the item. It will override any * Adds a list of descriptions to the item. It will override any existing
* existing description in each language. * description in each language.
* *
* @param descriptions * @param descriptions
* the descriptions to add * the descriptions to add
@ -146,8 +170,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Adds an alias to the item. It will be added to any existing * Adds an alias to the item. It will be added to any existing aliases in that
* aliases in that language. * language.
* *
* @param alias * @param alias
* the alias to add * the alias to add
@ -159,8 +183,8 @@ public class ItemUpdateBuilder {
} }
/** /**
* Adds a list of aliases to the item. They will be added to any * Adds a list of aliases to the item. They will be added to any existing
* existing aliases in each language. * aliases in each language.
* *
* @param aliases * @param aliases
* the aliases to add * the aliases to add
@ -173,12 +197,12 @@ public class ItemUpdateBuilder {
/** /**
* Constructs the {@link ItemUpdate}. * Constructs the {@link ItemUpdate}.
*
* @return * @return
*/ */
public ItemUpdate build() { public ItemUpdate build() {
built = true; built = true;
return new ItemUpdate(qid, addedStatements, deletedStatements, return new ItemUpdate(qid, addedStatements, deletedStatements, labels, descriptions, aliases);
labels, descriptions, aliases);
} }
} }

View File

@ -1,6 +1,28 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates.scheduler; package org.openrefine.wikidata.updates.scheduler;
public class ImpossibleSchedulingException extends Exception { public class ImpossibleSchedulingException extends Exception {
private static final long serialVersionUID = 6621563898380564148L; private static final long serialVersionUID = 6621563898380564148L;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates.scheduler; package org.openrefine.wikidata.updates.scheduler;
import java.util.Collections; import java.util.Collections;
@ -5,7 +28,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue;
import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue; import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue; import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
@ -21,8 +43,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor; import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
/** /**
* A class that extracts the new entity ids referred to * A class that extracts the new entity ids referred to in a statement.
* in a statement.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -30,21 +51,18 @@ import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
public class PointerExtractor implements ValueVisitor<Set<ReconItemIdValue>> { public class PointerExtractor implements ValueVisitor<Set<ReconItemIdValue>> {
/** /**
* Extracts all the new entities mentioned by this statement. This * Extracts all the new entities mentioned by this statement. This does not
* does not include the subject of the statement. * include the subject of the statement.
* *
* @param statement * @param statement
* the statement to inspect * the statement to inspect
* @return * @return the set of all new entities mentioned by the statement
* the set of all new entities mentioned by the statement
*/ */
public Set<ReconItemIdValue> extractPointers(Statement statement) { public Set<ReconItemIdValue> extractPointers(Statement statement) {
Set<ReconItemIdValue> result = new HashSet<>(); Set<ReconItemIdValue> result = new HashSet<>();
result.addAll(extractPointers(statement.getClaim().getMainSnak())); result.addAll(extractPointers(statement.getClaim().getMainSnak()));
result.addAll(extractPointers(statement.getClaim().getQualifiers())); result.addAll(extractPointers(statement.getClaim().getQualifiers()));
statement.getReferences().stream() statement.getReferences().stream().map(l -> extractPointers(l.getSnakGroups())).forEach(s -> result.addAll(s));
.map(l -> extractPointers(l.getSnakGroups()))
.forEach(s -> result.addAll(s));
return result; return result;
} }
@ -56,9 +74,7 @@ public class PointerExtractor implements ValueVisitor<Set<ReconItemIdValue>> {
*/ */
public Set<ReconItemIdValue> extractPointers(List<SnakGroup> snakGroups) { public Set<ReconItemIdValue> extractPointers(List<SnakGroup> snakGroups) {
Set<ReconItemIdValue> result = new HashSet<>(); Set<ReconItemIdValue> result = new HashSet<>();
snakGroups.stream() snakGroups.stream().map(s -> extractPointers(s)).forEach(s -> result.addAll(s));
.map(s -> extractPointers(s))
.forEach(s -> result.addAll(s));
return result; return result;
} }
@ -70,16 +86,14 @@ public class PointerExtractor implements ValueVisitor<Set<ReconItemIdValue>> {
*/ */
public Set<ReconItemIdValue> extractPointers(SnakGroup snakGroup) { public Set<ReconItemIdValue> extractPointers(SnakGroup snakGroup) {
Set<ReconItemIdValue> result = new HashSet<>(); Set<ReconItemIdValue> result = new HashSet<>();
snakGroup.getSnaks().stream() snakGroup.getSnaks().stream().map(s -> extractPointers(s)).forEach(s -> result.addAll(s));
.map(s -> extractPointers(s))
.forEach(s -> result.addAll(s));
return result; return result;
} }
/** /**
* Extracts all new entities mentioned by this snak group. * Extracts all new entities mentioned by this snak group. Currently there will
* Currently there will be at most one: the target of the snak * be at most one: the target of the snak (as property ids cannot be new for
* (as property ids cannot be new for now). * now).
* *
* @param snak * @param snak
* @return * @return

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates.scheduler; package org.openrefine.wikidata.updates.scheduler;
import java.util.ArrayList; import java.util.ArrayList;
@ -8,45 +31,41 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue;
import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue; import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.ItemUpdateBuilder; import org.openrefine.wikidata.updates.ItemUpdateBuilder;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
public class QuickStatementsUpdateScheduler implements UpdateScheduler { public class QuickStatementsUpdateScheduler implements UpdateScheduler {
private PointerExtractor extractor = new PointerExtractor(); private PointerExtractor extractor = new PointerExtractor();
/** /**
* This map holds for each new entity id value a list of updates * This map holds for each new entity id value a list of updates that refer to
* that refer to this id (and should hence be scheduled right after * this id (and should hence be scheduled right after creation of that entity).
* creation of that entity).
*/ */
private Map<ItemIdValue, UpdateSequence> pointerUpdates; private Map<ItemIdValue, UpdateSequence> pointerUpdates;
/** /**
* This contains all updates which do not refer to any new entity * This contains all updates which do not refer to any new entity apart from
* apart from possibly the subject, in the order that they were supplied to us. * possibly the subject, in the order that they were supplied to us.
*/ */
private UpdateSequence pointerFreeUpdates; private UpdateSequence pointerFreeUpdates;
/** /**
* Separates out the statements which refer to new items from the rest * Separates out the statements which refer to new items from the rest of the
* of the update. The resulting updates are stored in {@link referencingUpdates} * update. The resulting updates are stored in {@link referencingUpdates} and
* and {@link updatesWithoutReferences}. * {@link updatesWithoutReferences}.
* *
* @param update * @param update
* @throws ImpossibleSchedulingException * @throws ImpossibleSchedulingException
* if two new item ids are referred to in the same statement * if two new item ids are referred to in the same statement
*/ */
protected void splitUpdate(ItemUpdate update) throws ImpossibleSchedulingException { protected void splitUpdate(ItemUpdate update)
throws ImpossibleSchedulingException {
ItemUpdateBuilder remainingUpdateBuilder = new ItemUpdateBuilder(update.getItemId()) ItemUpdateBuilder remainingUpdateBuilder = new ItemUpdateBuilder(update.getItemId())
.addLabels(update.getLabels()) .addLabels(update.getLabels()).addDescriptions(update.getDescriptions()).addAliases(update.getAliases())
.addDescriptions(update.getDescriptions())
.addAliases(update.getAliases())
.deleteStatements(update.getDeletedStatements()); .deleteStatements(update.getDeletedStatements());
Map<ItemIdValue, ItemUpdateBuilder> referencingUpdates = new HashMap<>(); Map<ItemIdValue, ItemUpdateBuilder> referencingUpdates = new HashMap<>();
@ -87,7 +106,8 @@ public class QuickStatementsUpdateScheduler implements UpdateScheduler {
} }
@Override @Override
public List<ItemUpdate> schedule(List<ItemUpdate> updates) throws ImpossibleSchedulingException { public List<ItemUpdate> schedule(List<ItemUpdate> updates)
throws ImpossibleSchedulingException {
pointerUpdates = new HashMap<>(); pointerUpdates = new HashMap<>();
pointerFreeUpdates = new UpdateSequence(); pointerFreeUpdates = new UpdateSequence();

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates.scheduler; package org.openrefine.wikidata.updates.scheduler;
import java.util.List; import java.util.List;
@ -5,11 +28,9 @@ import java.util.List;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
/** /**
* A scheduling strategy for item updates. * A scheduling strategy for item updates. Given a list of initial updates, the
* Given a list of initial updates, the scheduler * scheduler reorganizes these updates (possibly splitting them or merging them)
* reorganizes these updates (possibly splitting them * to create a sequence that is suitable for a particular import process.
* or merging them) to create a sequence that is suitable
* for a particular import process.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
* *
@ -17,16 +38,16 @@ import org.openrefine.wikidata.updates.ItemUpdate;
public interface UpdateScheduler { public interface UpdateScheduler {
/** /**
* Performs the scheduling. The initial updates are provided * Performs the scheduling. The initial updates are provided as a list so that
* as a list so that the scheduler can attempt to respect the * the scheduler can attempt to respect the initial order (but no guarantee is
* initial order (but no guarantee is made for that in general). * made for that in general).
* *
* @param updates * @param updates
* the updates to schedule * the updates to schedule
* @return * @return the reorganized updates
* the reorganized updates
* @throws ImpossibleSchedulingException * @throws ImpossibleSchedulingException
* when the scheduler cannot cope with a particular edit plan. * when the scheduler cannot cope with a particular edit plan.
*/ */
public List<ItemUpdate> schedule(List<ItemUpdate> updates) throws ImpossibleSchedulingException; public List<ItemUpdate> schedule(List<ItemUpdate> updates)
throws ImpossibleSchedulingException;
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates.scheduler; package org.openrefine.wikidata.updates.scheduler;
import java.util.ArrayList; import java.util.ArrayList;
@ -10,12 +33,13 @@ import org.openrefine.wikidata.updates.ItemUpdate;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
/** /**
* Helper class to store a list of updates where each subject * Helper class to store a list of updates where each subject appears at most
* appears at most once. It preserves order of insertion. * once. It preserves order of insertion.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
*/ */
public class UpdateSequence { public class UpdateSequence {
/** /**
* The list of updates stored by this container * The list of updates stored by this container
*/ */
@ -26,8 +50,8 @@ public class UpdateSequence {
private Map<ItemIdValue, Integer> index = new HashMap<>(); private Map<ItemIdValue, Integer> index = new HashMap<>();
/** /**
* Adds a new update to the list, merging it with any existing * Adds a new update to the list, merging it with any existing one with the same
* one with the same subject. * subject.
* *
* @param update * @param update
*/ */

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.updates.scheduler; package org.openrefine.wikidata.updates.scheduler;
import java.util.ArrayList; import java.util.ArrayList;
@ -15,11 +38,10 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
/** /**
* A simple scheduler for batches commited via the Wikibase API. * A simple scheduler for batches commited via the Wikibase API.
* *
* The strategy is quite simple and makes at most two edits * The strategy is quite simple and makes at most two edits per touched item
* per touched item (which is not minimal though). Each update * (which is not minimal though). Each update is split between statements making
* is split between statements making references to new items, * references to new items, and statements not making these references. All
* and statements not making these references. All updates with no * updates with no references to new items are done first (which creates all new
* references to new items are done first (which creates all new
* items), then all other updates are done. * items), then all other updates are done.
* *
* @author Antonin Delpeuch * @author Antonin Delpeuch
@ -28,13 +50,13 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
public class WikibaseAPIUpdateScheduler implements UpdateScheduler { public class WikibaseAPIUpdateScheduler implements UpdateScheduler {
/** /**
* The first part of updates: the ones which create new items * The first part of updates: the ones which create new items without referring
* without referring to any other new item. * to any other new item.
*/ */
private UpdateSequence pointerFreeUpdates; private UpdateSequence pointerFreeUpdates;
/** /**
* The second part of the updates: all existing items, plus * The second part of the updates: all existing items, plus all parts of new
* all parts of new items that refer to other new items. * items that refer to other new items.
*/ */
private UpdateSequence pointerFullUpdates; private UpdateSequence pointerFullUpdates;
/** /**
@ -62,9 +84,7 @@ public class WikibaseAPIUpdateScheduler implements UpdateScheduler {
Set<ItemIdValue> unseenPointers = new HashSet<>(allPointers); Set<ItemIdValue> unseenPointers = new HashSet<>(allPointers);
unseenPointers.removeAll(pointerFreeUpdates.getSubjects()); unseenPointers.removeAll(pointerFreeUpdates.getSubjects());
result.addAll(unseenPointers.stream() result.addAll(unseenPointers.stream().map(e -> new ItemUpdateBuilder(e).build()).collect(Collectors.toList()));
.map(e -> new ItemUpdateBuilder(e).build())
.collect(Collectors.toList()));
// Part 2: add all the pointer full updates // Part 2: add all the pointer full updates
result.addAll(pointerFullUpdates.getUpdates()); result.addAll(pointerFullUpdates.getUpdates());
@ -74,13 +94,12 @@ public class WikibaseAPIUpdateScheduler implements UpdateScheduler {
/** /**
* Splits an update into two parts * Splits an update into two parts
*
* @param update * @param update
*/ */
protected void splitUpdate(ItemUpdate update) { protected void splitUpdate(ItemUpdate update) {
ItemUpdateBuilder pointerFreeBuilder = new ItemUpdateBuilder(update.getItemId()) ItemUpdateBuilder pointerFreeBuilder = new ItemUpdateBuilder(update.getItemId()).addLabels(update.getLabels())
.addLabels(update.getLabels()) .addDescriptions(update.getDescriptions()).addAliases(update.getAliases())
.addDescriptions(update.getDescriptions())
.addAliases(update.getAliases())
.deleteStatements(update.getDeletedStatements()); .deleteStatements(update.getDeletedStatements());
ItemUpdateBuilder pointerFullBuilder = new ItemUpdateBuilder(update.getItemId()); ItemUpdateBuilder pointerFullBuilder = new ItemUpdateBuilder(update.getItemId());

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.utils; package org.openrefine.wikidata.utils;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -14,6 +37,7 @@ import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
public class EntityCache { public class EntityCache {
private static EntityCache _entityCache = new EntityCache(); private static EntityCache _entityCache = new EntityCache();
private LoadingCache<String, EntityDocument> _cache = null; private LoadingCache<String, EntityDocument> _cache = null;
@ -23,12 +47,11 @@ public class EntityCache {
ApiConnection connection = ApiConnection.getWikidataApiConnection(); ApiConnection connection = ApiConnection.getWikidataApiConnection();
_fetcher = new WikibaseDataFetcher(connection, Datamodel.SITE_WIKIDATA); _fetcher = new WikibaseDataFetcher(connection, Datamodel.SITE_WIKIDATA);
_cache = CacheBuilder.newBuilder() _cache = CacheBuilder.newBuilder().maximumSize(4096).expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(4096) .build(new CacheLoader<String, EntityDocument>() {
.expireAfterWrite(1, TimeUnit.HOURS)
.build( public EntityDocument load(String entityId)
new CacheLoader<String, EntityDocument>() { throws Exception {
public EntityDocument load(String entityId) throws Exception {
EntityDocument doc = _fetcher.getEntityDocument(entityId); EntityDocument doc = _fetcher.getEntityDocument(entityId);
if (doc != null) { if (doc != null) {
return doc; return doc;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.utils; package org.openrefine.wikidata.utils;
import java.io.IOException; import java.io.IOException;
@ -6,17 +29,19 @@ import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
public class FirstLinesExtractor { public class FirstLinesExtractor {
/** /**
* Returns the first n lines of a given string * Returns the first n lines of a given string
*
* @param content * @param content
* the content, where lines are separated by '\n' * the content, where lines are separated by '\n'
* @param nbLines * @param nbLines
* the number of lines to extract * the number of lines to extract
* @return * @return the first lines of the string
* the first lines of the string
* @throws IOException * @throws IOException
*/ */
public static String extractFirstLines(String content, int nbLines) throws IOException { public static String extractFirstLines(String content, int nbLines)
throws IOException {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
LineNumberReader reader = new LineNumberReader(new StringReader(content)); LineNumberReader reader = new LineNumberReader(new StringReader(content));

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.utils; package org.openrefine.wikidata.utils;
import java.io.IOException; import java.io.IOException;
@ -16,13 +39,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.refine.Jsonizable; import com.google.refine.Jsonizable;
/** /**
* This class is inefficient because it serializes the * This class is inefficient because it serializes the object to string and then
* object to string and then deserializes it back. Unfortunately, * deserializes it back. Unfortunately, this is the only simple way to bridge
* this is the only simple way to bridge Jackson to org.json. * Jackson to org.json. This conversion should be removed when (if ?) we migrate
* This conversion should be removed when (if ?) we migrate OpenRefine * OpenRefine a better JSON library.
* a better JSON library.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@ -39,7 +61,8 @@ public abstract class JacksonJsonizable implements Jsonizable {
} }
} }
public static <T> T fromJSONClass(JSONObject obj, Class<T> klass) throws JSONException { public static <T> T fromJSONClass(JSONObject obj, Class<T> klass)
throws JSONException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String json = obj.toString(); String json = obj.toString();
try { try {

View File

@ -1,33 +1,49 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
import org.openrefine.wikidata.testing.TestingData;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.openrefine.wikidata.testing.TestingData;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
import com.google.refine.util.ParsingUtilities;
public abstract class CommandTest extends RefineTest { public abstract class CommandTest extends RefineTest {
protected Project project = null; protected Project project = null;
protected HttpServletRequest request = null; protected HttpServletRequest request = null;
protected HttpServletResponse response = null; protected HttpServletResponse response = null;
@ -36,7 +52,8 @@ public abstract class CommandTest extends RefineTest {
protected Command command = null; protected Command command = null;
@BeforeMethod(alwaysRun = true) @BeforeMethod(alwaysRun = true)
public void setUpProject() throws JSONException { public void setUpProject()
throws JSONException {
project = createCSVProject(TestingData.inceptionWithNewCsv); project = createCSVProject(TestingData.inceptionWithNewCsv);
TestingData.reconcileInceptionCells(project); TestingData.reconcileInceptionCells(project);
request = mock(HttpServletRequest.class); request = mock(HttpServletRequest.class);
@ -53,5 +70,4 @@ public abstract class CommandTest extends RefineTest {
} }
} }
} }

View File

@ -1,5 +1,34 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.openrefine.wikidata.testing.TestingData.jsonFromFile;
import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.json.JSONException; import org.json.JSONException;
@ -8,24 +37,19 @@ import org.openrefine.wikidata.testing.TestingData;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.io.IOException;
import com.google.refine.util.ParsingUtilities; import com.google.refine.util.ParsingUtilities;
import static org.openrefine.wikidata.testing.TestingData.jsonFromFile;
public class PreviewWikibaseSchemaCommandTest extends SchemaCommandTest { public class PreviewWikibaseSchemaCommandTest extends SchemaCommandTest {
@BeforeMethod @BeforeMethod
public void SetUp() throws JSONException { public void SetUp()
throws JSONException {
command = new PreviewWikibaseSchemaCommand(); command = new PreviewWikibaseSchemaCommand();
} }
@Test @Test
public void testValidSchema() throws JSONException, IOException, ServletException { public void testValidSchema()
throws JSONException, IOException, ServletException {
String schemaJson = jsonFromFile("data/schema/inception.json").toString(); String schemaJson = jsonFromFile("data/schema/inception.json").toString();
when(request.getParameter("schema")).thenReturn(schemaJson); when(request.getParameter("schema")).thenReturn(schemaJson);

View File

@ -1,16 +1,38 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import org.testng.annotations.BeforeMethod; import static org.junit.Assert.assertTrue;
import org.testng.annotations.Test; import static org.mockito.Mockito.when;
import static org.openrefine.wikidata.testing.TestingData.jsonFromFile; import static org.openrefine.wikidata.testing.TestingData.jsonFromFile;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import static org.junit.Assert.assertTrue; import org.testng.annotations.BeforeMethod;
import static org.mockito.Mockito.when; import org.testng.annotations.Test;
public class SaveWikibaseSchemaCommandTest extends SchemaCommandTest { public class SaveWikibaseSchemaCommandTest extends SchemaCommandTest {
@ -20,7 +42,8 @@ public class SaveWikibaseSchemaCommandTest extends SchemaCommandTest {
} }
@Test @Test
public void testValidSchema() throws ServletException, IOException { public void testValidSchema()
throws ServletException, IOException {
String schemaJson = jsonFromFile("data/schema/inception.json").toString(); String schemaJson = jsonFromFile("data/schema/inception.json").toString();
when(request.getParameter("schema")).thenReturn(schemaJson); when(request.getParameter("schema")).thenReturn(schemaJson);

View File

@ -1,8 +1,28 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -10,17 +30,21 @@ import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.testng.annotations.Test;
public abstract class SchemaCommandTest extends CommandTest { public abstract class SchemaCommandTest extends CommandTest {
@Test @Test
public void testNoSchema() throws ServletException, IOException { public void testNoSchema()
throws ServletException, IOException {
command.doPost(request, response); command.doPost(request, response);
assertEquals("{\"status\":\"error\",\"message\":\"No Wikibase schema provided.\"}", writer.toString()); assertEquals("{\"status\":\"error\",\"message\":\"No Wikibase schema provided.\"}", writer.toString());
} }
@Test @Test
public void testInvalidSchema() throws ServletException, IOException { public void testInvalidSchema()
throws ServletException, IOException {
when(request.getParameter("schema")).thenReturn("{bogus json"); when(request.getParameter("schema")).thenReturn("{bogus json");
command.doPost(request, response); command.doPost(request, response);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.testing.TestingData;
@ -31,7 +54,6 @@ import java.util.stream.Collectors;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
public class EditBatchProcessorTest extends RefineTest { public class EditBatchProcessorTest extends RefineTest {
private WikibaseDataFetcher fetcher = null; private WikibaseDataFetcher fetcher = null;
@ -48,27 +70,23 @@ public class EditBatchProcessorTest extends RefineTest {
} }
@Test @Test
public void testNewItem() throws InterruptedException, MediaWikiApiErrorException, IOException { public void testNewItem()
throws InterruptedException, MediaWikiApiErrorException, IOException {
List<ItemUpdate> batch = new ArrayList<>(); List<ItemUpdate> batch = new ArrayList<>();
batch.add(new ItemUpdateBuilder(TestingData.existingId) batch.add(new ItemUpdateBuilder(TestingData.existingId)
.addAlias(Datamodel.makeMonolingualTextValue("my new alias", "en")) .addAlias(Datamodel.makeMonolingualTextValue("my new alias", "en"))
.addStatement(TestingData.generateStatement(TestingData.existingId, TestingData.newIdA)) .addStatement(TestingData.generateStatement(TestingData.existingId, TestingData.newIdA)).build());
.build());
MonolingualTextValue label = Datamodel.makeMonolingualTextValue("better label", "en"); MonolingualTextValue label = Datamodel.makeMonolingualTextValue("better label", "en");
batch.add(new ItemUpdateBuilder(TestingData.newIdA) batch.add(new ItemUpdateBuilder(TestingData.newIdA).addAlias(label).build());
.addAlias(label)
.build());
// Plan expected edits // Plan expected edits
ItemDocument existingItem = ItemDocumentBuilder.forItemId(TestingData.existingId) ItemDocument existingItem = ItemDocumentBuilder.forItemId(TestingData.existingId)
.withLabel(Datamodel.makeMonolingualTextValue("pomme", "fr")) .withLabel(Datamodel.makeMonolingualTextValue("pomme", "fr"))
.withDescription(Datamodel.makeMonolingualTextValue("fruit délicieux", "fr")) .withDescription(Datamodel.makeMonolingualTextValue("fruit délicieux", "fr")).build();
.build();
when(fetcher.getEntityDocuments(Collections.singletonList(TestingData.existingId.getId()))) when(fetcher.getEntityDocuments(Collections.singletonList(TestingData.existingId.getId())))
.thenReturn(Collections.singletonMap(TestingData.existingId.getId(), existingItem)); .thenReturn(Collections.singletonMap(TestingData.existingId.getId(), existingItem));
ItemDocument expectedNewItem = ItemDocumentBuilder.forItemId(TestingData.newIdA) ItemDocument expectedNewItem = ItemDocumentBuilder.forItemId(TestingData.newIdA).withLabel(label).build();
.withLabel(label).build();
ItemDocument createdNewItem = ItemDocumentBuilder.forItemId(Datamodel.makeWikidataItemIdValue("Q1234")) ItemDocument createdNewItem = ItemDocumentBuilder.forItemId(Datamodel.makeWikidataItemIdValue("Q1234"))
.withLabel(label).withRevisionId(37828L).build(); .withLabel(label).withRevisionId(37828L).build();
when(editor.createItemDocument(expectedNewItem, summary)).thenReturn(createdNewItem); when(editor.createItemDocument(expectedNewItem, summary)).thenReturn(createdNewItem);
@ -92,27 +110,24 @@ public class EditBatchProcessorTest extends RefineTest {
} }
@Test @Test
public void testMultipleBatches() throws MediaWikiApiErrorException, InterruptedException, IOException { public void testMultipleBatches()
throws MediaWikiApiErrorException, InterruptedException, IOException {
// Prepare test data // Prepare test data
MonolingualTextValue description = Datamodel.makeMonolingualTextValue("village in Nepal", "en"); MonolingualTextValue description = Datamodel.makeMonolingualTextValue("village in Nepal", "en");
List<String> ids = new ArrayList<>(); List<String> ids = new ArrayList<>();
for (int i = 124; i < 190; i++) { for (int i = 124; i < 190; i++) {
ids.add("Q" + String.valueOf(i)); ids.add("Q" + String.valueOf(i));
} }
List<ItemIdValue> qids = ids.stream() List<ItemIdValue> qids = ids.stream().map(e -> Datamodel.makeWikidataItemIdValue(e))
.map(e -> Datamodel.makeWikidataItemIdValue(e))
.collect(Collectors.toList()); .collect(Collectors.toList());
List<ItemUpdate> batch = qids.stream() List<ItemUpdate> batch = qids.stream()
.map(qid -> new ItemUpdateBuilder(qid) .map(qid -> new ItemUpdateBuilder(qid).addDescription(description).build())
.addDescription(description)
.build())
.collect(Collectors.toList()); .collect(Collectors.toList());
int batchSize = 50; int batchSize = 50;
List<ItemDocument> fullBatch = qids.stream() List<ItemDocument> fullBatch = qids.stream()
.map(qid -> ItemDocumentBuilder.forItemId(qid) .map(qid -> ItemDocumentBuilder.forItemId(qid)
.withStatement(TestingData.generateStatement(qid, TestingData.existingId)) .withStatement(TestingData.generateStatement(qid, TestingData.existingId)).build())
.build())
.collect(Collectors.toList()); .collect(Collectors.toList());
List<ItemDocument> firstBatch = fullBatch.subList(0, batchSize); List<ItemDocument> firstBatch = fullBatch.subList(0, batchSize);
List<ItemDocument> secondBatch = fullBatch.subList(batchSize, fullBatch.size()); List<ItemDocument> secondBatch = fullBatch.subList(batchSize, fullBatch.size());
@ -142,8 +157,7 @@ public class EditBatchProcessorTest extends RefineTest {
} }
private Map<String, EntityDocument> toMap(List<ItemDocument> docs) { private Map<String, EntityDocument> toMap(List<ItemDocument> docs) {
return docs.stream() return docs.stream().collect(Collectors.toMap(doc -> doc.getItemId().getId(), doc -> doc));
.collect(Collectors.toMap(doc -> doc.getItemId().getId(), doc -> doc));
} }
private List<String> toQids(List<ItemDocument> docs) { private List<String> toQids(List<ItemDocument> docs) {

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -13,6 +36,7 @@ import com.google.refine.model.Recon;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
public class NewItemLibraryTest extends RefineTest { public class NewItemLibraryTest extends RefineTest {
private NewItemLibrary library; private NewItemLibrary library;
@BeforeMethod @BeforeMethod

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -52,16 +75,14 @@ public class ReconEntityRewriterTest {
.deleteStatement(TestingData.generateStatement(subject, TestingData.existingId)) .deleteStatement(TestingData.generateStatement(subject, TestingData.existingId))
.addLabel(Datamodel.makeMonolingualTextValue("label", "de")) .addLabel(Datamodel.makeMonolingualTextValue("label", "de"))
.addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de")) .addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"))
.addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")) .addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
.build();
ItemUpdate rewritten = rewriter.rewrite(update); ItemUpdate rewritten = rewriter.rewrite(update);
ItemUpdate expected = new ItemUpdateBuilder(subject) ItemUpdate expected = new ItemUpdateBuilder(subject)
.addStatement(TestingData.generateStatement(subject, newlyCreated)) .addStatement(TestingData.generateStatement(subject, newlyCreated))
.deleteStatement(TestingData.generateStatement(subject, TestingData.existingId)) .deleteStatement(TestingData.generateStatement(subject, TestingData.existingId))
.addLabel(Datamodel.makeMonolingualTextValue("label", "de")) .addLabel(Datamodel.makeMonolingualTextValue("label", "de"))
.addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de")) .addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"))
.addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")) .addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
.build();
assertEquals(expected, rewritten); assertEquals(expected, rewritten);
} }
} }

View File

@ -1,7 +1,29 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.exporters; package org.openrefine.wikidata.exporters;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -51,8 +73,10 @@ public class QSValuePrinterTest {
@Test @Test
public void printGlobeCoordinate() { public void printGlobeCoordinate() {
// I don't see how to avoid the trailing zeros - in any case it's not a big deal because // I don't see how to avoid the trailing zeros - in any case it's not a big deal
// the precision is governed by a different parameter that QuickStatements does not support. // because
// the precision is governed by a different parameter that QuickStatements does
// not support.
assertPrints("@43.261930/10.927080", Datamodel.makeGlobeCoordinatesValue(43.26193, 10.92708, assertPrints("@43.261930/10.927080", Datamodel.makeGlobeCoordinatesValue(43.26193, 10.92708,
GlobeCoordinatesValue.PREC_DEGREE, GlobeCoordinatesValue.GLOBE_EARTH)); GlobeCoordinatesValue.PREC_DEGREE, GlobeCoordinatesValue.GLOBE_EARTH));
} }
@ -68,20 +92,19 @@ public class QSValuePrinterTest {
@Test @Test
public void printSimpleQuantityValue() { public void printSimpleQuantityValue() {
assertPrints("10.00", Datamodel.makeQuantityValue(new BigDecimal("10.00"), assertPrints("10.00", Datamodel.makeQuantityValue(new BigDecimal("10.00"), null, null, "1"));
null, null, "1"));
} }
@Test @Test
public void printQuantityValueWithUnit() { public void printQuantityValueWithUnit() {
assertPrints("10.00U11573", Datamodel.makeQuantityValue(new BigDecimal("10.00"), assertPrints("10.00U11573", Datamodel.makeQuantityValue(new BigDecimal("10.00"), null, null,
null, null, "http://www.wikidata.org/entity/Q11573")); "http://www.wikidata.org/entity/Q11573"));
} }
@Test @Test
public void printQuantityValueWithBounds() { public void printQuantityValueWithBounds() {
assertPrints("10.00[9.0,11.05]", Datamodel.makeQuantityValue(new BigDecimal("10.00"), assertPrints("10.00[9.0,11.05]", Datamodel.makeQuantityValue(new BigDecimal("10.00"), new BigDecimal("9.0"),
new BigDecimal("9.0"), new BigDecimal("11.05"), "1")); new BigDecimal("11.05"), "1"));
} }
@Test @Test
@ -101,13 +124,13 @@ public class QSValuePrinterTest {
@Test @Test
public void printYear() { public void printYear() {
assertPrints("+1586-00-00T00:00:00Z/9", Datamodel.makeTimeValue(1586L, (byte)0, (byte)0, (byte)0, assertPrints("+1586-00-00T00:00:00Z/9", Datamodel.makeTimeValue(1586L, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
(byte)0, (byte)0, (byte)9, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO)); (byte) 0, (byte) 9, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO));
} }
@Test @Test
public void printDay() { public void printDay() {
assertPrints("+1586-03-09T00:00:00Z/11", Datamodel.makeTimeValue(1586L, (byte)3, (byte)9, (byte)0, assertPrints("+1586-03-09T00:00:00Z/11", Datamodel.makeTimeValue(1586L, (byte) 3, (byte) 9, (byte) 0, (byte) 0,
(byte)0, (byte)0, (byte)11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO)); (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO));
} }
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.exporters; package org.openrefine.wikidata.exporters;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -14,7 +37,6 @@ import org.openrefine.wikidata.schema.WikibaseSchema;
import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.testing.TestingData;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.ItemUpdateBuilder; import org.openrefine.wikidata.updates.ItemUpdateBuilder;
import org.openrefine.wikidata.updates.scheduler.UpdateSchedulerTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim; import org.wikidata.wdtk.datamodel.interfaces.Claim;
@ -36,16 +58,17 @@ public class QuickStatementsExporterTest extends RefineTest {
private ItemIdValue qid1 = Datamodel.makeWikidataItemIdValue("Q1377"); private ItemIdValue qid1 = Datamodel.makeWikidataItemIdValue("Q1377");
private ItemIdValue qid2 = Datamodel.makeWikidataItemIdValue("Q865528"); private ItemIdValue qid2 = Datamodel.makeWikidataItemIdValue("Q865528");
private String export(ItemUpdate... itemUpdates) throws IOException { private String export(ItemUpdate... itemUpdates)
throws IOException {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
exporter.translateItemList(Arrays.asList(itemUpdates), writer); exporter.translateItemList(Arrays.asList(itemUpdates), writer);
return writer.toString(); return writer.toString();
} }
@Test @Test
public void testSimpleProject() throws JSONException, IOException { public void testSimpleProject()
Project project = this.createCSVProject( throws JSONException, IOException {
TestingData.inceptionWithNewCsv); Project project = this.createCSVProject(TestingData.inceptionWithNewCsv);
TestingData.reconcileInceptionCells(project); TestingData.reconcileInceptionCells(project);
JSONObject serialized = TestingData.jsonFromFile("data/schema/inception.json"); JSONObject serialized = TestingData.jsonFromFile("data/schema/inception.json");
WikibaseSchema schema = WikibaseSchema.reconstruct(serialized); WikibaseSchema schema = WikibaseSchema.reconstruct(serialized);
@ -59,40 +82,38 @@ public class QuickStatementsExporterTest extends RefineTest {
} }
@Test @Test
public void testImpossibleScheduling() throws IOException { public void testImpossibleScheduling()
throws IOException {
Statement sNewAtoNewB = TestingData.generateStatement(newIdA, newIdB); Statement sNewAtoNewB = TestingData.generateStatement(newIdA, newIdB);
ItemUpdate update = new ItemUpdateBuilder(newIdA).addStatement(sNewAtoNewB).build(); ItemUpdate update = new ItemUpdateBuilder(newIdA).addStatement(sNewAtoNewB).build();
assertEquals(QuickStatementsExporter.impossibleSchedulingErrorMessage, assertEquals(QuickStatementsExporter.impossibleSchedulingErrorMessage, export(update));
export(update));
} }
@Test @Test
public void testNameDesc() throws IOException { public void testNameDesc()
throws IOException {
ItemUpdate update = new ItemUpdateBuilder(newIdA) ItemUpdate update = new ItemUpdateBuilder(newIdA)
.addLabel(Datamodel.makeMonolingualTextValue("my new item", "en")) .addLabel(Datamodel.makeMonolingualTextValue("my new item", "en"))
.addDescription(Datamodel.makeMonolingualTextValue("isn't it awesome?", "en")) .addDescription(Datamodel.makeMonolingualTextValue("isn't it awesome?", "en"))
.addAlias(Datamodel.makeMonolingualTextValue("fabitem", "en")) .addAlias(Datamodel.makeMonolingualTextValue("fabitem", "en")).build();
.build();
assertEquals("CREATE\n"+ assertEquals("CREATE\n" + "LAST\tLen\t\"my new item\"\n" + "LAST\tDen\t\"isn't it awesome?\"\n"
"LAST\tLen\t\"my new item\"\n"+ + "LAST\tAen\t\"fabitem\"\n", export(update));
"LAST\tDen\t\"isn't it awesome?\"\n"+
"LAST\tAen\t\"fabitem\"\n",
export(update));
} }
@Test @Test
public void testDeleteStatement() throws IOException { public void testDeleteStatement()
ItemUpdate update = new ItemUpdateBuilder(qid1) throws IOException {
.deleteStatement(TestingData.generateStatement(qid1, qid2)) ItemUpdate update = new ItemUpdateBuilder(qid1).deleteStatement(TestingData.generateStatement(qid1, qid2))
.build(); .build();
assertEquals("- Q1377\tP38\tQ865528\n", export(update)); assertEquals("- Q1377\tP38\tQ865528\n", export(update));
} }
@Test @Test
public void testQualifier() throws IOException { public void testQualifier()
throws IOException {
Statement baseStatement = TestingData.generateStatement(qid1, qid2); Statement baseStatement = TestingData.generateStatement(qid1, qid2);
Statement otherStatement = TestingData.generateStatement(qid2, qid1); Statement otherStatement = TestingData.generateStatement(qid2, qid1);
Snak qualifierSnak = otherStatement.getClaim().getMainSnak(); Snak qualifierSnak = otherStatement.getClaim().getMainSnak();
@ -100,15 +121,14 @@ public class QuickStatementsExporterTest extends RefineTest {
Claim claim = Datamodel.makeClaim(qid1, baseStatement.getClaim().getMainSnak(), Claim claim = Datamodel.makeClaim(qid1, baseStatement.getClaim().getMainSnak(),
Collections.singletonList(group)); Collections.singletonList(group));
Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, ""); Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
ItemUpdate update = new ItemUpdateBuilder(qid1) ItemUpdate update = new ItemUpdateBuilder(qid1).addStatement(statement).build();
.addStatement(statement)
.build();
assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\n", export(update)); assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\n", export(update));
} }
@Test @Test
public void testNoSchema() throws IOException { public void testNoSchema()
throws IOException {
Project project = this.createCSVProject("a,b\nc,d"); Project project = this.createCSVProject("a,b\nc,d");
Engine engine = new Engine(project); Engine engine = new Engine(project);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();

View File

@ -1,5 +1,31 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.operations; package org.openrefine.wikidata.operations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.StringReader; import java.io.StringReader;
@ -12,9 +38,6 @@ import org.openrefine.wikidata.testing.JacksonSerializationTest;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.refine.history.Change; import com.google.refine.history.Change;
import com.google.refine.model.AbstractOperation; import com.google.refine.model.AbstractOperation;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -42,12 +65,15 @@ public abstract class OperationTest extends RefineTest {
OperationRegistry.registerOperation(module, name, klass); OperationRegistry.registerOperation(module, name, klass);
} }
public abstract AbstractOperation reconstruct() throws Exception; public abstract AbstractOperation reconstruct()
throws Exception;
public abstract JSONObject getJson() throws Exception; public abstract JSONObject getJson()
throws Exception;
@Test @Test
public void testReconstruct() throws Exception { public void testReconstruct()
throws Exception {
JSONObject json = getJson(); JSONObject json = getJson();
AbstractOperation op = reconstruct(); AbstractOperation op = reconstruct();
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
@ -61,7 +87,8 @@ public abstract class OperationTest extends RefineTest {
return new LineNumberReader(reader); return new LineNumberReader(reader);
} }
protected String saveChange(Change change) throws IOException { protected String saveChange(Change change)
throws IOException {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
change.save(writer, new Properties()); change.save(writer, new Properties());
return writer.toString(); return writer.toString();

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.operations; package org.openrefine.wikidata.operations;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -21,20 +44,22 @@ public class PerformWikibaseEditsOperationTest extends OperationTest {
} }
@Override @Override
public AbstractOperation reconstruct() throws Exception { public AbstractOperation reconstruct()
throws Exception {
JSONObject json = getJson(); JSONObject json = getJson();
return PerformWikibaseEditsOperation.reconstruct(project, json); return PerformWikibaseEditsOperation.reconstruct(project, json);
} }
@Override @Override
public JSONObject getJson() throws Exception { public JSONObject getJson()
throws Exception {
return TestingData.jsonFromFile("data/operations/perform-edits.json"); return TestingData.jsonFromFile("data/operations/perform-edits.json");
} }
@Test @Test
public void testLoadChange() throws Exception { public void testLoadChange()
String changeString = "newItems={\"qidMap\":{\"1234\":\"Q789\"}}\n" + throws Exception {
"/ec/\n"; String changeString = "newItems={\"qidMap\":{\"1234\":\"Q789\"}}\n" + "/ec/\n";
LineNumberReader reader = makeReader(changeString); LineNumberReader reader = makeReader(changeString);
Change change = PerformWikibaseEditsOperation.PerformWikibaseEditsChange.load(reader, pool); Change change = PerformWikibaseEditsOperation.PerformWikibaseEditsChange.load(reader, pool);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.operations; package org.openrefine.wikidata.operations;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -14,7 +37,6 @@ import org.testng.annotations.Test;
import com.google.refine.history.Change; import com.google.refine.history.Change;
import com.google.refine.model.AbstractOperation; import com.google.refine.model.AbstractOperation;
public class SaveWikibaseSchemaOperationTest extends OperationTest { public class SaveWikibaseSchemaOperationTest extends OperationTest {
@BeforeMethod @BeforeMethod
@ -23,22 +45,22 @@ public class SaveWikibaseSchemaOperationTest extends OperationTest {
} }
@Override @Override
public AbstractOperation reconstruct() throws Exception { public AbstractOperation reconstruct()
throws Exception {
return SaveWikibaseSchemaOperation.reconstruct(project, getJson()); return SaveWikibaseSchemaOperation.reconstruct(project, getJson());
} }
@Override @Override
public JSONObject getJson() throws Exception { public JSONObject getJson()
throws Exception {
return TestingData.jsonFromFile("data/operations/save-schema.json"); return TestingData.jsonFromFile("data/operations/save-schema.json");
} }
@Test @Test
public void testLoadChange() throws Exception { public void testLoadChange()
throws Exception {
JSONObject schemaJson = TestingData.jsonFromFile("data/schema/inception.json"); JSONObject schemaJson = TestingData.jsonFromFile("data/schema/inception.json");
String changeString = String changeString = "newSchema=" + schemaJson.toString() + "\n" + "oldSchema=\n" + "/ec/";
"newSchema="+schemaJson.toString()+"\n" +
"oldSchema=\n" +
"/ec/";
WikibaseSchema schema = WikibaseSchema.reconstruct(schemaJson); WikibaseSchema schema = WikibaseSchema.reconstruct(schemaJson);
LineNumberReader reader = makeReader(changeString); LineNumberReader reader = makeReader(changeString);
@ -46,7 +68,8 @@ public class SaveWikibaseSchemaOperationTest extends OperationTest {
change.apply(project); change.apply(project);
assertEquals(schema, project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey)); assertEquals(schema,
project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey));
change.revert(project); change.revert(project);

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import java.util.Arrays; import java.util.Arrays;
@ -8,7 +31,6 @@ import java.util.stream.Collectors;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
public class MockConstraintFetcher implements ConstraintFetcher { public class MockConstraintFetcher implements ConstraintFetcher {
public static PropertyIdValue pidWithInverse = Datamodel.makeWikidataPropertyIdValue("P350"); public static PropertyIdValue pidWithInverse = Datamodel.makeWikidataPropertyIdValue("P350");
@ -26,8 +48,8 @@ public class MockConstraintFetcher implements ConstraintFetcher {
} }
/** /**
* This constraint is purposely left inconsistent (the inverse * This constraint is purposely left inconsistent (the inverse constraint holds
* constraint holds only on one side). * only on one side).
*/ */
@Override @Override
public PropertyIdValue getInversePid(PropertyIdValue pid) { public PropertyIdValue getInversePid(PropertyIdValue pid) {

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -7,13 +30,10 @@ import org.testng.annotations.Test;
public class QAWarningTest { public class QAWarningTest {
public static QAWarning exampleWarning = new QAWarning("add-statements-with-invalid-format", public static QAWarning exampleWarning = new QAWarning("add-statements-with-invalid-format", "P2427",
"P2427", QAWarning.Severity.IMPORTANT, 1);
QAWarning.Severity.IMPORTANT, public static String exampleJson = "{\"severity\":\"IMPORTANT\","
1); + "\"count\":1,\"bucketId\":\"P2427\",\"type\":\"add-statements-with-invalid-format\"}";
public static String exampleJson =
"{\"severity\":\"IMPORTANT\","+
"\"count\":1,\"bucketId\":\"P2427\",\"type\":\"add-statements-with-invalid-format\"}";
@Test @Test
public void testSerialize() { public void testSerialize() {
@ -22,9 +42,7 @@ public class QAWarningTest {
@Test @Test
public void testAggregate() { public void testAggregate() {
QAWarning firstWarning = new QAWarning("add-statements-with-invalid-format", QAWarning firstWarning = new QAWarning("add-statements-with-invalid-format", "P2427", QAWarning.Severity.INFO,
"P2427",
QAWarning.Severity.INFO,
1); 1);
firstWarning.setProperty("foo", "bar"); firstWarning.setProperty("foo", "bar");
assertEquals(exampleWarning.getAggregationId(), firstWarning.getAggregationId()); assertEquals(exampleWarning.getAggregationId(), firstWarning.getAggregationId());
@ -38,10 +56,7 @@ public class QAWarningTest {
@Test @Test
public void testCompare() { public void testCompare() {
QAWarning otherWarning = new QAWarning("no-reference", QAWarning otherWarning = new QAWarning("no-reference", "no-reference", QAWarning.Severity.WARNING, 1);
"no-reference",
QAWarning.Severity.WARNING,
1);
assertEquals(1, otherWarning.compareTo(exampleWarning)); assertEquals(1, otherWarning.compareTo(exampleWarning));
assertEquals(-1, exampleWarning.compareTo(otherWarning)); assertEquals(-1, exampleWarning.compareTo(otherWarning));
assertEquals(0, exampleWarning.compareTo(exampleWarning)); assertEquals(0, exampleWarning.compareTo(exampleWarning));

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa; package org.openrefine.wikidata.qa;
import org.testng.Assert; import org.testng.Assert;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.testing.TestingData;
@ -17,12 +40,8 @@ public class DistinctValuesScrutinizerTest extends StatementScrutinizerTest {
public void testTrigger() { public void testTrigger() {
ItemIdValue idA = TestingData.existingId; ItemIdValue idA = TestingData.existingId;
ItemIdValue idB = TestingData.matchedId; ItemIdValue idB = TestingData.matchedId;
ItemUpdate updateA = new ItemUpdateBuilder(idA) ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(TestingData.generateStatement(idA, idB)).build();
.addStatement(TestingData.generateStatement(idA, idB)) ItemUpdate updateB = new ItemUpdateBuilder(idB).addStatement(TestingData.generateStatement(idB, idB)).build();
.build();
ItemUpdate updateB = new ItemUpdateBuilder(idB)
.addStatement(TestingData.generateStatement(idB, idB))
.build();
scrutinize(updateA, updateB); scrutinize(updateA, updateB);
assertWarningsRaised(DistinctValuesScrutinizer.type); assertWarningsRaised(DistinctValuesScrutinizer.type);
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.testng.annotations.Test; import org.testng.annotations.Test;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.MockConstraintFetcher; import org.openrefine.wikidata.qa.MockConstraintFetcher;
@ -23,16 +46,14 @@ public class InverseConstaintScrutinizerTest extends StatementScrutinizerTest {
@Test @Test
public void testTrigger() { public void testTrigger() {
ItemUpdate update = new ItemUpdateBuilder(idA) ItemUpdate update = new ItemUpdateBuilder(idA)
.addStatement(TestingData.generateStatement(idA, pidWithInverse, idB)) .addStatement(TestingData.generateStatement(idA, pidWithInverse, idB)).build();
.build();
scrutinize(update); scrutinize(update);
assertWarningsRaised(InverseConstraintScrutinizer.type); assertWarningsRaised(InverseConstraintScrutinizer.type);
} }
@Test @Test
public void testNoSymmetricClosure() { public void testNoSymmetricClosure() {
ItemUpdate update = new ItemUpdateBuilder(idA) ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(TestingData.generateStatement(idA, inversePid, idB))
.addStatement(TestingData.generateStatement(idA, inversePid, idB))
.build(); .build();
scrutinize(update); scrutinize(update);
assertNoWarningRaised(); assertNoWarningRaised();

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.Collections; import java.util.Collections;
@ -11,7 +34,6 @@ import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank; import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
public class NewItemScrutinizerTest extends ScrutinizerTest { public class NewItemScrutinizerTest extends ScrutinizerTest {
private Claim claim = Datamodel.makeClaim(TestingData.newIdA, private Claim claim = Datamodel.makeClaim(TestingData.newIdA,
@ -28,11 +50,8 @@ public class NewItemScrutinizerTest extends ScrutinizerTest {
public void testTrigger() { public void testTrigger() {
ItemUpdate update = new ItemUpdateBuilder(TestingData.newIdA).build(); ItemUpdate update = new ItemUpdateBuilder(TestingData.newIdA).build();
scrutinize(update); scrutinize(update);
assertWarningsRaised( assertWarningsRaised(NewItemScrutinizer.noDescType, NewItemScrutinizer.noLabelType,
NewItemScrutinizer.noDescType, NewItemScrutinizer.noTypeType, NewItemScrutinizer.newItemType);
NewItemScrutinizer.noLabelType,
NewItemScrutinizer.noTypeType,
NewItemScrutinizer.newItemType);
} }
@Test @Test
@ -47,8 +66,7 @@ public class NewItemScrutinizerTest extends ScrutinizerTest {
ItemUpdate update = new ItemUpdateBuilder(TestingData.newIdA) ItemUpdate update = new ItemUpdateBuilder(TestingData.newIdA)
.addLabel(Datamodel.makeMonolingualTextValue("bonjour", "fr")) .addLabel(Datamodel.makeMonolingualTextValue("bonjour", "fr"))
.addDescription(Datamodel.makeMonolingualTextValue("interesting item", "en")) .addDescription(Datamodel.makeMonolingualTextValue("interesting item", "en")).addStatement(p31Statement)
.addStatement(p31Statement)
.build(); .build();
scrutinize(update); scrutinize(update);
assertWarningsRaised(NewItemScrutinizer.newItemType); assertWarningsRaised(NewItemScrutinizer.newItemType);
@ -58,11 +76,8 @@ public class NewItemScrutinizerTest extends ScrutinizerTest {
public void testDeletedStatements() { public void testDeletedStatements() {
ItemUpdate update = new ItemUpdateBuilder(TestingData.newIdA) ItemUpdate update = new ItemUpdateBuilder(TestingData.newIdA)
.addLabel(Datamodel.makeMonolingualTextValue("bonjour", "fr")) .addLabel(Datamodel.makeMonolingualTextValue("bonjour", "fr"))
.addDescription(Datamodel.makeMonolingualTextValue("interesting item", "en")) .addDescription(Datamodel.makeMonolingualTextValue("interesting item", "en")).addStatement(p31Statement)
.addStatement(p31Statement) .deleteStatement(TestingData.generateStatement(TestingData.newIdA, TestingData.matchedId)).build();
.deleteStatement(TestingData.generateStatement(TestingData.newIdA,
TestingData.matchedId))
.build();
scrutinize(update); scrutinize(update);
assertWarningsRaised(NewItemScrutinizer.newItemType, NewItemScrutinizer.deletedStatementsType); assertWarningsRaised(NewItemScrutinizer.newItemType, NewItemScrutinizer.deletedStatementsType);
} }

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.testing.TestingData;

View File

@ -1,3 +1,26 @@
/*******************************************************************************
* MIT License
*
* Copyright (c) 2018 Antonin Delpeuch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import java.util.Arrays; import java.util.Arrays;
@ -16,6 +39,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank; import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
public class QualifierCompatibilityScrutinizerTest extends StatementScrutinizerTest { public class QualifierCompatibilityScrutinizerTest extends StatementScrutinizerTest {
private Snak disallowedQualifier = Datamodel.makeNoValueSnak(MockConstraintFetcher.qualifierPid); private Snak disallowedQualifier = Datamodel.makeNoValueSnak(MockConstraintFetcher.qualifierPid);
private Snak mandatoryQualifier = Datamodel.makeNoValueSnak(MockConstraintFetcher.mandatoryQualifierPid); private Snak mandatoryQualifier = Datamodel.makeNoValueSnak(MockConstraintFetcher.mandatoryQualifierPid);
private Snak allowedQualifier = Datamodel.makeNoValueSnak(MockConstraintFetcher.allowedQualifierPid); private Snak allowedQualifier = Datamodel.makeNoValueSnak(MockConstraintFetcher.allowedQualifierPid);
@ -49,10 +73,10 @@ public class QualifierCompatibilityScrutinizerTest extends StatementScrutinizerT
Datamodel.makeNoValueSnak(MockConstraintFetcher.mainSnakPid), makeQualifiers(qualifiers)); Datamodel.makeNoValueSnak(MockConstraintFetcher.mainSnakPid), makeQualifiers(qualifiers));
return Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, ""); return Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
} }
private List<SnakGroup> makeQualifiers(Snak[] qualifiers) { private List<SnakGroup> makeQualifiers(Snak[] qualifiers) {
List<Snak> snaks = Arrays.asList(qualifiers); List<Snak> snaks = Arrays.asList(qualifiers);
return snaks.stream() return snaks.stream().map((Snak q) -> Datamodel.makeSnakGroup(Collections.<Snak> singletonList(q)))
.map((Snak q) -> Datamodel.makeSnakGroup(Collections.<Snak>singletonList(q)))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

Some files were not shown because too many files have changed in this diff Show More