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 July 19th 2003
009: * Filename $RCSfile: DIGQueryParentsTranslator.java,v $
010: * Revision $Revision: 1.9 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:07:09 $
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 org.w3c.dom.Element;
023: import org.w3c.dom.Document;
024:
025: import com.hp.hpl.jena.graph.Node;
026: import com.hp.hpl.jena.graph.Triple;
027: import com.hp.hpl.jena.reasoner.TriplePattern;
028: import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
029: import com.hp.hpl.jena.util.iterator.Filter;
030:
031: /**
032: * <p>
033: * Translator that generates DIG parents/childre queries in response to a find queries:
034: * <pre>
035: * :X direct-subClassOf *
036: * * direct-subClassOf :X
037: * </pre>
038: * or similar.
039: * </p>
040: *
041: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
042: * @version Release @release@ ($Id: DIGQueryParentsTranslator.java,v 1.9 2008/01/02 12:07:09 andy_seaborne Exp $)
043: */
044: public class DIGQueryParentsTranslator extends
045: DIGQueryAncestorsTranslator {
046:
047: // Constants
048: //////////////////////////////////
049:
050: // Static variables
051: //////////////////////////////////
052:
053: // Instance variables
054: //////////////////////////////////
055:
056: // Constructors
057: //////////////////////////////////
058:
059: /**
060: * <p>Construct a translator for the DIG query 'parents'.</p>
061: * @param predicate The predicate URI to trigger on
062: * @param parents If true, we are searching for parents of the class; if false, the children
063: */
064: public DIGQueryParentsTranslator(String predicate, boolean parents) {
065: super (predicate, parents);
066: }
067:
068: /**
069: * <p>Construct a translator for the DIG query 'parents', with explicit
070: * subject and object values.</p>
071: * @param subject The subject URI to trigger on
072: * @param predicate The predicate URI to trigger on
073: * @param object The object URI to trigger on
074: * @param parents If true, we are searching for parents of the class; if false, the children
075: */
076: public DIGQueryParentsTranslator(String subject, String predicate,
077: String object, boolean parents) {
078: super (subject, predicate, object, parents);
079: }
080:
081: // External signature methods
082: //////////////////////////////////
083:
084: /**
085: * <p>Answer a query that will generate the direct class hierarchy (one level up or down) for a node</p>
086: */
087: public Document translatePattern(TriplePattern pattern,
088: DIGAdapter da) {
089: DIGConnection dc = da.getConnection();
090: Document query = dc.createDigVerb(DIGProfile.ASKS, da
091: .getProfile());
092:
093: if (m_ancestors) {
094: Element parents = da.createQueryElement(query,
095: DIGProfile.PARENTS);
096: da.addClassDescription(parents, pattern.getSubject());
097: } else {
098: Element descendants = da.createQueryElement(query,
099: DIGProfile.CHILDREN);
100: da.addClassDescription(descendants, pattern.getObject());
101: }
102:
103: return query;
104: }
105:
106: /**
107: * <p>For direct sub-class, we must sometimes ask a more general query
108: * and filter the returned results against the original query.</p>
109: * @return An optional filter on the results of a DIG query
110: */
111: protected Filter getResultsTripleFilter(TriplePattern query) {
112: return new FilterSubjectAndObject(query.getSubject(), query
113: .getObject());
114: }
115:
116: // Internal implementation methods
117: //////////////////////////////////
118:
119: //==============================================================================
120: // Inner class definitions
121: //==============================================================================
122:
123: private class FilterSubjectAndObject extends Filter {
124: private Node m_subj;
125: private Node m_obj;
126:
127: private FilterSubjectAndObject(Node subj, Node obj) {
128: m_subj = subj;
129: m_obj = obj;
130: }
131:
132: public boolean accept(Object o) {
133: Triple t = (Triple) o;
134: return ((m_subj == null)
135: || (m_subj == Node_RuleVariable.WILD)
136: || (m_subj == Node.ANY) || (t.getSubject()
137: .equals(m_subj)))
138: && ((m_obj == null)
139: || (m_obj == Node_RuleVariable.WILD)
140: || (m_obj == Node.ANY) || (t.getObject()
141: .equals(m_obj)));
142: }
143:
144: }
145: }
146:
147: /*
148: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
149: * All rights reserved.
150: *
151: * Redistribution and use in source and binary forms, with or without
152: * modification, are permitted provided that the following conditions
153: * are met:
154: * 1. Redistributions of source code must retain the above copyright
155: * notice, this list of conditions and the following disclaimer.
156: * 2. Redistributions in binary form must reproduce the above copyright
157: * notice, this list of conditions and the following disclaimer in the
158: * documentation and/or other materials provided with the distribution.
159: * 3. The name of the author may not be used to endorse or promote products
160: * derived from this software without specific prior written permission.
161: *
162: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
163: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
164: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
165: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
166: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
167: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
168: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
169: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
170: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
171: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
172: */
|