📄 rdfsourcepool.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.sail.test.benchmark.pools;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import org.openrdf.model.BNode;import org.openrdf.model.Resource;import org.openrdf.model.Statement;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.model.impl.URIImpl;import org.openrdf.sesame.sail.RdfSource;import org.openrdf.sesame.sail.StatementIterator;import org.openrdf.sesame.sail.test.benchmark.model.BMStatement;import org.openrdf.sesame.sail.test.benchmark.model.Reference;import org.openrdf.sesame.sail.test.benchmark.util.BMUtil;/** * Pool used by benchmark test for testing of implementations of * org.openrdf.sesame.sail.RdfSource. * * @author Peter van 't Hof * @version %I%, %G% */public class RdfSourcePool extends SailPool {/*----------+| Variables |+----------*/ // RdfSource which to extract pool from. protected RdfSource _rdfSource; /** * !!! Anonymous resources are excluded from every variable, because * anonymous resources have different identifiers in different repositories * containing the same dataset. !!! * * Subject, predictate and object which do not exist. */ public static Resource noSubject; public static URI noPredicate; public static Value noObject; /** * List of statements, including implicit. */ public static List statements; /** * List of statements which do not exist, including implicit. */ public static List noStatements;/*-------------+| Constructors |+-------------*/ /** * Creates a new pool which contains variables which are used by benchmark * test for testing of org.openrdf.sesame.sail.RdfSource. * * @param rdfSource RdfSource which to extract variables from. */ public RdfSourcePool(RdfSource rdfSource) { super(rdfSource); _rdfSource = rdfSource; }/*--------+| Methods |+--------*/ protected void _load(ObjectInputStream inputStream) throws IOException, ClassNotFoundException { super._load(inputStream); noSubject = (Resource)inputStream.readObject(); noPredicate = (URI)inputStream.readObject(); noObject = (Value)inputStream.readObject(); statements = (List)inputStream.readObject(); noStatements = (List)inputStream.readObject(); } protected void _save(ObjectOutputStream outputStream) throws IOException { super._save(outputStream); outputStream.writeObject(noSubject); outputStream.writeObject(noPredicate); outputStream.writeObject(noObject); outputStream.writeObject(statements); outputStream.writeObject(noStatements); } protected void _initialize() { super._initialize(); _status("Creating subject which does not exist..."); noSubject = new URIImpl("http://www.foo.com#", "I don 't exist"); _status("Creating predicate which does not exist..."); noPredicate = new URIImpl("http://www.foo.com#", "Neither do I"); _status("Creating object which does not exist..."); noObject = new URIImpl("http://www.foo.com#", "Nor me"); _status("Searching for statements..."); statements = _getStatements(16, false); _status("Searching for statements which do not exist..."); noStatements = _getNoStatements(8); } /* Creates a List of statements which do not exist with size is * preferredSize. */ protected List _getNoStatements(int preferredSize) { /* 2-dimensional array containing every possible pattern of wildcards and * boolean indicating if object is an instance of rdfs:Resource or * rdfs:Literal. If object is a wildcard, object type is not relevant. * Pattern [X, null, null, false] is not included, because this pattern * is always true for every value of X and therefore always exists. */ boolean[][] patterns = { {false, false, false, true}, {false, false, false, false}, {true, false, false, true}, {true, false, false, false}, {false, true, false, true}, {false, true, false, false}, {false, false, true, false}, {true, true, false, true}, /* In the current interpretation of RDF(S) every * literal exists as a subject. Therefore, next * pattern is not useful: {true, true, false, * false}. */ {true, false, true, false}}; return _getNoStatements(preferredSize, patterns, false); } /* Creates a List of statements of a particular pattern which do not exist * with size is preferredTotalSize. */ protected List _getNoStatements(int preferredTotalSize, boolean[][] patterns, boolean explicitOnly) { // Number of patterns. int length = patterns.length; // Preferred number of statements which do not exist to retrieve. int preferredSize = preferredTotalSize/length +1; /* Array of Lists, each List contains statements of a particalur pattern * which do not exist. */ List[] lists = new ArrayList[length]; for (int i = 0; i < length; i++) { List list = _getNoStatements(patterns[i][0], patterns[i][1], patterns[i][2], explicitOnly, preferredSize, patterns[i][3]); lists[i] = list; } /* The size of each List must be equal. Therefore minimum number is * determined and the size of each List exceeding minimum is cut down to * minimum. * * Determine minimum size. */ int minimumSize = preferredSize; for (int i = 0; i < length; i++) { int size = lists[i].size(); if (size > 0 && size < minimumSize) { minimumSize = size; } } // Final List consisting of all statements which do not exist. List all = new ArrayList(); if (minimumSize < preferredSize) { for (int i = 0; i < length; i++) { /* Cut List consisting of statements of a particular pattern * which do not exist down to minimum and add remaining elements * to final List. */ all.addAll(lists[i].subList(0, minimumSize)); } } else { for (int i = 0; i < length; i++) { /* Add statements of a particular pattern which do not exist of * List to final List. */ all.addAll(lists[i]); } } return _stripIds(all); } /* Creates a List of statements which do not exist with size is * preferredSize. * * A statements which do not exist is a combination of a subject, predicate, * object and wildcard(s) which do not combine as a statement. Boolean * parameters subjectWild, predicateWild and objectwild indicate if statement * contains wildcards, type is true indicates object in statement is an * instance of rdfs:Resource, type is false rdfs:Literal. */ protected List _getNoStatements (boolean subjectWild, boolean predicateWild, boolean objectWild, boolean explicitOnly, int preferredSize, boolean type) { Set noStatements = new HashSet(); Set statements = new HashSet(); int currentSize = 0; Reference referenceSubject = new Reference(); Reference referencePredicate = new Reference(); Reference referenceObject = new Reference(); /* 2-dimensional array containing every possible combination of subject, * predicate and object, except for original combination, which is always * a statement. Table holds References to a subject, predicate or object. * References are resetted in while loop, if no wildcard. */ Reference[][] table = null; Reference[] row0 = {referencePredicate, referenceSubject, referenceObject}; /* Object can only be subject or predicate if it is an instance of * rdfs:Resource. Therefore if object is an instance of rdfs:Resource, * more combinations are possible and table will contain more rows. */ if (type) { table = new Reference[5][3]; Reference[] row1 = {referenceSubject, referenceObject, referencePredicate}; Reference[] row2 = {referencePredicate, referenceObject, referenceSubject}; Reference[] row3 = {referenceObject, referencePredicate, referenceSubject}; Reference[] row4 = {referenceObject, referenceSubject, referencePredicate}; table[0] = row0; table[1] = row1; table[2] = row2; table[3] = row3; table[4] = row4; } else { table = new Reference[1][3]; table[0] = row0; } int length = table.length; /* If subject is a wildcard replace column 0 by empty References, * representing a wildcard. */ if (subjectWild) { for (int j = 0; j < length; j++) { table[j][0] = new Reference(); } } // If predicate is a wildcard replace column 1 by empty References. if (predicateWild) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -