Search in sources :

Example 1 with TimeValue

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

the class WbDateConstant method parse.

/**
 * Parses a timestamp into a Wikibase {@link TimeValue}. The precision is
 * automatically inferred from the format.
 *
 * @param datestamp
 *            the time to parse
 * @return
 * @throws ParseException
 *             if the time cannot be parsed
 */
public static TimeValue parse(String datestamp) throws ParseException {
    Date bestDate = null;
    // default precision (will be overridden if successfully parsed)
    int precision = 0;
    // the maximum length parsed
    int maxLength = 0;
    // judge whether this is a BCE year
    boolean bceFlag = false;
    // Gregorian calendar is assumed by default
    String calendarIri = TimeValue.CM_GREGORIAN_PRO;
    String trimmedDatestamp = datestamp.trim();
    if ("TODAY".equals(trimmedDatestamp)) {
        Calendar calendar = Calendar.getInstance();
        TimeValue todaysDate = Datamodel.makeTimeValue(calendar.get(Calendar.YEAR), (byte) (calendar.get(Calendar.MONTH) + 1), (byte) calendar.get(Calendar.DAY_OF_MONTH), (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
        return todaysDate;
    }
    if (trimmedDatestamp.startsWith("-")) {
        trimmedDatestamp = trimmedDatestamp.substring(1);
        bceFlag = true;
    }
    for (Entry<SimpleDateFormat, Integer> entry : acceptedFormats.entrySet()) {
        ParsePosition position = new ParsePosition(0);
        Date date = entry.getKey().parse(trimmedDatestamp, position);
        if (date == null) {
            continue;
        }
        // Potentially parse the calendar Qid after the date
        int consumedUntil = position.getIndex();
        if (consumedUntil < trimmedDatestamp.length()) {
            Matcher matcher = calendarSuffixPattern.matcher(trimmedDatestamp.subSequence(position.getIndex(), trimmedDatestamp.length()));
            if (matcher.find()) {
                String calendarQid = matcher.group(1);
                calendarIri = Datamodel.SITE_WIKIDATA + calendarQid;
                consumedUntil = trimmedDatestamp.length();
            }
        }
        // Ignore parses which failed or do not consume all the input
        if (date != null && position.getIndex() > maxLength && // only allow to partially consume the input if the precision is day and followed by a T (as in ISO)
        (consumedUntil == trimmedDatestamp.length() || (entry.getValue() == 11 && trimmedDatestamp.charAt(consumedUntil) == 'T'))) {
            precision = entry.getValue();
            bestDate = date;
            maxLength = position.getIndex();
        }
    }
    if (bestDate == null || precision == 0) {
        throw new ParseException("Invalid date.", 0);
    } else {
        Calendar calendar = Calendar.getInstance();
        calendar = Calendar.getInstance();
        calendar.setTime(bestDate);
        long year = calendar.get(Calendar.YEAR);
        int month = precision < 10 ? 0 : calendar.get(Calendar.MONTH) + 1;
        int day_of_month = precision < 11 ? 0 : calendar.get(Calendar.DAY_OF_MONTH);
        if (bceFlag)
            year = -1 * year;
        return Datamodel.makeTimeValue(year, (byte) month, (byte) day_of_month, (byte) calendar.get(Calendar.HOUR_OF_DAY), (byte) calendar.get(Calendar.MINUTE), (byte) calendar.get(Calendar.SECOND), (byte) precision, 0, 0, 0, calendarIri);
    }
}
Also used : Matcher(java.util.regex.Matcher) Calendar(java.util.Calendar) Date(java.util.Date) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) ParsePosition(java.text.ParsePosition)

Example 2 with TimeValue

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

the class DifferenceWithinScrutinizerTest method testTrigger.

@Test
public void testTrigger() {
    ItemIdValue idA = TestingData.existingId;
    TimeValue lowerYear = new TimeValueImpl(1800, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
    TimeValue upperYear = new TimeValueImpl(2020, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
    ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
    ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
    Statement statement1 = new StatementImpl("P569", value1, idA);
    Statement statement2 = new StatementImpl("P570", value2, idA);
    TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement1)).addStatement(add(statement2)).build();
    Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
    Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
    Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
    List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
    List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(upperBoundPid, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(updateA);
    assertWarningsRaised(DifferenceWithinRangeScrutinizer.type);
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) TimeValueImpl(org.wikidata.wdtk.datamodel.implementation.TimeValueImpl) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) Test(org.testng.annotations.Test)

Example 3 with TimeValue

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

the class DifferenceWithinScrutinizerTest method testNoIssue.

@Test
public void testNoIssue() {
    ItemIdValue idA = TestingData.existingId;
    TimeValue lowerYear = new TimeValueImpl(2000, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
    TimeValue upperYear = new TimeValueImpl(2020, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
    ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
    ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
    Statement statement1 = new StatementImpl("P569", value1, idA);
    Statement statement2 = new StatementImpl("P570", value2, idA);
    TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement1)).addStatement(add(statement2)).build();
    Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
    Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
    Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
    List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
    List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(upperBoundPid, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(updateA);
    assertNoWarningRaised();
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) TimeValueImpl(org.wikidata.wdtk.datamodel.implementation.TimeValueImpl) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) Test(org.testng.annotations.Test)

Example 4 with TimeValue

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

the class WbDateConstantTest method testToday.

@Test
public void testToday() {
    Calendar calendar = Calendar.getInstance();
    TimeValue expectedDate = Datamodel.makeTimeValue(calendar.get(Calendar.YEAR), (byte) (calendar.get(Calendar.MONTH) + 1), (byte) calendar.get(Calendar.DAY_OF_MONTH), (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
    evaluatesTo(expectedDate, new WbDateConstant("TODAY"));
}
Also used : Calendar(java.util.Calendar) TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) JacksonSerializationTest(org.openrefine.wikidata.testing.JacksonSerializationTest) Test(org.testng.annotations.Test)

Example 5 with TimeValue

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

the class LaxValueMatcherTests method testTimeValue.

@Test
public void testTimeValue() {
    String calModel = "https://foo.com/calendars/Q123";
    TimeValue day1 = Datamodel.makeTimeValue(1987, (byte) 3, (byte) 8, (byte) 0, (byte) 0, (byte) 0, TimeValue.PREC_DAY, 0, 0, 0, calModel);
    TimeValue day2 = Datamodel.makeTimeValue(1987, (byte) 11, (byte) 10, (byte) 0, (byte) 0, (byte) 0, TimeValue.PREC_DAY, 0, 0, 0, calModel);
    TimeValue day3 = Datamodel.makeTimeValue(1987, (byte) 3, (byte) 8, (byte) 10, (byte) 11, (byte) 12, TimeValue.PREC_DAY, 0, 0, 0, calModel);
    TimeValue year1 = Datamodel.makeTimeValue(1987, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, TimeValue.PREC_YEAR, 0, 0, 0, calModel);
    TimeValue year2 = Datamodel.makeTimeValue(1987, (byte) 5, (byte) 6, (byte) 7, (byte) 0, (byte) 0, TimeValue.PREC_YEAR, 0, 0, 0, calModel);
    assertTrue(SUT.match(day1, day1));
    assertFalse(SUT.match(day1, day2));
    assertFalse(SUT.match(day2, day1));
    assertTrue(SUT.match(day1, day3));
    assertTrue(SUT.match(day3, day1));
    assertTrue(SUT.match(year1, year2));
    assertTrue(SUT.match(year2, year1));
}
Also used : TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) Test(org.testng.annotations.Test)

Aggregations

TimeValue (org.wikidata.wdtk.datamodel.interfaces.TimeValue)7 Test (org.testng.annotations.Test)4 Calendar (java.util.Calendar)2 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)2 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)2 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)2 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)2 TimeValueImpl (org.wikidata.wdtk.datamodel.implementation.TimeValueImpl)2 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)2 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)2 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)2 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)2 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)2 BigDecimal (java.math.BigDecimal)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 ParsePosition (java.text.ParsePosition)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1