001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: FasterTripleStore.java,v 1.23 2008/01/02 12:09:58 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.mem.faster;
007:
008: import java.util.*;
009:
010: import com.hp.hpl.jena.graph.*;
011: import com.hp.hpl.jena.graph.Triple.Field;
012: import com.hp.hpl.jena.graph.impl.TripleStore;
013: import com.hp.hpl.jena.graph.query.*;
014: import com.hp.hpl.jena.mem.*;
015:
016: public class FasterTripleStore extends GraphTripleStoreBase implements
017: TripleStore {
018: public FasterTripleStore(Graph parent) {
019: super (parent, new NodeToTriplesMapFaster(Field.getSubject,
020: Field.getPredicate, Field.getObject),
021: new NodeToTriplesMapFaster(Field.getPredicate,
022: Field.getObject, Field.getSubject),
023: new NodeToTriplesMapFaster(Field.getObject,
024: Field.getSubject, Field.getPredicate));
025: }
026:
027: public NodeToTriplesMapFaster getSubjects() {
028: return (NodeToTriplesMapFaster) subjects;
029: }
030:
031: public NodeToTriplesMapFaster getPredicates() {
032: return (NodeToTriplesMapFaster) predicates;
033: }
034:
035: protected NodeToTriplesMapFaster getObjects() {
036: return (NodeToTriplesMapFaster) objects;
037: }
038:
039: public Applyer createApplyer(ProcessedTriple pt) {
040: if (pt.hasNoVariables())
041: return containsApplyer(pt);
042: if (pt.S instanceof QueryNode.Fixed)
043: return getSubjects().createFixedSApplyer(pt);
044: if (pt.O instanceof QueryNode.Fixed)
045: return getObjects().createFixedOApplyer(pt);
046: if (pt.S instanceof QueryNode.Bound)
047: return getSubjects().createBoundSApplyer(pt);
048: if (pt.O instanceof QueryNode.Bound)
049: return getObjects().createBoundOApplyer(pt);
050: return varSvarOApplyer(pt);
051: }
052:
053: protected Applyer containsApplyer(final ProcessedTriple pt) {
054: return new Applyer() {
055: public void applyToTriples(Domain d, Matcher m,
056: StageElement next) {
057: Triple t = new Triple(pt.S.finder(d), pt.P.finder(d),
058: pt.O.finder(d));
059: if (objects.containsBySameValueAs(t))
060: next.run(d);
061: }
062: };
063: }
064:
065: protected Applyer varSvarOApplyer(final QueryTriple pt) {
066: return new Applyer() {
067: protected final QueryNode p = pt.P;
068:
069: public Iterator find(Domain d) {
070: Node P = p.finder(d);
071: if (P.isConcrete())
072: return predicates.iterator(P, Node.ANY, Node.ANY);
073: else
074: return subjects.iterateAll();
075: }
076:
077: public void applyToTriples(Domain d, Matcher m,
078: StageElement next) {
079: Iterator it = find(d);
080: while (it.hasNext())
081: if (m.match(d, (Triple) it.next()))
082: next.run(d);
083: }
084: };
085: }
086: }
087:
088: /*
089: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
090: * All rights reserved.
091: *
092: * Redistribution and use in source and binary forms, with or without
093: * modification, are permitted provided that the following conditions
094: * are met:
095: * 1. Redistributions of source code must retain the above copyright
096: * notice, this list of conditions and the following disclaimer.
097: * 2. Redistributions in binary form must reproduce the above copyright
098: * notice, this list of conditions and the following disclaimer in the
099: * documentation and/or other materials provided with the distribution.
100: * 3. The name of the author may not be used to endorse or promote products
101: * derived from this software without specific prior written permission.
102: *
103: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
104: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
105: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
106: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
107: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
108: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
109: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
110: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
111: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
112: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113: */
|