view src/de/mpiwg/anteater/events/processors/EventCreatorProcessor.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.List;

import de.mpiwg.anteater.events.Applicant;
import de.mpiwg.anteater.events.ResearchEvent;
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;

public class EventCreatorProcessor extends AEventProcessor {

	@Override
	public void processEvents(List<ResearchEvent> events, ResultsCarrier carrier) {
		TextInformation info = carrier.getTextInfo();

		// find how many distinct applicants there are
		List<ApplicantResult> distinctApplicants = getDistinctApplicants(carrier);

		// if there is only one applicant in text
		if (distinctApplicants.size() == 1) {
			ResearchEvent event = createEvent(distinctApplicants.get(0), info,
					carrier.getLocationResults(), carrier.getSpeciesResults());
			events.add(event);
			return;
		}

		// if there are several applicants start new event with each applicant
		List<ApplicantResult> appsInSummary = new ArrayList<ApplicantResult>();
		List<ApplicantResult> appsInSuppleInf = new ArrayList<ApplicantResult>();

		sortByTextType(distinctApplicants, appsInSummary, appsInSuppleInf);
		
		List<SpeciesScientificResult> speciesInSuppleInf = new ArrayList<SpeciesScientificResult>();
		sortSpeciesByTextType(carrier.getSpeciesResults(), new ArrayList<SpeciesScientificResult>(), speciesInSuppleInf);
		
		List<LocationResult> locationsInSuppleInf = new ArrayList<LocationResult>();
		sortLocsByTextType(carrier.getLocationResults(), new ArrayList<LocationResult>(), locationsInSuppleInf);

		// if there are no applicants in summary, start new event with every
		// applicant
		// unless there is no species in between
		if (appsInSummary.isEmpty()) {
			ResearchEvent event = null;
			for (int i = 0; i < appsInSuppleInf.size(); i++) {
				ApplicantResult applicant = appsInSuppleInf.get(i);
				ApplicantResult nextApplicant = null;
				if (i < appsInSuppleInf.size() - 1)
					nextApplicant = appsInSuppleInf.get(i + 1);

				List<SpeciesScientificResult> speciesForApp = new ArrayList<SpeciesScientificResult>();
				List<LocationResult> locationsForApp = new ArrayList<LocationResult>();

				int endOfApplicant = applicant.getFinding().getStart()
						+ applicant.getFinding().getLength();
				int startOfNextApplicant = nextApplicant != null ? nextApplicant
						.getFinding().getStart() : info.getSupplInfos().get(applicant.getResult().getTextIdx()).getText().length();

				// find all species between current applicant and next one
				for (SpeciesScientificResult speciesResult : speciesInSuppleInf) {
					if (speciesResult.getFinding().getStart() > endOfApplicant
							&& speciesResult.getFinding().getStart() < startOfNextApplicant)
						speciesForApp.add(speciesResult);
				}

				// find all locations between current applicant and next one
				for (LocationResult locationResult : locationsInSuppleInf) {
					if (locationResult.getFinding().getStart() >= endOfApplicant
							&& locationResult.getFinding().getStart() < startOfNextApplicant)
						locationsForApp.add(locationResult);
				}

				// if there are no specie in between, applicant goes to last
				// event
				if (event != null) {
					List<Applicant> applicantsInEvent = event.getApplicants();
					List<Applicant> applicantsWithoutLoc = new ArrayList<Applicant>();

					Applicant newApplicant = createApplicant(applicant);
					event.getApplicants().add(newApplicant);

					for (Applicant applicantInEvent : applicantsInEvent)
						if (applicantInEvent.getApplicantInstitution()
								.isEmpty()
								&& applicantInEvent.getLocation().isEmpty())
							applicantsWithoutLoc.add(applicantInEvent);

					setLocations(applicantsWithoutLoc, locationsForApp, event);
					setSpecies(speciesForApp, event);
				}

				if (event == null)
					event = createEvent(applicant, info, locationsForApp,
							speciesForApp);

				if (speciesForApp.size() > 0) {
					events.add(event);
					event = null;
				}

			}
			return;
		}

	}

	@Override
	public int getRank() {
		return 0;
	}

	

	
}