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 java.util.HashMap;
087: import java.util.Map;
088: import java.util.Set;
089: import java.util.concurrent.ConcurrentHashMap;
090:
091: import org.zilonis.network.LeftActivatable;
092: import org.zilonis.network.Token;
093: import org.zilonis.network.alpha.AlphaMemory;
094: import org.zilonis.scope.Scope;
095: import org.zilonis.scope.ScopedLinkedList;
096: import org.zilonis.scope.ScopedMultiList;
097:
098: public class BetaMemory implements LeftActivatable, TokensHolder {
099:
100: private ConcurrentHashMap<AlphaMemory, ScopedLinkedList<LeftActivatable>> children;
101:
102: private ScopedMultiList<Token> tokens;
103:
104: public BetaMemory() {
105: children = new ConcurrentHashMap<AlphaMemory, ScopedLinkedList<LeftActivatable>>();
106: tokens = new ScopedMultiList<Token>(Token.MEMORY);
107: }
108:
109: public void addToken(Token token) {
110: addToken(Scope.ROOT, token);
111: }
112:
113: public void addToken(Scope scope, Token token) {
114: tokens.add(scope, token);
115: }
116:
117: public String toString(Scope scope) {
118: String result = "- BetaMemory < ";
119: for (ScopedLinkedList<LeftActivatable> child : children
120: .values())
121: for (LeftActivatable node : child.getIterable(scope))
122: result += node.toString(scope);
123: result += " > -\n";
124: return result;
125: }
126:
127: public void leftActivate(Scope scope, Token token) {
128: tokens.add(scope, token);
129: token.setTokensHolder(this );
130: activateChildren(scope, token);
131: }
132:
133: public void activateChildren(Scope scope, Token token) {
134: Set<Map.Entry<AlphaMemory, ScopedLinkedList<LeftActivatable>>> set = children
135: .entrySet();
136: for (Map.Entry<AlphaMemory, ScopedLinkedList<LeftActivatable>> entry : set)
137: for (LeftActivatable child : entry.getValue().getIterable(
138: scope))
139: child.leftActivate(scope, token.createClone());
140: }
141:
142: public Iterable<Token> getTokensIterable(Scope scope) {
143: return tokens.getIterable(scope);
144: }
145:
146: public Iterable<LeftActivatable> getChildrenIterable(Scope scope,
147: AlphaMemory alphaMemory) {
148:
149: ScopedLinkedList<LeftActivatable> children = this .children
150: .get(alphaMemory);
151: return (children == null) ? null : children.getIterable(scope);
152: }
153:
154: public void addChild(Scope scope, AlphaMemory alphaMemory,
155: LeftActivatable child) {
156: ScopedLinkedList<LeftActivatable> list = children
157: .get(alphaMemory);
158: if (list == null) {
159: list = new ScopedLinkedList<LeftActivatable>();
160: children.put(alphaMemory, list);
161: }
162: list.add(scope, child);
163: }
164:
165: /*
166: * this is equivalent to buildOrShareJoinNode
167: */
168: public JoinNode getJoinNode(Scope scope, AlphaMemory alphaMemory,
169: Test tests[]) {
170: Iterable<LeftActivatable> iterable = getChildrenIterable(scope,
171: alphaMemory);
172: if (iterable != null)
173: for (LeftActivatable node : iterable)
174: if (node instanceof JoinNode) {
175: JoinNode joinNode = (JoinNode) node;
176: if (joinNode.testsMatch(scope, tests))
177: return joinNode;
178: }
179: JoinNode joinNode = new JoinNode(scope, alphaMemory, this,
180: tests);
181: alphaMemory.addChild(scope, joinNode);
182: addChild(scope, alphaMemory, joinNode);
183: return joinNode;
184: }
185:
186: }
|