001: /******************************************************************
002: * File: OWLExptRuleTranslationHook.java
003: * Created by: Dave Reynolds
004: * Created on: 10-Jul-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: OWLExptRuleTranslationHook.java,v 1.8 2008/01/02 12:06:15 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.impl;
010:
011: import com.hp.hpl.jena.reasoner.rulesys.*;
012: import com.hp.hpl.jena.reasoner.*;
013: import com.hp.hpl.jena.graph.*;
014: import com.hp.hpl.jena.vocabulary.*;
015:
016: import java.util.*;
017:
018: /**
019: * Experimental change to OWL translation hook that doesn't handle translation
020: * of restrictions to functors.
021: *
022: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
023: * @version $Revision: 1.8 $ on $Date: 2008/01/02 12:06:15 $
024: */
025: public class OWLExptRuleTranslationHook implements RulePreprocessHook {
026:
027: /**
028: * Invoke the preprocessing hook. This will be called during the
029: * preparation time of the hybrid reasoner.
030: * @param infGraph the inference graph which is being prepared,
031: * the hook code can use this to addDeductions or add additional
032: * rules (using addRuleDuringPrepare).
033: * @param dataFind the finder which packages up the raw data (both
034: * schema and data bind) and any cached transitive closures.
035: * @param inserts a temporary graph into which the hook should insert
036: * all new deductions that should be seen by the rules.
037: */
038: public void run(FBRuleInfGraph infGraph, Finder dataFind,
039: Graph inserts) {
040: Iterator it = dataFind.find(new TriplePattern(null,
041: OWL.intersectionOf.asNode(), null));
042: while (it.hasNext()) {
043: Triple decl = (Triple) it.next();
044: Node className = decl.getSubject();
045: List elements = new ArrayList();
046: translateIntersectionList(decl.getObject(), dataFind,
047: elements);
048: // Generate the corresponding ruleset
049: List recognitionBody = new ArrayList();
050: Node var = new Node_RuleVariable("?x", 0);
051: for (Iterator i = elements.iterator(); i.hasNext();) {
052: Node description = (Node) i.next();
053: // Implication rule
054: Rule ir = new Rule("intersectionImplication",
055: new ClauseEntry[] { new TriplePattern(
056: className, RDFS.subClassOf.asNode(),
057: description) }, new ClauseEntry[0]);
058: ir.setBackward(false);
059: infGraph.addRuleDuringPrepare(ir);
060: // Recognition rule elements
061: recognitionBody.add(new TriplePattern(var, RDF.type
062: .asNode(), description));
063: }
064: List recognitionHead = new ArrayList(1);
065: recognitionHead.add(new TriplePattern(var, RDF.type
066: .asNode(), className));
067: Rule rr = new Rule("intersectionRecognition",
068: recognitionHead, recognitionBody);
069: rr.setBackward(true);
070: infGraph.addRuleDuringPrepare(rr);
071: }
072: }
073:
074: /**
075: * Translation code to translate a list of intersection elements into a
076: * Java list of corresponding class names or restriction functors.
077: * @param node the list node currently being processed
078: * @param data the source data to use as a context for this processing
079: * @param elements the list of elements found so far
080: */
081: protected static void translateIntersectionList(Node node,
082: Finder dataFind, List elements) {
083: if (node.equals(RDF.nil.asNode())) {
084: return; // end of list
085: }
086: Node description = Util.getPropValue(node, RDF.first.asNode(),
087: dataFind);
088: elements.add(description);
089: // Process the list tail
090: Node next = Util
091: .getPropValue(node, RDF.rest.asNode(), dataFind);
092: translateIntersectionList(next, dataFind, elements);
093: }
094:
095: /**
096: * Validate a triple add to see if it should reinvoke the hook. If so
097: * then the inference will be restarted at next prepare time. Incremental
098: * re-processing is not yet supported.
099: */
100: public boolean needsRerun(FBRuleInfGraph infGraph, Triple t) {
101: return (t.getPredicate().equals(OWL.intersectionOf.asNode()));
102: }
103:
104: }
105:
106: /*
107: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
108: All rights reserved.
109:
110: Redistribution and use in source and binary forms, with or without
111: modification, are permitted provided that the following conditions
112: are met:
113:
114: 1. Redistributions of source code must retain the above copyright
115: notice, this list of conditions and the following disclaimer.
116:
117: 2. Redistributions in binary form must reproduce the above copyright
118: notice, this list of conditions and the following disclaimer in the
119: documentation and/or other materials provided with the distribution.
120:
121: 3. The name of the author may not be used to endorse or promote products
122: derived from this software without specific prior written permission.
123:
124: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
125: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
126: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
127: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
128: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
129: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
130: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
131: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
132: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
133: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
134: */
|