diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java index d587c249d..c4639a73c 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java @@ -29,8 +29,8 @@ import org.wikidata.wdtk.datamodel.interfaces.QuantityValue; import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Value; +import java.util.List; import java.util.Set; -import java.util.stream.Stream; /** * An object that fetches constraints about properties. @@ -150,9 +150,9 @@ public interface ConstraintFetcher { * the property to retrieve the constraints for * @param qid * the type of the constraints - * @return the stream of matching constraint statements + * @return the list of matching constraint statements */ - Stream getConstraintsByType(PropertyIdValue pid, String qid); + List getConstraintsByType(PropertyIdValue pid, String qid); /** * Retrieves the lower bound of the range diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/WikidataConstraintFetcher.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/WikidataConstraintFetcher.java index b9dc401b3..4c19f3442 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/WikidataConstraintFetcher.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/WikidataConstraintFetcher.java @@ -281,9 +281,12 @@ public class WikidataConstraintFetcher implements ConstraintFetcher { * exist */ protected List getSingleConstraint(PropertyIdValue pid, String qid) { - Statement statement = getConstraintsByType(pid, qid).findFirst().orElse(null); - if (statement != null) { - return statement.getClaim().getQualifiers(); + List statementList = getConstraintsByType(pid, qid); + if (!statementList.isEmpty()) { + Statement statement = statementList.get(0); + if (statement != null) { + return statement.getClaim().getQualifiers(); + } } return null; } @@ -295,14 +298,14 @@ public class WikidataConstraintFetcher implements ConstraintFetcher { * the property to retrieve the constraints for * @param qid * the type of the constraints - * @return the stream of matching constraint statements + * @return the list of matching constraint statements */ @Override - public Stream getConstraintsByType(PropertyIdValue pid, String qid) { + public List getConstraintsByType(PropertyIdValue pid, String qid) { Stream allConstraints = getConstraintStatements(pid).stream() .filter(s -> s.getValue() != null && ((EntityIdValue) s.getValue()).getId().equals(qid)) .filter(s -> !StatementRank.DEPRECATED.equals(s.getRank())); - return allConstraints; + return allConstraints.collect(Collectors.toList()); } /** diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizer.java index 0faf5bc5c..07e79359b 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizer.java @@ -14,7 +14,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; public class ConflictsWithScrutinizer extends EditScrutinizer { @@ -65,7 +64,7 @@ public class ConflictsWithScrutinizer extends EditScrutinizer { } for(PropertyIdValue propertyId : propertyIdValueValueMap.keySet()){ - List statementList = _fetcher.getConstraintsByType(propertyId, CONFLICTS_WITH_CONSTRAINT_QID).collect(Collectors.toList()); + List statementList = _fetcher.getConstraintsByType(propertyId, CONFLICTS_WITH_CONSTRAINT_QID); for (Statement statement : statementList) { ConflictsWithConstraint constraint = new ConflictsWithConstraint(statement); PropertyIdValue conflictingPid = constraint.conflictingPid; diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizer.java index 138f76de4..74296e773 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizer.java @@ -40,14 +40,13 @@ public class MultiValueScrutinizer extends EditScrutinizer { for (Statement statement : update.getAddedStatements()) { PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); + List statementList = _fetcher.getConstraintsByType(pid, MULTI_VALUE_CONSTRAINT_QID); if (propertyCount.containsKey(pid)) { propertyCount.put(pid, propertyCount.get(pid) + 1); - } else { - Statement constraintStatement = _fetcher.getConstraintsByType(pid, MULTI_VALUE_CONSTRAINT_QID).findFirst().orElse(null); - if (constraintStatement != null) { - MultivalueConstraint constraint = new MultivalueConstraint(constraintStatement); - propertyCount.put(pid, 1); - } + } else if (!statementList.isEmpty()) { + Statement constraintStatement = statementList.get(0); + MultivalueConstraint constraint = new MultivalueConstraint(constraintStatement); + propertyCount.put(pid, 1); } } diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java index bfd65fef8..aa1b77f2f 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java @@ -42,7 +42,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; public class MockConstraintFetcher implements ConstraintFetcher { @@ -220,7 +219,7 @@ public class MockConstraintFetcher implements ConstraintFetcher { } @Override - public Stream getConstraintsByType(PropertyIdValue pid, String qid) { + public List getConstraintsByType(PropertyIdValue pid, String qid) { EntityIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q21502838"); PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302"); Snak snak = Datamodel.makeValueSnak(propertyIdValue,entityIdValue); @@ -247,8 +246,6 @@ public class MockConstraintFetcher implements ConstraintFetcher { Statement statement = Datamodel.makeStatement(claim, referenceList, StatementRank.NORMAL, "P2302$77BD7FE4-C051-4776-855C-543F0CE697D0"); List statements = Collections.singletonList(statement); - return statements.stream() - .filter(s -> s.getValue() != null && ((EntityIdValue) s.getValue()).getId().equals(qid)) - .filter(s -> !StatementRank.DEPRECATED.equals(s.getRank())); + return statements; } } diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java index 79d1947fe..666f134b8 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.stream.Stream; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -67,10 +66,10 @@ public class ConflictsWithScrutinizerTest extends ScrutinizerTest { SnakGroup snakGroup1 = Datamodel.makeSnakGroup(snakList1); SnakGroup snakGroup2 = Datamodel.makeSnakGroup(snakList2); List snakGroupList = Arrays.asList(snakGroup1, snakGroup2); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementList); setFetcher(fetcher); scrutinize(updateA); @@ -94,10 +93,10 @@ public class ConflictsWithScrutinizerTest extends ScrutinizerTest { SnakGroup snakGroup1 = Datamodel.makeSnakGroup(snakList1); SnakGroup snakGroup2 = Datamodel.makeSnakGroup(snakList2); List snakGroupList = Arrays.asList(snakGroup1, snakGroup2); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementList); setFetcher(fetcher); scrutinize(update); @@ -123,10 +122,10 @@ public class ConflictsWithScrutinizerTest extends ScrutinizerTest { SnakGroup snakGroup1 = Datamodel.makeSnakGroup(snakList1); SnakGroup snakGroup2 = Datamodel.makeSnakGroup(snakList2); List snakGroupList = Arrays.asList(snakGroup1, snakGroup2); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementList); setFetcher(fetcher); scrutinize(updateA); @@ -143,11 +142,10 @@ public class ConflictsWithScrutinizerTest extends ScrutinizerTest { ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(statement).build(); - List statements = new ArrayList<>(); - Stream statementStream = statements.stream(); + List statementList = new ArrayList<>(); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(propertyWithConflictsPid1, "Q21502838")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(propertyWithConflictsPid1, "Q21502838")).thenReturn(statementList); setFetcher(fetcher); scrutinize(updateA); @@ -177,10 +175,10 @@ public class ConflictsWithScrutinizerTest extends ScrutinizerTest { SnakGroup snakGroup1 = Datamodel.makeSnakGroup(snakList1); SnakGroup snakGroup2 = Datamodel.makeSnakGroup(snakList2); List snakGroupList = Arrays.asList(snakGroup1, snakGroup2); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(conflictsWithPid, "Q21502838")).thenReturn(statementList); setFetcher(fetcher); scrutinize(updateA); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizerTest.java index 7fafbefa1..73dd7f512 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/MultiValueScrutinizerTest.java @@ -16,7 +16,6 @@ import org.wikidata.wdtk.datamodel.interfaces.Value; import java.util.Collections; import java.util.List; -import java.util.stream.Stream; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -50,9 +49,9 @@ public class MultiValueScrutinizerTest extends ScrutinizerTest { List snakList1 = Collections.singletonList(snak); SnakGroup snakGroup = Datamodel.makeSnakGroup(snakList1); List snakGroupList = Collections.singletonList(snakGroup); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(propertyIdValue, "Q21510857")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(propertyIdValue, "Q21510857")).thenReturn(statementList); setFetcher(fetcher); scrutinize(update); @@ -72,9 +71,9 @@ public class MultiValueScrutinizerTest extends ScrutinizerTest { List snakList1 = Collections.singletonList(snak); SnakGroup snakGroup = Datamodel.makeSnakGroup(snakList1); List snakGroupList = Collections.singletonList(snakGroup); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(propertyIdValue, "Q21510857")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(propertyIdValue, "Q21510857")).thenReturn(statementList); setFetcher(fetcher); scrutinize(updateA, updateB); @@ -94,9 +93,9 @@ public class MultiValueScrutinizerTest extends ScrutinizerTest { List snakList1 = Collections.singletonList(snak); SnakGroup snakGroup = Datamodel.makeSnakGroup(snakList1); List snakGroupList = Collections.singletonList(snakGroup); - Stream statementStream = constraintParameterStatementStream(entityIdValue, snakGroupList); + List statementList = constraintParameterStatementList(entityIdValue, snakGroupList); ConstraintFetcher fetcher = mock(ConstraintFetcher.class); - when(fetcher.getConstraintsByType(propertyIdValue, "Q21510857")).thenReturn(statementStream); + when(fetcher.getConstraintsByType(propertyIdValue, "Q21510857")).thenReturn(statementList); setFetcher(fetcher); scrutinize(updateA, updateB); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java index a3966f8f1..7824f1446 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java @@ -44,7 +44,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @@ -96,7 +95,7 @@ public abstract class ScrutinizerTest { scrutinizer.setFetcher(fetcher); } - public Stream constraintParameterStatementStream(ItemIdValue itemIdValue, List listSnakGroup) { + public List constraintParameterStatementList(ItemIdValue itemIdValue, List listSnakGroup) { PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302"); Snak snakValue = Datamodel.makeValueSnak(propertyIdValue,itemIdValue); @@ -108,7 +107,7 @@ public abstract class ScrutinizerTest { Statement statement = Datamodel.makeStatement(claim, referenceList, StatementRank.NORMAL, "P2302$77BD7FE4-C051-4776-855C-543F0CE697D0"); List statements = Collections.singletonList(statement); - return statements.stream(); + return statements; } }