001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: SimpleReifierTripleMap.java,v 1.14 2008/01/02 12:05:19 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.graph.impl;
007:
008: import java.util.HashSet;
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.util.CollectionFactory;
013: import com.hp.hpl.jena.util.iterator.*;
014:
015: /**
016: SimpleReifierTripleMap - a map storing complete node -> triple maps.
017:
018: @author kers
019: */
020: public class SimpleReifierTripleMap implements ReifierTripleMap {
021: protected Map inverseMap = CollectionFactory.createHashedMap();
022:
023: protected Map forwardMap = CollectionFactory.createHashedMap();
024:
025: public Triple getTriple(Node tag) {
026: return (Triple) forwardMap.get(tag);
027: }
028:
029: public void clear() {
030: forwardMap.clear();
031: inverseMap.clear();
032: }
033:
034: /**
035: Answer true iff we have a reified triple <code>t</code>.
036: */
037: public boolean hasTriple(Triple t) {
038: return inverseMap.containsKey(t);
039: }
040:
041: public Triple putTriple(Node key, Triple value) {
042: forwardMap.put(key, value);
043: inversePut(value, key);
044: return value;
045: }
046:
047: public void removeTriple(Node key) {
048: Object t = forwardMap.get(key);
049: forwardMap.remove(key);
050: if (t instanceof Triple)
051: inverseRemove((Triple) t, key);
052: }
053:
054: public void removeTriple(Node key, Triple value) {
055: forwardMap.remove(key);
056: inverseRemove(value, key);
057: }
058:
059: public void removeTriple(Triple t) {
060: ExtendedIterator it = tagIterator(t);
061: Set nodes = CollectionFactory.createHashedSet();
062: while (it.hasNext())
063: nodes.add(it.next());
064: Iterator them = nodes.iterator();
065: while (them.hasNext())
066: removeTriple((Node) them.next());
067: }
068:
069: protected void inverseRemove(Triple value, Node key) {
070: Set s = (Set) inverseMap.get(value);
071: if (s != null) {
072: s.remove(key);
073: if (s.isEmpty())
074: inverseMap.remove(value);
075: }
076: }
077:
078: protected void inversePut(Triple value, Node key) {
079: Set s = (Set) inverseMap.get(value);
080: if (s == null)
081: inverseMap.put(value, s = new HashSet());
082: s.add(key);
083: }
084:
085: public ExtendedIterator tagIterator(Triple t) {
086: Set s = (Set) inverseMap.get(t);
087: return s == null ? (ExtendedIterator) NullIterator.instance
088: : WrappedIterator.create(s.iterator());
089: }
090:
091: protected ExtendedIterator allTriples(TripleMatch tm) {
092: if (forwardMap.isEmpty())
093: return NullIterator.instance;
094: Triple pattern = tm.asTriple();
095: Node tag = pattern.getSubject();
096: if (tag.isConcrete()) {
097: Triple x = getTriple(tag);
098: return x == null ? NullIterator.instance : explodeTriple(
099: pattern, tag, x);
100: } else {
101: final Iterator it = forwardMap.entrySet().iterator();
102: return new FragmentTripleIterator(pattern, it) {
103: public void fill(GraphAdd ga, Node n,
104: Object fragmentsObject) {
105: SimpleReifier.graphAddQuad(ga, n,
106: (Triple) fragmentsObject);
107: }
108: };
109: }
110: }
111:
112: /**
113: Answer an interator over all of the quadlets of <code>toExplode</code> with
114: the reifying node <code>tag</code> that match <code>pattern</code>.
115: */
116: public static ExtendedIterator explodeTriple(Triple pattern,
117: Node tag, Triple toExplode) {
118: GraphAddList L = new GraphAddList(pattern);
119: SimpleReifier.graphAddQuad(L, tag, toExplode);
120: return WrappedIterator.create(L.iterator());
121: }
122:
123: /**
124: Return the fragment map as a read-only Graph of triples. We rely on the
125: default code in GraphBase which allows us to only implement find(TripleMatch)
126: to present a Graph. All the hard work is done by allTriples.
127: */
128: public Graph asGraph() {
129: return new GraphBase() {
130: public ExtendedIterator graphBaseFind(TripleMatch tm) {
131: return allTriples(tm);
132: }
133: };
134: }
135:
136: public ExtendedIterator find(TripleMatch m) {
137: return allTriples(m);
138: }
139:
140: public int size() {
141: return forwardMap.size() * 4;
142: }
143:
144: /**
145: Answer an iterator over all the fragment tags in this map.
146: */
147: public ExtendedIterator tagIterator() {
148: return WrappedIterator.create(forwardMap.keySet().iterator());
149: }
150: }
151:
152: /*
153: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
154: All rights reserved.
155:
156: Redistribution and use in source and binary forms, with or without
157: modification, are permitted provided that the following conditions
158: are met:
159:
160: 1. Redistributions of source code must retain the above copyright
161: notice, this list of conditions and the following disclaimer.
162:
163: 2. Redistributions in binary form must reproduce the above copyright
164: notice, this list of conditions and the following disclaimer in the
165: documentation and/or other materials provided with the distribution.
166:
167: 3. The name of the author may not be used to endorse or promote products
168: derived from this software without specific prior written permission.
169:
170: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
174: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
175: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
176: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
177: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
178: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
179: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180: */
|