001: /******************************************************************
002: * File: ChoicePointFrame.java
003: * Created by: Dave Reynolds
004: * Created on: 22-Jul-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: ChoicePointFrame.java,v 1.8 2008/01/02 12:06:16 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.impl;
010:
011: import com.hp.hpl.jena.graph.Node;
012:
013: import java.util.*;
014:
015: /**
016: * Represents a single frame in the LP interpreter's choice point stack,
017: * represents the OR part of the search tree.
018: * <p>
019: * This is used in the inner loop of the interpreter and so is a pure data structure
020: * not an abstract data type and assumes privileged access to the interpreter state.
021: * </p>
022: *
023: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
024: * @version $Revision: 1.8 $ on $Date: 2008/01/02 12:06:16 $
025: */
026: public class ChoicePointFrame extends GenericChoiceFrame {
027:
028: /** The set of argument variables for the call */
029: Node[] argVars = new Node[RuleClauseCode.MAX_ARGUMENT_VARS];
030:
031: /** Iterator over the clauses being searched */
032: Iterator clauseIterator;
033:
034: /** Flag that this is a singleton choice point */
035: boolean isSingleton = false;
036:
037: /**
038: * Constructor.
039: * Initialize a choice point to preserve the current context of the given intepreter
040: * and then call the given set of predicates.
041: * @param interpreter the LPInterpreter whose state is to be preserved
042: * @param predicateClauses the list of predicates for this choice point
043: * @param isSingleton true if this choice should abort after one successful result
044: */
045: public ChoicePointFrame(LPInterpreter interpreter,
046: List predicateClauses, boolean isSingleton) {
047: init(interpreter, predicateClauses);
048: this .isSingleton = isSingleton;
049: }
050:
051: /**
052: * Initialize a choice point to preserve the current context of the given intepreter
053: * and then call the given set of predicates.
054: * @param interpreter the LPInterpreter whose state is to be preserved
055: * @param predicateClauses the list of predicates for this choice point
056: */
057: public void init(LPInterpreter interpreter, List predicateClauses) {
058: super .init(interpreter);
059: System.arraycopy(interpreter.argVars, 0, argVars, 0,
060: argVars.length);
061: clauseIterator = predicateClauses.iterator();
062: }
063:
064: /**
065: * Is there another clause in the sequence?
066: */
067: public boolean hasNext() {
068: if (clauseIterator == null) {
069: return false;
070: } else {
071: return clauseIterator.hasNext();
072: }
073: }
074:
075: /**
076: * Return the next clause in the sequence.
077: */
078: public RuleClauseCode nextClause() {
079: if (clauseIterator == null)
080: return null;
081: return (RuleClauseCode) clauseIterator.next();
082: }
083:
084: /**
085: * Note successful return from this choice point. This closes
086: * the choice point if it is a singleton.
087: */
088: public void noteSuccess() {
089: if (isSingleton) {
090: clauseIterator = null;
091: }
092: }
093: }
094:
095: /*
096: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
097: All rights reserved.
098:
099: Redistribution and use in source and binary forms, with or without
100: modification, are permitted provided that the following conditions
101: are met:
102:
103: 1. Redistributions of source code must retain the above copyright
104: notice, this list of conditions and the following disclaimer.
105:
106: 2. Redistributions in binary form must reproduce the above copyright
107: notice, this list of conditions and the following disclaimer in the
108: documentation and/or other materials provided with the distribution.
109:
110: 3. The name of the author may not be used to endorse or promote products
111: derived from this software without specific prior written permission.
112:
113: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
114: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
115: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
116: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
117: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
118: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
119: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
120: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
121: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
122: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123: */
|