KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > regression > HyperCube


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $Id: HyperCube.java,v 1.4 2005/02/21 12:18:39 andy_seaborne Exp $
28  *
29    HyperCube.java
30  *
31  * Created on June 29, 2001, 9:36 PM
32  */

33
34 package com.hp.hpl.jena.regression;
35
36 import com.hp.hpl.jena.rdf.model.*;
37 import com.hp.hpl.jena.vocabulary.RDF;
38
39 /**
40  * A theoretical graph for testing purposes.
41  * All nodes are anonymous resources. All edges are labelled
42  * rdf:value.
43  * The basic HyperCube consists of the nodes being the
44  * corners of a hypercube (e.g. in 3D a cube)
45  * with the statements being the edges of the cube, in both
46  * directions. The labels are not present in the model.
47  * This basic graph is then extended, for test purposes
48  * by duplicating a node. Or by adding/deleting an edge between
49  * two nodes.
50  *
51  * @author jjc
52  
53  * @version Release='$Name: $' Revision='$Revision: 1.4 $' Date='$Date: 2005/02/21 12:18:39 $'
54  */

55 class HyperCube extends java.lang.Object JavaDoc {
56     final private Resource corners[];
57     final private int dim;
58     final private Model model;
59     
60     private int id = 2000;
61     /** Creates new DiHyperCube */
62     public HyperCube(int dimension,Model m) {
63         dim = dimension;
64         model = m;
65         corners = new Resource[1<<dim];
66         for (int i=0;i<corners.length;i++) {
67             corners[i] = m.createResource();
68            // ((ResourceImpl)corners[i]).id = 1000 + i;
69
}
70         for (int i=0;i<corners.length;i++)
71             add(i,corners[i]);
72     }
73     
74     private void add(int corner,Resource r) {
75         for (int j=0;j<dim;j++) {
76             int bit = 1<<j;
77             model.add(r,RDF.value,corners[corner^bit]);
78         }
79     }
80     
81     HyperCube dupe(int corner) {
82         Resource dup = model.createResource();
83      // dup.id = id++;
84
add(corner,dup);
85         return this;
86     }
87     
88     HyperCube toggle(int from,int to) {
89         Resource f = corners[from];
90         Resource t = corners[to];
91         Statement s = model.createStatement(f,RDF.value,t);
92         if ( model.contains(s) ) {
93             model.remove(s);
94         } else {
95             model.add(s);
96         }
97         return this;
98     }
99     
100     static int bitCount(int i) {
101         return java.math.BigInteger.valueOf(i).bitCount();
102     }
103     /*
104      * We have two HyperCube's
105       * to one we have added N a1's and M a2's.
106      * to the other we have added N b1's and M b2's.
107      * or we have toggled an edge between a1 and a2, and
108      * between b1 and b2.
109      * Returns true if they are equal.
110      */

111     static boolean equal(int a1, int a2, int b1, int b2) {
112         return bitCount(a1^a2)==bitCount(b1^b2);
113     }
114
115 }
116
Popular Tags