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 13 May 2004
009: * Filename $RCSfile: DIGQueryRoleFillerTranslator.java,v $
010: * Revision $Revision: 1.9 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:07:12 $
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 java.util.ArrayList;
023: import java.util.List;
024:
025: import org.w3c.dom.*;
026:
027: import com.hp.hpl.jena.rdf.model.Model;
028: import com.hp.hpl.jena.rdf.model.Resource;
029: import com.hp.hpl.jena.reasoner.TriplePattern;
030: import com.hp.hpl.jena.util.iterator.*;
031: import com.hp.hpl.jena.util.xml.SimpleXMLPath;
032: import com.hp.hpl.jena.vocabulary.*;
033: import com.hp.hpl.jena.vocabulary.RDF;
034: import com.hp.hpl.jena.vocabulary.RDFS;
035:
036: /**
037: * <p>
038: * Translator that generates a DIG roleFillers query in response to a find queries:
039: * <pre>
040: * :a :r :b
041: * </pre>
042: * where both a and b are known.
043: * </p>
044: *
045: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
046: * @version CVS $Id: DIGQueryRoleFillerTranslator.java,v 1.9 2008/01/02 12:07:12 andy_seaborne Exp $
047: */
048: public class DIGQueryRoleFillerTranslator extends DIGQueryTranslator {
049:
050: // Constants
051: //////////////////////////////////
052:
053: // Static variables
054: //////////////////////////////////
055:
056: // Instance variables
057: //////////////////////////////////
058:
059: // Constructors
060: //////////////////////////////////
061:
062: /**
063: * <p>Construct a translator for the DIG query 'roleFillers'.</p>
064: */
065: public DIGQueryRoleFillerTranslator() {
066: super (null, null, null);
067: }
068:
069: // External signature methods
070: //////////////////////////////////
071:
072: /**
073: * <p>Answer a query that will list the role fillers for an individual-role pair</p>
074: */
075: public Document translatePattern(TriplePattern pattern,
076: DIGAdapter da) {
077: DIGConnection dc = da.getConnection();
078: Document query = dc.createDigVerb(DIGProfile.ASKS, da
079: .getProfile());
080:
081: Element instances = da.createQueryElement(query,
082: DIGProfile.ROLE_FILLERS);
083: da.addNamedElement(instances, DIGProfile.INDIVIDUAL, da
084: .getNodeID(pattern.getSubject()));
085: da.addNamedElement(instances, DIGProfile.RATOM, da
086: .getNodeID(pattern.getPredicate()));
087:
088: return query;
089: }
090:
091: /**
092: * <p>Answer an iterator of triples that match the original find query.</p>
093: */
094: public ExtendedIterator translateResponseHook(Document response,
095: TriplePattern query, DIGAdapter da) {
096: // evaluate a path through the return value to give us an iterator over catom names
097: SimpleXMLPath p = new SimpleXMLPath(true);
098: p.appendElementPath(DIGProfile.INDIVIDUAL_SET);
099: p.appendElementPath(DIGProfile.INDIVIDUAL);
100: p.appendAttrPath(DIGProfile.NAME);
101:
102: // and evaluate it
103: List matches = new ArrayList();
104: ExtendedIterator iNodes = p.getAll(response).mapWith(
105: new DIGValueToNodeMapper());
106: try {
107: while (iNodes.hasNext()) {
108: com.hp.hpl.jena.graph.Node result = (com.hp.hpl.jena.graph.Node) iNodes
109: .next();
110: if (result.equals(query.getObject())) {
111: matches.add(query.asTriple());
112: break;
113: }
114: }
115: } finally {
116: iNodes.close();
117: }
118:
119: // will contain either zero or one result
120: return WrappedIterator.create(matches.iterator());
121: }
122:
123: public Document translatePattern(TriplePattern pattern,
124: DIGAdapter da, Model premises) {
125: return translatePattern(pattern, da);
126: }
127:
128: public boolean checkSubject(com.hp.hpl.jena.graph.Node subject,
129: DIGAdapter da, Model premises) {
130: return subject.isConcrete() && da.isIndividual(subject);
131: }
132:
133: public boolean checkObject(com.hp.hpl.jena.graph.Node object,
134: DIGAdapter da, Model premises) {
135: return object.isConcrete() && da.isIndividual(object);
136: }
137:
138: public boolean checkPredicate(com.hp.hpl.jena.graph.Node predicate,
139: DIGAdapter da, Model premises) {
140: // check that the predicate is not a datatype property
141: // and that it is not an RDF or OWL reserved predicate
142: if (predicate.isConcrete()) {
143: Resource p = (Resource) da.m_sourceData
144: .getRDFNode(predicate);
145: String pNS = p.getNameSpace();
146: return !(da.m_sourceData.contains(p, RDF.type,
147: da.m_sourceData.getProfile().DATATYPE_PROPERTY())
148: || RDFS.getURI().equals(pNS)
149: || RDF.getURI().equals(pNS) || OWL.getURI().equals(
150: pNS));
151: } else {
152: return false;
153: }
154: }
155:
156: // Internal implementation methods
157: //////////////////////////////////
158:
159: //==============================================================================
160: // Inner class definitions
161: //==============================================================================
162:
163: }
164:
165: /*
166: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
167: * All rights reserved.
168: *
169: * Redistribution and use in source and binary forms, with or without
170: * modification, are permitted provided that the following conditions
171: * are met:
172: * 1. Redistributions of source code must retain the above copyright
173: * notice, this list of conditions and the following disclaimer.
174: * 2. Redistributions in binary form must reproduce the above copyright
175: * notice, this list of conditions and the following disclaimer in the
176: * documentation and/or other materials provided with the distribution.
177: * 3. The name of the author may not be used to endorse or promote products
178: * derived from this software without specific prior written permission.
179: *
180: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
183: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
184: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
185: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
186: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
187: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
188: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
189: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
190: */
|