KickJava   Java API By Example, From Geeks To Geeks.

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


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 13 May 2004
9  * Filename $RCSfile: DIGQueryRoleFillerTranslator.java,v $
10  * Revision $Revision: 1.6 $
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 java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.w3c.dom.*;
31
32 import com.hp.hpl.jena.rdf.model.Model;
33 import com.hp.hpl.jena.rdf.model.Resource;
34 import com.hp.hpl.jena.reasoner.TriplePattern;
35 import com.hp.hpl.jena.util.iterator.*;
36 import com.hp.hpl.jena.util.xml.SimpleXMLPath;
37 import com.hp.hpl.jena.vocabulary.*;
38 import com.hp.hpl.jena.vocabulary.RDF;
39 import com.hp.hpl.jena.vocabulary.RDFS;
40
41
42
43 /**
44  * <p>
45  * Translator that generates a DIG roleFillers query in response to a find queries:
46  * <pre>
47  * :a :r :b
48  * </pre>
49  * where both a and b are known.
50  * </p>
51  *
52  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
53  * @version CVS $Id: DIGQueryRoleFillerTranslator.java,v 1.6 2005/03/16 18:52:28 ian_dickinson Exp $
54  */

55 public class DIGQueryRoleFillerTranslator
56     extends DIGQueryTranslator
57 {
58
59     // Constants
60
//////////////////////////////////
61

62     // Static variables
63
//////////////////////////////////
64

65     // Instance variables
66
//////////////////////////////////
67

68     
69     // Constructors
70
//////////////////////////////////
71

72     /**
73      * <p>Construct a translator for the DIG query 'roleFillers'.</p>
74      */

75     public DIGQueryRoleFillerTranslator() {
76         super( null, null, null );
77     }
78     
79
80     // External signature methods
81
//////////////////////////////////
82

83
84     /**
85      * <p>Answer a query that will list the role fillers for an individual-role pair</p>
86      */

87     public Document translatePattern( TriplePattern pattern, DIGAdapter da ) {
88         DIGConnection dc = da.getConnection();
89         Document query = dc.createDigVerb( DIGProfile.ASKS, da.getProfile() );
90         
91         Element instances = da.createQueryElement( query, DIGProfile.ROLE_FILLERS );
92         da.addNamedElement( instances, DIGProfile.INDIVIDUAL, da.getNodeID( pattern.getSubject() ) );
93         da.addNamedElement( instances, DIGProfile.RATOM, da.getNodeID( pattern.getPredicate() ) );
94         
95         return query;
96     }
97
98
99     /**
100      * <p>Answer an iterator of triples that match the original find query.</p>
101      */

102     public ExtendedIterator translateResponseHook( Document response, TriplePattern query, DIGAdapter da ) {
103         // evaluate a path through the return value to give us an iterator over catom names
104
SimpleXMLPath p = new SimpleXMLPath( true );
105         p.appendElementPath( DIGProfile.INDIVIDUAL_SET );
106         p.appendElementPath( DIGProfile.INDIVIDUAL );
107         p.appendAttrPath( DIGProfile.NAME );
108         
109         // and evaluate it
110
List JavaDoc matches = new ArrayList JavaDoc();
111         ExtendedIterator iNodes = p.getAll( response ).mapWith( new DIGValueToNodeMapper() );
112         try {
113             while (iNodes.hasNext()) {
114                 com.hp.hpl.jena.graph.Node result = (com.hp.hpl.jena.graph.Node) iNodes.next();
115                 if (result.equals( query.getObject() )) {
116                     matches.add( query.asTriple() );
117                     break;
118                 }
119             }
120         }
121         finally {
122             iNodes.close();
123         }
124         
125         // will contain either zero or one result
126
return WrappedIterator.create( matches.iterator() );
127     }
128     
129     
130     public Document translatePattern( TriplePattern pattern, DIGAdapter da, Model premises ) {
131         return translatePattern( pattern, da );
132     }
133
134     public boolean checkSubject( com.hp.hpl.jena.graph.Node subject, DIGAdapter da, Model premises ) {
135         return subject.isConcrete() && da.isIndividual( subject );
136     }
137
138     public boolean checkObject( com.hp.hpl.jena.graph.Node object, DIGAdapter da, Model premises ) {
139         return object.isConcrete() && da.isIndividual( object );
140     }
141
142     public boolean checkPredicate( com.hp.hpl.jena.graph.Node predicate, DIGAdapter da, Model premises ) {
143         // check that the predicate is not a datatype property
144
// and that it is not an RDF or OWL reserved predicate
145
if (predicate.isConcrete()) {
146             Resource p = (Resource) da.m_sourceData.getRDFNode( predicate );
147             String JavaDoc pNS = p.getNameSpace();
148             return !(da.m_sourceData.contains( p, RDF.type, da.m_sourceData.getProfile().DATATYPE_PROPERTY() ) ||
149                      RDFS.getURI().equals( pNS ) ||
150                      RDF.getURI().equals( pNS ) ||
151                      OWL.getURI().equals( pNS ));
152         }
153         else {
154             return false;
155         }
156     }
157
158
159     // Internal implementation methods
160
//////////////////////////////////
161

162     //==============================================================================
163
// Inner class definitions
164
//==============================================================================
165

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

195
Popular Tags