001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestFasterGraphMem.java,v 1.9 2008/01/02 12:10:11 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.mem.faster.test;
008:
009: import junit.framework.TestSuite;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.graph.test.*;
013: import com.hp.hpl.jena.mem.faster.GraphMemFaster;
014: import com.hp.hpl.jena.shared.*;
015: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
016:
017: public class TestFasterGraphMem extends AbstractTestGraph {
018: public TestFasterGraphMem(String name) {
019: super (name);
020: }
021:
022: public static TestSuite suite() {
023: return new TestSuite(TestFasterGraphMem.class);
024: }
025:
026: public Graph getGraph() {
027: return new GraphMemFaster();
028: }
029:
030: public void testRemoveAllDoesntUseFind() {
031: Graph g = new GraphMemWithoutFind();
032: graphAdd(g, "x P y; a Q b");
033: g.getBulkUpdateHandler().removeAll();
034: assertEquals(0, g.size());
035: }
036:
037: public void testSizeAfterRemove() {
038: Graph g = getGraphWith("x p y");
039: ExtendedIterator it = g.find(triple("x ?? ??"));
040: it.removeNext();
041: assertEquals(0, g.size());
042: }
043:
044: public void testContainsConcreteDoesntUseFind() {
045: Graph g = new GraphMemWithoutFind();
046: graphAdd(g, "x P y; a Q b");
047: assertTrue(g.contains(triple("x P y")));
048: assertTrue(g.contains(triple("a Q b")));
049: assertFalse(g.contains(triple("a P y")));
050: assertFalse(g.contains(triple("y R b")));
051: }
052:
053: public void testSingletonStatisticsWithSingleTriple() {
054: Graph g = getGraphWith("a P b");
055: GraphStatisticsHandler h = g.getStatisticsHandler();
056: assertNotNull(h);
057: assertEquals(1L, h.getStatistic(node("a"), Node.ANY, Node.ANY));
058: assertEquals(0L, h.getStatistic(node("x"), Node.ANY, Node.ANY));
059: //
060: assertEquals(1L, h.getStatistic(Node.ANY, node("P"), Node.ANY));
061: assertEquals(0L, h.getStatistic(Node.ANY, node("Q"), Node.ANY));
062: //
063: assertEquals(1L, h.getStatistic(Node.ANY, Node.ANY, node("b")));
064: assertEquals(0L, h.getStatistic(Node.ANY, Node.ANY, node("y")));
065: }
066:
067: public void testSingletonStatisticsWithSeveralTriples() {
068: Graph g = getGraphWith("a P b; a P c; a Q b; x S y");
069: GraphStatisticsHandler h = g.getStatisticsHandler();
070: assertNotNull(h);
071: assertEquals(3L, h.getStatistic(node("a"), Node.ANY, Node.ANY));
072: assertEquals(1L, h.getStatistic(node("x"), Node.ANY, Node.ANY));
073: assertEquals(0L, h.getStatistic(node("y"), Node.ANY, Node.ANY));
074: //
075: assertEquals(2L, h.getStatistic(Node.ANY, node("P"), Node.ANY));
076: assertEquals(1L, h.getStatistic(Node.ANY, node("Q"), Node.ANY));
077: assertEquals(0L, h.getStatistic(Node.ANY, node("R"), Node.ANY));
078: //
079: assertEquals(2L, h.getStatistic(Node.ANY, Node.ANY, node("b")));
080: assertEquals(1L, h.getStatistic(Node.ANY, Node.ANY, node("c")));
081: assertEquals(0L, h.getStatistic(Node.ANY, Node.ANY, node("d")));
082: }
083:
084: public void testDoubletonStatisticsWithTriples() {
085: Graph g = getGraphWith("a P b; a P c; a Q b; x S y");
086: GraphStatisticsHandler h = g.getStatisticsHandler();
087: assertNotNull(h);
088: assertEquals(-1L, h
089: .getStatistic(node("a"), node("P"), Node.ANY));
090: assertEquals(-1L, h
091: .getStatistic(Node.ANY, node("P"), node("b")));
092: assertEquals(-1L, h
093: .getStatistic(node("a"), Node.ANY, node("b")));
094: //
095: assertEquals(0L, h
096: .getStatistic(node("no"), node("P"), Node.ANY));
097: }
098:
099: public void testStatisticsWithOnlyVariables() {
100: testStatsWithAllVariables("");
101: testStatsWithAllVariables("a P b");
102: testStatsWithAllVariables("a P b; a P c");
103: testStatsWithAllVariables("a P b; a P c; a Q b; x S y");
104: }
105:
106: private void testStatsWithAllVariables(String triples) {
107: Graph g = getGraphWith(triples);
108: GraphStatisticsHandler h = g.getStatisticsHandler();
109: assertEquals(g.size(), h.getStatistic(Node.ANY, Node.ANY,
110: Node.ANY));
111: }
112:
113: public void testStatsWithConcreteTriple() {
114: testStatsWithConcreteTriple(0, "x P y", "");
115: }
116:
117: public void testStatsWithreification() {
118: ReificationStyle style = ReificationStyle.Standard;
119: Graph g = new GraphMemFaster(style);
120: GraphStatisticsHandler h = g.getStatisticsHandler();
121: graphAdd(g, "x rdf:subject A");
122: assertEquals(-1L, h.getStatistic(node("x"), Node.ANY, Node.ANY));
123: }
124:
125: private void testStatsWithConcreteTriple(int expect, String triple,
126: String graph) {
127: Graph g = getGraphWith(graph);
128: GraphStatisticsHandler h = g.getStatisticsHandler();
129: Triple t = triple(triple);
130: assertEquals(expect, h.getStatistic(t.getSubject(), t
131: .getPredicate(), t.getObject()));
132: }
133:
134: protected final class GraphMemWithoutFind extends GraphMemFaster {
135: public ExtendedIterator graphBaseFind(TripleMatch t) {
136: throw new JenaException("find is Not Allowed");
137: }
138: }
139: }
140:
141: /*
142: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
143: * All rights reserved.
144: *
145: * Redistribution and use in source and binary forms, with or without
146: * modification, are permitted provided that the following conditions
147: * are met:
148: * 1. Redistributions of source code must retain the above copyright
149: * notice, this list of conditions and the following disclaimer.
150: * 2. Redistributions in binary form must reproduce the above copyright
151: * notice, this list of conditions and the following disclaimer in the
152: * documentation and/or other materials provided with the distribution.
153: * 3. The name of the author may not be used to endorse or promote products
154: * derived from this software without specific prior written permission.
155: *
156: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
157: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
158: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
159: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
160: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
161: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
162: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
163: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
164: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
165: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166: */
|