KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > GenericTripleMatchFrame


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

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.reasoner.TriplePattern;
14 import com.hp.hpl.jena.reasoner.rulesys.Functor;
15 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
16
17 /**
18  * Frame on the choice point stack used to represent the state of some form of triple
19  * match - this is either a direct graph query or a query to a cached set of results.
20  * <p>
21  * This is used in the inner loop of the interpreter and so is a pure data structure
22  * not an abstract data type and assumes privileged access to the interpreter state.
23  * </p>
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.4 $ on $Date: 2005/02/21 12:17:40 $
27  */

28 public class GenericTripleMatchFrame extends GenericChoiceFrame {
29     
30     /** The variable to the subject of the triple, null if no binding required */
31     Node_RuleVariable subjectVar;
32     
33     /** The variable to the predicate of the triple, null if no binding required */
34     Node_RuleVariable predicateVar;
35     
36     /** The variable to the subject of the triple, null if no binding required */
37     Node_RuleVariable objectVar;
38     
39     /** The functor variable structure to explode the object into, null if not required */
40     Functor objectFunctor;
41     
42     /** The goal state being evaluated */
43     TriplePattern goal;
44
45     /**
46      * Bind the goal variables to the given result triple.
47      * Returns false if the triple doesn't match the goal (due to a functor match failure).
48      */

49     public boolean bindResult(Triple triple, LPInterpreter interpreter) {
50         if (objectVar != null) interpreter.bind(objectVar, triple.getObject());
51         int mark = interpreter.trail.size();
52         if (objectFunctor != null) {
53             if (! functorMatch(triple, interpreter)) {
54                 interpreter.unwindTrail(mark);
55                 return false;
56             }
57         }
58         if (subjectVar != null) {
59             if (! interpreter.unify(subjectVar, triple.getSubject()) ) {
60                 interpreter.unwindTrail(mark);
61                 return false;
62             }
63         }
64         if (predicateVar != null) {
65             if (! interpreter.unify(predicateVar, triple.getPredicate()) ) {
66                 interpreter.unwindTrail(mark);
67                 return false;
68             }
69                 
70         }
71         return true;
72     }
73     
74     /**
75      * Check that the object of a triple match corresponds to the given functor pattern.
76      * Side effects the variable bindings.
77      */

78     public boolean functorMatch(Triple t, LPInterpreter interpreter) {
79         Node o = t.getObject();
80         if (!Functor.isFunctor(o)) return false;
81         Functor f = (Functor)o.getLiteral().getValue();
82         if ( ! f.getName().equals(objectFunctor.getName())) return false;
83         if ( f.getArgLength() != objectFunctor.getArgLength()) return false;
84         Node[] fargs = f.getArgs();
85         Node[] oFargs = objectFunctor.getArgs();
86         for (int i = 0; i < fargs.length; i++) {
87             if (!interpreter.unify(oFargs[i], fargs[i])) return false;
88         }
89         return true;
90     }
91     
92     /**
93      * Initialize the triple match to preserve the current context of the given
94      * LPInterpreter and search for the match defined by the current argument registers
95      * @param intepreter the interpreter instance whose env, trail and arg values are to be preserved
96      */

97     public void init(LPInterpreter interpreter) {
98         super.init(interpreter);
99         Node s = LPInterpreter.deref(interpreter.argVars[0]);
100         subjectVar = (s instanceof Node_RuleVariable) ? (Node_RuleVariable) s : null;
101         Node p = LPInterpreter.deref(interpreter.argVars[1]);
102         predicateVar = (p instanceof Node_RuleVariable) ? (Node_RuleVariable) p : null;
103         Node o = LPInterpreter.deref(interpreter.argVars[2]);
104         objectVar = (o instanceof Node_RuleVariable) ? (Node_RuleVariable) o : null;
105         if (Functor.isFunctor(o)) {
106             objectFunctor = (Functor)o.getLiteral().getValue();
107             goal = new TriplePattern(s, p, null);
108         } else {
109             objectFunctor = null;
110             goal = new TriplePattern(s, p, o);
111         }
112     }
113         
114 }
115
116
117 /*
118     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
119     All rights reserved.
120
121     Redistribution and use in source and binary forms, with or without
122     modification, are permitted provided that the following conditions
123     are met:
124
125     1. Redistributions of source code must retain the above copyright
126        notice, this list of conditions and the following disclaimer.
127
128     2. Redistributions in binary form must reproduce the above copyright
129        notice, this list of conditions and the following disclaimer in the
130        documentation and/or other materials provided with the distribution.
131
132     3. The name of the author may not be used to endorse or promote products
133        derived from this software without specific prior written permission.
134
135     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
136     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
137     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
138     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
139     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
140     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
144     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145 */
Popular Tags