001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SimpleSelector.java,v 1.16 2008/01/02 12:05:46 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model;
008:
009: import com.hp.hpl.jena.rdf.model.impl.*;
010: import com.hp.hpl.jena.graph.*;
011:
012: /**
013: A general selector class for use when querying models.
014:
015: <p>An instance of this class is passed with query calls to models. The model
016: will use the <CODE>test</CODE> method of this class to decide whether
017: a statement should be included in the selection.
018:
019: <p>Instances of this class can be provided with subject, predicate and object
020: constraints. If a subject, a predicate or an object are provided,
021: the model implementation <b>may</b> restrict the statements that it tests
022: to statements whose subject, predicate and object match those provided in
023: the constructor. This can provide for considerably more efficient
024: searching. However, the model implementation is not required to do this.
025: If no subject, predicate or object are provided in
026: the constructor, then all statements in the model must be tested.
027:
028: <p>This class is designed to be subclassed by the application, defining
029: defining further selection criteria of its own by providing its own
030: <CODE>selects</CODE> method.
031:
032: <p>A direct instance of SimpleSelector returns <code>true</code> for the
033: Selector::isSimple() predicate. Instances of subclasses of SimpleSelector
034: return <code>false</code>, since the only reason to have such subclasses
035: is to provide a non-trivial <code>test</code> predicate or S/P/O tests other
036: than equality.
037:
038: <p>The <CODE>test</CODE> method first verifies that a statement satisfies
039: any subject, predicate or object constraints and the calls the <CODE>
040: selects</CODE> method to test for any application supplied constraint. The
041: default <CODE>selects</CODE> method simply returns true.
042:
043: @author bwm
044: @version Release='$Name: $ $Revision: 1.16 $ $Date: 2008/01/02 12:05:46 $
045: */
046:
047: public class SimpleSelector extends Object implements Selector {
048:
049: protected Resource subject;
050: protected Property predicate;
051: protected RDFNode object;
052:
053: /** Create a selector. Since no subject, predicate or object constraints are
054: * specified a model will test all statements.
055: */
056: public SimpleSelector() {
057: subject = null;
058: predicate = null;
059: object = null;
060: }
061:
062: /** Create a selector. A model <b>may</b> restrict statements that are tested using
063: * the <CODE>selects</CODE> method to those whose subject matches the
064: * subject parameter, whose predicate matches the predicate parameter and whose
065: * object matches the object paramater. Any null parameter is considered to
066: * match anything.
067: * @param subject if not null, the subject of selected statements
068: * must equal this argument.
069: * @param predicate if not null, the predicate of selected statements
070: * must equal this argument.
071: * @param object if not null, the object of selected statements
072: * must equal this argument.
073: */
074: public SimpleSelector(Resource subject, Property predicate,
075: RDFNode object) {
076: this .subject = subject;
077: this .predicate = predicate;
078: this .object = object;
079: }
080:
081: /** Create a selector. A model <b>may</b> restrict statements that are tested using
082: * the <CODE>selects</CODE> method to those whose subject matches the
083: * subject parameter, whose predicate matches the predicate parameter and whose
084: * object matches the object paramater. Any null parameter is considered to
085: * match anything.
086: * @param subject if not null, the subject of selected statements
087: * must equal this argument.
088: * @param predicate if not null, the predicate of selected statements
089: * must equal this argument.
090: * @param object if not null, the object of selected statements
091: * must equal this argument.
092: */
093: public SimpleSelector(Resource subject, Property predicate,
094: boolean object) {
095: this (subject, predicate, String.valueOf(object));
096: }
097:
098: /** Create a selector. A model <b>may</b> restrict statements that are tested using
099: * the <CODE>selects</CODE> method to those whose subject matches the
100: * subject parameter, whose predicate matches the predicate parameter and whose
101: * object matches the object paramater. Any null parameter is considered to
102: * match anything.
103: * @param subject if not null, the subject of selected statements
104: * must equal this argument.
105: * @param predicate if not null, the predicate of selected statements
106: * must equal this argument.
107: * @param object the object of selected statements
108: * must equal this argument.
109: */
110: public SimpleSelector(Resource subject, Property predicate,
111: long object) {
112: this (subject, predicate, String.valueOf(object));
113: }
114:
115: /** Create a selector. A model <b>may</b> restrict statements that are tested using
116: * the <CODE>selects</CODE> method to those whose subject matches the
117: * subject parameter, whose predicate matches the predicate parameter and whose
118: * object matches the object paramater. Any null parameter is considered to
119: * match anything.
120: * @param subject if not null, the subject of selected statements
121: * must equal this argument.
122: * @param predicate if not null, the predicate of selected statements
123: * must equal this argument.
124: * @param object the object of selected statements
125: * must equal this argument.
126: */
127: public SimpleSelector(Resource subject, Property predicate,
128: char object) {
129: this (subject, predicate, String.valueOf(object));
130: }
131:
132: /** Create a selector. A model <b>may</b> restrict statements that are tested using
133: * the <CODE>selects</CODE> method to those whose subject matches the
134: * subject parameter, whose predicate matches the predicate parameter and whose
135: * object matches the object paramater. Any null parameter is considered to
136: * match anything.
137: * @param subject if not null, the subject of selected statements
138: * must equal this argument.
139: * @param predicate if not null, the predicate of selected statements
140: * must equal this argument.
141: * @param object the object of selected statements
142: * must equal this argument.
143: */
144: public SimpleSelector(Resource subject, Property predicate,
145: float object) {
146: this (subject, predicate, String.valueOf(object));
147: }
148:
149: /** Create a selector. A model <b>may</b> restrict statements that are tested using
150: * the <CODE>selects</CODE> method to those whose subject matches the
151: * subject parameter, whose predicate matches the predicate parameter and whose
152: * object matches the object paramater. Any null parameter is considered to
153: * match anything.
154: * @param subject if not null, the subject of selected statements
155: * must equal this argument.
156: * @param predicate if not null, the predicate of selected statements
157: * must equal this argument.
158: * @param object the object of selected statements
159: * must equal this argument.
160: */
161: public SimpleSelector(Resource subject, Property predicate,
162: double object) {
163: this (subject, predicate, String.valueOf(object));
164: }
165:
166: /** Create a selector. A model <b>may</b> restrict statements that are tested using
167: * the <CODE>selects</CODE> method to those whose subject matches the
168: * subject parameter, whose predicate matches the predicate parameter and whose
169: * object matches the object paramater. Any null parameter is considered to
170: * match anything.
171: * @param subject if not null, the subject of selected statements
172: * must equal this argument.
173: * @param predicate if not null, the predicate of selected statements
174: * must equal this argument.
175: * @param object the object of selected statements
176: * must equal this argument - a null string matches the empty string
177: */
178: public SimpleSelector(Resource subject, Property predicate,
179: String object) {
180: this (subject, predicate, object, "");
181: }
182:
183: /** Create a selector. A model <b>may</b> restrict statements that are tested using
184: * the <CODE>selects</CODE> method to those whose subject matches the
185: * subject parameter, whose predicate matches the predicate parameter and whose
186: * object matches the object paramater. Any null parameter is considered to
187: * match anything.
188: * @param subject if not null, the subject of selected statements
189: * must equal this argument.
190: * @param predicate if not null, the predicate of selected statements
191: * must equal this argument.
192: * @param object the object of selected statements
193: * must equal this argument - the null string matches the empty string
194: * @param language the language of the object constraint
195: */
196: public SimpleSelector(Resource subject, Property predicate,
197: String object, String language) {
198: this .subject = subject;
199: this .predicate = predicate;
200: if (object != null) {
201: this .object = literal(object, language);
202: } else {
203: this .object = null;
204: }
205: }
206:
207: private Literal literal(String s, String lang) {
208: return new LiteralImpl(Node.createLiteral(s, lang, false),
209: (ModelCom) null);
210: }
211:
212: /** Create a selector. A model <b>may</b> restrict statements that are tested using
213: * the <CODE>selects</CODE> method to those whose subject matches the
214: * subject parameter, whose predicate matches the predicate parameter and whose
215: * object matches the object paramater. Any null parameter is considered to
216: * match anything.
217: * @param subject if not null, the subject of selected statements
218: * must equal this argument.
219: * @param predicate if not null, the predicate of selected statements
220: * must equal this argument.
221: * @param object if not null, the object of selected statements
222: * must equal this argument.
223: */
224: public SimpleSelector(Resource subject, Property predicate,
225: Object object) {
226: this .subject = subject;
227: this .predicate = predicate;
228: if (object != null) {
229: this .object = literal(object.toString(), "");
230: } else {
231: this .object = null;
232: }
233: }
234:
235: /** Return the subject constraint of this selector.
236: * @return the subject constraint
237: */
238: public Resource getSubject() {
239: return subject;
240: }
241:
242: /** Return the predicate constraint of this selector.
243: * @return the predicate constraint
244: */
245: public Property getPredicate() {
246: return predicate;
247: }
248:
249: /** Return the object constraint of this selector.
250: * @return the object constraint
251: */
252: public RDFNode getObject() {
253: return object;
254: }
255:
256: /**
257: Answer true iff this Selector is completely characterised by its
258: S/P/O triple. Subclasses will by default return false, so this method need not
259: be over-ridden (the only reason for subclassing SimpleSelector is to make
260: a test not dependent only on the S/P/O identity).
261:
262: @return true iff this selector only depends on S/P/O identity.
263: */
264: public boolean isSimple() {
265: return this .getClass() == SimpleSelector.class;
266: }
267:
268: /** Test whether a statement should be included in a selection. This method
269: * tests whether the supplied statement satisfies the subject, predicate and
270: * object constraints of the selector and then tests whether it matches the
271: * application provided <CODE>selects</CODE> method.
272: * @param s the statement to be tested
273: * @return true if the statement satisfies the subject, object
274: * and predicate constraints and the selects constraint.
275: */
276: public boolean test(Statement s) {
277: return (subject == null || subject.equals(s.getSubject()))
278: && (predicate == null || predicate.equals(s
279: .getPredicate()))
280: && (object == null || object.equals(s.getObject()))
281: && selects(s);
282: }
283:
284: /** This method is designed to be over ridden by subclasses to define application
285: * specific constraints on the statements selected.
286: * @param s the statement to be tested
287: * @return true if the statement satisfies the constraint
288: */
289: public boolean selects(Statement s) {
290: return true;
291: }
292:
293: }
294: /*
295: * (c) Copyright 2000 - 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
296: * All rights reserved.
297: *
298: * Redistribution and use in source and binary forms, with or without
299: * modification, are permitted provided that the following conditions
300: * are met:
301: * 1. Redistributions of source code must retain the above copyright
302: * notice, this list of conditions and the following disclaimer.
303: * 2. Redistributions in binary form must reproduce the above copyright
304: * notice, this list of conditions and the following disclaimer in the
305: * documentation and/or other materials provided with the distribution.
306: * 3. The name of the author may not be used to endorse or promote products
307: * derived from this software without specific prior written permission.
308:
309: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
310: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
311: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
312: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
313: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
314: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
315: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
316: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
317: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
318: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
319: *
320: * SimpleSelector.java
321: *
322: * Created on 25 August 2000, 10:12
323: */
|