JBeanClass

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author tl
 * @version [版本号, 2010-11-4]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class JBeanClass
{
	// 所支持的类型
	private static List<String> supportType = new ArrayList<String>();

	private static final int FIX_NUMBER2 = 2;

	private static final int FIX_NUMBER3 = 3;

	private static final int FIX_NUMBER4 = 4;

	static
	{
		supportType.add("byte");
		supportType.add("java.lang.Byte");
		supportType.add("short");
		supportType.add("java.lang.Short");
		supportType.add("int");
		supportType.add("java.lang.Integer");
		supportType.add("long");
		supportType.add("java.lang.Long");
		supportType.add("java.lang.BigInteger");
		supportType.add("float");
		supportType.add("java.lang.Float");
		supportType.add("double");
		supportType.add("java.lang.Double");
		supportType.add("char");
		supportType.add("boolean");
		supportType.add("java.lang.Boolean");
		supportType.add("java.lang.String");
		supportType.add("java.lang.Character");
	}

	/**
	 * 添加方法说明
	 */
	public JBeanClass()
	{
	}

	/**
	 * 根据对象获取对象所有属性
	 * @param entityName
	 * @return
	 * @throws Exception
	 */
	public static Field[] getProperts(Object entityName) throws Exception
	{
		Field field[] = entityName.getClass().getDeclaredFields();
		StringBuffer sb = new StringBuffer();
		for (Field f : field)
		{
			sb.append(f.getName()).append(" : ").append(getGetterMethodInvokeValue(entityName, f.getName())).append("\n");
		}
		Log.getLogger().info("getPropertyString : " + sb.toString());
		return field;
	}

	/**
	 * 获取getter方法的执行结果
	 * @param entity 方法所属的实体
	 * @param methodName 要执行的方法名
	 * @return 执行结果
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Object getGetterMethodInvokeValue(Object entity, String methodName)
	{
		methodName = "get" + CommonTools.createFirstCharToUpperCase(methodName);
		return getMethodInvokeValue(entity, methodName, null, null);
	}

	/**
	 * 获取方法的执行结果
	 * @param entity 方法所属的实体
	 * @param methodName 要执行的方法名
	 * @return 执行结果
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Object getMethodInvokeValue(Object entity, String methodName, Class[] paramType, Object[] paramValue)
	{
		try
		{
			Class c = entity.getClass();
			Method method = c.getMethod(methodName, paramType);
			return method.invoke(entity, paramValue);
		}
		catch (Exception e)
		{
			Log.getLogger().error("can't find method " + methodName + " in " + entity, e);
		}
		return null;
	}

	/**
	 * 从getter方法名称中获取属性的名称
	 * @param methodName 方法名称
	 * @return String 方法对应的属性名
	 */
	public static String getPropertyName(String methodName)
	{
		String propertyName = null;
		// 截取方法名"get"字符串后的所有字符 将获取的字符串首字母小写作为返回的属性名称
		if (methodName.length() > FIX_NUMBER3)
		{
			propertyName = methodName.substring(FIX_NUMBER3, FIX_NUMBER4).toLowerCase(Locale.ENGLISH) + methodName.substring(FIX_NUMBER4);
		}
		return propertyName;
	}

	/**
	 * 获取方法对象的返回值类型
	 * @param method 方法对象
	 * @return String 方法对象的返回值的类型
	 */
	public static String getMethodReturnType(Method method)
	{
		String type = null;
		if (method != null && method.getReturnType() != null)
		{
			type = method.getReturnType().getName();
		}
		return type;
	}

	/**
	 * 获取Set方法对象的第一个参数的类型
	 * @param method 方法对象
	 * @return String 方法对象的第一个参数的类型
	 */
	public static String getSetterMethodFirstParameterType(Method method)
	{
		String type = null;
		if (method != null && method.getParameterTypes().length >= 1)
		{
			type = method.getParameterTypes()[0].getName();
		}
		return type;
	}

	/**
	 * 设置对象指定名称的参数值
	 * @param entity 要操作的对象
	 * @param propertyName 属性名
	 * @param value 要设置的属性值
	 * @return Object 设置后的对象
	 * @throws java.lang.Exception Exception异常
	 */
	@SuppressWarnings("unchecked")
	public static Object setPropertyValueFromString(Object entity, String propertyName, String value) throws Exception
	{
		// 检查参数的有效性
		if ((null == entity) || (null == propertyName) || propertyName.equals("") || (null == value) || value.equals(""))
		{
			return entity;
		}

		propertyName = "set" + CommonTools.createFirstCharToUpperCase(propertyName);
		Method method = getMethod(entity, propertyName);
		if (null == method)
		{
			Log.getLogger().error("can't find method " + propertyName + " in " + entity, null);
		}

		String paramType = getSetterMethodFirstParameterType(method);

		// 复杂度太高,拆分成二个函数
		Object[] params = createParamsNumType(paramType, value);
		if (null == params)
		{
			params = createParamsOther(paramType, value);
		}
		if (params != null)
		{
			method.invoke(entity, params);
		}
		else
		{
			Log.getLogger().error("setPropertyValueFromString parametertype(" + paramType + "," + value + ")not supported。", null);
		}
		return entity;
	}

	/**
	 * 根据方法名获取方法 如果有多个同名的方法,函数只会随机取一个返回
	 * @param obj 要获取方法所在的类
	 * @param methodName 方法的名字
	 * @return 对应的方法
	 */
	public static Method getMethod(Object obj, String methodName)
	{
		Method[] methods = obj.getClass().getMethods();
		// 遍历所有的方法对象,获取所有的符合标准JAVA BEANS规范的setter方法对象集合
		for (int i = 0; i < methods.length; i++)
		{
			if (methods[i].getName().equals(methodName))
			{
				return methods[i];
			}
		}
		return null;
	}

	/**
	 * 根据参数类型和参数值,将参数值转换成指定类型的数据返回
	 * @param paramType
	 * @param value
	 * @return
	 */
	private static Object[] createParamsNumType(String paramType, String value)
	{
		Object[] params = null;
		if (paramType.equals("java.lang.Byte") || paramType.equals("byte"))
		{
			String ss = value;
			if (ss.length() <= FIX_NUMBER2)
			{
				params = new Byte[1];
				params[0] = Byte.valueOf(ss);
			}
		}
		else if (paramType.equals("java.lang.Short") || paramType.equals("short"))
		{
			params = new Short[1];
			params[0] = Short.valueOf(value);
		}
		else if (paramType.equals("java.lang.Integer") || paramType.equals("int"))
		{
			params = new Integer[1];
			params[0] = Integer.valueOf(value);
		}
		else if (paramType.equals("java.lang.Long") || paramType.equals("long"))
		{
			params = new Long[1];
			params[0] = Long.valueOf(value);
		}
		else if (paramType.equals("java.lang.Float") || paramType.equals("float"))
		{
			params = new Float[1];
			params[0] = Float.valueOf(value);
		}
		else if (paramType.equals("java.lang.Double") || paramType.equals("double"))
		{
			params = new Double[1];
			params[0] = Double.valueOf(value);
		}
		return params;
	}

	/**
	 * 根据参数类型和参数值,将参数值转换成指定类型的数据返回
	 * @param paramType
	 * @param value
	 * @return
	 */
	private static Object[] createParamsOther(String paramType, String value)
	{
		Object[] params = null;
		if (paramType.equals("java.lang.String"))
		{
			params = new String[1];
			params[0] = value;
		}
		else if (paramType.equals("java.lang.Character") || paramType.equals("char"))
		{
			String ss = value;
			if (ss.length() == 1)
			{
				params = new Character[1];
				params[0] = Character.valueOf(ss.charAt(0));
			}
		}
		else if (paramType.equals("java.lang.Boolean") || paramType.equals("bool"))
		{
			params = new Boolean[1];
			params[0] = Boolean.valueOf(value);
		}
		return params;
	}
}

 

内容概要:本文档详细介绍了一个基于多任务学习(MTL)结合Transformer编码器的多变量时间序列预测项目。该项目旨在解决多变量时间序列预测中的复杂性、长序列依赖、任务间干扰等问题,通过融合多任务学习与Transformer架构,构建了一套高效且可扩展的预测系统。项目覆盖了从环境准备、数据预处理、模型构建与训练、性能评估到GUI界面设计的完整流程,并提供了详细的代码实现。模型采用多头自注意力机制有效捕捉变量间及时间维度上的复杂依赖,通过多任务共享层和任务特异性层实现任务间知识共享与个性化特征提取。此外,项目还引入了动态任务权重调整、轻量化设计、数据预处理优化等关键技术,确保模型在实际应用中的高效性和准确性。 适合人群:具备一定编程基础,特别是对深度学习和时间序列分析有一定了解的研发人员和技术爱好者。 使用场景及目标:① 提升多变量时间序列预测的准确率;② 实现高效的时间序列特征提取;③ 促进多任务学习在时间序列领域的应用推广;④ 提供可扩展的模型架构设计,适应不同应用场景的需求;⑤ 降低模型训练与推断的计算资源需求;⑥ 促进复杂系统的智能决策能力,如智能制造、能源管理、金融分析、气象预报等领域的精准预测支持。 其他说明:项目不仅提供了完整的代码实现和详细的文档说明,还设计了用户友好的GUI界面,方便用户进行数据加载、参数设置、模型训练及结果可视化等操作。未来改进方向
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值