KickJava   Java API By Example, From Geeks To Geeks.

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


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: DIGQueryParentsTranslator.java,v $
10  * Revision $Revision: 1.5 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/03/16 18:52:28 $
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.Element JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
29
30 import com.hp.hpl.jena.graph.Node;
31 import com.hp.hpl.jena.graph.Triple;
32 import com.hp.hpl.jena.reasoner.TriplePattern;
33 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
34 import com.hp.hpl.jena.util.iterator.Filter;
35
36
37
38 /**
39  * <p>
40  * Translator that generates DIG parents/childre queries in response to a find queries:
41  * <pre>
42  * :X direct-subClassOf *
43  * * direct-subClassOf :X
44  * </pre>
45  * or similar.
46  * </p>
47  *
48  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
49  * @version Release @release@ ($Id: DIGQueryParentsTranslator.java,v 1.5 2005/03/16 18:52:28 ian_dickinson Exp $)
50  */

51 public class DIGQueryParentsTranslator
52     extends DIGQueryAncestorsTranslator
53 {
54
55     // Constants
56
//////////////////////////////////
57

58     // Static variables
59
//////////////////////////////////
60

61     // Instance variables
62
//////////////////////////////////
63

64
65     // Constructors
66
//////////////////////////////////
67

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

73     public DIGQueryParentsTranslator( String JavaDoc predicate, boolean parents ) {
74         super( predicate, parents );
75     }
76
77
78     /**
79      * <p>Construct a translator for the DIG query 'parents', with explicit
80      * subject and object values.</p>
81      * @param subject The subject URI to trigger on
82      * @param predicate The predicate URI to trigger on
83      * @param object The object URI to trigger on
84      * @param parents If true, we are searching for parents of the class; if false, the children
85      */

86     public DIGQueryParentsTranslator( String JavaDoc subject, String JavaDoc predicate, String JavaDoc object, boolean parents ) {
87         super( subject, predicate, object, parents );
88     }
89
90
91     // External signature methods
92
//////////////////////////////////
93

94
95     /**
96      * <p>Answer a query that will generate the direct class hierarchy (one level up or down) for a node</p>
97      */

98     public Document JavaDoc translatePattern( TriplePattern pattern, DIGAdapter da ) {
99         DIGConnection dc = da.getConnection();
100         Document JavaDoc query = dc.createDigVerb( DIGProfile.ASKS, da.getProfile() );
101
102         if (m_ancestors) {
103             Element JavaDoc parents = da.createQueryElement( query, DIGProfile.PARENTS );
104             da.addClassDescription( parents, pattern.getSubject() );
105         }
106         else {
107             Element JavaDoc descendants = da.createQueryElement( query, DIGProfile.CHILDREN );
108             da.addClassDescription( descendants, pattern.getObject() );
109         }
110
111         return query;
112     }
113
114     /**
115      * <p>For direct sub-class, we must sometimes ask a more general query
116      * and filter the returned results against the original query.</p>
117      * @return An optional filter on the results of a DIG query
118      */

119     protected Filter getResultsTripleFilter( TriplePattern query ) {
120         return new FilterSubjectAndObject( query.getSubject(), query.getObject() );
121     }
122
123
124     // Internal implementation methods
125
//////////////////////////////////
126

127     //==============================================================================
128
// Inner class definitions
129
//==============================================================================
130

131     private class FilterSubjectAndObject
132         implements Filter
133     {
134         private Node m_subj;
135         private Node m_obj;
136
137         private FilterSubjectAndObject( Node subj, Node obj ) {
138             m_subj = subj;
139             m_obj = obj;
140         }
141
142         public boolean accept( Object JavaDoc o ) {
143             Triple t = (Triple) o;
144             return ((m_subj == null) ||
145                     (m_subj == Node_RuleVariable.WILD) ||
146                     (m_subj == Node.ANY) ||
147                     (t.getSubject().equals( m_subj ))) &&
148                    ((m_obj == null) ||
149                     (m_obj == Node_RuleVariable.WILD) ||
150                     (m_obj == Node.ANY) ||
151                     (t.getObject().equals( m_obj )));
152         }
153
154     }
155 }
156
157
158 /*
159  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
160  * All rights reserved.
161  *
162  * Redistribution and use in source and binary forms, with or without
163  * modification, are permitted provided that the following conditions
164  * are met:
165  * 1. Redistributions of source code must retain the above copyright
166  * notice, this list of conditions and the following disclaimer.
167  * 2. Redistributions in binary form must reproduce the above copyright
168  * notice, this list of conditions and the following disclaimer in the
169  * documentation and/or other materials provided with the distribution.
170  * 3. The name of the author may not be used to endorse or promote products
171  * derived from this software without specific prior written permission.
172  *
173  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
174  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
175  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
176  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
178  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
179  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
180  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
181  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
182  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
183  */

184
Popular Tags