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

toxgene.core.Engine Maven / Gradle / Ivy

/*
 * Engine.java implements the engine that drives the actual data generation
 *
 * @author Denilson Barbosa
 *
 * @version 0.1
 */

package toxgene.core;

import java.io.IOException;
import java.io.PrintStream;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.util.jar.JarFile;
import java.util.Random;
import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import toxgene.core.genes.Gene;
import toxgene.core.genes.lists.ListElement;
import toxgene.core.genes.lists.ToxList;
import toxgene.core.genes.trees.ToxCollection;
import toxgene.core.parser.ToxTemplate;
import toxgene.core.parser.ToxValidatingParser;
import toxgene.core.random.*;
import toxgene.interfaces.ToXgeneCdataDescriptor;
import toxgene.interfaces.ToXgeneDocumentCollection;
import toxgene.interfaces.ToXgeneEngine;
import toxgene.interfaces.ToXgeneReporter;
import toxgene.interfaces.ToXgeneSession;
import toxgene.util.CdataDescriptor;
import toxgene.util.Dictionary;
import toxgene.util.DuplicateKeyException;
import toxgene.util.FileLoader;
import toxgene.util.KeyNotFoundException;
import toxgene.util.ToXgeneDocumentCollectionImpl;
import toxgene.util.ToxVector;

public class Engine implements ToXgeneEngine{
  private final static String version = "2.3";

	private ToXgeneReporter tgReporter;
	private boolean usePOM;
	private boolean addNewLines;
	private String inputPath;
	private String jarPath;
	private boolean useJarLoading = true;
	private int pomNextFile;
	private String bufferPath;
	private float memFracBuffer;
	private int bufferSize;
	private int numBuffers;
	
	private ToxTemplate template;

	public Random seedGenerator;

	public Dictionary toxLists;
	public Dictionary toxFiles;
	public Dictionary simpleTypes;
	public Dictionary randomGenerators;
	public Dictionary registeredCdata;

	private Vector toxVectors;

	private Vector tagNames = new Vector(10,10);
	private Dictionary tagIndex = new Dictionary();

	private boolean doneLists = false;

	public Engine(){
	}

	public void startSession(ToXgeneSession session){
		tgReporter = session.reporter;
	
		if (session.initialSeed != 0)
			seedGenerator = new Random(session.initialSeed);
		else
			seedGenerator = new Random(System.currentTimeMillis());			

		// determine where to find files
		try {
			if (useJarLoading)
				FileLoader.inst.setupJarFileForLoading(jarPath);
		}
		catch (IOException e1) {
			e1.printStackTrace(); //TODO
		}
		
		addNewLines = session.addNewLines;
		inputPath = session.inputPath;
		usePOM = session.usePOM;
		bufferPath = session.pomBufferPath;
		memFracBuffer = session.pomMemFracBuffer;
		bufferSize = session.pomBufferSize;
		pomNextFile = 1;

		toxLists = new Dictionary();
		toxFiles = new Dictionary();
		simpleTypes = new Dictionary();
		randomGenerators = new Dictionary();
		registeredCdata = new Dictionary();
		
		if (session.cdataDescriptors != null){
			for (int i=0; i< session.cdataDescriptors.size(); i++){
				ToXgeneCdataDescriptor tgcdd = (ToXgeneCdataDescriptor)
					session.cdataDescriptors.get(i);
				try{
					registeredCdata.add(tgcdd.cdataName,
															new CdataDescriptor(tgcdd.cdataClass,
																									tgcdd.minLength,
																									tgcdd.maxLength));
				}
				catch (DuplicateKeyException e){
					throw new ToXgeneErrorException("duplicate CDATA generators for  \""+
																					tgcdd.cdataName+"\"");
					
				}
			}
		}
		else{
			getDefaultCdataDescriptors();
		}
	}

	public boolean parseTemplate(InputStream inputTemplate){
		template = new ToxTemplate(this, tgReporter);

		try{
			template.buildTemplate(inputTemplate);
		}
		catch (ToXgeneErrorException e){
			throw e;
		}
		catch (Exception e){
			ByteArrayOutputStream temp = new ByteArrayOutputStream();
			PrintStream stream = new PrintStream(temp);
			e.printStackTrace(stream);
			
			throw new
				ToXgeneErrorException("UNRECOGNIZED ERROR: An unrecognized "
															+"error has occurred.\nPlease report the "
															+"following debug information to "
															+"[email protected]\nPlease include "
															+"the template that causes this problem.\n"
															+temp.toString());

		}
		
		if (usePOM){
			toxVectors = new Vector(10,10);

			Runtime rt = Runtime.getRuntime();
			long totalMemory = rt.freeMemory();
			
			Vector lists = toxLists.values();
			Vector documents = toxFiles.values();
			
			int maxIterators = 0, ni=0;
			// we estimate how many buffers we will use
			for (int i=0; i maxIterators)
					maxIterators = ni;
			}
			for (int i=0; i maxIterators)
					maxIterators = ni;
			}
			
			if (maxIterators == 0){
				tgReporter.warning("template does not create or import large lists. "+
													 "Will not use POM.");
				usePOM = false;
			}
			else{
				double m1 = Math.floor((double)(memFracBuffer*totalMemory)/
															 (bufferSize));
				numBuffers = (int)m1/maxIterators;
				
				if (numBuffers < 2){
					tgReporter.warning("low in memory for buffering query results. "+
														 "Try increasing the java VM memory.");
					numBuffers = 1;
				}
			}
		}

		return true;
	}


	public void materialize(ToXgeneDocumentCollection collection,
													PrintStream outStream){

		if (!doneLists){
			generateLists();
		}
		
		try{
			ToxCollection tColl = (ToxCollection) toxFiles.get(collection.getName());
			tColl.materialize(outStream);
		}
		catch (KeyNotFoundException e1){
			endSession();
			throw new ToXgeneErrorException("cannot find document collection \""
																			+collection.getName()+"\"");
		}
		catch (ToXgeneErrorException e2){
			endSession();
			throw e2;
		}
		catch(Exception e){
			endSession();
			ByteArrayOutputStream temp = new ByteArrayOutputStream();
			PrintStream stream = new PrintStream(temp);
			e.printStackTrace(stream);
			
			throw new
				ToXgeneErrorException("UNRECOGNIZED ERROR: An unrecognized "
															+"error has occurred.\nPlease report the "
															+"following debug information to "
															+"[email protected]\nPlease include "
															+"the template that causes this problem.\n"
															+temp.toString());
		}
	}

	public void generateLists(){
		Vector lists = toxLists.values();
		/**
		 * generate the lists once!
		 */					
		for (int i=0; i");
			break;
		}
		case ListElement.ELEMENTS:{
			Vector children = node.getChildren();
			output.print("<"+tag+">");
			if (children != null){
				for (int i = 0; i < children.size(); i++) {
					dumpNode(((ListElement) children.get(i)),output);
				}
			}
			output.print("\n");
			break;
		}
		case ListElement.CDATA:{
			output.print("<"+tag+">");
			output.print(new String(node.getContent()));
			output.print("\n");
		}
		}
	}

	private void getDefaultCdataDescriptors(){
		toxgene.core.parser.ToxValidatingParser parser;
		Document doc = null;
		
		try {
//			FileLoader.inst.trySetupJarFile();
//			System.out.print(tgHome+"/toxgene.jar");
//			JarFile jar_file = new JarFile(tgHome+"/toxgene.jar");
			
			InputStream cfgFile = FileLoader.inst.loadFile("cdata.xml");
//			jar_file.
//				getInputStream(jar_file.getEntry("config/cdata.xml"));

			parser = new ToxValidatingParser(false, tgReporter);
			doc = parser.parse(cfgFile, true);
			
		} catch (Exception e) {
			//throw new ToXgeneErrorException("cannot load default CDATA descriptors"
				//															+" from toxgene.jar/config/cdata.xml");
			System.out.println(e.getMessage());
		}

		Node root, node;
		String node_name;

		root = doc.getDocumentElement();
		NodeList children = root.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			node = children.item(i);
			node_name = node.getNodeName();

			if (node_name.compareTo("cdata_generators") == 0) {
				process_cdata_generators(node);
			}
		}
	}

	private void process_cdata_generators(Node node){
		String typeName = "", className="";
		int min=0, max=0;
		Element elm;
		Node node2;
		NodeList list;
		
		try{
			NodeList children = node.getChildNodes();
			for (int i = 0; i < children.getLength(); i++) {
				elm = (Element) children.item(i);
				if (elm.getNodeName().compareTo("cdata_generator")!=0){
					throw (new Exception());
				}
				
				list = elm.getElementsByTagName("name");
				if (list.getLength()!=1)
					throw (new Exception(ToxValidatingParser.prettyPrintDOMTree(elm)));
				typeName = list.item(0).getFirstChild().getNodeValue(); 
				
				list = elm.getElementsByTagName("class");
				if (list.getLength()!=1)
					throw (new Exception(ToxValidatingParser.prettyPrintDOMTree(elm)));
				className = list.item(0).getFirstChild().getNodeValue();
				
				list = elm.getElementsByTagName("minLength");
				if (list.getLength()!=1)
					throw (new Exception(ToxValidatingParser.prettyPrintDOMTree(elm)));
				min = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
				
				list = elm.getElementsByTagName("maxLength");
				if (list.getLength()!=1)
					throw (new Exception(ToxValidatingParser.prettyPrintDOMTree(elm)));
				max = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
				
				registeredCdata.add(typeName, new CdataDescriptor(className,min,max));
			}
		}
		catch (DuplicateKeyException e){
			throw new ToXgeneErrorException("duplicate CDATA generators for \""
																			+typeName+"\"");
		}
		catch (Exception e){
// 			throw new ToXgeneErrorException("Invalid cdata generator declaration:\n"
// 																			+e.getMessage());
			e.printStackTrace();
			System.exit(1);
		}
	}

	public String getJarPath() {
		return jarPath;
	}

	public void setJarPath(String jarPath) {
		this.jarPath = jarPath;
	}

	public boolean isUseJarLoading() {
		return useJarLoading;
	}

	public void setUseJarLoading(boolean useJarLoading) {
		this.useJarLoading = useJarLoading;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy