KickJava   Java API By Example, From Geeks To Geeks.

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


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 10-Dec-2003
9  * Filename $RCSfile: DIGQueryIsEquivalentTranslator.java,v $
10  * Revision $Revision: 1.12 $
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.apache.commons.logging.LogFactory;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 import com.hp.hpl.jena.graph.Node;
32 import com.hp.hpl.jena.rdf.model.*;
33 import com.hp.hpl.jena.reasoner.TriplePattern;
34 import com.hp.hpl.jena.util.iterator.*;
35
36
37 /**
38  * <p>
39  * Translator to map variants of owl:equivalentClass to the DIG &lt;equivalents&gt; query,
40  * where the query is testing if two concepts are indeed equivalent (rather than listing the
41  * atoms that are, in fact, equivalent to a given concept, which is what
42  * {@link DIGQueryEquivalentsTranslator} does).
43  * </p>
44  *
45  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
46  * @version CVS $Id: DIGQueryIsEquivalentTranslator.java,v 1.12 2005/03/16 18:52:28 ian_dickinson Exp $
47  */

48 public class DIGQueryIsEquivalentTranslator
49     extends DIGQueryTranslator
50 {
51     // Constants
52
//////////////////////////////////
53

54     // Static variables
55
//////////////////////////////////
56

57     // Instance variables
58
//////////////////////////////////
59

60     /** URI of the predicate we are testing for */
61     protected String JavaDoc m_predicate;
62     
63     protected Node m_qSubject;
64     protected Node m_qObject;
65     
66
67     // Constructors
68
//////////////////////////////////
69

70     /**
71      * <p>Construct a translator for the DIG query 'equivalents'.</p>
72      * @param predicate The predicate URI to trigger on
73      */

74     public DIGQueryIsEquivalentTranslator( String JavaDoc predicate ) {
75         super( null, null, null );
76         m_predicate = predicate;
77     }
78     
79
80     // External signature methods
81
//////////////////////////////////
82

83     /**
84      * <p>Answer a query that will generate a query to see if two concepts are equivalent</p>
85      */

86     public Document JavaDoc translatePattern( TriplePattern pattern, DIGAdapter da ) {
87         return translatePattern( pattern, da, null );
88     }
89
90
91     public Document JavaDoc translatePattern( TriplePattern pattern, DIGAdapter da, Model premises ) {
92         DIGConnection dc = da.getConnection();
93         Document JavaDoc query = dc.createDigVerb( DIGProfile.ASKS, da.getProfile() );
94         
95         // re-order the argument so that we can ask equivalent between one atom
96
// and one expression (can't do more because of DIG limitations)
97
m_qSubject = pattern.getSubject();
98         m_qObject = pattern.getObject();
99         
100         if (m_qSubject.isBlank() && m_qObject.isBlank()) {
101             LogFactory.getLog( getClass() ).warn( "DIG 1.1 cannot handle isConcept query with two expressions" );
102             return null;
103         }
104         else if (m_qObject.isBlank()) {
105             // we want subject to be an expression if there is one
106
Node temp = m_qSubject;
107             m_qSubject = m_qObject;
108             m_qObject = temp;
109         }
110         
111         // we have to introduce a bNode, in the mode of the OWL comprehension axioms, if
112
// the query is of the form :c owl:unionOf [A,B]
113
Node p = pattern.getPredicate();
114         if (!m_qObject.isBlank() &&
115             (p.getURI().equals( da.getOntLanguage().UNION_OF().getURI()) ||
116              p.getURI().equals( da.getOntLanguage().INTERSECTION_OF().getURI()) ||
117              p.getURI().equals( da.getOntLanguage().COMPLEMENT_OF().getURI()) ) ||
118              p.getURI().equals( da.getOntLanguage().ONE_OF().getURI())
119             )
120         {
121             if (premises == null) {
122                 LogFactory.getLog( getClass() ).warn( "Cannot add comprehension axiom bNode for query because premises model is null" );
123             }
124             else {
125                 // create a bNode that has the same relationship to the class expression operands as the given
126
Resource comp = premises.createResource( da.getOntLanguage().CLASS() );
127                 premises.add( comp, premises.getProperty( p.getURI() ), premises.getRDFNode( m_qSubject ) );
128                 m_qSubject = comp.getNode();
129             }
130         }
131         
132         Element JavaDoc equivalents = da.createQueryElement( query, DIGProfile.EQUIVALENTS );
133         da.addClassDescription( equivalents, m_qSubject, premises );
134         
135         return query;
136     }
137
138
139     /**
140      * <p>Answer an iterator of triples that match the original find query.</p>
141      */

142     public ExtendedIterator translateResponseHook( Document JavaDoc response, TriplePattern query, DIGAdapter da ) {
143         return conceptSetNameCheck( response, da, m_qObject, query.asTriple() );
144     }
145     
146     
147     /**
148      * <p>Check whether the pattern matches the preconditions for the translation
149      * step. This means that the subject and object must be concepts or bNodes.
150      * A limitation on DIG means that both cannot be expressions (bNodes). The
151      * predicate must be equivalence, or one of the boolean definition
152      * relations (in which case the query will have to introduce a bNode as a
153      * comprehension step).
154      */

155     public boolean checkTriple( TriplePattern pattern, DIGAdapter da, Model premises ) {
156         Node object = pattern.getObject();
157         Node subject = pattern.getSubject();
158         Node pred = pattern.getPredicate();
159         
160         // ignore patterns with vars
161
boolean pass = subject.isConcrete() && object.isConcrete() && pred.isConcrete();
162         
163         // at least one of subject and object is a concept
164
pass = pass && ((object.isBlank() || da.isConcept( object, premises )) &&
165                         (subject.isBlank()) || da.isConcept( subject, premises ) &&
166                         (!subject.isBlank() || !object.isBlank()));
167         
168         // appropriate predicate
169
pass = pass &&
170                 (pred.getURI().equals( m_predicate ) ||
171                  pred.getURI().equals( da.getOntLanguage().UNION_OF().getURI() ) ||
172                  pred.getURI().equals( da.getOntLanguage().INTERSECTION_OF().getURI() ) ||
173                  pred.getURI().equals( da.getOntLanguage().COMPLEMENT_OF().getURI() ) ||
174                  pred.getURI().equals( da.getOntLanguage().ONE_OF().getURI() )
175                 );
176         
177         return pass;
178     }
179
180     public boolean trigger( TriplePattern pattern, DIGAdapter da, Model premises ) {
181         return super.trigger( pattern, da, premises );
182     }
183
184     // Internal implementation methods
185
//////////////////////////////////
186

187     //==============================================================================
188
// Inner class definitions
189
//==============================================================================
190

191 }
192
193
194 /*
195  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
196  * All rights reserved.
197  *
198  * Redistribution and use in source and binary forms, with or without
199  * modification, are permitted provided that the following conditions
200  * are met:
201  * 1. Redistributions of source code must retain the above copyright
202  * notice, this list of conditions and the following disclaimer.
203  * 2. Redistributions in binary form must reproduce the above copyright
204  * notice, this list of conditions and the following disclaimer in the
205  * documentation and/or other materials provided with the distribution.
206  * 3. The name of the author may not be used to endorse or promote products
207  * derived from this software without specific prior written permission.
208  *
209  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
210  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
211  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
212  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
214  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
215  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
216  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
218  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
219  */

220
Popular Tags