001package org.intellimate.izou.sdk.contentgenerator; 002 003import org.intellimate.izou.events.EventModel; 004import org.intellimate.izou.resource.ResourceModel; 005import org.intellimate.izou.sdk.Context; 006import org.intellimate.izou.sdk.resource.Resource; 007import org.intellimate.izou.sdk.specification.ContentGeneratorModel; 008import org.intellimate.izou.sdk.util.AddOnModule; 009import org.intellimate.izou.sdk.util.ResourceCreator; 010 011import java.util.ArrayList; 012import java.util.List; 013import java.util.Optional; 014import java.util.stream.Collectors; 015 016/** 017 * The Task of an ContentGenerator is to generate a Resources-Object when a Event it subscribed to was fired. 018 * <p> 019 * When an Event this ContentGenerator subscribed to was fired, the ContentGeneratorManager will run the instance 020 * of it in a ThreadPool and generate(String eventID) will be called. 021 * </p> 022 */ 023public abstract class ContentGenerator extends AddOnModule implements ContentGeneratorModel, ResourceCreator { 024 025 /** 026 * Creates a new content generator. 027 * 028 * @param id the id of the content generator 029 * @param context the context of the addOn 030 */ 031 public ContentGenerator(String id, Context context) { 032 super(context, id); 033 } 034 035 /** 036 * this method is called to register for what Events it wants to provide Resources. 037 * <p> 038 * The Event has to be in the following format: It should contain only one Descriptor and and one Resource with the 039 * ID "description", which contains an description of the Event. 040 * </p> 041 * @return a List containing ID's for the Events 042 */ 043 @Override 044 public List<? extends EventModel<?>> announceEvents() { 045 return getTriggeredEvents().stream() 046 .map(EventListener::getEvent) 047 .collect(Collectors.toList()); 048 } 049 050 /** 051 * This method is called to register what resources the object provides. 052 * just pass a List of Resources without Data in it. 053 * 054 * @return a List containing the resources the object provides 055 */ 056 @Override 057 public List<? extends ResourceModel> announceResources() { 058 return getTriggeredResources(); 059 } 060 061 /** 062 * this method is called when an object wants to get a Resource. 063 * <p> 064 * Don't use the Resources provided as arguments, they are just the requests. 065 * There is a timeout after 1 second. 066 * </p> 067 * @param list a list of resources without data 068 * @param optional if an event caused the action, it gets passed. It can also be null. 069 * @return a list of resources with data 070 */ 071 @Override 072 public List<ResourceModel> provideResource(List<? extends ResourceModel> list, Optional<EventModel> optional) { 073 //TODO: check arguments and return type here! Missing ID etc. Fail fast! 074 return new ArrayList<>(triggered(list, optional)); 075 } 076 077 /** 078 * this method is called when an object wants to get a Resource. 079 * <p> 080 * Don't use the Resources provided as arguments, they are just the requests. 081 * There is a timeout after 1 second. 082 * </p> 083 * @param list a list of resources without data 084 * @param optional if an event caused the action, it gets passed. It can also be null. 085 * @return a list of resources with data 086 */ 087 public abstract List<? extends Resource> triggered(List<? extends ResourceModel> list, Optional<EventModel> optional); 088}