Skip to content
Snippets Groups Projects
Configurator.jak 4.1 KiB
Newer Older
marvin's avatar
marvin committed

import java.util.List;
import java.util.ArrayList;
import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;

/**
 * Main class for the car configurator.
 * Is refined by modules, which add selected components to component list.
 * This class has to save a file containing the configuration
 * and launch the test seletion application afterwards.
 * 
 * 
 * Naming conventions: 
 * m_: Member variable
 * s: String
 * n: Integer number (int, long,...)
 * f: Floating point number (float, double,...)
 * o: Object
 * a: Collection (List, Array,...)
 * 
 */

public class Configurator {	
	//module list
	private List m_aModuleList;
	//output path
	private String m_sOutputPath = "resources/output/featuremodel.xml";
	//TODO: set this
	//path for test case selector, which has to be launched from this application
	private String m_sTestCaseSelectorPath;
	//path to the feature model xml
	private String m_sFeatureModelPath = "model.xml";
	//document representation of the feature model
	private Document m_oFeatureModelDoc; 
	//representation of a single module with all of its types
	class Module {
		String sModuleName;
		ArrayList aModuleTypes;
	}
	
	
	public static void main(String[] args) {
		new Configurator().save();
	}
	
	public void save() {						
		//create empty xml
		Element oStruct = new Element("specification");
		Document oDoc = new Document(oStruct);
		
		//if the module list was initialized
		if(m_aModuleList!=null) {
			for(int i=0; i<m_aModuleList.size(); i++) {
				//Add module and tag to Document
				Element oModuleElement = new Element("module");
				//Add name
				Module oModule = (Module)m_aModuleList.get(i);
				oModuleElement.setAttribute("name", oModule.sModuleName);
				//Create type String
				ArrayList<String> aTypes = oModule.aModuleTypes;	
				String sTypesString = "";
				//concat String
				for(int j=0; j<aTypes.size(); j++) {
					sTypesString += aTypes.get(j);
					//add comma if it's not the last Type
					if(j<aTypes.size()-1)
						sTypesString += ", ";
				}
				oModuleElement.setAttribute("types", sTypesString);
				oStruct.addContent(oModuleElement);
			}
		}
		
		//generate xml String and save it to a file
		try {
			Adapter.saveAsXml(oDoc, m_sOutputPath);	
		}
		catch (Exception oEx) {
			System.out.println(oEx);
			return;
		}
		
		//create and launch Adapter
		new Adapter();
	}
	
	//get all parents of a given module from the Feature Model file
	private Module GetModuleParents(String sModuleName) throws IOException, JDOMException {
		Module oModule = new Module();
		//set name
		oModule.sModuleName = sModuleName;
		//init Doc if not done
		if(m_oFeatureModelDoc==null) {
			m_oFeatureModelDoc = Adapter.readXml(m_sFeatureModelPath);
			System.out.println(m_oFeatureModelDoc.toString());
		}
		//get element
		String sQuery = "//*[@name= '"+sModuleName+"']";
		List<Element> aModules = Adapter.xQueryElements(m_oFeatureModelDoc, sQuery);
		Element oModuleElement;
		try {
			oModuleElement = aModules.get(0);
		}
		catch (Exception oEx) {
			System.out.println(oEx.toString() + "\nList empty for query "+ sQuery);
			return null;
		}
		//get all anchestors
		Element oParent = oModuleElement.getParentElement();
		ArrayList<String> aTypes = new ArrayList();
		//itterate through parents of module
		while(true) {
			String sElementName = oParent.getAttribute("name").getValue();
			//break if we reach the Main class, as there are no types further up
			if(sElementName.equals("ConfiguratorMain")) {
				break;
			}
			aTypes.add(sElementName);
			//iterate further trough List
			oModuleElement = oParent;
			oParent = oModuleElement.getParentElement();
		}		
		oModule.aModuleTypes = aTypes;
		return oModule;
	}
	
	//add a module name  and module type to the feature list. Init list, if not already done
	public void addToModuleList(String sModuleName) {
		//init List, if not already done
		if(m_aModuleList==null) {
			m_aModuleList = new ArrayList();
		}		
		//get the module types
		Module oModule;
		try {
			oModule = GetModuleParents(sModuleName);		
		}
		catch (Exception oEx) {
			System.out.print(oEx);
			return;
		}		
		m_aModuleList.add(oModule);
	}
}