Package anvil.core.brain

Source Code of anvil.core.brain.AnyBrain

/*
* $Id: AnyBrain.java,v 1.6 2002/09/16 08:05:02 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.brain;

import java.io.IOException;
import java.io.Writer;
import anvil.java.util.BindingEnumeration;
import anvil.core.Any;
import anvil.core.AnyList;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyBindingEnumeration;
import anvil.core.IndexedEnumeration;
import anvil.script.Context;
import anvil.script.Function;
import anvil.core.Serializer;
import anvil.core.Unserializer;
import anvil.core.UnserializationException;

import anvil.brain.Brain;
import anvil.brain.Synapse;
import anvil.brain.Dimension;
import anvil.server.OperationFailedException;

/// @class Brain
/// Brain is set of synapses. Each synapse has id, type and name
/// property.

/**
* class AnyBrain
*
* @author: Jani Lehtim�ki
*/
public class AnyBrain extends AnyAbstractClass
{

  /// @constructor Brain
  /// Creates a new brain.
  /// @param poolName Database pool to use
  /// @param cacheSize Cache size
  public static final Object[] newInstance = new Object[] { "context", "poolname", "cacheSize" };
  public static final Any newInstance(Context context, String poolname, int cachesize)
  {
    Brain brain = new Brain(context.address().getZone(), poolname, cachesize);
    return new AnyBrain(brain);
  }
 

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Brain", AnyBrain.class,
    //DOC{{
    ""+
      " @class Brain \n" +
      " Brain is set of synapses. Each synapse has id, type and name\n" +
      " property.\n" +
      " @constructor Brain\n" +
      " Creates a new brain.\n" +
      " @param poolName Database pool to use\n" +
      " @param cacheSize Cache size\n" +
      " @method get\n" +
      " Gets a synapse with given id.\n" +
      " @synopsis Synapse get(int id)\n" +
      " @throws OperationFailed If synapse was not found\n" +
      " @method create\n" +
      " Creates new synapse with given type and name.\n" +
      " @synopsis Synapse create(string type, string name)\n" +
      " @throws OperationFailed If an error occurred\n" +
      " @method commit\n" +
      " Commits all changes.\n" +
      " @synopsis Brain commit()\n" +
      " @throws OperationFailed If an error occurred\n" +
      " @method find\n" +
      " Return list of synapses matching given type and/or name.\n" +
      " @synopsis list find(string type)\n" +
      " @synopsis list find(string type, string name)\n" +
      " @throws OperationFailed If an error occurred\n"
    //}}DOC
    );
  static {
    BrainModule.class.getName();
  }
   
  private Brain _brain;

 
  public AnyBrain(Brain brain)
  {
    _brain = brain;
  }
 
 
  public final anvil.script.ClassType classOf() {
    return __class__;
  }

 
  public Object toObject()
  {
    return _brain;
  }
 


  public Any getReference(Context context, Any index)
  {
    try {
      Synapse syn = _brain.get(index.toLong());
      return new AnySynapse(syn);
    } catch (OperationFailedException e) {
      throw context.exception(e);
    }
  }
 
 

  public Any checkReference(Context context, Any index)
  {
    return getReference(context, index);
  }


  /// @method get
  /// Gets a synapse with given id.
  /// @synopsis Synapse get(int id)
  /// @throws OperationFailed If synapse was not found
  public Any m_get(Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "get");
    }
    try {
      Synapse syn = _brain.get(parameters[0].toLong());
      return new AnySynapse(syn);
    } catch (OperationFailedException e) {
      throw context.exception(e);
    }
  }



  /// @method create
  /// Creates new synapse with given type and name.
  /// @synopsis Synapse create(string type, string name)
  /// @throws OperationFailed If an error occurred
  public Any m_create(Context context, Any[] parameters)
  {
    if (parameters.length < 2) {
      throw parametersMissing(context, "create");
    }
    try {
      Synapse syn = _brain.create(parameters[0].toString(), parameters[1].toString());
      return new AnySynapse(syn);
    } catch (OperationFailedException e) {
      throw context.exception(e);
    }
  }


  /// @method commit
  /// Commits all changes.
  /// @synopsis Brain commit()
  /// @throws OperationFailed If an error occurred
  public Any m_commit(Context context, Any[] parameters)
  {
    try {
      _brain.commit();
      return this;
    } catch (OperationFailedException e) {
      throw context.exception(e);
    }
  }
 

  /// @method find
  /// Return list of synapses matching given type and/or name.
  /// @synopsis list find(string type)
  /// @synopsis list find(string type, string name)
  /// @throws OperationFailed If an error occurred
  public Any m_find(Context context, Any[] parameters)
  {
    int n = parameters.length;
    if (n<1) {
      throw parametersMissing(context, "find");
    }
    try {
      Synapse[] result;
      if (n == 1) {
        result = _brain.find(parameters[0].toString(), null);
      } else {
        result = _brain.find(parameters[0].toString(), parameters[1].toString());
      }
      n = result.length;
      Any[] list = new Any[n];
      for(int i=0; i<n; i++) {
        list[i] = new AnySynapse(result[i]);
      }
      return new AnyList(list);
    } catch (OperationFailedException e) {
      throw context.exception(e);
    }
  }
 

 
 
}
TOP

Related Classes of anvil.core.brain.AnyBrain

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.