All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wikidata.query.rdf.tool.rdf.ClassifiedStatements Maven / Gradle / Ivy

Go to download

Tools to sync Wikibase to RDF stores. Also contains overall integration tests that rely on everything else.

The newest version!
package org.wikidata.query.rdf.tool.rdf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.concurrent.NotThreadSafe;

import org.openrdf.model.Statement;
import org.wikidata.query.rdf.common.uri.UrisScheme;

/**
 * Sort statements into a set of specialized collections, by subject.
 *
 * Multiple calls to {@code classify()} will accumulate statements in this class.
 */
@NotThreadSafe
public class ClassifiedStatements {

    /** Subject is entity. */
    public final List statementStatements = new ArrayList<>();
    /** Subject is any statement. */
    public final List entityStatements = new ArrayList<>();
    /** Not entity, not statement, not value and not reference. */
    public final Set aboutStatements = new HashSet<>();
    private final UrisScheme uris;

    private long dataSize;

    public ClassifiedStatements(UrisScheme uris) {
        this.uris = uris;
    }

    public void clear() {
        entityStatements.clear();
        aboutStatements.clear();
        statementStatements.clear();
        dataSize = 0;
    }

    /**
     * Sort statements into a set of specialized collections, by subject.
     *
     * @param statements List of statements to process
     * @param entityId Entity identifier (e.g. Q-id)
     */
    public void classify(Collection statements, String entityId) {
        for (Statement statement: statements) {
            String subject = statement.getSubject().stringValue();
            if (subject.equals(uris.entityIdToURI(entityId))) {
                entityStatements.add(statement);
            }
            if (subject.startsWith(uris.statement())) {
                statementStatements.add(statement);
            }
            if (!subject.equals(uris.entityIdToURI(entityId))
                    && !subject.startsWith(uris.statement())
                    && !subject.startsWith(uris.value())
                    && !subject.startsWith(uris.reference())
                    && !subject.startsWith(uris.entityIdToURI(entityId) + "-")
            ) {
                aboutStatements.add(statement);
            }
            dataSize += subject.length() + statement.getPredicate().stringValue().length() + statement.getObject().stringValue().length();
        }
    }

    public long getDataSize() {
        return dataSize;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy