/*
* $Id: AnyDimension.java,v 1.8 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.util.Enumeration;
import anvil.core.Any;
import anvil.core.AnyList;
import anvil.core.AnyAbstractClass;
import anvil.core.IndexedEnumeration;
import anvil.script.Context;
import anvil.java.util.BindingEnumeration;
import anvil.brain.Synapse;
import anvil.brain.Dimension;
import anvil.server.OperationFailedException;
/// @class Dimension
/// Dimension is ordered list of synapses.
/// @reference "object <b>Dimension</b>[int <i>index</i>]"
/// Gets the synapse at given index.
/// @reference "<b>delete Dimension</b>[int <i>index</i>]"
/// Deletes the synapse at given index.
/// @reference "<b>Dimension</b>[int <i>index</i>] = <i>value</i>"
/// Sets the synapse at given index.
/**
* class AnyDimension
*
* @author: Jani Lehtim�ki
*/
public class AnyDimension extends AnyAbstractClass
{
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Dimension", AnyDimension.class,
//DOC{{
""+
" @class Dimension\n" +
" Dimension is ordered list of synapses.\n" +
" @reference \"object <b>Dimension</b>[int <i>index</i>]\"\n" +
" Gets the synapse at given index.\n" +
" @reference \"<b>delete Dimension</b>[int <i>index</i>]\"\n" +
" Deletes the synapse at given index.\n" +
" @reference \"<b>Dimension</b>[int <i>index</i>] = <i>value</i>\"\n" +
" Sets the synapse at given index.\n" +
" @method getSynapse\n" +
" Returns the the synapse where this dimension is attached to.\n" +
" @synopsis Synapse getSynapse()\n" +
" @method getName\n" +
" Returns the name of this dimension.\n" +
" @synopsis string getName()\n" +
" @method clear\n" +
" Clears this dimension.\n" +
" @synopsis Dimension clear()\n" +
" @method insert\n" +
" Inserts a new synapse to this dimension before the given index.\n" +
" @synopsis Dimension insert(int index, Synapse synapse)\n" +
" @method add\n" +
" Adds a new synapse to this dimension.\n" +
" @synopsis Dimension add(Synapse synapse)\n" +
" @method remove\n" +
" Removes the synapse at given index.\n" +
" @synopsis Dimension remove(int index)\n" +
" @method get\n" +
" Gets the synapse at given index.\n" +
" @synopsis Synapse get(int index)\n" +
" @method set\n" +
" Sets the synapse at given index.\n" +
" @synopsis Dimension set(int index, Synapse synapse)\n" +
" @method find\n" +
" Finds all the synapses from this dimension having \n" +
" given attribute mapping.\n" +
" @synopsis list find(string key, object value)\n" +
" @throws OperationFailed If an error occurred\n"
//}}DOC
);
static {
BrainModule.class.getName();
}
private Dimension _dim;
public AnyDimension(Dimension dim)
{
_dim = dim;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public int sizeOf()
{
return _dim.size();
}
public Object toObject()
{
return _dim;
}
public String toString()
{
return _dim.toString();
}
public Any getReference(Context context, Any index)
{
Synapse syn = _dim.get(index.toInt());
return (syn != null) ? new AnySynapse(syn) : UNDEFINED;
}
public Any setReference(Context context, Any index, Any value)
{
if (value instanceof AnySynapse) {
_dim.set(index.toInt(), (Synapse)value.toObject());
return value;
} else {
throw context.BadParameter("Dimension may only contain anvil.brain.Synapse instances");
}
}
public Any setReference(Context context, Any value)
{
if (value instanceof AnySynapse) {
_dim.add((Synapse)value.toObject());
return value;
} else {
throw context.BadParameter("Dimension may only contain anvil.brain.Synapse instances");
}
}
public boolean deleteReference(Context context, Any index)
{
if (index instanceof AnySynapse) {
Synapse synapse = (Synapse)index.toObject();
_dim.remove(synapse);
return true;
} else {
return _dim.remove(index.toInt()) != null;
}
}
public BindingEnumeration enumeration()
{
return new IndexedEnumeration(_dim.elements());
}
/// @method getSynapse
/// Returns the the synapse where this dimension is attached to.
/// @synopsis Synapse getSynapse()
public Any m_getSynapse(Context context, Any[] parameters)
{
return new AnySynapse(_dim.getSynapse());
}
/// @method getName
/// Returns the name of this dimension.
/// @synopsis string getName()
public Any m_getName(Context context, Any[] parameters)
{
return Any.create(_dim.getName());
}
/// @method clear
/// Clears this dimension.
/// @synopsis Dimension clear()
public Any m_clear(Context context, Any[] parameters)
{
_dim.clear();
return this;
}
/// @method insert
/// Inserts a new synapse to this dimension before the given index.
/// @synopsis Dimension insert(int index, Synapse synapse)
public Any m_insert(Context context, Any[] parameters)
{
if (parameters.length<2) {
throw parametersMissing(context, "insert");
}
Any value = parameters[1];
if (value instanceof AnySynapse) {
_dim.insert(parameters[0].toInt(), (Synapse)value.toObject());
return this;
} else {
throw context.BadParameter("Dimension may only contain anvil.brain.Synapse instances");
}
}
/// @method add
/// Adds a new synapse to this dimension.
/// @synopsis Dimension add(Synapse synapse)
public Any m_add(Context context, Any[] parameters)
{
if (parameters.length<1) {
throw parametersMissing(context, "add");
}
Any value = parameters[0];
if (value instanceof AnySynapse) {
_dim.add((Synapse)value.toObject());
return this;
} else {
throw context.BadParameter("Dimension may only contain anvil.brain.Synapse instances");
}
}
/// @method remove
/// Removes the synapse at given index.
/// @synopsis Dimension remove(int index)
public Any m_remove(Context context, Any[] parameters)
{
if (parameters.length<1) {
throw parametersMissing(context, "remove");
}
Any index = parameters[0];
if (index instanceof AnySynapse) {
Synapse synapse = (Synapse)index.toObject();
_dim.remove(synapse);
} else {
_dim.remove(index.toInt());
}
return this;
}
/// @method get
/// Gets the synapse at given index.
/// @synopsis Synapse get(int index)
public Any m_get(Context context, Any[] parameters)
{
if (parameters.length<1) {
throw parametersMissing(context, "get");
}
Synapse syn = _dim.get(parameters[0].toInt());
return syn != null ? new AnySynapse(syn) : UNDEFINED;
}
/// @method set
/// Sets the synapse at given index.
/// @synopsis Dimension set(int index, Synapse synapse)
public Any m_set(Context context, Any[] parameters)
{
if (parameters.length<2) {
throw parametersMissing(context, "set");
}
Any value = parameters[1];
if (value instanceof AnySynapse) {
_dim.set(parameters[0].toInt(), (Synapse)value.toObject());
return this;
} else {
throw context.BadParameter("Dimension may only contain anvil.brain.Synapse instances");
}
}
/// @method find
/// Finds all the synapses from this dimension having
/// given attribute mapping.
/// @synopsis list find(string key, object value)
/// @throws OperationFailed If an error occurred
public Any m_find(Context context, Any[] parameters)
{
if (parameters.length<2) {
throw parametersMissing(context, "find");
}
String name = parameters[0].toString();
Any value = parameters[1];
try {
AnyList list = new AnyList();
Enumeration e = _dim.elements();
while(e.hasMoreElements()) {
Synapse synapse = (Synapse)e.nextElement();
Any cand = synapse.checkVariable(name);
if (cand.isDefined()) {
if (cand.equals(value)) {
list.append(new AnySynapse(synapse));
}
}
}
return list;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
}