⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testbasicoperations.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		addRemove(model.createStatement(s, p, o));
	}

	public void testNoCompressHugeURI() throws java.lang.Exception {
		// in this test, the prefix exceeed longObjectLength but the
		// compressed URI is less than longObjectLength
		IDBConnection conn = TestConnection.makeAndCleanTestConnection();
		IRDBDriver d = conn.getDriver();
		d.setDoCompressURI(false);
		model = ModelRDB.createModel(conn);
		String pfx = "a123456789";
		String longPfx = "";
		long longLen = dbDriver.getLongObjectLength();
		// make long prefix
		while ( longPfx.length() < longLen )
			longPfx += pfx;
		String URI = longPfx + ":/www.foo/#bar";
		Resource s = model.createResource(URI);
		Property p = model.createProperty("test#predicate");
		Resource o = model.createResource("test#object");
		addRemove(model.createStatement(s, p, o));
	}
	
	public void testNoCompressHugeURI1() throws java.lang.Exception {
		// in this test, the prefix exceeed longObjectLength but the
		// compressed URI also exceeds longObjectLength
		IDBConnection conn = TestConnection.makeAndCleanTestConnection();
		IRDBDriver d = conn.getDriver();
		d.setDoCompressURI(false);
		model = ModelRDB.createModel(conn);
		String pfx = "a123456789";
		String longPfx = "";
		long longLen = dbDriver.getLongObjectLength();
		// make long prefix
		while ( longPfx.length() < longLen )
			longPfx += pfx;
		String URI = longPfx + ":/www.foo/#bar" + longPfx;
		Resource s = model.createResource(URI);
		Property p = model.createProperty("test#predicate");
		Resource o = model.createResource("test#object");
		addRemove(model.createStatement(s, p, o));
	}

	public void testAddRemoveHugeURI() throws java.lang.Exception {
		String pfx = "a123456789";
		String longPfx = "";
		long longLen = dbDriver.getLongObjectLength();
		// make long prefix
		while ( longPfx.length() < longLen )
			longPfx += pfx;
		String URI = longPfx + ":/www.foo/#bar" + longPfx;
		Resource s = model.createResource(URI);
		Property p = model.createProperty("test#predicate");
		Resource o = model.createResource("test#object");
		addRemove(model.createStatement(s, p, o));
	}
	
	public void testPrefixCache() throws java.lang.Exception {
		// in this test, add a number of long prefixes until the cache
		// overflows and then make sure they can be retrieved.
		IDBConnection conn = TestConnection.makeAndCleanTestConnection();
		IRDBDriver d = conn.getDriver();
		d.setDoCompressURI(true);
		model = ModelRDB.createModel(conn);
		int cacheSize = d.getCompressCacheSize();
		cacheSize = 10;
		d.setCompressCacheSize(cacheSize);

		String pfx = "a123456789";
		String longPfx = "";
		long longLen = dbDriver.getLongObjectLength();
		// make long prefix
		while ( longPfx.length() < longLen )
			longPfx += pfx;
		int i;
		for(i=0;i<cacheSize*2;i++) {
			String URI = longPfx + i + ":/www.foo/#bar";
			Resource s = model.createResource(URI);
			Property p = model.createProperty("test#predicate");
			Resource o = model.createResource("test#object");
			model.add(s, p, o);
		}
		for(i=0;i<cacheSize*2;i++) {
			String URI = longPfx + i + ":/www.foo/#bar";
			Resource s = model.createResource(URI);
			Property p = model.createProperty("test#predicate");
			Resource o = model.createResource("test#object");
			assertTrue(model.contains(s, p, o));
		}

	}
	
	public void testPrefixCachePersists() throws java.lang.Exception {
		// check that the prefix cache persists and affects all models in db.
		IDBConnection conn = TestConnection.makeAndCleanTestConnection();
		IRDBDriver d = conn.getDriver();
		d.setDoCompressURI(true);
		model = ModelRDB.createModel(conn);
		int cacheSize = d.getCompressCacheSize();
		d.setCompressCacheSize(cacheSize/2);	
		model.close();
		conn.close();
		
		conn = TestConnection.makeTestConnection();
		d = conn.getDriver();
		try {
			d.setDoCompressURI(false);
			assertFalse(true); // should not get here
		} catch (Exception e) {
			model = ModelRDB.createModel(conn,"NamedModel");
			assertTrue(d.getDoCompressURI() == true);
			assertTrue(d.getCompressCacheSize() == cacheSize);
		}
	}


	public void testAddRemoveDatatype() {
		Resource s = model.createResource("test#subject");
		Property p = model.createProperty("test#predicate");
		Literal l = model.createTypedLiteral("stringType");

		addRemove(model.createStatement(s, p, l));
	}

	public void testAddRemoveHugeDatatype() {
		String base = Data.strLong ;
		StringBuffer buffer = new StringBuffer(4096);
		while (buffer.length() < 4000)
			buffer.append(base);
		Resource s = model.createResource("test#subject");
		Property p = model.createProperty("test#predicate");
		Literal l2 = model.createTypedLiteral(buffer.toString());

		addRemove(model.createStatement(s, p, l2));
	}

	public void testAddRemoveBNode() {
		Resource s = model.createResource();
		Property p = model.createProperty("test#predicate");
		Resource o = model.createResource();

		addRemove(model.createStatement(s, p, o));

	}

	public void testBNodeIdentityPreservation() {
		Resource s = model.createResource();
		Property p = model.createProperty("test#predicate");
		Resource o = model.createResource();

		// Create two statements that differ only in
		// the identity of the bnodes - then perform
		// add-remove on one and verify the other is
		// unchanged.
		Statement spo = model.createStatement(s, p, o);
		Statement ops = model.createStatement(o, p, s);
		model.add(spo);
		addRemove(ops);
		assertTrue(model.contains(spo));
		model.remove(spo);
	}

	public void testDuplicateCheck() {
		Resource s = model.createResource();
		Property p = model.createProperty("test#predicate");
		Resource o = model.createResource();
		Statement spo = model.createStatement(s, p, o);
		model.add(spo);
		try {
			model.add(spo); // should be fine
			assertTrue(model.size() == 1);
		} catch (Exception e) {
			assertTrue(false); // should not get here
		}
		model.setDoDuplicateCheck(false);
		try {
			model.add(spo); // should be fine - just inserted a dup
			assertTrue(model.size() == 2);
		} catch (Exception e) {
			assertTrue(false); // should not get here
		}
	}
	
	private int countIter( Iterator it ) {
		int i = 0;
		while( it.hasNext()) {
			Statement s = (Statement) it.next();
			i++;
		}
		return i;
	}

	private int countAll()
	{
		Iterator it = model.listStatements();
		return countIter(it);
	}

	private int countSubj(Resource s)
	{
		Iterator it = model.listStatements(s,(Property)null,(RDFNode)null);
		return countIter(it);
	}

	private int countObj(RDFNode o)
	{
		Iterator it = model.listStatements((Resource)null,(Property)null,o);
		return countIter(it);
	}

	private int countPred(Property o)
	{
		Iterator it = model.listStatements((Resource)null,(Property)o,(RDFNode)null);
		return countIter(it);
	}

	public void testQueryOnlyOption() {
		GraphRDB g = new GraphRDB( conn, null, null, 
				GraphRDB.OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING, false);
		model = new ModelRDB(g);

		Statement a1 = crAssertedStmt("s1","p1","o1");
		Statement a2 = crAssertedStmt("s2","p2","s1");
		Resource r1 = crReifiedStmt("r1",a1);
		Resource r2 = crReifiedStmt("r2",a2);
		
		// should find 10 statements total
		int cnt;
		Resource s1 = a1.getSubject();
		Property p1 = a1.getPredicate();
		RDFNode o1 = a1.getObject();
		
		cnt = countAll();
		assertTrue(cnt==10);
		cnt = countSubj(s1);
		assertTrue(cnt==1);
		cnt = countObj(o1);
		assertTrue(cnt==2);
		cnt = countObj(s1);
		assertTrue(cnt==3);
		cnt = countPred(p1);
		assertTrue(cnt==1);
		cnt = countPred(RDF.predicate);
		assertTrue(cnt==2);
		cnt = countSubj(r2);
		assertTrue(cnt==4);
		
		model.setQueryOnlyAsserted(true);
		cnt = countAll();
		assertTrue(cnt==2);
		cnt = countSubj(s1);
		assertTrue(cnt==1);
		cnt = countObj(o1);
		assertTrue(cnt==1);
		cnt = countObj(s1);
		assertTrue(cnt==1);
		cnt = countPred(p1);
		assertTrue(cnt==1);
		cnt = countPred(RDF.predicate);
		assertTrue(cnt==0);
		cnt = countSubj(r2);
		assertTrue(cnt==0);

		model.setQueryOnlyReified(true);
		cnt = countAll();
		assertTrue(cnt==8);
		cnt = countSubj(s1);
		assertTrue(cnt==0);
		cnt = countObj(o1);
		assertTrue(cnt==1);
		cnt = countObj(s1);
		assertTrue(cnt==2);
		cnt = countPred(p1);
		assertTrue(cnt==0);
		cnt = countPred(RDF.predicate);
		assertTrue(cnt==2);
		cnt = countSubj(r2);
		assertTrue(cnt==4);

		model.setQueryOnlyReified(false);
		cnt = countAll();
		assertTrue(cnt==10);
	}

}
    	

/*
    (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -