001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SimpleTreeQueryPlan.java,v 1.14 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: import com.hp.hpl.jena.util.iterator.*;
012:
013: import java.util.*;
014:
015: /**
016: Incomplete class. Do not use.
017: */
018: public class SimpleTreeQueryPlan implements TreeQueryPlan {
019: private Graph pattern;
020: private Graph target;
021:
022: public SimpleTreeQueryPlan(Graph target, Graph pattern) {
023: this .target = target;
024: this .pattern = pattern;
025: }
026:
027: public Graph executeTree() {
028: Graph result = Factory.createGraphMem();
029: Set roots = getRoots(pattern);
030: for (Iterator it = roots.iterator(); it.hasNext(); handleRoot(
031: result, (Node) it.next(), CollectionFactory
032: .createHashedSet())) {
033: }
034: return result;
035: }
036:
037: private Iterator findFromTriple(Graph g, Triple t) {
038: return g.find(asPattern(t.getSubject()), asPattern(t
039: .getPredicate()), asPattern(t.getObject()));
040: }
041:
042: private Node asPattern(Node x) {
043: return x.isBlank() ? null : x;
044: }
045:
046: private void handleRoot(Graph result, Node root, Set pending) {
047: ClosableIterator it = pattern.find(root, null, null);
048: if (!it.hasNext()) {
049: absorb(result, pending);
050: return;
051: }
052: while (it.hasNext()) {
053: Triple base = (Triple) it.next();
054: Iterator that = findFromTriple(target, base); // target.find( base.getSubject(), base.getPredicate(), base.getObject() );
055: while (that.hasNext()) {
056: Triple x = (Triple) that.next();
057: pending.add(x);
058: handleRoot(result, base.getObject(), pending);
059: }
060: }
061: }
062:
063: private void absorb(Graph result, Set triples) {
064: for (Iterator it = triples.iterator(); it.hasNext(); result
065: .add((Triple) it.next())) {
066: }
067: triples.clear();
068: }
069:
070: public static Set getRoots(Graph pattern) {
071: Set roots = CollectionFactory.createHashedSet();
072: ClosableIterator sub = GraphUtil.findAll(pattern);
073: while (sub.hasNext())
074: roots.add(((Triple) sub.next()).getSubject());
075: ClosableIterator obj = GraphUtil.findAll(pattern);
076: while (obj.hasNext())
077: roots.remove(((Triple) obj.next()).getObject());
078: return roots;
079: }
080: }
081: /*
082: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
083: All rights reserved.
084:
085: Redistribution and use in source and binary forms, with or without
086: modification, are permitted provided that the following conditions
087: are met:
088:
089: 1. Redistributions of source code must retain the above copyright
090: notice, this list of conditions and the following disclaimer.
091:
092: 2. Redistributions in binary form must reproduce the above copyright
093: notice, this list of conditions and the following disclaimer in the
094: documentation and/or other materials provided with the distribution.
095:
096: 3. The name of the author may not be used to endorse or promote products
097: derived from this software without specific prior written permission.
098:
099: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
100: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
101: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
102: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
103: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
104: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
108: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109: */
|