KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > OWLFBRuleReasoner


1 /******************************************************************
2  * File: OWLBRuleReasoner.java
3  * Created by: Dave Reynolds
4  * Created on: 12-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: OWLFBRuleReasoner.java,v 1.16 2005/02/21 12:17:00 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys;
11
12 import java.util.*;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import com.hp.hpl.jena.reasoner.*;
17 import com.hp.hpl.jena.reasoner.rulesys.impl.OWLRuleTranslationHook;
18 import com.hp.hpl.jena.shared.impl.JenaParameters;
19 import com.hp.hpl.jena.graph.*;
20
21 /**
22  * A hybrid forward/backward implementation of the OWL closure rules.
23  *
24  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
25  * @version $Revision: 1.16 $ on $Date: 2005/02/21 12:17:00 $
26  */

27 public class OWLFBRuleReasoner extends FBRuleReasoner {
28     
29     /** The location of the OWL rule definitions on the class path */
30     protected static final String JavaDoc RULE_FILE = "etc/owl-fb.rules";
31     
32     /** The parsed rules */
33     protected static List ruleSet;
34     
35     /** The precomputed axiom closure and compiled rule set */
36     protected static FBRuleInfGraph staticPreload;
37     
38     protected static Log logger = LogFactory.getLog(OWLFBRuleReasoner.class);
39     
40     /**
41      * Constructor
42      */

43     public OWLFBRuleReasoner(ReasonerFactory factory) {
44         super(loadRules(), factory);
45         
46     }
47     
48     /**
49      * Internal constructor, used to generated a partial binding of a schema
50      * to a rule reasoner instance.
51      */

52     private OWLFBRuleReasoner(OWLFBRuleReasoner parent, InfGraph schemaGraph) {
53         super(parent.rules, schemaGraph, parent.factory);
54     }
55     
56     /**
57      * Return the rule set, loading it in if necessary
58      */

59     public static List loadRules() {
60         if (ruleSet == null) ruleSet = loadRules( RULE_FILE );
61         return ruleSet;
62     }
63     
64     
65     /**
66      * Precompute the implications of a schema graph. The statements in the graph
67      * will be combined with the data when the final InfGraph is created.
68      */

69     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
70         checkArgGraph(tbox);
71         if (schemaGraph != null) {
72             throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner");
73         }
74         FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox);
75         graph.addPreprocessingHook(new OWLRuleTranslationHook());
76         graph.prepare();
77         return new OWLFBRuleReasoner(this, graph);
78     }
79         
80     /**
81      * Attach the reasoner to a set of RDF data to process.
82      * The reasoner may already have been bound to specific rules or ontology
83      * axioms (encoded in RDF) through earlier bindRuleset calls.
84      *
85      * @param data the RDF data to be processed, some reasoners may restrict
86      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
87      * @return an inference graph through which the data+reasoner can be queried.
88      * @throws ReasonerException if the data is ill-formed according to the
89      * constraints imposed by this reasoner.
90      */

91     public InfGraph bind(Graph data) throws ReasonerException {
92         checkArgGraph(data);
93         FBRuleInfGraph graph = null;
94         InfGraph schemaArg = schemaGraph == null ? getPreload() : (FBRuleInfGraph)schemaGraph;
95         List baseRules = ((FBRuleInfGraph)schemaArg).getRules();
96         graph = new FBRuleInfGraph(this, baseRules, schemaArg);
97         graph.addPreprocessingHook(new OWLRuleTranslationHook());
98         graph.setDerivationLogging(recordDerivations);
99         graph.setTraceOn(traceOn);
100         graph.rebind(data);
101                 
102         return graph;
103     }
104     
105     /**
106      * Get the single static precomputed rule closure.
107      */

108     public InfGraph getPreload() {
109         synchronized (OWLFBRuleReasoner.class) {
110             if (staticPreload == null) {
111                 boolean prior = JenaParameters.enableFilteringOfHiddenInfNodes;
112                 try {
113                     JenaParameters.enableFilteringOfHiddenInfNodes = true;
114                     staticPreload = new FBRuleInfGraph(this, rules, null);
115                     staticPreload.prepare();
116                 } finally {
117                     JenaParameters.enableFilteringOfHiddenInfNodes = prior;
118                 }
119             }
120             return staticPreload;
121         }
122     }
123     
124     /**
125      * Check an argument graph to make sure it is not an OWL rule graph
126      * already and if so log a warning message.
127      */

128     private void checkArgGraph(Graph g) {
129         if (JenaParameters.enableOWLRuleOverOWLRuleWarnings) {
130             if (g instanceof InfGraph) {
131                 if (((InfGraph)g).getReasoner() instanceof OWLFBRuleReasoner) {
132                     logger.warn("Creating OWL rule reasoner working over another OWL rule reasoner");
133                 }
134             }
135         }
136     }
137 }
138
139
140 /*
141     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
142     All rights reserved.
143
144     Redistribution and use in source and binary forms, with or without
145     modification, are permitted provided that the following conditions
146     are met:
147
148     1. Redistributions of source code must retain the above copyright
149        notice, this list of conditions and the following disclaimer.
150
151     2. Redistributions in binary form must reproduce the above copyright
152        notice, this list of conditions and the following disclaimer in the
153        documentation and/or other materials provided with the distribution.
154
155     3. The name of the author may not be used to endorse or promote products
156        derived from this software without specific prior written permission.
157
158     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168 */
Popular Tags