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: DIGQueryAncestorsTranslator.java,v $
010: * Revision $Revision: 1.12 $
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.graph.Triple;
025: import com.hp.hpl.jena.rdf.model.Model;
026: import com.hp.hpl.jena.reasoner.TriplePattern;
027: import com.hp.hpl.jena.util.iterator.*;
028:
029: /**
030: * <p>
031: * Translator that generates DIG ancestors/desendants queries in response to a find queries:
032: * <pre>
033: * :X rdf:subClassOf *
034: * * rdf:subClassOf :X
035: * </pre>
036: * or similar.
037: * </p>
038: *
039: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
040: * @version CVS $Id: DIGQueryAncestorsTranslator.java,v 1.12 2008/01/02 12:07:09 andy_seaborne Exp $
041: */
042: public class DIGQueryAncestorsTranslator extends DIGQueryTranslator {
043:
044: // Constants
045: //////////////////////////////////
046:
047: // Static variables
048: //////////////////////////////////
049:
050: // Instance variables
051: //////////////////////////////////
052:
053: /** Flag for querying for ancestors */
054: protected boolean m_ancestors;
055:
056: // Constructors
057: //////////////////////////////////
058:
059: /**
060: * <p>Construct a translator for the DIG query 'parents'.</p>
061: * @param predicate The predicate URI to trigger on
062: * @param ancestors If true, we are searching for parents of the class; if false, the descendants
063: */
064: public DIGQueryAncestorsTranslator(String predicate,
065: boolean ancestors) {
066: this ((ancestors ? null : ALL), predicate, (ancestors ? ALL
067: : null), ancestors);
068: }
069:
070: /**
071: * <p>Constructor for sub-classes to specify the subject and object trigger
072: * values.</p>
073: */
074: protected DIGQueryAncestorsTranslator(String subj, String pred,
075: String obj, boolean anc) {
076: super (subj, pred, obj);
077: m_ancestors = anc;
078: }
079:
080: // External signature methods
081: //////////////////////////////////
082:
083: /**
084: * <p>Answer a query that will generate the class hierachy for a concept</p>
085: */
086: public Document translatePattern(TriplePattern pattern,
087: DIGAdapter da) {
088: DIGConnection dc = da.getConnection();
089: Document query = dc.createDigVerb(DIGProfile.ASKS, da
090: .getProfile());
091:
092: if (m_ancestors) {
093: Element ancestors = da.createQueryElement(query,
094: DIGProfile.ANCESTORS);
095: da.addClassDescription(ancestors, pattern.getSubject());
096: } else {
097: Element descendants = da.createQueryElement(query,
098: DIGProfile.DESCENDANTS);
099: da.addClassDescription(descendants, pattern.getObject());
100: }
101:
102: return query;
103: }
104:
105: /**
106: * <p>Answer an iterator of triples that match the original find query.</p>
107: */
108: public ExtendedIterator translateResponseHook(Document response,
109: TriplePattern query, DIGAdapter da) {
110: // translate the concept set to triples, but then we must add :a rdfs:subClassOf :a to match owl semantics
111: return translateConceptSetResponse(response, query,
112: m_ancestors, da).andThen(
113: new SingletonIterator(new Triple(m_ancestors ? query
114: .getSubject() : query.getObject(), query
115: .getPredicate(), m_ancestors ? query
116: .getSubject() : query.getObject())));
117: }
118:
119: public Document translatePattern(TriplePattern pattern,
120: DIGAdapter da, Model premises) {
121: // not used
122: return null;
123: }
124:
125: public boolean checkSubject(com.hp.hpl.jena.graph.Node subject,
126: DIGAdapter da, Model premises) {
127: return !m_ancestors || da.isConcept(subject, premises);
128: }
129:
130: public boolean checkObject(com.hp.hpl.jena.graph.Node object,
131: DIGAdapter da, Model premises) {
132: return m_ancestors || da.isConcept(object, premises);
133: }
134:
135: // Internal implementation methods
136: //////////////////////////////////
137:
138: //==============================================================================
139: // Inner class definitions
140: //==============================================================================
141:
142: }
143:
144: /*
145: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
146: * All rights reserved.
147: *
148: * Redistribution and use in source and binary forms, with or without
149: * modification, are permitted provided that the following conditions
150: * are met:
151: * 1. Redistributions of source code must retain the above copyright
152: * notice, this list of conditions and the following disclaimer.
153: * 2. Redistributions in binary form must reproduce the above copyright
154: * notice, this list of conditions and the following disclaimer in the
155: * documentation and/or other materials provided with the distribution.
156: * 3. The name of the author may not be used to endorse or promote products
157: * derived from this software without specific prior written permission.
158: *
159: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
160: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
162: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
163: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
164: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
165: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
166: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
167: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
168: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
169: */
|