001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * 3. The name of the author may not be used to endorse or promote products
014: * derived from this software without specific prior written permission.
015:
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * $Id: DiHyperCube.java,v 1.7 2008/01/02 12:07:04 andy_seaborne Exp $
028: *
029: * DiHyperCube.java
030: *
031: * Created on June 29, 2001, 9:36 PM
032: */
033:
034: package com.hp.hpl.jena.regression;
035:
036: import com.hp.hpl.jena.rdf.model.*;
037: import com.hp.hpl.jena.vocabulary.RDF;
038:
039: /**
040: * A theoretical graph for testing purposes.
041: * All nodes are anonymous resources. All edges are labelled
042: * rdf:value.
043: * The basic DiHyperCube consists of the nodes being the
044: * corners of a hypercube (e.g. in 3D a cube)
045: * with the statements being the edges of the cube, directed
046: * from one corner labelled 2^n-1 to the opposite corner
047: * labelled 0. The labels are not present in the model.
048: * This basic graph is then extended, for test purposes
049: * by duplicating a node.
050: *
051: * @author jjc
052: * @version Release='$Name: $' Revision='$Revision: 1.7 $' Date='$Date: 2008/01/02 12:07:04 $'
053: */
054: class DiHyperCube extends java.lang.Object {
055: final private Resource corners[];
056: final private int dim;
057: final private Model model;
058:
059: private int id = 2000;
060:
061: /** Creates new DiHyperCube */
062: public DiHyperCube(int dimension, Model m) {
063: dim = dimension;
064: model = m;
065: corners = new Resource[1 << dim];
066: for (int i = 0; i < corners.length; i++) {
067: corners[i] = m.createResource();
068: // ((ResourceImpl)corners[i]).id = 1000 + i;
069: }
070: for (int i = 0; i < corners.length; i++)
071: addDown(i, corners[i]);
072: }
073:
074: private void addDown(int corner, Resource r) {
075: for (int j = 0; j < dim; j++) {
076: int bit = 1 << j;
077: if ((corner & bit) != 0) {
078: model.add(r, RDF.value, corners[corner ^ bit]);
079: }
080: }
081: }
082:
083: DiHyperCube dupe(int corner) {
084: Resource dup = model.createResource();
085: //dup.id = id++;
086: for (int j = 0; j < dim; j++) {
087: int bit = 1 << j;
088: if ((corner & bit) != 0) {
089: model.add(dup, RDF.value, corners[corner ^ bit]);
090: } else {
091: model.add(corners[corner ^ bit], RDF.value, dup);
092: }
093: }
094: return this ;
095: }
096:
097: static int bitCount(int i) {
098: return java.math.BigInteger.valueOf(i).bitCount();
099: }
100:
101: /*
102: * We have two DiHyperCube's
103: * to one we have added N a1's
104: * to the other we have added N b1's
105: * Returns true if they are equal.
106: */
107: static boolean equal(int a1, int b1) {
108: return bitCount(a1) == bitCount(b1);
109: }
110:
111: /*
112: * We have two DiHyperCube's
113: * to one we have added N a1's and N a2's.
114: * to the other we have added N b1's and N b2's.
115: * Returns true if they are equal.
116: */
117: static boolean equal(int a1, int a2, int b1, int b2) {
118: return bitCount(a1 ^ a2) == bitCount(b1 ^ b2)
119: && bitCount(a1 & a2) == bitCount(b1 & b2)
120: && bitCount(a1 | a2) == bitCount(b1 | b2)
121: && Math.min(bitCount(a1), bitCount(a2)) == Math.min(
122: bitCount(b1), bitCount(b2));
123: }
124:
125: }
|