📄 rdfcoretests.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;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.Date;import java.util.HashMap;import java.util.Map;import junit.framework.TestCase;import org.openrdf.vocabulary.RDF;import org.openrdf.vocabulary.RDFS;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.BNodeImpl;import org.openrdf.model.impl.LiteralImpl;import org.openrdf.model.impl.URIImpl;import org.openrdf.rio.ParseException;import org.openrdf.rio.Parser;import org.openrdf.rio.RdfDocumentWriter;import org.openrdf.rio.StatementHandler;import org.openrdf.rio.StatementHandlerException;import org.openrdf.rio.ntriples.NTriplesParser;import org.openrdf.rio.rdfxml.RdfXmlParser;import org.openrdf.rio.rdfxml.RdfXmlWriter;import org.openrdf.sesame.admin.AdminMsgCollector;import org.openrdf.sesame.config.AccessDeniedException;import org.openrdf.sesame.config.UnknownRepositoryException;import org.openrdf.sesame.constants.QueryLanguage;import org.openrdf.sesame.constants.RDFFormat;import org.openrdf.sesame.export.RdfExport;import org.openrdf.sesame.query.MalformedQueryException;import org.openrdf.sesame.query.QueryEvaluationException;import org.openrdf.sesame.query.QueryResultsTable;import org.openrdf.sesame.repository.remote.HTTPRepository;import org.openrdf.sesame.repository.remote.HTTPService;import org.openrdf.sesame.sail.util.EmptyStatementIterator;public class RdfCoreTests extends TestCase {/*--------------------------------------+| Variables |+--------------------------------------*/ protected RdfXmlParser _rdfXmlParser; protected NTriplesParser _nTriplesParser; protected HTTPService _demoServer; protected URL _demoServerURL; protected HTTPRepository _manifestRep; protected HTTPRepository _logRep; protected String _manifestId = "rdfcore"; protected String _logId = "rdfcore-results"; protected RdfRepository _parsedModel; protected RdfRepository _expectedModel; protected RdfRepository _logModel; protected String _rdfCoreURL = "http://www.w3.org/2000/10/rdf-tests/rdfcore/"; protected String _testSchemaURL = _rdfCoreURL + "testSchema#"; protected String _resOntURL = "http://www.w3.org/2002/03owlt/resultsOntology#"; protected String _sesameURL = "http://sesame.aidministrator.nl/"; protected String _resultFeedURL = _sesameURL + "sesame/servlets/extractRDF?repository=rdfcore-results&format=n3&onto=yes&data=yes&niceOutput=yes"; // protected String _resultQueryURL = _sesameURL + "sesame/servlets/performSerqlQuery?repository=rdfcore-results&format=n3&queryType=construct&query="; // protected String _resultQuery = "construct distinct * from {a} <!" + _testSchemaURL + "test> {X}; p {Y} where X = ";/*--------------------------------------+| Constructors |+--------------------------------------*/ public RdfCoreTests() { super(); } public RdfCoreTests(String name) { super(name); }/*--------------------------------------+| Methods |+--------------------------------------*/ protected void setUp() { _logModel = new org.openrdf.sesame.sailimpl.memory.RdfRepository(); try { _demoServerURL = new URL("http://sesame.aidministrator.nl/sesame/"); } catch (MalformedURLException e) { fail("malformed url: " + e.getMessage()); } try { Map noParams = new HashMap(); _logModel.initialize(noParams); } catch (SailInitializationException e) { fail("Unable to initialize models: " + e.getMessage()); } _rdfXmlParser = new RdfXmlParser(); _rdfXmlParser.setDatatypeHandling(Parser.DT_NORMALIZE); // FIXME necessary for entailment? _rdfXmlParser.setParseStandAloneDocuments(true); _nTriplesParser = new NTriplesParser(); _nTriplesParser.setDatatypeHandling(Parser.DT_NORMALIZE); // read the manifest try { _demoServer = new HTTPService(_demoServerURL); // FIXME fill in appropriate username and password _demoServer.login("username", "password"); _manifestRep = (HTTPRepository)_demoServer.getRepository(_manifestId); _logRep = (HTTPRepository)_demoServer.getRepository(_logId); //_manifestRep.clear(new AdminMsgCollector()); //_manifestRep.addData(new URL(_rdfCoreURL + "Manifest.rdf"), _rdfCoreURL, RDFFormat.RDFXML, true, new AdminMsgCollector()); } catch (UnknownRepositoryException e) { fail("unknown repository: " + e.getMessage()); } catch (MalformedURLException e) { fail("malformed url: " + e.getMessage()); } catch (AccessDeniedException e) { fail("access denied: " + e.getMessage()); } catch (IOException e) { fail("unable to upload manifest: " + e.getMessage()); } _addLogStatement("system", RDFS.LABEL, "Sesame", 2, 3); _addLogStatement( "system", RDFS.COMMENT, "These tests were run on the current CVS version of Sesame and the Rio RDF parser", 2, 3); _addLogStatement("system", "http://xmlns.com/foaf/0.1/homepage", "http://sourceforge.net/projects/sesame", 2, 1); } protected void tearDown() { _rdfXmlParser = null; _nTriplesParser = null; RdfExport exporter = new RdfExport(); try { _logModel.changeNamespacePrefix(_resOntURL, "r"); _logModel.changeNamespacePrefix(_testSchemaURL, "test"); _logModel.changeNamespacePrefix("http://xmlns.com/foaf/0.1/", "foaf"); OutputStream out = new FileOutputStream("/tmp/results.rdf"); RdfDocumentWriter xmlWriter = new RdfXmlWriter(out); exporter.exportRdf(_logModel, xmlWriter, true); out.close(); //_logRep.clear(new AdminMsgCollector()); InputStream in = new FileInputStream("/tmp/results.rdf"); _logRep.addData(in, _sesameURL + "entailmentTest", RDFFormat.RDFXML, true, new AdminMsgCollector()); in.close(); } catch (SailUpdateException e) { // nothing } catch (FileNotFoundException e) { //nothing } catch (IOException e) { //nothing } catch (AccessDeniedException e) { // nothing } finally { _logModel.shutDown(); _logModel = _parsedModel = _expectedModel = null; } }/*--------------------------------------+| Test methods |+--------------------------------------*/ public void testRdfCoreCases() { // positive tests try { QueryResultsTable positiveCases = _manifestRep.performTableQuery(QueryLanguage.SERQL, " select CASE, PREMISE, CONCLUSION, PTYPE, CTYPE" + " from {CASE} <rdf:type> {<test:PositiveEntailmentTest>};" + " <test:premiseDocument> {PREMISE}; " + " <test:conclusionDocument> {CONCLUSION};" + " <test:status> {STATUS}," + " {PREMISE} <rdf:type> {PTYPE}, " + " {CONCLUSION} <rdf:type> {CTYPE}" + " where label(STATUS) = \"APPROVED\"" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfs-entailment/Manifest.rdf#test002>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes-intensional/Manifest.rdf#xsd-integer-string-incompatible>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes-intensional/Manifest.rdf#xsd-integer-decimal-compatible>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#semantic-equivalence-between-datatypes>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#plain-literal-and-xsd-string>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#range-clash>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#test010>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/pfps-10/Manifest.rdf#non-well-formed-literal-1>" + " using namespace " + " test = <!" + _testSchemaURL + ">"); int noPosCases = positiveCases.getRowCount(); for (int row = 0; row < noPosCases; row++) { String testName = positiveCases.getValue(row, 0).toString(); String inputFile = positiveCases.getValue(row, 1).toString(); String outputFile = positiveCases.getValue(row, 2).toString(); String pType = positiveCases.getValue(row, 3).toString(); String cType = positiveCases.getValue(row, 4).toString(); QueryResultsTable dtTestTable = _manifestRep.performTableQuery(QueryLanguage.SERQL, " select DT " + " from {<!" + testName + ">} <test:entailmentRules> {DT}" + " where DT = <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes#>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfs-entailment/Manifest.rdf#test002>" + " using namespace " + " test = <!" + _testSchemaURL + ">"); if (dtTestTable.getRowCount() == 0) { // Datatype inferencing does not play a role in this // test. _rdfXmlParser.setDatatypeHandling(Parser.DT_IGNORE); _nTriplesParser.setDatatypeHandling(Parser.DT_IGNORE); } else { _rdfXmlParser.setDatatypeHandling(Parser.DT_NORMALIZE); _nTriplesParser.setDatatypeHandling(Parser.DT_NORMALIZE); } _positiveEntailmentTest(row, testName, inputFile, outputFile, pType, cType); } // negative tests QueryResultsTable negativeCases = _manifestRep.performTableQuery(QueryLanguage.SERQL, " select CASE, PREMISE, CONCLUSION, PTYPE, CTYPE" + " from {CASE} <rdf:type> {<test:NegativeEntailmentTest>};" + " <test:premiseDocument> {PREMISE}; " + " <test:conclusionDocument> {CONCLUSION};" + " <test:status> {STATUS}," + " {PREMISE} <rdf:type> {PTYPE}, " + " {CONCLUSION} <rdf:type> {CTYPE}" + " where label(STATUS) = \"APPROVED\"" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfs-entailment/Manifest.rdf#test002>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes-intensional/Manifest.rdf#xsd-integer-string-incompatible>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes-intensional/Manifest.rdf#xsd-integer-decimal-compatible>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#semantic-equivalence-between-datatypes>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#plain-literal-and-xsd-string>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#range-clash>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes/Manifest.rdf#test010>" + " and CASE != <!http://www.w3.org/2000/10/rdf-tests/rdfcore/pfps-10/Manifest.rdf#non-well-formed-literal-1>" + " using namespace " + " test = <!" + _testSchemaURL + ">"); int noNegCases = negativeCases.getRowCount(); for (int row = 0; row < noNegCases; row++) { String testName = negativeCases.getValue(row, 0).toString(); String inputFile = negativeCases.getValue(row, 1).toString(); String outputFile = negativeCases.getValue(row, 2).toString(); String pType = negativeCases.getValue(row, 3).toString(); String cType = negativeCases.getValue(row, 4).toString(); QueryResultsTable dtTestTable = _manifestRep.performTableQuery(QueryLanguage.SERQL, " select DT " + " from {<!" + testName + ">} <test:entailmentRules> {DT}" + " where DT = <!http://www.w3.org/2000/10/rdf-tests/rdfcore/datatypes#>" + " using namespace " + " test = <!" + _testSchemaURL + ">"); if (dtTestTable.getRowCount() == 0) { // Datatype inferencing does not play a role in this // test. _rdfXmlParser.setDatatypeHandling(Parser.DT_IGNORE); _nTriplesParser.setDatatypeHandling(Parser.DT_IGNORE); } else { _rdfXmlParser.setDatatypeHandling(Parser.DT_NORMALIZE); _nTriplesParser.setDatatypeHandling(Parser.DT_NORMALIZE); } _negativeEntailmentTest(row + 1 + noPosCases, testName, inputFile, outputFile, pType, cType); } } catch (MalformedQueryException e) { fail("malformed query: " + e.getMessage()); } catch (IOException e) { fail("io exception: " + e.getMessage()); } catch (AccessDeniedException e) { fail("access denied: " + e.getMessage()); } catch (QueryEvaluationException e) { e.printStackTrace(); fail("Query Evaluation error: " + e.getMessage()); } } protected void _positiveEntailmentTest(int nodeId, String testName, String inputFile, String outputFile, String pType, String cType) { boolean succeeded = true; String begin = null; String end = null; String bNode = "eposnode" + String.valueOf(nodeId); _parsedModel = new org.openrdf.sesame.sailimpl.memory.RdfSchemaRepository(); _expectedModel = new org.openrdf.sesame.sailimpl.memory.RdfRepository(); try { Map noParams = new HashMap(); _parsedModel.initialize(noParams); _expectedModel.initialize(noParams); } catch (SailInitializationException e) { fail("Unable to initialize models: " + e.getMessage()); } try { begin = new Date().toString(); boolean dTypeCheckFailed = false; try { // read input model if (pType.endsWith("RDF-XML-Document")) { //System.out.println("Parsing file " + inputFile); URL inputURL = new URL(inputFile); InputStream inputStream = inputURL.openStream(); _rdfXmlParser.setStatementHandler( new StatementHandler() { public void handleStatement(Resource subj, URI pred, Value obj) { try { _parsedModel.addStatement(subj, pred, obj); } catch (SailUpdateException e) { fail("Unable to add statements to models: " + e.getMessage());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -