/*
* $Id: AnySynapse.java,v 1.9 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.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.Synapse;
import anvil.brain.Dimension;
import anvil.server.OperationFailedException;
/// @class Synapse
/// Synapse is object that contains abritrary attributes and
/// dimensions which are ordered lists linking this synapse to
/// another synapses.
/// @attribute name
/// @synopsis object <b>Synapse</b>.<i>name</i> ;
/// Returns attribute given given name
/// @synopsis object <b>Synapse</b>.<i>name</i> = <i>value</i> ;
/// Sets the attribute given attribute to given value.
/// @synopsis <b>delete Synapse</b>.<i>name</i> ;
/// Removes attribute with given name.
/// @reference name
/// @synopsis object <b>Synapse</b>[<i>name</i>] ;
/// Returns attribute given given name
/// @synopsis object <b>Synapse</b>[<i>name</i>] = <i>value</i> ;
/// Sets the attribute given attribute to given value.
/// @synopsis <b>delete Synapse</b>{<i>name</i>] ;
/// Removes attribute with given name.
/// @operator enumeration
/// Returns enumeration from attributes.
/// @synopsis enumeration * <b>Synapse</b>
/**
* class AnySynapse
*
* @author: Jani Lehtim�ki
*/
public class AnySynapse extends AnyAbstractClass
{
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Synapse", AnySynapse.class,
//DOC{{
""+
" @class Synapse\n" +
" Synapse is object that contains abritrary attributes and \n" +
" dimensions which are ordered lists linking this synapse to\n" +
" another synapses.\n" +
" @attribute name\n" +
" @synopsis object <b>Synapse</b>.<i>name</i> ;\n" +
" Returns attribute given given name\n" +
" @synopsis object <b>Synapse</b>.<i>name</i> = <i>value</i> ;\n" +
" Sets the attribute given attribute to given value.\n" +
" @synopsis <b>delete Synapse</b>.<i>name</i> ;\n" +
" Removes attribute with given name.\n" +
" @reference name\n" +
" @synopsis object <b>Synapse</b>[<i>name</i>] ;\n" +
" Returns attribute given given name\n" +
" @synopsis object <b>Synapse</b>[<i>name</i>] = <i>value</i> ;\n" +
" Sets the attribute given attribute to given value.\n" +
" @synopsis <b>delete Synapse</b>{<i>name</i>] ;\n" +
" Removes attribute with given name.\n" +
" @operator enumeration\n" +
" Returns enumeration from attributes.\n" +
" @synopsis enumeration * <b>Synapse</b>\n" +
" @method getID\n" +
" Returns the id of synapse.\n" +
" @synopsis int getID()\n" +
" @method getName\n" +
" Returns the name of synapse.\n" +
" @synopsis string getName()\n" +
" @method getType\n" +
" Returns the type of synapse.\n" +
" @synopsis string getType()\n" +
" @method setName\n" +
" Sets the name of synapse.\n" +
" @synopsis Dimension setName(string name)\n" +
" @method setType\n" +
" Sets the type of synapse.\n" +
" @synopsis Dimension setType(string name)\n" +
" @throws OperationFailed If an error occurred\n" +
" @method create\n" +
" Creates a new dimension.\n" +
" @synopsis Dimension create(string name)\n" +
" @throws OperationFailed If an error occurred\n" +
" @method dim\n" +
" Gets or creates dimension.\n" +
" @synopsis Dimension dim(string name)\n" +
" @throws OperationFailed If an error occurred\n" +
" @method get\n" +
" Gets a dimension with given name.\n" +
" @synopsis Dimension get(string name)\n" +
" @throws OperationFailed If an error occurred\n" +
" @method set\n" +
" Sets an attribute.\n" +
" @synopsis Synapse set(string name, object value)\n" +
" @throws OperationFailed If an error occurred\n" +
" @method remove\n" +
" Removes a dimension.\n" +
" @synopsis void remove()\n" +
" @synopsis Synapse remove(string name)\n" +
" @throws OperationFailed If an error occurred\n" +
" @method dims\n" +
" Returns a enumeration of dimensions.\n" +
" @synopsis enumeration dims()\n" +
" @throws OperationFailed If an error occurred\n" +
" @method commit\n" +
" Commits changes.\n" +
" @synopsis Synapse commit()\n" +
" @throws OperationFailed If an error occurred\n"
//}}DOC
);
static {
BrainModule.class.getName();
}
private Synapse _synapse;
public AnySynapse(Synapse synapse)
{
_synapse = synapse;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public String toString()
{
return _synapse.toString();
}
public Object toObject()
{
return _synapse;
}
public Any getAttribute(Context context, String attribute)
{
try {
return _synapse.getVariable(attribute);
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
public Any setAttribute(Context context, String attribute, Any value)
{
try {
_synapse.setVariable(attribute, value);
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
public Any checkAttribute(Context context, String attribute)
{
try {
return _synapse.checkVariable(attribute);
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
public boolean deleteAttribute(Context context, String attribute)
{
try {
return _synapse.deleteVariable(attribute);
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
public Any getReference(Context context, Any index)
{
return getAttribute(context, index.toString());
}
public Any setReference(Context context, Any index, Any value)
{
return setAttribute(context, index.toString(), value);
}
public Any setReference(Context context, Any value)
{
return this;
}
public Any checkReference(Context context, Any index)
{
return checkAttribute(context, index.toString());
}
public boolean deleteReference(Context context, Any index)
{
return deleteAttribute(context, index.toString());
}
public BindingEnumeration enumeration()
{
try {
return _synapse.getVariables();
} catch (OperationFailedException e) {
throw Context.getInstance().exception(e);
}
}
/// @method getID
/// Returns the id of synapse.
/// @synopsis int getID()
public Any m_getID(Context context, Any[] parameters)
{
return Any.create(_synapse.getId());
}
/// @method getName
/// Returns the name of synapse.
/// @synopsis string getName()
public Any m_getName(Context context, Any[] parameters)
{
return Any.create(_synapse.getName());
}
/// @method getType
/// Returns the type of synapse.
/// @synopsis string getType()
public Any m_getType(Context context, Any[] parameters)
{
return Any.create(_synapse.getType());
}
/// @method setName
/// Sets the name of synapse.
/// @synopsis Dimension setName(string name)
public Any m_setName(Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setName");
}
try {
_synapse.setName(parameters[0].toString());
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method setType
/// Sets the type of synapse.
/// @synopsis Dimension setType(string name)
/// @throws OperationFailed If an error occurred
public Any m_setType(Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setType");
}
try {
_synapse.setType(parameters[0].toString());
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method create
/// Creates a new dimension.
/// @synopsis Dimension create(string name)
/// @throws OperationFailed If an error occurred
public Any m_create(Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "create");
}
try {
Dimension dim = _synapse.createDimension(parameters[0].toString());
return (dim != null) ? new AnyDimension(dim) : UNDEFINED;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method dim
/// Gets or creates dimension.
/// @synopsis Dimension dim(string name)
/// @throws OperationFailed If an error occurred
public Any m_dim(Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "dim");
}
try {
Dimension dim = _synapse.getOrCreateDimension(parameters[0].toString());
return (dim != null) ? new AnyDimension(dim) : UNDEFINED;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method get
/// Gets a dimension with given name.
/// @synopsis Dimension get(string name)
/// @throws OperationFailed If an error occurred
public Any m_get(Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "get");
}
try {
Dimension dim = _synapse.getDimension(parameters[0].toString());
return (dim != null) ? new AnyDimension(dim) : UNDEFINED;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method set
/// Sets an attribute.
/// @synopsis Synapse set(string name, object value)
/// @throws OperationFailed If an error occurred
public Any m_set(Context context, Any[] parameters)
{
if (parameters.length < 2) {
throw parametersMissing(context, "set");
}
try {
_synapse.setVariable(parameters[0].toString(), parameters[1]);
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method remove
/// Removes a dimension.
/// @synopsis void remove()
/// @synopsis Synapse remove(string name)
/// @throws OperationFailed If an error occurred
public Any m_remove(Context context, Any[] parameters)
{
try {
if (parameters.length == 0) {
_synapse.remove();
return NULL;
} else {
_synapse.removeDimension(parameters[0].toString());
return this;
}
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method dims
/// Returns a enumeration of dimensions.
/// @synopsis enumeration dims()
/// @throws OperationFailed If an error occurred
public Any m_dims(Context context, Any[] parameters)
{
try {
return new AnyBindingEnumeration(
new IndexedEnumeration(_synapse.getDimensions()));
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/* @method links
@synopsis enumeration links()
public Any m _links(Context context, Any[] parameters)
{
final Dimension[] dim = _synapse.getReferers();
return new AnyBindingEnumeration(new BindingEnumeration()
{
int _index = 0;
public boolean hasMoreElements()
{
return _index < dim.length;
}
public Object nextKey()
{
return Any.create(_index);
}
public Object nextElement()
{
return new AnyDimension(dim[_index++]);
}
}
);
}*/
/// @method commit
/// Commits changes.
/// @synopsis Synapse commit()
/// @throws OperationFailed If an error occurred
public Any m_commit(Context context, Any[] parameters)
{
try {
_synapse.commit();
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
}