001: /*
002: (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: TestUnionStatistics.java,v 1.2 2008/01/02 12:07:21 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.compose.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.graph.compose.MultiUnion;
011: import com.hp.hpl.jena.graph.impl.GraphBase;
012: import com.hp.hpl.jena.graph.test.GraphTestBase;
013: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
014:
015: public class TestUnionStatistics extends GraphTestBase {
016: public TestUnionStatistics(String name) {
017: super (name);
018: }
019:
020: static class AnInteger {
021: public int value = 0;
022:
023: public AnInteger(int value) {
024: this .value = value;
025: }
026: }
027:
028: public void testUnionValues() {
029: testUnion(0, 0, 0, 0);
030: }
031:
032: public void testCopiesSingleNonZeroResult() {
033: testUnion(1, 1, 0, 0);
034: testUnion(1, 0, 1, 0);
035: testUnion(1, 0, 0, 1);
036: testUnion(1, 1, 0, 0);
037: testUnion(2, 0, 2, 0);
038: testUnion(4, 0, 0, 4);
039: }
040:
041: public void testResultIsSumOfBaseResults() {
042: testUnion(3, 1, 2, 0);
043: testUnion(5, 1, 0, 4);
044: testUnion(6, 0, 2, 4);
045: testUnion(7, 1, 2, 4);
046: testUnion(3, 0, 2, 1);
047: testUnion(5, 4, 1, 0);
048: testUnion(6, 2, 2, 2);
049: testUnion(7, 6, 0, 1);
050: }
051:
052: public void testUnknownOverrulesAll() {
053: testUnion(-1, -1, 0, 0);
054: testUnion(-1, 0, -1, 0);
055: testUnion(-1, 0, 0, -1);
056: testUnion(-1, -1, 1, 1);
057: testUnion(-1, 1, -1, 1);
058: testUnion(-1, 1, 1, -1);
059: }
060:
061: /**
062: Asserts that the statistic obtained by probing the three-element union
063: with statistics <code>av</code>, <code>bv</code>, and
064: <code>cv</code> is <code>expected</code>.
065: */
066: private void testUnion(int expected, int av, int bv, int cv) {
067: AnInteger a = new AnInteger(av), b = new AnInteger(bv), c = new AnInteger(
068: cv);
069: Graph g1 = graphWithGivenStatistic(a);
070: Graph g2 = graphWithGivenStatistic(b);
071: Graph g3 = graphWithGivenStatistic(c);
072: Graph[] graphs = new Graph[] { g1, g2, g3 };
073: MultiUnion mu = new MultiUnion(graphs);
074: GraphStatisticsHandler gs = new MultiUnion.MultiUnionStatisticsHandler(
075: mu);
076: assertEquals(expected, gs.getStatistic(Node.ANY, Node.ANY,
077: Node.ANY));
078: }
079:
080: static Graph graphWithGivenStatistic(final AnInteger x) {
081: return new GraphBase() {
082: protected ExtendedIterator graphBaseFind(TripleMatch m) {
083: throw new RuntimeException("should never be called");
084: }
085:
086: protected GraphStatisticsHandler createStatisticsHandler() {
087: return new GraphStatisticsHandler() {
088: public long getStatistic(Node S, Node P, Node O) {
089: return x.value;
090: }
091: };
092: }
093: };
094: }
095: }
096:
097: /*
098: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
099: * All rights reserved.
100: *
101: * Redistribution and use in source and binary forms, with or without
102: * modification, are permitted provided that the following conditions
103: * are met:
104: * 1. Redistributions of source code must retain the above copyright
105: * notice, this list of conditions and the following disclaimer.
106: * 2. Redistributions in binary form must reproduce the above copyright
107: * notice, this list of conditions and the following disclaimer in the
108: * documentation and/or other materials provided with the distribution.
109: * 3. The name of the author may not be used to endorse or promote products
110: * derived from this software without specific prior written permission.
111: *
112: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
113: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
114: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
115: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
116: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
117: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
121: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122: */
|