001: package org.zilonis.network.production;
002:
003: /**
004: * Copyright (c) 2005 Elie Levy <elie.levy@zilonis.org>
005: * All rights reserved
006: *
007: * This License governs use of the accompanying Software, and your use of the
008: * Software constitutes acceptance of this license.
009: *
010: * You may use this Software for any non-commercial purpose, subject to the
011: * restrictions in this license. Some purposes which can be non-commercial are
012: * teaching, academic research, and personal experimentation. You may also
013: * distribute this Software with books or other teaching materials, or publish
014: * the Software on websites, that are intended to teach the use of the
015: * Software.
016: *
017: *
018: * You may not use or distribute this Software or any derivative works in any
019: * form for commercial purposes. Examples of commercial purposes would be
020: * running business operations, licensing, leasing, or selling the Software, or
021: * distributing the Software for use with commercial products.
022: *
023: * You may modify this Software and distribute the modified Software for
024: * non-commercial purposes, however, you may not grant rights to the Software
025: * or derivative works that are broader than those provided by this License.
026: * For example, you may not distribute modifications of the Software under
027: * terms that would permit commercial use, or under terms that purport to
028: * require the Software or derivative works to be sublicensed to others.
029: *
030: * You may use any information in intangible form that you remember after
031: * accessing the Software. However, this right does not grant you a license to
032: * any of the copyrights or patents for anything you might create using such
033: * information.
034: *
035: * In return, we simply require that you agree:
036: *
037: * Not to remove any copyright or other notices from the Software.
038: *
039: *
040: * That if you distribute the Software in source or object form, you will
041: * include a verbatim copy of this license.
042: *
043: *
044: * That if you distribute derivative works of the Software in source code form
045: * you do so only under a license that includes all of the provisions of this
046: * License, and if you distribute derivative works of the Software solely in
047: * object form you do so only under a license that complies with this License.
048: *
049: *
050: * That if you have modified the Software or created derivative works, and
051: * distribute such modifications or derivative works, you will cause the
052: * modified files to carry prominent notices so that recipients know that they
053: * are not receiving the original Software. Such notices must state: (i) that
054: * you have changed the Software; and (ii) the date of any changes.
055: *
056: *
057: * THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS,
058: * IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF
059: * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR ANY WARRANTY OF TITLE
060: * OR NON-INFRINGEMENT. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU
061: * DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
062: *
063: *
064: * THAT NEITHER ZILONIS NOR THE AUTHOR WILL BE LIABLE FOR ANY DAMAGES RELATED
065: * TO THE SOFTWARE OR THIS LICENSE, INCLUDING DIRECT, INDIRECT, SPECIAL,
066: * CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS,
067: * NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS
068: * LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR
069: * DERIVATIVE WORKS.
070: *
071: *
072: * That if you sue anyone over patents that you think may apply to the Software
073: * or anyone's use of the Software, your license to the Software ends
074: * automatically.
075: *
076: *
077: * That your rights under the License end automatically if you breach it in any
078: * way.
079: *
080: *
081: * Elie Levy reserves all rights not expressly granted to you in this
082: * license.
083: *
084: */
085:
086: import org.zilonis.symbol.Triplet;
087: import org.zilonis.network.Token;
088: import org.zilonis.network.beta.Test;
089: import org.zilonis.network.Condition;
090: import org.zilonis.scope.Scope;
091:
092: import java.util.logging.Logger;
093:
094: public class Action {
095:
096: public final static int ASSERT = 0;
097:
098: public final static int RETRACT = 1;
099:
100: public final static int EXPRESSION = 2;
101:
102: public final static int MODIFY = 4;
103:
104: private final static Logger logger = Logger
105: .getLogger("org.zilonis.network.production.Action");
106:
107: // for the single link list
108: private Action next;
109:
110: private int id;
111:
112: private static int lastid = 0;
113:
114: private Triplet triplet;
115:
116: private Triplet tripletToRemove;
117:
118: private Test bindings[];
119:
120: private Test bindingsForRemove[];
121:
122: private int type;
123:
124: private int priority;
125:
126: protected Action(int type) {
127: id = lastid++;
128: this .type = type;
129: }
130:
131: public Action(int type, Triplet triplet) {
132: id = lastid++;
133: logger.finest("***************************************");
134: logger.finest("Action type:" + type);
135: logger.finest("Action id:" + id);
136: if (type == MODIFY)
137: throw new IllegalArgumentException(
138: "Invalid constructor for modify");
139: this .type = type;
140: this .triplet = triplet;
141: }
142:
143: public Action(int type, Triplet tripletToRetract,
144: Triplet tripletToAssert) {
145: logger.finest("using modify constructor");
146: id = lastid++;
147: this .type = type;
148: this .triplet = tripletToAssert;
149: this .tripletToRemove = tripletToRetract;
150: }
151:
152: public void setEarlierConditions(Condition earlierConditions) {
153: bindings = Test.getTests(triplet, earlierConditions);
154: if (tripletToRemove != null) {
155: bindingsForRemove = Test.getTests(tripletToRemove,
156: earlierConditions);
157: }
158: }
159:
160: public void setPriority(int priority) {
161: this .priority = priority;
162: }
163:
164: public int getPriority() {
165: return priority;
166: }
167:
168: public void setNext(Action next) {
169: this .next = next;
170: }
171:
172: public Action getNext() {
173: return next;
174: }
175:
176: public boolean isType(int type) {
177: return (this .type == type);
178: }
179:
180: public int getType() {
181: return type;
182: }
183:
184: public String getActionId() {
185: return triplet.get(0);
186: }
187:
188: public Triplet buildTriplet(Scope scope, String id, Token token) {
189: logger.finest("Action id:" + this .id);
190: logger.finest("In Action: triplet = " + triplet);
191: logger.finest("Action type = " + type);
192: String result[] = new String[3];
193: result[0] = id;
194: result[1] = triplet.get(1);
195: result[2] = triplet.get(2);
196: logger.finest("building triplet with: " + bindings.length
197: + " bindings");
198: for (Test binding : bindings) {
199: logger.finest("binding.field1:" + binding.getField1());
200: logger.finest("binding.len:" + binding.getLength());
201: logger.finest("binding.field2:" + binding.getField2());
202: result[binding.getField1()] = token.getParent(
203: binding.getLength()).getWME().get(
204: binding.getField2());
205: }
206: logger.finest("returning triplet: "
207: + new Triplet(result[0], result[1], result[2]));
208: return new Triplet(result[0], result[1], result[2]);
209: }
210:
211: public Triplet buildTripletToRemove(String id, Token token) {
212: logger.finest("Action id:" + this .id);
213: logger.finest("In Action: triplet = " + triplet);
214: logger.finest("Action type = " + type);
215: String result[] = new String[3];
216: result[0] = id;
217: result[1] = tripletToRemove.get(1);
218: result[2] = tripletToRemove.get(2);
219: logger.finest("building triplet with: "
220: + bindingsForRemove.length + " bindings");
221: for (Test binding : bindingsForRemove) {
222: logger.finest("binding.field1:" + binding.getField1());
223: logger.finest("binding.len:" + binding.getLength());
224: logger.finest("binding.field2:" + binding.getField2());
225: result[binding.getField1()] = token.getParent(
226: binding.getLength()).getWME().get(
227: binding.getField2());
228: }
229: logger.finest("returning triplet: "
230: + new Triplet(result[0], result[1], result[2]));
231: return new Triplet(result[0], result[1], result[2]);
232: }
233:
234: public String toString() {
235: return "Action: type = " + type + " " + triplet
236: + ((type == 4) ? tripletToRemove : "");
237: // " with next:"+next;
238: }
239: }
|