KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > TempNodeCache


1 /******************************************************************
2  * File: TempNodeCache.java
3  * Created by: Dave Reynolds
4  * Created on: 09-Jul-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TempNodeCache.java,v 1.8 2005/02/21 12:18:00 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import java.util.*;
13
14 import com.hp.hpl.jena.graph.Node;
15 import com.hp.hpl.jena.reasoner.InfGraph;
16 import com.hp.hpl.jena.util.OneToManyMap;
17
18 /**
19  * In some rules we need to be able to create temporary property values
20  * which are inferred from ontology constraints but not present in the ground
21  * data. This structure is used to manage a pool of such temporary nodes.
22  * It is only needed in situations where the data can not be added directly
23  * to a deductions graph due to the risk of concurrent access.
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:18:00 $
27  */

28
29 // Implementation note: We need to map from a pair of values (instance and prop).
30
// The current implementation in terms on NodePair will turn over storage during
31
// lookup. Could replace this with a cascaded hash table.
32

33 public class TempNodeCache {
34
35     /** Map from instance+property to value */
36     protected OneToManyMap ipMap = new OneToManyMap();
37     
38     /** Map from temp to RDF class, if any */
39     protected Map classMap = new HashMap();
40     
41     /**
42      * Cosntructor.
43      * @param infgraph Parent inference graph, used to be needed for synchronization, don't think
44      * we need it any more
45      */

46     public TempNodeCache(InfGraph infgraph) {
47     }
48     
49     /**
50      * Retrieve or create a bNode representing an inferred property value.
51      * @param instance the base instance node to which the property applies
52      * @param prop the property node whose value is being inferred
53      * @param pclass the (optional, can be null) class for the inferred value.
54      * @return the bNode representing the property value
55      */

56     public synchronized Node getTemp(Node instance, Node prop, Node pclass) {
57         NodePair ip = new NodePair(instance, prop);
58         Node result = null;
59         for (Iterator i = ipMap.getAll(ip); i.hasNext(); ) {
60             Node t = (Node)i.next();
61             if (pclass != null) {
62                 Object JavaDoc tClass = classMap.get(t);
63                 if (tClass != null && tClass.equals(pclass)) {
64                     result = t;
65                     break;
66                 }
67             } else {
68                 result = t;
69                 break;
70             }
71         }
72         if (result == null) {
73             // No value yet, so create one
74
result = Node.createAnon();
75             ipMap.put(ip, result);
76             if (pclass != null) {
77                 classMap.put(result, pclass);
78             }
79         }
80         return result;
81     }
82     
83     /**
84      * Inner class used to hold and hash a node pair.
85      */

86     public static class NodePair {
87         /** first node in the pair */
88         protected Node first;
89         
90         /** second node in the pair */
91         protected Node second;
92         
93         /** Constructor */
94         public NodePair(Node first, Node second) {
95             this.first = first;
96             this.second = second;
97         }
98         
99         /**
100          * Return the first node in the pair.
101          */

102         public Node getFirst() {
103             return first;
104         }
105         
106         /**
107          * Return the second node in the pair.
108          */

109         public Node getSecond() {
110             return second;
111         }
112         
113         /**
114          * Equality of each component.
115          */

116         public boolean equals(Object JavaDoc o) {
117             return o instanceof NodePair &&
118                         first.equals(((NodePair)o).first) &&
119                         second.equals(((NodePair)o).second);
120         }
121         /**
122          * Simple combined hashcode.
123          */

124         public int hashCode() {
125             return first.hashCode() ^ (second.hashCode() << 1);
126         }
127
128     }
129     
130 }
131
132
133
134 /*
135     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
136     All rights reserved.
137
138     Redistribution and use in source and binary forms, with or without
139     modification, are permitted provided that the following conditions
140     are met:
141
142     1. Redistributions of source code must retain the above copyright
143        notice, this list of conditions and the following disclaimer.
144
145     2. Redistributions in binary form must reproduce the above copyright
146        notice, this list of conditions and the following disclaimer in the
147        documentation and/or other materials provided with the distribution.
148
149     3. The name of the author may not be used to endorse or promote products
150        derived from this software without specific prior written permission.
151
152     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
155     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
156     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
157     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
161     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162 */
Popular Tags