001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: Mapping.java,v 1.18 2008/01/02 12:07:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.util.CollectionFactory;
011:
012: import java.util.*;
013:
014: /**
015: this class is used to record the mapping from [variable] Node's to
016: the indexes they are bound to in a Query. Nodes bound to negative values
017: are predeclared; the negative value is converted on index allocation.
018: */
019:
020: public class Mapping implements VariableIndexes {
021: private Map map;
022:
023: private int index = 0;
024: private int preIndex = 0;
025:
026: /**
027: Create a new mapping in which all variables are unbound and the variables
028: of <code>preDeclare</code> will be allocated the first slots in the map in their
029: natural order. [This is so that the query domain elements that come out of the
030: matching process will be positioned to be suitable as query answers.]
031: */
032: public Mapping(Node[] preDeclare) {
033: this .map = CollectionFactory.createHashedMap();
034: index = preDeclare.length;
035: for (int i = 0; i < preDeclare.length; i += 1)
036: preDeclare(preDeclare[i]);
037: }
038:
039: private void preDeclare(Node v) {
040: map.put(v, new Integer(--preIndex));
041: }
042:
043: /**
044: get the index of a node in the mapping; undefined if the
045: node is not mapped.
046:
047: @param v the node to look up
048: @return the index of v in the mapping
049: */
050: public int indexOf(Node v) {
051: int res = lookUp(v);
052: if (res < 0)
053: throw new Query.UnboundVariableException(v);
054: return res;
055: }
056:
057: public int indexOf(String name) {
058: return indexOf(Node.createVariable(name));
059: }
060:
061: /**
062: get the index of a node in the mapping; return -1
063: if the node is not mapped.
064: @param v the node to look up
065: @return the index of v in the mapping
066: */
067: public int lookUp(Node v) {
068: Integer i = (Integer) map.get(v);
069: if (i == null || i.intValue() < 0)
070: return -1;
071: return i.intValue();
072: }
073:
074: /**
075: allocate an index to the node <code>v</code>. <code>v</code>
076: must not already be mapped.
077:
078: @param v the node to be given an index
079: @return the value of the allocated index
080: */
081: public int newIndex(Node v) {
082: Integer already = (Integer) map.get(v);
083: int result = already == null ? index++
084: : -already.intValue() - 1;
085: map.put(v, new Integer(result));
086: return result;
087: }
088:
089: /**
090: Answer the number of names currently held in the map
091: @return the number of names in the map
092: */
093: public int size() {
094: return map.size();
095: }
096:
097: /**
098: Answer true iff we have already bound v (predeclaration doesn't count)
099: @param v the node to look up
100: @return true iff this mapping has seen a binding occurance of v
101: */
102: public boolean hasBound(Node v) {
103: return map.containsKey(v)
104: && ((Integer) map.get(v)).intValue() > -1;
105: }
106:
107: /**
108: @return a string representing this mapping
109: */
110: public String toString() {
111: return map.toString();
112: }
113: }
114:
115: /*
116: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
117: All rights reserved.
118:
119: Redistribution and use in source and binary forms, with or without
120: modification, are permitted provided that the following conditions
121: are met:
122:
123: 1. Redistributions of source code must retain the above copyright
124: notice, this list of conditions and the following disclaimer.
125:
126: 2. Redistributions in binary form must reproduce the above copyright
127: notice, this list of conditions and the following disclaimer in the
128: documentation and/or other materials provided with the distribution.
129:
130: 3. The name of the author may not be used to endorse or promote products
131: derived from this software without specific prior written permission.
132:
133: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
134: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
135: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
136: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
137: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
138: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
139: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
140: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
141: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
142: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
143: */
|