All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.dm.op.Derive Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.dm.op;

import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.dm.DataStruct;
import com.scudata.dm.Sequence;
import com.scudata.expression.Expression;
import com.scudata.expression.Function;
import com.scudata.resources.EngineMessage;

/**
 * ?α??ܵ????ӳ??????ֶδ?????
 * @author RunQian
 *
 */
public class Derive extends Operation  {
	private Expression[] exps; // ?????ֶεı???ʽ????
	private String[] names; // ?????ֶε??ֶ???????
	private String opt; // ѡ??
	
	private int oldColCount; // Դ?ֶ???
	private DataStruct newDs; // ????????ݽṹ
	//private boolean containNull; // ?Ƿ???null
	
	private int level = 0;
	
	public Derive(Expression []exps, String []names, String opt) {
		this(null, exps, names, opt, 0);
	}
	
	public Derive(Function function, Expression []exps, String []names, String opt, int level) {
		super(function);
		this.exps = exps;
		this.names = names;
		this.opt = opt;
		//this.containNull = opt == null || opt.indexOf('i') == -1;
		this.level = level;
	}
	
	/**
	 * ???????????ڶ??̼߳??㣬??Ϊ????ʽ???ܶ??̼߳???
	 * @param ctx ??????????
	 * @return Operation
	 */
	public Operation duplicate(Context ctx) {
		Expression []dupExps = dupExpressions(exps, ctx);
		return new Derive(function, dupExps, names, opt, level);
	}
	
	private DataStruct getNewDataStruct(Sequence seq) {
		if (newDs == null) {
			DataStruct ds = seq.dataStruct();
			if (ds == null) {
				MessageManager mm = EngineMessage.get();
				throw new RQException(mm.getMessage("engine.needPurePmt"));
			}
			
			Expression[] exps = this.exps;
			int colCount = exps.length;
			for (int i = 0; i < colCount; ++i) {
				if (names[i] == null || names[i].length() == 0) {
					if (exps[i] == null) {
						MessageManager mm = EngineMessage.get();
						throw new RQException("derive" + mm.getMessage("function.invalidParam"));
					}
	
					names[i] = exps[i].getFieldName(ds);;
				} else {
					if (exps[i] == null) {
						exps[i] = Expression.NULL;
					}
				}
			}
			
			String []oldNames = ds.getFieldNames();
			oldColCount = oldNames.length;
			
			// ?ϲ??ֶ?
			int newColCount = oldColCount + colCount;
			String []totalNames = new String[newColCount];
			System.arraycopy(oldNames, 0, totalNames, 0, oldColCount);
			System.arraycopy(names, 0, totalNames, oldColCount, colCount);
			newDs = ds.create(totalNames);
		}
		
		return newDs;
	}
	
	/**
	 * ?????α??ܵ???ǰ???͵?????
	 * @param seq ????
	 * @param ctx ??????????
	 * @return
	 */
	public Sequence process(Sequence seq, Context ctx) {
		if (exps == null) {
			return seq.derive(opt);
		} else if (level > 1) {
			return seq.derive(names, exps, opt, ctx, level);
		} else {
			DataStruct newDs = getNewDataStruct(seq);
			return seq.derive(newDs, exps, opt, ctx);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy