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: HyperCube.java,v 1.7 2008/01/02 12:07:04 andy_seaborne Exp $
028: *
029: HyperCube.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 HyperCube 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, in both
046: * directions. The labels are not present in the model.
047: * This basic graph is then extended, for test purposes
048: * by duplicating a node. Or by adding/deleting an edge between
049: * two nodes.
050: *
051: * @author jjc
052:
053: * @version Release='$Name: $' Revision='$Revision: 1.7 $' Date='$Date: 2008/01/02 12:07:04 $'
054: */
055: class HyperCube extends java.lang.Object {
056: final private Resource corners[];
057: final private int dim;
058: final private Model model;
059:
060: private int id = 2000;
061:
062: /** Creates new DiHyperCube */
063: public HyperCube(int dimension, Model m) {
064: dim = dimension;
065: model = m;
066: corners = new Resource[1 << dim];
067: for (int i = 0; i < corners.length; i++) {
068: corners[i] = m.createResource();
069: // ((ResourceImpl)corners[i]).id = 1000 + i;
070: }
071: for (int i = 0; i < corners.length; i++)
072: add(i, corners[i]);
073: }
074:
075: private void add(int corner, Resource r) {
076: for (int j = 0; j < dim; j++) {
077: int bit = 1 << j;
078: model.add(r, RDF.value, corners[corner ^ bit]);
079: }
080: }
081:
082: HyperCube dupe(int corner) {
083: Resource dup = model.createResource();
084: // dup.id = id++;
085: add(corner, dup);
086: return this ;
087: }
088:
089: HyperCube toggle(int from, int to) {
090: Resource f = corners[from];
091: Resource t = corners[to];
092: Statement s = model.createStatement(f, RDF.value, t);
093: if (model.contains(s)) {
094: model.remove(s);
095: } else {
096: model.add(s);
097: }
098: return this ;
099: }
100:
101: static int bitCount(int i) {
102: return java.math.BigInteger.valueOf(i).bitCount();
103: }
104:
105: /*
106: * We have two HyperCube's
107: * to one we have added N a1's and M a2's.
108: * to the other we have added N b1's and M b2's.
109: * or we have toggled an edge between a1 and a2, and
110: * between b1 and b2.
111: * Returns true if they are equal.
112: */
113: static boolean equal(int a1, int a2, int b1, int b2) {
114: return bitCount(a1 ^ a2) == bitCount(b1 ^ b2);
115: }
116:
117: }
|