KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > dig > DIGQueryAncestorsTranslator


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created July 19th 2003
9  * Filename $RCSfile: DIGQueryAncestorsTranslator.java,v $
10  * Revision $Revision: 1.9 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/03/16 18:52:27 $
14  * by $Author: ian_dickinson $
15  *
16  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * [See end of file]
18  * ****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.reasoner.dig;
23
24
25 // Imports
26
///////////////
27
import org.w3c.dom.*;
28
29 import com.hp.hpl.jena.graph.Triple;
30 import com.hp.hpl.jena.rdf.model.Model;
31 import com.hp.hpl.jena.reasoner.TriplePattern;
32 import com.hp.hpl.jena.util.iterator.*;
33
34
35
36 /**
37  * <p>
38  * Translator that generates DIG ancestors/desendants queries in response to a find queries:
39  * <pre>
40  * :X rdf:subClassOf *
41  * * rdf:subClassOf :X
42  * </pre>
43  * or similar.
44  * </p>
45  *
46  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
47  * @version CVS $Id: DIGQueryAncestorsTranslator.java,v 1.9 2005/03/16 18:52:27 ian_dickinson Exp $
48  */

49 public class DIGQueryAncestorsTranslator
50     extends DIGQueryTranslator
51 {
52
53     // Constants
54
//////////////////////////////////
55

56     // Static variables
57
//////////////////////////////////
58

59     // Instance variables
60
//////////////////////////////////
61

62     /** Flag for querying for ancestors */
63     protected boolean m_ancestors;
64
65
66     // Constructors
67
//////////////////////////////////
68

69     /**
70      * <p>Construct a translator for the DIG query 'parents'.</p>
71      * @param predicate The predicate URI to trigger on
72      * @param ancestors If true, we are searching for parents of the class; if false, the descendants
73      */

74     public DIGQueryAncestorsTranslator( String JavaDoc predicate, boolean ancestors ) {
75         this( (ancestors ? null : ALL), predicate, (ancestors ? ALL : null), ancestors );
76     }
77
78     /**
79      * <p>Constructor for sub-classes to specify the subject and object trigger
80      * values.</p>
81      */

82     protected DIGQueryAncestorsTranslator( String JavaDoc subj, String JavaDoc pred, String JavaDoc obj, boolean anc ) {
83         super( subj, pred, obj );
84         m_ancestors = anc;
85     }
86
87
88     // External signature methods
89
//////////////////////////////////
90

91
92     /**
93      * <p>Answer a query that will generate the class hierachy for a concept</p>
94      */

95     public Document translatePattern( TriplePattern pattern, DIGAdapter da ) {
96         DIGConnection dc = da.getConnection();
97         Document query = dc.createDigVerb( DIGProfile.ASKS, da.getProfile() );
98
99         if (m_ancestors) {
100             Element ancestors = da.createQueryElement( query, DIGProfile.ANCESTORS );
101             da.addClassDescription( ancestors, pattern.getSubject() );
102         }
103         else {
104             Element descendants = da.createQueryElement( query, DIGProfile.DESCENDANTS );
105             da.addClassDescription( descendants, pattern.getObject() );
106         }
107
108         return query;
109     }
110
111
112     /**
113      * <p>Answer an iterator of triples that match the original find query.</p>
114      */

115     public ExtendedIterator translateResponseHook( Document response, TriplePattern query, DIGAdapter da ) {
116         // translate the concept set to triples, but then we must add :a rdfs:subClassOf :a to match owl semantics
117
return translateConceptSetResponse( response, query, m_ancestors, da )
118                .andThen( new SingletonIterator(
119                             new Triple( m_ancestors ? query.getSubject() : query.getObject(),
120                                         query.getPredicate(),
121                                         m_ancestors ? query.getSubject() : query.getObject() ) ) );
122     }
123
124
125     public Document translatePattern( TriplePattern pattern, DIGAdapter da, Model premises ) {
126         // not used
127
return null;
128     }
129
130     public boolean checkSubject( com.hp.hpl.jena.graph.Node subject, DIGAdapter da, Model premises ) {
131         return !m_ancestors || da.isConcept( subject, premises );
132     }
133
134     public boolean checkObject( com.hp.hpl.jena.graph.Node object, DIGAdapter da, Model premises ) {
135         return m_ancestors || da.isConcept( object, premises );
136     }
137
138
139     // Internal implementation methods
140
//////////////////////////////////
141

142     //==============================================================================
143
// Inner class definitions
144
//==============================================================================
145

146 }
147
148
149 /*
150  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
151  * All rights reserved.
152  *
153  * Redistribution and use in source and binary forms, with or without
154  * modification, are permitted provided that the following conditions
155  * are met:
156  * 1. Redistributions of source code must retain the above copyright
157  * notice, this list of conditions and the following disclaimer.
158  * 2. Redistributions in binary form must reproduce the above copyright
159  * notice, this list of conditions and the following disclaimer in the
160  * documentation and/or other materials provided with the distribution.
161  * 3. The name of the author may not be used to endorse or promote products
162  * derived from this software without specific prior written permission.
163  *
164  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
165  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
166  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
168  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
169  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
170  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
171  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
172  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
173  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
174  */

175
Popular Tags