UserDefinedContinuousStaticSystem.java
/*
* Created on 2007/07/16
* Copyright (C) 2007 Koga Laboratory. All rights reserved.
*
*/
package org.mklab.tool.control.system.continuous;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.ParameterUtil;
import org.mklab.tool.control.system.UserDefinedSystem;
import org.mklab.tool.control.system.parameter.NoSuchParameterException;
import org.mklab.tool.control.system.parameter.ParameterException;
/**
* ユーザ定義連続時間静的システムを表わすクラスです。
*
* @author koga
* @version $Revision: 1.14 $, 2007/07/16
* @param <RS> type of real scalar
* @param <RM> type of real matrix
* @param <CS> type of complex scalar
* @param <CM> type of complex matrix
*/
public class UserDefinedContinuousStaticSystem<RS extends RealNumericalScalar<RS,RM,CS,CM>, RM extends RealNumericalMatrix<RS,RM,CS,CM>, CS extends ComplexNumericalScalar<RS,RM,CS,CM>, CM extends ComplexNumericalMatrix<RS,RM,CS,CM>> extends BaseContinuousStaticSystem<RS,RM,CS,CM> implements UserDefinedSystem {
/** システムの出力方程式を定義したクラス */
private Class<?> systemKlass;
/** システムの初期化処理を定義したメソッド */
private Method initializeFunction;
/** システムの更新処理を定義したメソッド */
private Method updateFunction;
/** システムの出力方程式を定義したメソッド */
private Method outputFunction;
/** 対象オブジェクト */
private Object object;
/**
* 新しく生成された<code>UserDefinedStaticSystem</code>オブジェクトを初期化します。
*
* @param obj 呼び出し対象オブジェクト。nullの場合は静的メソッドを参照します
* @param sunit unit of scalar
*/
public UserDefinedContinuousStaticSystem(Object obj, RS sunit) {
super(1, 1, sunit);
setHasDirectFeedthrough(true);
this.object = obj;
}
/**
* 新しく生成された<code>UserDefinedStaticSystem</code>オブジェクトを初期化します。
* @param sunit unit of scalar
*/
public UserDefinedContinuousStaticSystem(RS sunit) {
this(null, sunit);
}
/**
* {@inheritDoc}
*/
@Override
public RM outputEquation(RS t, RM u) throws SolverStopException {
try {
return (RM)this.outputFunction.invoke(this.object, t, u);
} catch (IllegalArgumentException e) {
throw new SolverStopException(e);
} catch (IllegalAccessException e) {
throw new SolverStopException(e);
} catch (InvocationTargetException e) {
throw new SolverStopException(e.getTargetException());
}
}
/**
* {@inheritDoc}
*/
@Override
public RM outputEquation(RS t) throws SolverStopException {
try {
return (RM)this.outputFunction.invoke(this.object, t);
} catch (IllegalArgumentException e) {
throw new SolverStopException(e);
} catch (IllegalAccessException e) {
throw new SolverStopException(e);
} catch (InvocationTargetException e) {
throw new SolverStopException(e.getTargetException());
}
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#setSystemClass(java.lang.Class)
*/
public void setSystemClass(final Class<?> klass) {
this.systemKlass = klass;
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#getSystemClass()
*/
public Class<?> getSystemClass() {
return this.systemKlass;
}
/**
* システムの出力方程式を定義したメソッドを設定します。
*
* @param method システムの出力方程式を定義したメソッド
* @throws SecurityException メソッドにアクセスする権利が無い場合
*/
public void setOutputFunction(Method method) throws SecurityException {
this.outputFunction = method;
this.outputFunction.setAccessible(true);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#setInitializeFunction(java.lang.reflect.Method)
*/
public void setInitializeFunction(final Method method) throws SecurityException {
this.initializeFunction = method;
this.initializeFunction.setAccessible(true);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#setUpdateFunction(java.lang.reflect.Method)
*/
public void setUpdateFunction(final Method method) throws SecurityException {
this.updateFunction = method;
this.updateFunction.setAccessible(true);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#getParameterValue(java.lang.String)
*/
public Object getParameterValue(String name) throws NoSuchParameterException {
return ParameterUtil.getValue(this.systemKlass, name);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#getParameterNames()
*/
public Set<String> getParameterNames() throws NoSuchParameterException {
return ParameterUtil.getParameterNames(this.systemKlass);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#setParameterValue(java.lang.String, java.lang.Object)
*/
public void setParameterValue(String name, Object value) throws NoSuchParameterException {
ParameterUtil.setValue(this.systemKlass, name, value);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#containParameter(java.lang.String)
*/
public boolean containParameter(String name) {
return ParameterUtil.contains(this.systemKlass, name);
}
/**
* @see org.mklab.tool.control.system.UserDefinedSystem#update()
*/
public void update() throws ParameterException {
try {
this.updateFunction.invoke(null);
} catch (IllegalArgumentException e) {
throw new ParameterException(e);
} catch (IllegalAccessException e) {
throw new ParameterException(e);
} catch (InvocationTargetException e) {
throw new ParameterException(e.getTargetException());
} catch (Exception e) {
throw new ParameterException(e);
}
}
/**
* @see org.mklab.tool.control.system.discrete.BaseDiscreteStaticSystem#initialize()
*/
@Override
public void initialize() {
super.initialize();
try {
if (this.initializeFunction != null) {
this.initializeFunction.invoke(this.object);
}
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}