001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created July 19th 2003
009: * Filename $RCSfile: DIGQueryInstanceTranslator.java,v $
010: * Revision $Revision: 1.10 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:07:09 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * [See end of file]
018: * ****************************************************************************/package com.hp.hpl.jena.reasoner.dig;
019:
020: // Imports
021: ///////////////
022: import org.w3c.dom.*;
023:
024: import com.hp.hpl.jena.rdf.model.Model;
025: import com.hp.hpl.jena.reasoner.TriplePattern;
026: import com.hp.hpl.jena.util.iterator.*;
027: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
028:
029: /**
030: * <p>
031: * Translator that generates DIG 'instance' queries in response to a find query:
032: * <pre>
033: * :x rdf:type :A
034: * </pre>
035: * or similar.
036: * </p>
037: *
038: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
039: * @version Release @release@ ($Id: DIGQueryInstanceTranslator.java,v 1.10 2008/01/02 12:07:09 andy_seaborne Exp $)
040: */
041: public class DIGQueryInstanceTranslator extends DIGQueryTranslator {
042:
043: // Constants
044: //////////////////////////////////
045:
046: // Static variables
047: //////////////////////////////////
048:
049: // Instance variables
050: //////////////////////////////////
051:
052: // Constructors
053: //////////////////////////////////
054:
055: /**
056: * <p>Construct a translator for the DIG query 'instance'.</p>
057: * @param predicate The predicate URI to trigger on
058: */
059: public DIGQueryInstanceTranslator(String predicate) {
060: super (null, predicate, null);
061: }
062:
063: // External signature methods
064: //////////////////////////////////
065:
066: /**
067: * <p>Answer a query that will test whether an invidividual is a member of
068: * a given named class</p>
069: */
070: public Document translatePattern(TriplePattern pattern,
071: DIGAdapter da) {
072: DIGConnection dc = da.getConnection();
073: Document query = dc.createDigVerb(DIGProfile.ASKS, da
074: .getProfile());
075: Element instance = da.createQueryElement(query,
076: DIGProfile.INSTANCE);
077: da.addNamedElement(instance, DIGProfile.INDIVIDUAL, da
078: .getNodeID(pattern.getSubject()));
079: da.addClassDescription(instance, pattern.getObject());
080:
081: return query;
082: }
083:
084: /**
085: * <p>Answer a query that will test whether an invidividual is a member of
086: * a given named class or class expression (defined by the premises)</p>
087: */
088: public Document translatePattern(TriplePattern pattern,
089: DIGAdapter da, Model premises) {
090: DIGConnection dc = da.getConnection();
091: Document query = dc.createDigVerb(DIGProfile.ASKS, da
092: .getProfile());
093: Element instance = da.createQueryElement(query,
094: DIGProfile.INSTANCE);
095:
096: da.addNamedElement(instance, DIGProfile.INDIVIDUAL, da
097: .getNodeID(pattern.getSubject()));
098:
099: if (pattern.getObject().isBlank()) {
100: da.addClassDescription(instance, pattern.getObject(),
101: premises);
102: } else {
103: da.addClassDescription(instance, pattern.getObject());
104: }
105: return query;
106: }
107:
108: /**
109: * <p>Answer an iterator of triples that match the original find query.</p>
110: */
111: public ExtendedIterator translateResponseHook(Document response,
112: TriplePattern query, DIGAdapter da) {
113: return isFalse(response) ? NullIterator.instance
114: : (ExtendedIterator) new SingletonIterator(query
115: .asTriple());
116: }
117:
118: public boolean checkObject(com.hp.hpl.jena.graph.Node object,
119: DIGAdapter da, Model premises) {
120: return da.isConcept(object, premises);
121: }
122:
123: // Internal implementation methods
124: //////////////////////////////////
125:
126: //==============================================================================
127: // Inner class definitions
128: //==============================================================================
129:
130: }
131:
132: /*
133: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
134: * All rights reserved.
135: *
136: * Redistribution and use in source and binary forms, with or without
137: * modification, are permitted provided that the following conditions
138: * are met:
139: * 1. Redistributions of source code must retain the above copyright
140: * notice, this list of conditions and the following disclaimer.
141: * 2. Redistributions in binary form must reproduce the above copyright
142: * notice, this list of conditions and the following disclaimer in the
143: * documentation and/or other materials provided with the distribution.
144: * 3. The name of the author may not be used to endorse or promote products
145: * derived from this software without specific prior written permission.
146: *
147: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
148: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
149: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
150: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
151: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
152: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
153: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
154: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
155: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
156: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
157: */
|