001: /******************************************************************
002: * File: Drop.java
003: * Created by: Dave Reynolds
004: * Created on: 26-Jul-2005
005: *
006: * (c) Copyright 2005, Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: Drop.java,v 1.6 2008/01/02 12:06:21 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.builtins;
010:
011: import com.hp.hpl.jena.graph.Graph;
012: import com.hp.hpl.jena.graph.Node;
013: import com.hp.hpl.jena.graph.Triple;
014: import com.hp.hpl.jena.reasoner.InfGraph;
015: import com.hp.hpl.jena.reasoner.TriplePattern;
016: import com.hp.hpl.jena.reasoner.rulesys.BuiltinException;
017: import com.hp.hpl.jena.reasoner.rulesys.RuleContext;
018: import com.hp.hpl.jena.reasoner.rulesys.Util;
019:
020: /**
021: * A variant of the "remove" builtin that will delete matched triples
022: * from the graph but will not trigger further rule processing for
023: * the removed triples. This makes it seriously non-monotonic but
024: * useful for rewrite rules.
025: *
026: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
027: * @version $Revision: 1.6 $
028: */
029:
030: public class Drop extends BaseBuiltin {
031: /**
032: * Return a name for this builtin, normally this will be the name of the
033: * functor that will be used to invoke it.
034: */
035: public String getName() {
036: return "drop";
037: }
038:
039: /**
040: * This method is invoked when the builtin is called in a rule head.
041: * Such a use is only valid in a forward rule.
042: * @param args the array of argument values for the builtin, this is an array
043: * of Nodes.
044: * @param length the length of the argument list, may be less than the length of the args array
045: * for some rule engines
046: * @param context an execution context giving access to other relevant data
047: */
048: public void headAction(Node[] args, int length, RuleContext context) {
049: boolean ok = false;
050: InfGraph inf = context.getGraph();
051: Graph raw = inf.getRawGraph();
052: Graph deductions = inf.getDeductionsGraph();
053: for (int i = 0; i < length; i++) {
054: Node clauseN = getArg(i, args, context);
055: if (Util.isNumeric(clauseN)) {
056: int clauseIndex = Util.getIntValue(clauseN);
057: Object clause = context.getRule().getBodyElement(
058: clauseIndex);
059: if (clause instanceof TriplePattern) {
060: Triple t = context.getEnv().instantiate(
061: (TriplePattern) clause);
062: raw.delete(t);
063: deductions.delete(t);
064: } else {
065: throw new BuiltinException(this , context,
066: "illegal triple to remove non-triple clause");
067: }
068: } else {
069: throw new BuiltinException(this , context,
070: "illegal arg to remove (" + clauseN
071: + "), must be an integer");
072: }
073: }
074: }
075:
076: /**
077: * Returns false if this builtin is non-monotonic. This includes non-monotonic checks like noValue
078: * and non-monotonic actions like remove/drop. A non-monotonic call in a head is assumed to
079: * be an action and makes the overall rule and ruleset non-monotonic.
080: * Most JenaRules are monotonic deductive closure rules in which this should be false.
081: */
082: public boolean isMonotonic() {
083: return false;
084: }
085:
086: }
087:
088: /*
089: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
090: All rights reserved.
091:
092: Redistribution and use in source and binary forms, with or without
093: modification, are permitted provided that the following conditions
094: are met:
095:
096: 1. Redistributions of source code must retain the above copyright
097: notice, this list of conditions and the following disclaimer.
098:
099: 2. Redistributions in binary form must reproduce the above copyright
100: notice, this list of conditions and the following disclaimer in the
101: documentation and/or other materials provided with the distribution.
102:
103: 3. The name of the author may not be used to endorse or promote products
104: derived from this software without specific prior written permission.
105:
106: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
107: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
108: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
109: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
110: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
111: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
112: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
113: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
114: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
115: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116: */
|