AbstractOPS.java :  » Rule-Engine » Mandarax » org » mandarax » zkb » Java Open Source

Java Open Source » Rule Engine » Mandarax 
Mandarax » org » mandarax » zkb » AbstractOPS.java
package org.mandarax.zkb;
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.*;
import javax.sql.DataSource;
import org.mandarax.kernel.*;

/**
 * An abstract OPS implementation.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.2
 */
public abstract class AbstractOPS implements ObjectPersistencyService {
  
  protected Map urisByObject = new IdentityHashMap();
  protected Map objectsByUri = new HashMap();
  
  // prefixes for primitives
  public static final String BOOLEAN = "boolean:";
  public static final String BYTE = "byte:";
  public static final String CHAR = "char:";
  public static final String DOUBLE = "double:";
  public static final String FLOAT = "float:";
  public static final String INT = "int:";
  public static final String LONG = "long:";
  public static final String SHORT = "short:";
  public static final String STRING = "string:";
  public static final String OBJECT = "object:java:";

  /**
   * Get the extension of the associated files.
   * @return a file extension
   */
  public abstract String getExtension() ;
  /**
   * Generate an URI for an object.
   * @param obj an object
   * @return an URI
   */
  public synchronized String createName(Object obj) {
    if (obj instanceof Integer) return INT+String.valueOf(obj);
    else if (obj instanceof String) return STRING+obj;
    else if (obj instanceof Boolean) return BOOLEAN+String.valueOf(obj);
    else if (obj instanceof Character) return CHAR+String.valueOf(obj);
    else if (obj instanceof Double) return DOUBLE+String.valueOf(obj);
    else if (obj instanceof Float) return FLOAT+String.valueOf(obj);
    else if (obj instanceof Long) return LONG+String.valueOf(obj);
    else if (obj instanceof Short) return SHORT+String.valueOf(obj);
    else if (obj instanceof Byte) return BYTE+String.valueOf(obj);
    else {
      // old code (prior to 3.2)
      /*
      StringBuffer buf = new StringBuffer();
      buf.append(OBJECT);
      buf.append(getExtension());
      buf.append(":");
      String type = getType(obj);
      buf.append(type);
      buf.append("/id=");
      buf.append(getCounter(type));
      return buf.toString();
      */
      // changes in 3.2: there have been some problems with inconsistent IDs
      // we cannot reproduce them, but for now use less descriptive ids which are 
      // unique
      return new StringBuffer()
        .append(OBJECT)
        .append(getType(obj))
        .append("/id=")
        .append(new java.rmi.server.UID())
        .toString();
    }
  }
  /**
   * Get the type (name) of an object.
   * @param obj an object
   * @return the type name 
   */
  private String getType(Object obj) {
    if (obj instanceof Predicate) return "predicate";
    if (obj instanceof Function) return "function";
    if (obj instanceof ConstantTerm) return "constant_term";
    if (obj instanceof ComplexTerm) return "complex_term";
    if (obj instanceof VariableTerm) return "variable_term";
    if (obj instanceof Prerequisite) return "prerequisite";
    if (obj instanceof Fact) return"fact";
    if (obj instanceof Rule) return "rule";
    if (obj instanceof Query) return"query";
    if (obj instanceof ClauseSet) return "clause set";
    if (obj instanceof DataSource) return "data source";
        
    return obj==null?"null":obj.getClass().getName();
  }
  /**
   * Get the class (name) of an object.
   * We return some top-level superclasses in order to improve readability
   * of the files.
   * @param obj an object
   * @return the class 
   */
  private Class getClass(Object obj) {
    if (obj instanceof Predicate) return Predicate.class;
    if (obj instanceof Function) return Function.class;
    if (obj instanceof ConstantTerm) return ConstantTerm.class;
    if (obj instanceof ComplexTerm) return ComplexTerm.class;
    if (obj instanceof VariableTerm) return VariableTerm.class;
    if (obj instanceof Prerequisite) return Prerequisite.class;
    if (obj instanceof Fact) return Fact.class;
    if (obj instanceof Rule) return Rule.class;
    if (obj instanceof ClauseSet) return ClauseSet.class;
    if (obj instanceof DataSource) return DataSource.class;
        
    return obj==null?null:obj.getClass();
  }
  /**
   * Bind an object to a name (uri).
   * @param uri an uri
   * @param obj an object
   */
  public synchronized void bind(String uri,Object obj) {
    if (!isPrimitive(obj)) {
      objectsByUri.put(uri,obj);
      urisByObject.put(obj,uri);
    }
  }
  /**
   * Indicates whether an object is primitive.
   * @param obj an object.
   * @return true if the object is a primitive wrapper or a string
   */
  private boolean isPrimitive(Object obj) {
    return obj instanceof Number || obj instanceof String || obj instanceof Boolean || obj instanceof Character;
  } 
  /**
   * Find an object. If the object is not yet registered, bind it
   * using the generated name.
   * @param obj an object
   */
  public synchronized String findOrBind(Object obj) {
    String uri = null;
    if ((uri=(String)urisByObject.get(obj))==null) {
      uri = createName(obj);
      bind(uri,obj);
    }
    return uri;
  }
  /**
   * Unbind the name (uri).
   * @param uri an uri
   */
  public synchronized void unbind(String uri) {
    Object obj = lookup(uri);
    if (obj!=null) urisByObject.remove(obj);
    objectsByUri.remove(uri);
  }
  /**
   * Get an object by name.
   * @param uri an uri
   * @return obj an object
   */
  public synchronized Object lookup(String uri){
    if (uri.startsWith(OBJECT)) {
      // object - proper look up
      return objectsByUri.get(uri);
    }
    else {
      // primitive - decode
      if (uri.startsWith(INT)) return Integer.valueOf(uri.substring(INT.length()));
      else if (uri.startsWith(STRING)) return uri.substring(STRING.length());
      else if (uri.startsWith(BOOLEAN)) return Boolean.valueOf(uri.substring(BOOLEAN.length()));
      else if (uri.startsWith(CHAR)) return new Character(uri.substring(CHAR.length()).charAt(0));
      else if (uri.startsWith(DOUBLE)) return Double.valueOf(uri.substring(DOUBLE.length()));
      else if (uri.startsWith(FLOAT)) return Float.valueOf(uri.substring(FLOAT.length()));
      else if (uri.startsWith(LONG)) return Long.valueOf(uri.substring(LONG.length()));
      else if (uri.startsWith(SHORT)) return Short.valueOf(uri.substring(SHORT.length()));
      else if (uri.startsWith(BYTE)) return Byte.valueOf(uri.substring(BYTE.length()));
      else return null;
      
    }
  }
  
  /**
   * Reverse a map (flip keys and values).
   * @param originalMap the map to be reversed
   * @param reversedMap an empty map
   */
  protected synchronized void reverseMap(Map originalMap,Map reversedMap) {
      for (Iterator iter = originalMap.entrySet().iterator();iter.hasNext();) {
        Map.Entry nextEntry = (Map.Entry)iter.next();
        reversedMap.put(nextEntry.getValue(),nextEntry.getKey()); 
      }
  }
  /**
   * Reset the state.
   */
  public synchronized void reset() {
    urisByObject.clear();
    objectsByUri.clear();
  }
}
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.