KickJava   Java API By Example, From Geeks To Geeks.

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


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: DiHyperCube.java,v 1.4 2005/02/21 12:18:39 andy_seaborne Exp $
28  *
29  * DiHyperCube.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 DiHyperCube 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, directed
46  * from one corner labelled 2^n-1 to the opposite corner
47  * labelled 0. The labels are not present in the model.
48  * This basic graph is then extended, for test purposes
49  * by duplicating a node.
50  *
51  * @author jjc
52  * @version Release='$Name: $' Revision='$Revision: 1.4 $' Date='$Date: 2005/02/21 12:18:39 $'
53  */

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

105     static boolean equal(int a1, int b1) {
106         return bitCount(a1)==bitCount(b1);
107     }
108     /*
109      * We have two DiHyperCube's
110       * to one we have added N a1's and N a2's.
111      * to the other we have added N b1's and N b2's.
112      * Returns true if they are equal.
113      */

114     static boolean equal(int a1, int a2, int b1, int b2) {
115         return bitCount(a1^a2)==bitCount(b1^b2)
116            && bitCount(a1&a2)==bitCount(b1&b2)
117            && bitCount(a1|a2)==bitCount(b1|b2)
118            && Math.min(bitCount(a1),bitCount(a2))==
119               Math.min(bitCount(b1),bitCount(b2));
120     }
121
122 }
123
Popular Tags