Search in sources :

Example 1 with ItemIdValue

use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.

the class QSValuePrinter method visit.

@Override
public String visit(QuantityValue value) {
    ItemIdValue unit = value.getUnitItemId();
    String unitRepresentation = "", boundsRepresentation = "";
    if (unit != null) {
        unitRepresentation = "U" + unit.getId().substring(1);
    }
    if (value.getLowerBound() != null) {
        // bounds are always null at the same time so we know they are both not null
        BigDecimal lowerBound = value.getLowerBound();
        BigDecimal upperBound = value.getUpperBound();
        boundsRepresentation = String.format(Locale.US, "[%s,%s]", lowerBound.toString(), upperBound.toString());
    }
    return String.format(Locale.US, "%s%s%s", value.getNumericValue().toString(), boundsRepresentation, unitRepresentation);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) BigDecimal(java.math.BigDecimal)

Example 2 with ItemIdValue

use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.

the class QuantityScrutinizer method scrutinize.

@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
    if (!added) {
        return;
    }
    if (snak instanceof ValueSnak && ((ValueSnak) snak).getValue() instanceof QuantityValue && added) {
        PropertyIdValue pid = snak.getPropertyId();
        QuantityValue value = (QuantityValue) ((ValueSnak) snak).getValue();
        if (!_fetcher.getConstraintsByType(pid, noBoundsConstraintQid).isEmpty() && (value.getUpperBound() != null || value.getLowerBound() != null)) {
            QAWarning issue = new QAWarning(boundsDisallowedType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
            issue.setProperty("property_entity", pid);
            issue.setProperty("example_value", value.getNumericValue().toString());
            issue.setProperty("example_item_entity", entityId);
            addIssue(issue);
        }
        if (!_fetcher.getConstraintsByType(pid, integerValuedConstraintQid).isEmpty() && value.getNumericValue().scale() > 0) {
            QAWarning issue = new QAWarning(integerConstraintType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
            issue.setProperty("property_entity", pid);
            issue.setProperty("example_value", value.getNumericValue().toString());
            issue.setProperty("example_item_entity", entityId);
            addIssue(issue);
        }
        List<Statement> statementList = _fetcher.getConstraintsByType(pid, allowedUnitsConstraintQid);
        Set<ItemIdValue> allowedUnits = null;
        if (!statementList.isEmpty()) {
            AllowedUnitsConstraint allowedUnitsConstraint = new AllowedUnitsConstraint(statementList.get(0));
            allowedUnits = allowedUnitsConstraint.allowedUnits;
        }
        ItemIdValue currentUnit = null;
        if (value.getUnitItemId() != null) {
            currentUnit = value.getUnitItemId();
        }
        if (allowedUnits != null && !allowedUnits.contains(currentUnit)) {
            String issueType = currentUnit == null ? noUnitProvidedType : invalidUnitType;
            QAWarning issue = new QAWarning(issueType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
            issue.setProperty("property_entity", pid);
            issue.setProperty("example_value", value.getNumericValue().toString());
            issue.setProperty("example_item_entity", entityId);
            if (currentUnit != null) {
                issue.setProperty("unit_entity", value.getUnitItemId());
            }
            addIssue(issue);
        }
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) QuantityValue(org.wikidata.wdtk.datamodel.interfaces.QuantityValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 3 with ItemIdValue

use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.

the class WbQuantityExpr method evaluate.

@Override
public QuantityValue evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException {
    StringValue amount = getAmountExpr().evaluate(ctxt);
    // we know the amount is nonnull, nonempty here
    BigDecimal parsedAmount = null;
    BigDecimal lowerBound = null;
    BigDecimal upperBound = null;
    String originalAmount = amount.getString().toUpperCase();
    try {
        parsedAmount = new BigDecimal(originalAmount);
        if (originalAmount.contains("E")) {
            // engineering notation: we derive the precision from
            // the expression (feature!)
            BigDecimal uncertainty = new BigDecimal("0.5").scaleByPowerOfTen(-parsedAmount.scale());
            lowerBound = new BigDecimal(parsedAmount.subtract(uncertainty).toPlainString());
            upperBound = new BigDecimal(parsedAmount.add(uncertainty).toPlainString());
        }
        // workaround for https://github.com/Wikidata/Wikidata-Toolkit/issues/341
        parsedAmount = new BigDecimal(parsedAmount.toPlainString());
    } catch (NumberFormatException e) {
        if (!originalAmount.isEmpty()) {
            QAWarning issue = new QAWarning("ignored-amount", null, QAWarning.Severity.WARNING, 1);
            issue.setProperty("example_value", originalAmount);
            ctxt.addWarning(issue);
        }
        throw new SkipSchemaExpressionException();
    }
    if (getUnitExpr() != null) {
        ItemIdValue unit = getUnitExpr().evaluate(ctxt);
        return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound, unit);
    }
    return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) SkipSchemaExpressionException(org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException) StringValue(org.wikidata.wdtk.datamodel.interfaces.StringValue) QAWarning(org.openrefine.wikidata.qa.QAWarning) BigDecimal(java.math.BigDecimal)

Example 4 with ItemIdValue

use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.

the class EditBatchProcessorTest method testMultipleBatches.

@Test
public void testMultipleBatches() throws MediaWikiApiErrorException, InterruptedException, IOException {
    // Prepare test data
    MonolingualTextValue description = Datamodel.makeMonolingualTextValue("village in Nepal", "en");
    List<String> ids = new ArrayList<>();
    for (int i = 124; i < 190; i++) {
        ids.add("Q" + String.valueOf(i));
    }
    List<ItemIdValue> qids = ids.stream().map(e -> Datamodel.makeWikidataItemIdValue(e)).collect(Collectors.toList());
    List<TermedStatementEntityEdit> batch = qids.stream().map(qid -> new TermedStatementEntityEditBuilder(qid).addDescription(description, true).build()).collect(Collectors.toList());
    int batchSize = 50;
    List<ItemDocument> fullBatch = qids.stream().map(qid -> ItemDocumentBuilder.forItemId(qid).withStatement(TestingData.generateStatement(qid, TestingData.existingId)).build()).collect(Collectors.toList());
    List<ItemDocument> firstBatch = fullBatch.subList(0, batchSize);
    List<ItemDocument> secondBatch = fullBatch.subList(batchSize, fullBatch.size());
    when(fetcher.getEntityDocuments(toQids(firstBatch))).thenReturn(toMap(firstBatch));
    when(fetcher.getEntityDocuments(toQids(secondBatch))).thenReturn(toMap(secondBatch));
    // Run edits
    EditBatchProcessor processor = new EditBatchProcessor(fetcher, editor, batch, library, summary, maxlag, tags, batchSize);
    assertEquals(0, processor.progress());
    for (int i = 124; i < 190; i++) {
        assertEquals(processor.remainingEdits(), 190 - i);
        processor.performEdit();
    }
    assertEquals(0, processor.remainingEdits());
    assertEquals(100, processor.progress());
    // Check result
    assertEquals(new NewEntityLibrary(), library);
    verify(fetcher, times(1)).getEntityDocuments(toQids(firstBatch));
    verify(fetcher, times(1)).getEntityDocuments(toQids(secondBatch));
    for (ItemDocument doc : fullBatch) {
        verify(editor, times(1)).editEntityDocument(Datamodel.makeItemUpdate(doc.getEntityId(), doc.getRevisionId(), Datamodel.makeTermUpdate(Collections.emptyList(), Collections.emptyList()), Datamodel.makeTermUpdate(Collections.singletonList(description), Collections.emptyList()), Collections.emptyMap(), Datamodel.makeStatementUpdate(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()), Collections.emptyList(), Collections.emptyList()), false, summary, tags);
    }
}
Also used : Arrays(java.util.Arrays) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) MediaInfoDocument(org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument) EntityDocument(org.wikidata.wdtk.datamodel.interfaces.EntityDocument) ArrayList(java.util.ArrayList) TermUpdate(org.wikidata.wdtk.datamodel.interfaces.TermUpdate) WikibaseDataFetcher(org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Map(java.util.Map) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) BeforeMethod(org.testng.annotations.BeforeMethod) ItemDocumentBuilder(org.wikidata.wdtk.datamodel.helpers.ItemDocumentBuilder) WikibaseDataEditor(org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) Mockito.verify(org.mockito.Mockito.verify) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) List(java.util.List) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Datamodel(org.wikidata.wdtk.datamodel.helpers.Datamodel) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) MediaWikiApiErrorException(org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException) Collections(java.util.Collections) TestingData(org.openrefine.wikidata.testing.TestingData) Mockito.mock(org.mockito.Mockito.mock) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) ArrayList(java.util.ArrayList) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Example 5 with ItemIdValue

use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.

the class ReconEntityRewriterTest method testRewriteCreate.

@Test
public void testRewriteCreate() throws NewEntityNotCreatedYetException {
    ItemIdValue subject = TestingData.newIdA;
    rewriter = new ReconEntityRewriter(library, subject);
    library.setId(4567L, "Q1234");
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, TestingData.newIdB)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    TermedStatementEntityEdit rewritten = rewriter.rewrite(update);
    TermedStatementEntityEdit expected = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, newlyCreated)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    assertEquals(rewritten, expected);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Test(org.testng.annotations.Test)

Aggregations

ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)68 Test (org.testng.annotations.Test)61 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)57 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)56 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)54 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)52 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)48 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)44 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)32 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)20 ArrayList (java.util.ArrayList)6 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)5 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)4 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)4 QAWarning (org.openrefine.wikidata.qa.QAWarning)3 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)3 ItemDocument (org.wikidata.wdtk.datamodel.interfaces.ItemDocument)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Collections (java.util.Collections)2