001: /******************************************************************
002: * File: GenericTripleMatchFrame.java
003: * Created by: Dave Reynolds
004: * Created on: 07-Aug-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: GenericTripleMatchFrame.java,v 1.8 2008/01/02 12:06:17 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.impl;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.reasoner.TriplePattern;
013: import com.hp.hpl.jena.reasoner.rulesys.Functor;
014: import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
015:
016: /**
017: * Frame on the choice point stack used to represent the state of some form of triple
018: * match - this is either a direct graph query or a query to a cached set of results.
019: * <p>
020: * This is used in the inner loop of the interpreter and so is a pure data structure
021: * not an abstract data type and assumes privileged access to the interpreter state.
022: * </p>
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.8 $ on $Date: 2008/01/02 12:06:17 $
026: */
027: public class GenericTripleMatchFrame extends GenericChoiceFrame {
028:
029: /** The variable to the subject of the triple, null if no binding required */
030: Node_RuleVariable subjectVar;
031:
032: /** The variable to the predicate of the triple, null if no binding required */
033: Node_RuleVariable predicateVar;
034:
035: /** The variable to the subject of the triple, null if no binding required */
036: Node_RuleVariable objectVar;
037:
038: /** The functor variable structure to explode the object into, null if not required */
039: Functor objectFunctor;
040:
041: /** The goal state being evaluated */
042: TriplePattern goal;
043:
044: /**
045: * Bind the goal variables to the given result triple.
046: * Returns false if the triple doesn't match the goal (due to a functor match failure).
047: */
048: public boolean bindResult(Triple triple, LPInterpreter interpreter) {
049: if (objectVar != null)
050: interpreter.bind(objectVar, triple.getObject());
051: int mark = interpreter.trail.size();
052: if (objectFunctor != null) {
053: if (!functorMatch(triple, interpreter)) {
054: interpreter.unwindTrail(mark);
055: return false;
056: }
057: }
058: if (subjectVar != null) {
059: if (!interpreter.unify(subjectVar, triple.getSubject())) {
060: interpreter.unwindTrail(mark);
061: return false;
062: }
063: }
064: if (predicateVar != null) {
065: if (!interpreter.unify(predicateVar, triple.getPredicate())) {
066: interpreter.unwindTrail(mark);
067: return false;
068: }
069:
070: }
071: return true;
072: }
073:
074: /**
075: * Check that the object of a triple match corresponds to the given functor pattern.
076: * Side effects the variable bindings.
077: */
078: public boolean functorMatch(Triple t, LPInterpreter interpreter) {
079: Node o = t.getObject();
080: if (!Functor.isFunctor(o))
081: return false;
082: Functor f = (Functor) o.getLiteralValue();
083: if (!f.getName().equals(objectFunctor.getName()))
084: return false;
085: if (f.getArgLength() != objectFunctor.getArgLength())
086: return false;
087: Node[] fargs = f.getArgs();
088: Node[] oFargs = objectFunctor.getArgs();
089: for (int i = 0; i < fargs.length; i++) {
090: if (!interpreter.unify(oFargs[i], fargs[i]))
091: return false;
092: }
093: return true;
094: }
095:
096: /**
097: * Initialize the triple match to preserve the current context of the given
098: * LPInterpreter and search for the match defined by the current argument registers
099: * @param intepreter the interpreter instance whose env, trail and arg values are to be preserved
100: */
101: public void init(LPInterpreter interpreter) {
102: super .init(interpreter);
103: Node s = LPInterpreter.deref(interpreter.argVars[0]);
104: subjectVar = (s instanceof Node_RuleVariable) ? (Node_RuleVariable) s
105: : null;
106: Node p = LPInterpreter.deref(interpreter.argVars[1]);
107: predicateVar = (p instanceof Node_RuleVariable) ? (Node_RuleVariable) p
108: : null;
109: Node o = LPInterpreter.deref(interpreter.argVars[2]);
110: objectVar = (o instanceof Node_RuleVariable) ? (Node_RuleVariable) o
111: : null;
112: if (Functor.isFunctor(o)) {
113: objectFunctor = (Functor) o.getLiteralValue();
114: goal = new TriplePattern(s, p, null);
115: } else {
116: objectFunctor = null;
117: goal = new TriplePattern(s, p, o);
118: }
119: }
120:
121: }
122:
123: /*
124: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
125: All rights reserved.
126:
127: Redistribution and use in source and binary forms, with or without
128: modification, are permitted provided that the following conditions
129: are met:
130:
131: 1. Redistributions of source code must retain the above copyright
132: notice, this list of conditions and the following disclaimer.
133:
134: 2. Redistributions in binary form must reproduce the above copyright
135: notice, this list of conditions and the following disclaimer in the
136: documentation and/or other materials provided with the distribution.
137:
138: 3. The name of the author may not be used to endorse or promote products
139: derived from this software without specific prior written permission.
140:
141: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
142: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
143: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
144: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
145: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
146: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
147: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
148: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
149: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
150: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
151: */
|