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

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

Go to download

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

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

/**
 * ?α??ܵ????ӵ?ģʽƥ?????㴦????
 * op.regex(rs,F) op???α??ܵ?
 * @author RunQian
 *
 */
public class Regex extends Operation {
	private Pattern pattern; // ģʽ
	private String []names; // ?ֶ???????
	private Expression exp; // ??Ҫ??ƥ????ֶΣ?ȱʡ??~
	private DataStruct ds; // ????????ݽṹ?????ڿ?ʱ????Դ??¼

	public Regex(Pattern pattern, String []names, Expression exp) {
		this(null, pattern, names, exp);
	}
	
	public Regex(Function function, Pattern pattern, String []names, Expression exp) {
		super(function);
		this.pattern = pattern;
		this.names = names;
		this.exp = exp;
		
		if (names != null) {
			ds = new DataStruct(names);
		}
	}
	
	/**
	 * ???????????ڶ??̼߳??㣬??Ϊ????ʽ???ܶ??̼߳???
	 * @param ctx ??????????
	 * @return Operation
	 */
	public Operation duplicate(Context ctx) {
		Expression dupExp = dupExpression(exp, ctx);
		return new Regex(function, pattern, names, dupExp);
	}

	/**
	 * ?????α??ܵ???ǰ???͵?????
	 * @param seq ????
	 * @param ctx ??????????
	 * @return
	 */
	public Sequence process(Sequence seq, Context ctx) {
		Pattern pattern = this.pattern;
		DataStruct ds = this.ds;
		int gcount = ds == null ? 0 : ds.getFieldCount();

		int len = seq.length();
		Sequence data = new Sequence(len);
		Sequence strs;
		if (exp == null) {
			strs = seq; // .fieldValues(0)
		} else {
			strs = seq.calc(exp, ctx);
		}
		
		IArray strMems = strs.getMems();
		IArray srcMems = seq.getMems();
		
		for (int i = 1; i <= len; ++i) {
			Object obj = strMems.get(i);
			if (obj instanceof String) {
				Matcher m = pattern.matcher((String)obj);
				if (m.find()) {
					if (ds == null) {
						data.add(srcMems.get(i));
					} else {
						Record r = new Record(ds);
						data.add(r);
						for (int g = 1; g <= gcount; ++g) {
							r.setNormalFieldValue(g - 1, m.group(g));
						}
					}
				}
			} else if (obj != null) {
				MessageManager mm = EngineMessage.get();
				throw new RQException(mm.getMessage("engine.needStringExp"));
			}
		}
					
		if (data.length() != 0) {
			return data;
		} else {
			return null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy