コード例 #1
0
  /**
   * sets the resource URI as label right now
   *
   * @param resource
   * @return
   */
  private List<MonolingualTextValue> generateLabels(final Resource resource) {

    final String resourceURI = resource.getUri();

    return generateLabels(resourceURI);
  }
コード例 #2
0
  private void processGDMResource(final Resource resource)
      throws JsonProcessingException, WikidataImporterException {

    resourceCount.incrementAndGet();

    final String resourceURI = resource.getUri();

    final List<MonolingualTextValue> labels = generateLabels(resource);
    final List<MonolingualTextValue> descriptions = generateLabels(resourceURI);
    final List<MonolingualTextValue> aliases = new ArrayList<>();

    final Map<String, List<org.wikidata.wdtk.datamodel.interfaces.Statement>>
        wikidataStatementsMap = new HashMap<>();

    final Set<Statement> gdmStatements = resource.getStatements();

    if (gdmStatements != null) {

      // write statements (if available)

      for (final Statement gdmStatement : gdmStatements) {

        statementCount.incrementAndGet();

        final String predicateURI = gdmStatement.getPredicate().getUri();

        if (!wikidataStatementsMap.containsKey(predicateURI)) {

          final List<org.wikidata.wdtk.datamodel.interfaces.Statement> wikidataStatements =
              new ArrayList<>();

          wikidataStatementsMap.put(predicateURI, wikidataStatements);
        }

        final Optional<org.wikidata.wdtk.datamodel.interfaces.Statement> optionalWikidataStmt =
            processGDMStatement(gdmStatement);

        if (!optionalWikidataStmt.isPresent()) {

          // log non-created statements
          LOG.debug("could not process statement '{}'", printGDMStatement(gdmStatement));

          continue;
        }

        final org.wikidata.wdtk.datamodel.interfaces.Statement wikidataStmt =
            optionalWikidataStmt.get();

        wikidataStatementsMap.get(predicateURI).add(wikidataStmt);

        processedStatementCount.incrementAndGet();

        final boolean updated =
            checkAndOptionallyUpdateBigCounter(statementCount, bigStatementCount);

        if (updated) {

          final long currentStatementCount = statementCount.get();

          LOG.info(
              "processed '{}' from '{}' statements",
              processedStatementCount.get(),
              currentStatementCount);
        }
      }
    }

    final List<StatementGroup> statementGroups = new ArrayList<>();

    // create statement groups property-wise
    for (final Map.Entry<String, List<org.wikidata.wdtk.datamodel.interfaces.Statement>>
        statmentsPerPropertyEntry : wikidataStatementsMap.entrySet()) {

      final List<org.wikidata.wdtk.datamodel.interfaces.Statement> statementsPerProperty =
          statmentsPerPropertyEntry.getValue();

      final StatementGroup statementGroup = Datamodel.makeStatementGroup(statementsPerProperty);

      statementGroups.add(statementGroup);
    }

    final Map<String, SiteLink> siteLinkMap = new HashMap<>();

    // we can also create an item with all it's statements at once, i.e., this would save some HTTP
    // API calls
    // TODO: check ItemIdValue in map (i.e. whether an wikidata for this gdm resource exists
    // already, or not; because if it exists already, then we need to update the existing one, i.e.,
    // do a slightly different API call)
    final ItemDocument wikidataItem =
        Datamodel.makeItemDocument(
            null, labels, descriptions, aliases, statementGroups, siteLinkMap);

    // create item at wikibase (check whether statements are created as well - otherwise we need to
    // create them separately)
    final ItemIdValue itemIdValue = createWikidataItem(resourceURI, wikidataItem);

    // add/update item id value at the resources items map
    gdmResourceURIWikidataItemMap.putIfAbsent(resourceURI, itemIdValue);

    final boolean updated = checkAndOptionallyUpdateBigCounter(resourceCount, bigResourceCount);

    if (updated) {

      final long currentResourceCount = resourceCount.get();

      LOG.info(
          "processed '{}' resources ('{}' from '{}' statements)",
          currentResourceCount,
          processedStatementCount.get(),
          statementCount.get());
    }
  }