<commit_before>package gov.nih.nci.cagrid.cadsr.service;

import gov.nih.nci.cadsr.umlproject.domain.Project;
import gov.nih.nci.cadsr.umlproject.domain.UMLAssociationMetadata;
import gov.nih.nci.cadsr.umlproject.domain.UMLClassMetadata;
import gov.nih.nci.cadsr.umlproject.domain.UMLPackageMetadata;
import gov.nih.nci.cagrid.cadsr.common.CaDSRUtils;
import gov.nih.nci.cagrid.metadata.common.UMLClass;
import gov.nih.nci.cagrid.metadata.dataservice.DomainModel;
import gov.nih.nci.cagrid.metadata.dataservice.DomainModelExposedUMLAssociationCollection;
import gov.nih.nci.cagrid.metadata.dataservice.DomainModelExposedUMLClassCollection;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationEdge;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationEdgeUmlClass;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationSourceUMLAssociationEdge;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationTargetUMLAssociationEdge;
import gov.nih.nci.system.applicationservice.ApplicationException;
import gov.nih.nci.system.applicationservice.ApplicationService;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/** 
 *  DomainModelBuilder
 *  Builds a DomainModel
 * 
 * @author <A HREF="MAILTO:<EMAIL>"><NAME></A>
 * 
 * @created Jun 1, 2006 
 * @version $Id$ 
 */
public class DomainModelBuilder {
	protected static Log LOG = LogFactory.getLog(DomainModelBuilder.class.getName());
	
	// Project -> (packageName, UMLPackageMetadata)
	private static Map projectUmlPackages = null; 
	// UMLPackageMetadata -> Set(UMLClassMetadata)
	private static Map packageClasses = null;
	// UMLClassMetadata -> Set(UMLAssociationMetadata)
	private static Map classAssociations = null;
	
	private ApplicationService cadsr = null;

	public DomainModelBuilder(ApplicationService cadsr) {
		this.cadsr = cadsr;
	}
	
	
	/**
	 * Gets a DomainModel that represents the entire project
	 * @param proj
	 * @return
	 */
	public DomainModel getDomainModel(Project proj) throws RemoteException {
		// find all packages in the project and hand off to getDomainModel(Package, String[]);
		cachePackages(proj);
		Map umlPackages = (Map) projectUmlPackages.get(proj);
		String[] packageNames = new String[umlPackages.keySet().size()];
		umlPackages.keySet().toArray(packageNames);
		return getDomainModel(proj, packageNames);
	}
	
	
	/**
	 * Gets a DomainModel that represents the project and packages
	 * @param proj
	 * 		The project to build a domain model for
	 * @param packageNames
	 * 		The names of packages to include in the domain model
	 * @return
	 * @throws RemoteException
	 */
	public DomainModel getDomainModel(Project proj, String[] packageNames) throws RemoteException {
		// grab the classes out of the packages
		List classes = new ArrayList();
		for (int i = 0; i < packageNames.length; i++) {
			UMLPackageMetadata pack = getPackageMetadata(proj, packageNames[i]);
			UMLClassMetadata[] mdArray = getClasses(pack);
			Collections.addAll(classes, mdArray);
		}
		UMLClassMetadata[] classArray = new UMLClassMetadata[classes.size()];
		classes.toArray(classArray);
		
		// grab associations for each class
		List associations = new ArrayList();
		for (int i = 0; i < classArray.length; i++) {
			UMLClassMetadata clazz = classArray[i];
			UMLAssociationMetadata[] mdArray = getAssociations(clazz);
			Collections.addAll(associations, mdArray);
		}
		UMLAssociationMetadata[] associationArray = new UMLAssociationMetadata[associations.size()];
		associations.toArray(associationArray);
		
		// hand off
		return getDomainModel(proj, classArray, associationArray);
	}
	
	
	/**
	 * Generates a DomainModel that represents the project and the given subset of 
	 * classes and associations
	 * @param proj
	 * 		The project to build a domain model for
	 * @param classes
	 * 		The classes to include in the domain model
	 * @param associations
	 * 		The asociations to include in the domain model
	 * @return
	 * @throws RemoteException
	 */
	public DomainModel getDomainModel(Project proj, UMLClassMetadata[] classes, UMLAssociationMetadata[] associations) throws RemoteException {
		DomainModel model = new DomainModel();
		// project
		model.setProjectDescription(proj.getDescription());
		model.setProjectLongName(proj.getLongName());
		model.setProjectShortName(proj.getShortName());
		model.setProjectVersion(proj.getVersion());
		
		// classes
		DomainModelExposedUMLClassCollection exposedClasses = new DomainModelExposedUMLClassCollection();
		UMLClass[] umlClasses = new UMLClass[classes.length];
		for (int i = 0; i < classes.length; i++) {
			umlClasses[i] = CaDSRUtils.convertClass(classes[i]);
		}
		exposedClasses.setUMLClass(umlClasses);
		model.setExposedUMLClassCollection(exposedClasses);
		
		// associations
		DomainModelExposedUMLAssociationCollection exposedAssociations = new DomainModelExposedUMLAssociationCollection();
		UMLAssociation[] umlAssociations = new UMLAssociation[associations.length];
		for (int i = 0; i < associations.length; i++) {
			gov.nih.nci.cagrid.cadsr.domain.UMLAssociation cadsrAssociation = CaDSRUtils.convertAssociation(associations[i]);
			umlAssociations[i] = convertAssociation(cadsrAssociation);
		}
		exposedAssociations.setUMLAssociation(umlAssociations);
		model.setExposedUMLAssociationCollection(exposedAssociations);
		return model;
	}
	
	
	private UMLAssociation convertAssociation(gov.nih.nci.cagrid.cadsr.domain.UMLAssociation cadsrAssociation) {
		UMLAssociation converted = new UMLAssociation();
		converted.setBidirectional(cadsrAssociation.isIsBidirectional());
		UMLAssociationSourceUMLAssociationEdge convertedSourceEdge = new UMLAssociationSourceUMLAssociationEdge();
		UMLAssociationEdge sourceEdge = new UMLAssociationEdge();
		sourceEdge.setMaxCardinality(cadsrAssociation.getSourceMaxCardinality());
		sourceEdge.setMinCardinality(cadsrAssociation.getSourceMinCardinality());
		sourceEdge.setRoleName(cadsrAssociation.getSourceRoleName());
		sourceEdge.setUmlClass(new UMLAssociationEdgeUmlClass(CaDSRUtils.convertClass(
			cadsrAssociation.getSourceUMLClassMetadata().getUMLClassMetadata())));
		convertedSourceEdge.setUMLAssociationEdge(sourceEdge);
		converted.setSourceUMLAssociationEdge(convertedSourceEdge);
		UMLAssociationTargetUMLAssociationEdge convertedTargetEdge = new UMLAssociationTargetUMLAssociationEdge();
		UMLAssociationEdge targetEdge = new UMLAssociationEdge();
		targetEdge.setMaxCardinality(cadsrAssociation.getTargetMaxCardinality());
		targetEdge.setMinCardinality(cadsrAssociation.getTargetMinCardinality());
		targetEdge.setRoleName(cadsrAssociation.getTargetRoleName());<commit_msg>*** empty log message ***<commit_after>package gov.nih.nci.cagrid.cadsr.service;

import gov.nih.nci.cadsr.umlproject.domain.Project;
import gov.nih.nci.cadsr.umlproject.domain.UMLAssociationMetadata;
import gov.nih.nci.cadsr.umlproject.domain.UMLClassMetadata;
import gov.nih.nci.cadsr.umlproject.domain.UMLPackageMetadata;
import gov.nih.nci.cagrid.cadsr.common.CaDSRUtils;
import gov.nih.nci.cagrid.cadsr.domain.UMLAssociation;
import gov.nih.nci.cagrid.metadata.common.UMLClass;
import gov.nih.nci.cagrid.metadata.dataservice.DomainModel;
import gov.nih.nci.cagrid.metadata.dataservice.DomainModelExposedUMLAssociationCollection;
import gov.nih.nci.cagrid.metadata.dataservice.DomainModelExposedUMLClassCollection;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationEdge;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationEdgeUmlClass;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationSourceUMLAssociationEdge;
import gov.nih.nci.cagrid.metadata.dataservice.UMLAssociationTargetUMLAssociationEdge;
import gov.nih.nci.system.applicationservice.ApplicationException;
import gov.nih.nci.system.applicationservice.ApplicationService;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/** 
 *  DomainModelBuilder
 *  Builds a DomainModel
 * 
 * @author <A HREF="MAILTO:<EMAIL>"><NAME></A>
 * 
 * @created Jun 1, 2006 
 * @version $Id$ 
 */
public class DomainModelBuilder {
	protected static Log LOG = LogFactory.getLog(DomainModelBuilder.class.getName());
	
	// Project -> (packageName, UMLPackageMetadata)
	private static Map projectUmlPackages = null; 
	// UMLPackageMetadata -> Set(UMLClassMetadata)
	private static Map packageClasses = null;
	// UMLClassMetadata -> Set(UMLAssociationMetadata)
	private static Map classAssociations = null;
	
	private ApplicationService cadsr = null;

	public DomainModelBuilder(ApplicationService cadsr) {
		this.cadsr = cadsr;
	}
	
	
	/**
	 * Gets a DomainModel that represents the entire project
	 * @param proj
	 * @return
	 */
	public DomainModel getDomainModel(Project proj) throws RemoteException {
		// find all packages in the project and hand off to getDomainModel(Package, String[]);
		cachePackages(proj);
		Map umlPackages = (Map) projectUmlPackages.get(proj);
		String[] packageNames = new String[umlPackages.keySet().size()];
		umlPackages.keySet().toArray(packageNames);
		return getDomainModel(proj, packageNames);
	}
	
	
	/**
	 * Gets a DomainModel that represents the project and packages
	 * @param proj
	 * 		The project to build a domain model for
	 * @param packageNames
	 * 		The names of packages to include in the domain model
	 * @return
	 * @throws RemoteException
	 */
	public DomainModel getDomainModel(Project proj, String[] packageNames) throws RemoteException {
		// grab the classes out of the packages
		List classes = new ArrayList();
		for (int i = 0; i < packageNames.length; i++) {
			UMLPackageMetadata pack = getPackageMetadata(proj, packageNames[i]);
			UMLClassMetadata[] mdArray = getClasses(pack);
			Collections.addAll(classes, mdArray);
		}
		UMLClassMetadata[] classArray = new UMLClassMetadata[classes.size()];
		classes.toArray(classArray);
		
		// grab associations for each class
		List associationMetadataList = new ArrayList();
		for (int i = 0; i < classArray.length; i++) {
			UMLClassMetadata clazz = classArray[i];
			UMLAssociationMetadata[] mdArray = getAssociations(clazz);
			Collections.addAll(associationMetadataList, mdArray);
		}
		UMLAssociation[] associationArray = new UMLAssociation[associationMetadataList.size()];
		for (int i = 0; i < associationMetadataList.size(); i++) {
			UMLAssociationMetadata assocMd = (UMLAssociationMetadata) associationMetadataList.get(i);
			associationArray[i] = CaDSRUtils.convertAssociation(assocMd);
		}
		
		// hand off
		return getDomainModel(proj, classArray, associationArray);
	}
	
	
	/**
	 * Generates a DomainModel that represents the project and the given subset of 
	 * classes and associations
	 * @param proj
	 * 		The project to build a domain model for
	 * @param classes
	 * 		The classes to include in the domain model
	 * @param associations
	 * 		The asociations to include in the domain model
	 * @return
	 * @throws RemoteException
	 */
	public DomainModel getDomainModel(Project proj, UMLClassMetadata[] classes, UMLAssociation[] associations) throws RemoteException {
		DomainModel model = new DomainModel();
		// project
		model.setProjectDescription(proj.getDescription());
		model.setProjectLongName(proj.getLongName());
		model.setProjectShortName(proj.getShortName());
		model.setProjectVersion(proj.getVersion());
		
		// classes
		DomainModelExposedUMLClassCollection exposedClasses = new DomainModelExposedUMLClassCollection();
		UMLClass[] umlClasses = new UMLClass[classes.length];
		for (int i = 0; i < classes.length; i++) {
			umlClasses[i] = CaDSRUtils.convertClass(classes[i]);
		}
		exposedClasses.setUMLClass(umlClasses);
		model.setExposedUMLClassCollection(exposedClasses);
		
		// associations
		DomainModelExposedUMLAssociationCollection exposedAssociations = new DomainModelExposedUMLAssociationCollection();
		gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation[] umlAssociations = 
			new gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation[associations.length];
		for (int i = 0; i < associations.length; i++) {
			umlAssociations[i] = convertAssociation(associations[i]);
		}
		gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation[] dsAssociations = 
			new gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation[umlAssociations.length];
		for (int i = 0; i < umlAssociations.length; i++) {
			// dsAssociations[i] = convertAssociation(umlAssociations[i]);
		}
		exposedAssociations.setUMLAssociation(dsAssociations);
		model.setExposedUMLAssociationCollection(exposedAssociations);
		return model;
	}
	
	
	private gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation convertAssociation(UMLAssociation cadsrAssociation) {
		gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation converted = 
			new gov.nih.nci.cagrid.metadata.dataservice.UMLAssociation();
		converted.setBidirectional(cadsrAssociation.isIsBidirectional());
		UMLAssociationSourceUMLAssociationEdge convertedSourceEdge = new UMLAssociationSourceUMLAssociationEdge();
		UMLAssociationEdge sourceEdge = new UMLAssociationEdge();
		sourceEdge.setMaxCardinality(cadsrAssociation.getSourceMaxCardinality());
		sourceEdge.setMinCardinality(cadsrAssociation.getSourceMinCardinality());
		sourceEdge.setRoleName(cadsrAssociation.getSourceRoleName());
		sourceEdge.setUmlClass(new UMLAssociationEdgeUmlClass(CaDSRUtils.convertClass(
			cadsrAssociation.getSourceUMLClassMetadata().getUMLClassMetadata())));
		convertedSourceEdge.setUMLAssociationEdge(sourceEdge);
		converted.setSourceUMLAssociationEdge(convertedSourceEdge);
		UMLAssociationTargetUMLAssociationEdge convertedTargetEdge = new UMLAssociationTargetUMLAssociationEdge();
		UMLAssociationEdge targetEdge = new UMLAssociationEdge();
		targetEdge.setMaxCardinality(cadsrAssociation.getTargetMaxCardinality());
		targetEdge.setMinCardinality(cadsrAssociation.getTargetMinCardinality());
		targetEdge.setRoleName(cadsrAssociation.getTargetRoleName());
{'fraction_non_alphanumeric': 0.09805838804302276, 'fraction_numerical': 0.0013968431345159939, 'mean_word_length': 5.8185714285714285, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3528282', 'n_tokens_mistral': 4774, 'n_tokens_neox': 4269, 'n_words': 831}