001: package org.zilonis.network.beta;
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 static java.util.logging.Level.FINEST;
087: import static org.zilonis.network.Condition.POSITIVE_CONDITION;
088:
089: import java.util.LinkedList;
090: import java.util.logging.Logger;
091:
092: import org.zilonis.network.Condition;
093: import org.zilonis.network.Token;
094: import org.zilonis.network.WME;
095: import org.zilonis.symbol.Triplet;
096:
097: public class Test {
098:
099: private int field1;
100:
101: private int length;
102:
103: private int field2;
104:
105: private static final Logger logger = Logger
106: .getLogger("org.zilonis.network.beta.Test");
107:
108: public static Test[] getTests(Triplet condition,
109: Condition earlierConditions) {
110: LinkedList<Test> result = new LinkedList<Test>();
111: for (int field = 0; field < 3; ++field) {
112: String symbol = condition.get(field);
113: logger.finest("symbol = " + symbol + " is variable? "
114: + condition.isVariable(field));
115: if (condition.isVariable(field)) {
116: Test test = getTestFor(symbol, field, earlierConditions);
117: logger.finest("and the test for it = " + test);
118: if (test != null)
119: result.add(test);
120: }
121: }
122: return result.toArray(new Test[result.size()]);
123: }
124:
125: public static Test[] getTests(Triplet condition,
126: LinkedList<Triplet> earlierConditions) {
127: LinkedList<Test> result = new LinkedList<Test>();
128: for (int field = 0; field < 3; ++field) {
129: String symbol = condition.get(field);
130: if (condition.isVariable(field)) {
131: Test test = getTestFor(symbol, field, earlierConditions);
132: if (test != null)
133: result.add(test);
134: }
135: }
136: return result.toArray(new Test[result.size()]);
137: }
138:
139: public int getField1() {
140: return field1;
141: }
142:
143: public int getLength() {
144: return length;
145: }
146:
147: public int getField2() {
148: return field2;
149: }
150:
151: private static Test getTestFor(String symbol, int field,
152: Condition earlierConditions) {
153: int pos = 0;
154: for (; earlierConditions != null; earlierConditions = earlierConditions
155: .getPrev()) {
156: logger.finest("**** symbol = >>" + symbol + "<<<");
157: if (earlierConditions.isType(POSITIVE_CONDITION)) {
158: Triplet triplet = earlierConditions.getTriplet();
159: logger.finest("looking in:" + triplet);
160: for (int i = 0; i < 3; ++i) {
161: if (logger.isLoggable(FINEST))
162: if (triplet.get(i).equals(symbol))
163: logger.finest("******* match "
164: + (triplet.get(i) == symbol));
165: if (triplet.get(i) == symbol) {
166: return new Test(field, pos, i);
167: }
168: }
169: }
170: pos++;
171: }
172: return null;
173: }
174:
175: private static Test getTestFor(String symbol, int field,
176: LinkedList<Triplet> earlierConditions) {
177: int pos = 0;
178: for (Triplet triplet : earlierConditions) {
179: for (int i = 0; i < 3; ++i) {
180: if (triplet.get(i) == symbol) {
181: return new Test(field, pos, i);
182: }
183: }
184: pos++;
185: }
186: return null;
187: }
188:
189: private Test(int field1, int length, int field2) {
190: this .field1 = field1;
191: this .length = length;
192: this .field2 = field2;
193: }
194:
195: public boolean match(WME wme, Token token) {
196: for (int pos = length; (token != null) && (pos > 0); --pos)
197: token = token.getParent();
198: if ((token == null) || (token.getWME() == null))
199: return false;
200: return (wme.get(field1) == token.getWME().get(field2));
201: }
202:
203: public boolean equals(Object object) {
204: if (!(object instanceof Test))
205: return false;
206: Test test = (Test) object;
207: return ((test.field1 == field1) && (test.length == length) && (test.field2 == field2));
208: }
209:
210: public int hashCode() {
211: return field1 + field2 + length;
212: }
213:
214: public String toString() {
215: return "Test:(" + field1 + "," + length + "," + field2 + ")";
216: }
217: }
|