view src/de/mpiwg/anteater/events/processors/AEventProcessor.java @ 0:036535fcd179

anteater
author jdamerow
date Fri, 14 Sep 2012 10:30:43 +0200
parents
children
line wrap: on
line source

package de.mpiwg.anteater.events.processors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import de.mpiwg.anteater.events.Applicant;
import de.mpiwg.anteater.events.Location;
import de.mpiwg.anteater.events.ResearchEvent;
import de.mpiwg.anteater.events.Species;
import de.mpiwg.anteater.ml.PlaceClasses;
import de.mpiwg.anteater.results.ApplicantResult;
import de.mpiwg.anteater.results.LocationResult;
import de.mpiwg.anteater.results.ResultsCarrier;
import de.mpiwg.anteater.results.SpeciesScientificResult;
import de.mpiwg.anteater.text.TextInformation;
import de.mpiwg.anteater.text.TextType;


public abstract class AEventProcessor implements IEventProcessor {

	protected List<ApplicantResult> getDistinctApplicants(ResultsCarrier carrier) {

		List<ApplicantResult> applicants = carrier.getApplicantResults();

		// find how many distinct applicants there are
		List<ApplicantResult> distinctApplicants = new ArrayList<ApplicantResult>();
		ApplicantLoop: for (ApplicantResult result : applicants) {
			for (ApplicantResult storedResult : distinctApplicants) {
				String resultApplicantName = result.getFinding()
						.getReferenceInText();
				String storedApplicantName = storedResult.getFinding()
						.getReferenceInText();
				if (resultApplicantName.contains(storedApplicantName)
						|| storedApplicantName.contains(resultApplicantName))
					continue ApplicantLoop;
			}

			distinctApplicants.add(result);
		}
		
		// sort by start
		Collections.sort(distinctApplicants, new Comparator<ApplicantResult>() {

			@Override
			public int compare(ApplicantResult o1, ApplicantResult o2) {
				return o1.getFinding().getStart() - o2.getFinding().getStart();
			}
		});
		
		return distinctApplicants;
	}
	
	protected void sortByTextType(List<ApplicantResult> applicants, List<ApplicantResult> appsInSummary, List<ApplicantResult> appsInSuppleInf) {
		for (ApplicantResult applicant : applicants) {
			if (applicant.getResult().getType() == TextType.TYPE_SUMMARY)
				appsInSummary.add(applicant);
			else
				appsInSuppleInf.add(applicant);
		}
	}
	
	protected void sortLocsByTextType(List<LocationResult> locations, List<LocationResult> locsInSummary, List<LocationResult> locsInSuppleInf) {
		for (LocationResult applicant : locations) {
			if (applicant.getResult().getType() == TextType.TYPE_SUMMARY)
				locsInSummary.add(applicant);
			else
				locsInSuppleInf.add(applicant);
		}
	}
	
	protected void sortSpeciesByTextType(List<SpeciesScientificResult> species, List<SpeciesScientificResult> spInSummary, List<SpeciesScientificResult> spInSuppleInf) {
		for (SpeciesScientificResult applicant : species) {
			if (applicant.getResult().getType() == TextType.TYPE_SUMMARY)
				spInSummary.add(applicant);
			else
				spInSuppleInf.add(applicant);
		}
	}
	
	protected Applicant createApplicant(ApplicantResult applicantResult) {
		Applicant applicant = new Applicant();
		applicant.setApplicant(applicantResult.getFinding());
		applicant.setApplicantResult(applicantResult);
		applicant.setApplicantInstitution(new ArrayList<Location>());
		applicant.setLocation(new ArrayList<Location>());
		return applicant;
	}
	
	protected void setLocations(List<Applicant> applicants,
			List<LocationResult> locationResults, ResearchEvent event) {
		for (LocationResult loc : locationResults) {

			Location location = createLocation(loc);

			if (location.getLocationResult().getPrediction() == PlaceClasses.APPLICANT_INSTITUTION) {
				for (Applicant applicant : applicants)
					applicant.getApplicantInstitution().add(location);
			}
			if (location.getLocationResult().getPrediction() == PlaceClasses.APPLICANT_LOCATION) {
				for (Applicant applicant : applicants)
					applicant.getLocation().add(location);
			}

			if (location.getLocationResult().getPrediction() == PlaceClasses.RESEARCH_LOCATION)
				event.getResearchLocations().add(location);
		}
	}
	
	protected Location createLocation(LocationResult locResult) {
		Location location = new Location();
		location.setLocation(locResult.getFinding());
		location.setLocationResult(locResult);

		return location;
	}

	protected void setSpecies(List<SpeciesScientificResult> speciesResults,
			ResearchEvent event) {
		for (SpeciesScientificResult speciesResult : speciesResults) {
			Species species = createSpecies(speciesResult);
			event.getResearchedSpecies().add(species);
		}
	}

	protected Species createSpecies(SpeciesScientificResult speciesResult) {
		Species species = new Species();
		species.setSpeciesName(speciesResult.getFinding());
		species.setSpeciesResult(speciesResult);
		return species;
	}

	protected ResearchEvent createEvent(ApplicantResult applicantResult,
			TextInformation info, List<LocationResult> locationResults,
			List<SpeciesScientificResult> speciesResults) {
		ResearchEvent event = new ResearchEvent();
		event.setApplicants(new ArrayList<Applicant>());
		event.setResearchedSpecies(new ArrayList<Species>());
		event.setResearchLocations(new ArrayList<Location>());
		event.setTextInformation(info);
		
		if (info.getSummaries() != null && info.getSummaries().size() > 0) {
			event.setDate(info.getSummaries().get(0).getDate());
		}
		else if (info.getSupplInfos() != null && info.getSupplInfos().size() > 0) {
			event.setDate(info.getSupplInfos().get(0).getDate());
		}

		// set applicant
		Applicant applicant = createApplicant(applicantResult);
		List<Applicant> applicants = new ArrayList<Applicant>();
		applicants.add(applicant);

		setLocations(applicants, locationResults, event);

		event.getApplicants().add(applicant);

		setSpecies(speciesResults, event);

		return event;
	}

}