/*
 * FunctionCatalog.java
 *
 * Created on September 18, 2002, 2:01 PM
 */

package hep.aida.ref.function;
import hep.aida.IAnalysisFactory;
import hep.aida.IFunction;
import hep.aida.IFunctionCatalog;
import hep.aida.IFunctionFactory;
import hep.aida.IModelFunction;
import hep.aida.IRangeSet;
import hep.aida.ITree;
import hep.aida.dev.IDevFunctionCatalog;
import hep.aida.ext.IManagedFunction;
import hep.aida.ref.ManagedObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author  serbo
 */
public class FunctionCatalog implements IDevFunctionCatalog, Serializable {

    private static Pattern pattern = Pattern.compile("\\s*(\\w+)\\s*\\+??");
    
    public static String[] defaultNames = { "g2", "g", "e", 
                                            "moyal", "lorentzian", 
                                            "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9" };
    public static String prefix = "codelet:";

    protected static FunctionCatalog catalog = null;
    protected TreeMap hash;
    protected TreeMap userHash;
    protected FunctionCreator creator;

    // Constructors
    public static FunctionCatalog getFunctionCatalog() {
	if (catalog == null) catalog = new FunctionCatalog();
	return catalog;
    }
    public static FunctionCatalog getFunctionCatalog(String fileName) throws IOException {
	try {
	    if (catalog == null) catalog = new FunctionCatalog(fileName);
	} catch (IOException ioe) { 
	    throw ioe; 
	} 
	return catalog;
    }

    protected FunctionCatalog() {
	creator = new FunctionCreator();
	hash = new TreeMap();
	userHash = new TreeMap();
	for (int i=0; i 0 || model.indexOf("-") > 0 || model.indexOf("/") >0 )
            throw new IllegalArgumentException("Unsupported operation. We currently only support adding functions.");
        
        ArrayList functions = new ArrayList();
        Matcher matcher = pattern.matcher(model);
        while (matcher.find()) {
            String m = matcher.group(1);
            String codeletString = codeletForName(m);
            functions.add( creator.createFromCodelet(name, codeletString) );
        }
        if ( functions.size() == 0 )
            throw new IllegalArgumentException("Problem parsing model "+model+". Please report this problem.");
        else if ( functions.size() == 1 )
            return (IFunction) functions.get(0);
        else
            return new SumOfFunctions(name,functions);
    }

    public IFunction clone(String nameInATree, IFunction f) {
	IFunction clone;
        String codeletString = f.codeletString();
        if ( CodeletUtils.isCodeletFromCatalog(codeletString) ) {
            String functionModel = CodeletUtils.modelFromCodelet(f.codeletString());
            clone = create(nameInATree,functionModel);
        } else if (f instanceof AbstractIFunction) {
	    try {
                clone = (IFunction) ((AbstractIFunction) f).clone();
		if (clone instanceof ManagedObject) ((ManagedObject) clone).setName(nameInATree);
		if (clone instanceof IManagedFunction) ((IManagedFunction) clone).setName(nameInATree);
            } catch (Exception e) { 
		throw new RuntimeException("Can not create clone of AbstractIFunction", e);
	    }
        } else if (f instanceof Cloneable) {
	    try {
                Class[] tmpC = new Class[0];
                Method m = f.getClass().getMethod("clone", tmpC);               
                clone = (IFunction) m.invoke(f, tmpC);
		if (clone instanceof ManagedObject) ((ManagedObject) clone).setName(nameInATree);
		if (clone instanceof IManagedFunction) ((IManagedFunction) clone).setName(nameInATree);
            } catch (Exception ec) { 
		throw new RuntimeException("Can not create clone of user function", ec);
	    }
        } else {
            clone = creator.createFromCodelet(nameInATree,codeletString);
        }

        if ( !(clone instanceof IModelFunction) )
            clone = new BaseModelFunction(nameInATree, clone.title(), clone);
        
	if (f instanceof IModelFunction) {
	    ((IModelFunction) clone).normalize(((IModelFunction) f).isNormalized());
	    IRangeSet c_rs = null;
	    IRangeSet f_rs = null;
	    double[] upper = null;
	    double[] lower = null;
	    ((IModelFunction) clone).excludeNormalizationAll();

	    for (int i=0; i 

Ads help maintain this website.