AlphaNetwork.java :  » Rule-Engine » zilonis » org » zilonis » network » alpha » Java Open Source

Java Open Source » Rule Engine » zilonis 
zilonis » org » zilonis » network » alpha » AlphaNetwork.java
package org.zilonis.network.alpha;

/**
 * Copyright (c) 2005 Elie Levy <elie.levy@zilonis.org>
 * All rights reserved
 * 
 * This License governs use of the accompanying Software, and your use of the
 * Software constitutes acceptance of this license.
 * 
 * You may use this Software for any non-commercial purpose, subject to the
 * restrictions in this license. Some purposes which can be non-commercial are
 * teaching, academic research, and personal experimentation. You may also
 * distribute this Software with books or other teaching materials, or publish
 * the Software on websites, that are intended to teach the use of the 
 * Software.
 * 
 * 
 * You may not use or distribute this Software or any derivative works in any
 * form for commercial purposes. Examples of commercial purposes would be
 * running business operations, licensing, leasing, or selling the Software, or
 * distributing the Software for use with commercial products.
 * 
 * You may modify this Software and distribute the modified Software for
 * non-commercial purposes, however, you may not grant rights to the Software 
 * or derivative works that are broader than those provided by this License. 
 * For example, you may not distribute modifications of the Software under
 * terms that would permit commercial use, or under terms that purport to 
 * require the Software or derivative works to be sublicensed to others.
 * 
 * You may use any information in intangible form that you remember after
 * accessing the Software. However, this right does not grant you a license to
 * any of the copyrights or patents for anything you might create using such
 * information.
 * 
 * In return, we simply require that you agree:
 * 
 * Not to remove any copyright or other notices from the Software.
 * 
 * 
 * That if you distribute the Software in source or object form, you will
 * include a verbatim copy of this license.
 * 
 * 
 * That if you distribute derivative works of the Software in source code form
 * you do so only under a license that includes all of the provisions of this
 * License, and if you distribute derivative works of the Software solely in
 * object form you do so only under a license that complies with this License.
 * 
 * 
 * That if you have modified the Software or created derivative works, and
 * distribute such modifications or derivative works, you will cause the
 * modified files to carry prominent notices so that recipients know that they
 * are not receiving the original Software. Such notices must state: (i) that
 * you have changed the Software; and (ii) the date of any changes.
 * 
 * 
 * THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS,
 * IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR ANY WARRANTY OF TITLE
 * OR NON-INFRINGEMENT. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU
 * DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
 * 
 * 
 * THAT NEITHER ZILONIS NOR THE AUTHOR WILL BE LIABLE FOR ANY DAMAGES RELATED
 * TO THE SOFTWARE OR THIS LICENSE, INCLUDING DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS,
 * NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS
 * LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR 
 * DERIVATIVE WORKS.
 * 
 * 
 * That if you sue anyone over patents that you think may apply to the Software
 * or anyone's use of the Software, your license to the Software ends
 * automatically.
 * 
 * 
 * That your rights under the License end automatically if you breach it in any
 * way.
 * 
 * 
 * Elie Levy reserves all rights not expressly granted to you in this 
 * license.
 *  
 */

import static org.zilonis.symbol.Triplet.ATTRIBUTE;
import static org.zilonis.symbol.Triplet.IDENTIFIER;
import static org.zilonis.symbol.Triplet.VALUE;

import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import org.zilonis.network.WME;
import org.zilonis.scope.Scope;
import org.zilonis.symbol.Triplet;

public class AlphaNetwork {

  private ConcurrentHashMap<String, AlphaMemory> alphaNetwork;

  public AlphaNetwork() {
    alphaNetwork = new ConcurrentHashMap<String, AlphaMemory>();
  }

  public void addWME(Scope scope, WME wme) {
    for (int i = 0; i < 8; ++i) {
      AlphaMemory alphaMemory = alphaNetwork.get(getKey(i, wme
          .getTriplet()));
      if (alphaMemory != null)
        alphaMemory.rightActivate(scope, wme);
    }
  }

  public AlphaMemory getAlphaMemory(Scope scope, Triplet condition) {
    String alphaKey = getKey(condition);
    AlphaMemory alphaMemory = alphaNetwork.get(alphaKey);    
    if (alphaMemory == null) {
      alphaMemory = new AlphaMemory(condition);
      alphaNetwork.put(alphaKey, alphaMemory);
      initializeAlphaMemory(scope, alphaMemory, condition);
    }
    return alphaMemory;
  }

  public void initializeAlphaMemory(Scope scope, AlphaMemory alphaMemory,
      Triplet condition) {
    AlphaMemory moreGeneralAlphaMemory = null;
    if (!condition.isVariable(Triplet.IDENTIFIER)) {
      Triplet moreGeneralCondition = new Triplet("?c", condition
          .get(Triplet.ATTRIBUTE), condition.get(Triplet.VALUE));
      moreGeneralAlphaMemory = getAlphaMemory(scope, moreGeneralCondition);
    }
    if ((moreGeneralAlphaMemory == null)
        && (!condition.isVariable(Triplet.VALUE))) {
      Triplet moreGeneralCondition = new Triplet("?c", condition
          .get(Triplet.ATTRIBUTE), "?v");
      moreGeneralAlphaMemory = getAlphaMemory(scope, moreGeneralCondition);
    }
    Iterator<WME> iterator = (moreGeneralAlphaMemory != null) ? moreGeneralAlphaMemory
        .getWMEIterable(scope).iterator()
        : scope.getWMEIterator();
    while (iterator.hasNext()) {
      WME wme = iterator.next();
      if (wme.getTriplet().match(condition))
        alphaMemory.rightActivate(scope, wme);
    }
  }

  public String getKey(Triplet condition) {
    return (condition.isVariable(IDENTIFIER) ? "*" : condition
        .get(IDENTIFIER))
        + "_"
        + (condition.isVariable(ATTRIBUTE) ? "*" : condition
            .get(ATTRIBUTE))
        + "_"
        + (condition.isVariable(VALUE) ? "*" : condition.get(VALUE));
  }

  public String getKey(int i, Triplet wme) {
    switch (i) {
    case 0:
      return wme.get(IDENTIFIER) + "_" + wme.get(ATTRIBUTE) + "_"
          + wme.get(VALUE);
    case 1:
      return wme.get(IDENTIFIER) + "_" + wme.get(ATTRIBUTE) + "_*";
    case 2:
      return wme.get(IDENTIFIER) + "_*_" + wme.get(VALUE);
    case 3:
      return wme.get(IDENTIFIER) + "_*_*";
    case 4:
      return "*_" + wme.get(ATTRIBUTE) + "_" + wme.get(VALUE);
    case 5:
      return "*_" + wme.get(ATTRIBUTE) + "_*";
    case 6:
      return "*_*_" + wme.get(VALUE);
    case 7:
      return "*_*_*";
    }
    return "";
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.