standup.joke
Class Backend

java.lang.Object
  extended by standup.joke.Backend
All Implemented Interfaces:
Profileable
Direct Known Subclasses:
BackendJokeSetOnly, BackendJokeSetSQL, BackendSQL

public abstract class Backend
extends Object
implements Profileable

The main class through which joke generation and related functionality should be accessed.

A Backend provides the following main functionality:

Author:
Ruli Manurung

Field Summary
protected  Generator generator
           
private  LexicalComponents lexicalComponents
           
private  ProfileJokeGeneration profileJokeGeneration
           
 
Constructor Summary
Backend()
          Default constructor that instantiates a default Generator and a default LexicalComponents, which will eventually be configured when a user profile is set.
 
Method Summary
 void addToFavourites(JokeStructure j)
          Adds the given JokeStructure to the current user's favourite jokes collection.
 JokeConstraints buildUserProfileConstraints()
          Builds and returns a JokeConstraints that represents the various parameters and choices in the current ProfileJokeGeneration.
 boolean createProfile(File directory)
          Creates a default profile in the given directory.
(package private) abstract  JokeStructure generateNewJoke(String newJokeID, JokeConstraints jcs)
          Attempts to generate a new joke that satisfies the given JokeConstraints and labels it with the given joke ID.
 JokeTypeSet getCurrentUserUsedJokeTypes()
          Returns a JokeTypeSet containing the current user's used JokeTypes.
 JokeSet getFavouriteJokes()
          Returns the subset of the current user's favourite jokes that satisfy the current user's joke constraints.
 Generator getGenerator()
          Returns the Generator instance.
 LexicalComponents getLexicalComponents()
          Returns the LexicalComponents instance.
 JokeStructure getNewJoke()
          Attempts to generate a new joke that satisfies the current user profile's constraints.
 JokeStructure getNewJoke(JokeConstraints jcs)
          Tries to generate a new joke that satisfies the given JokeConstraints.
 JokeStructure getNewJoke(JokeType t)
          Attempts to generate a new joke that satisfies the current user profile's constraints and is of the given JokeType.
 JokeStructure getNewJoke(Lexeme k)
          Attempts to generate a new joke that satisfies the current user profile's constraints and uses the given Lexeme in its answer.
 JokeStructure getNewJoke(LexemeSet lxs)
          Attempts to generate a new joke that satisfies the current user profile's constraints and uses at least one Lexeme from the given LexemeSet in its answer.
 JokeStructure getNewJoke(Topic t)
          Attempts to generate a new joke that satisfies the current user profile's constraints and uses at least one Lexeme 'about' the given Topic in its answer.
private  String getNextJokeID()
          Returns a unique joke ID for the next joke to be generated.
 JokeSet getOldJokes()
          Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints.
 JokeSet getOldJokes(JokeConstraints constraints)
          Returns the subset of the current user's previously generated jokes that satisfy the given JokeConstraints.
 JokeSet getOldJokes(JokeType t)
          Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and is of the given JokeType.
 JokeSet getOldJokes(Lexeme k)
          Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and use the given Lexeme in either their question or answer.
 JokeSet getOldJokes(LexemeSet lxs)
          Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and use at least one Lexeme from the given LexemeSet in their question or answer.
 JokeSet getOldJokes(Topic t)
          Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and use at least one Lexeme 'about' the given Topic in their question or answer.
 ProfileJokeGeneration getProfile()
          Returns the ProfileJokeGeneration currently being used by this Backend.
 String getProfileFilename()
          Returns the filename for joke generation profiles.
 Topic getRootTopic()
          Currently returns the first found root topic as specified by LexicalComponents#getRootTopicLabels().
 ProfileJokeGeneration loadProfile(File directory)
          Loads the joke generation profile from the specified directory and returns it.
 boolean setProfile(Profile profile, boolean repeat)
          Sets this Backend to use the specified joke generation profile.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

generator

protected Generator generator

profileJokeGeneration

private ProfileJokeGeneration profileJokeGeneration

lexicalComponents

private final LexicalComponents lexicalComponents
Constructor Detail

Backend

public Backend()
        throws GeneratorException
Default constructor that instantiates a default Generator and a default LexicalComponents, which will eventually be configured when a user profile is set.

Throws:
GeneratorException
Method Detail

buildUserProfileConstraints

public JokeConstraints buildUserProfileConstraints()
Builds and returns a JokeConstraints that represents the various parameters and choices in the current ProfileJokeGeneration.

Returns:
a JokeConstraints representing the current user profile's settings.
See Also:
ProfileJokeGeneration#addUserConstraints(JokeConstraints, JokeTypeSet)

getNextJokeID

private String getNextJokeID()
Returns a unique joke ID for the next joke to be generated. The ID takes the following form:

joke_ + current user name + joke_ + next joke counter

For example, if current user "ruli" has previously generated 10 jokes, this method will return "joke_ruli_10".

Returns:
See Also:
ProfileJokeGeneration.getNextJokeCounter()

getNewJoke

public JokeStructure getNewJoke(JokeConstraints jcs)
Tries to generate a new joke that satisfies the given JokeConstraints. If successful, adds it to the current user profile's generated joke log and returns it. Otherwise, returns null.

In practice, this method calls on generateNewJoke(String, JokeConstraints), whose implementation is specific to the subclass of Backend.

Parameters:
jcs - the constraints on the joke to be generated
Returns:
a new joke that satisfies jcs, or null if none could be generated

generateNewJoke

abstract JokeStructure generateNewJoke(String newJokeID,
                                       JokeConstraints jcs)
Attempts to generate a new joke that satisfies the given JokeConstraints and labels it with the given joke ID. This should never be called directly -- use getNewJoke(JokeConstraints) instead.

Parameters:
newJokeID - the joke ID for the generated joke
jcs - the constraints on the joke to be generated
Returns:
a new joke that satisfies jcs, or null if none could be generated

getNewJoke

public JokeStructure getNewJoke()
Attempts to generate a new joke that satisfies the current user profile's constraints.

Returns:
a new joke if successfully generated, null otherwise.
See Also:
buildUserProfileConstraints()

getNewJoke

public JokeStructure getNewJoke(JokeType t)
Attempts to generate a new joke that satisfies the current user profile's constraints and is of the given JokeType.

Parameters:
t - the type of joke requested
Returns:
a new joke if successfully generated, null otherwise.
See Also:
buildUserProfileConstraints(), JokeConstraintType

getNewJoke

public JokeStructure getNewJoke(Lexeme k)
Attempts to generate a new joke that satisfies the current user profile's constraints and uses the given Lexeme in its answer.

Currently this does not support generating a joke where the Lexeme appears in the question of the joke.

Parameters:
k - the lexeme to appear in the joke's answer
Returns:
a new joke if successfully generated, null otherwise.
See Also:
buildUserProfileConstraints(), InstantiationConstraintLexemeSchema

getNewJoke

public JokeStructure getNewJoke(LexemeSet lxs)
Attempts to generate a new joke that satisfies the current user profile's constraints and uses at least one Lexeme from the given LexemeSet in its answer.

Currently this does not support generating a joke where the Lexeme appears in the question of the joke.

Parameters:
lxs - the set of which at least one member Lexeme appears in the joke's answer
Returns:
a new joke if successfully generated, null otherwise.
See Also:
buildUserProfileConstraints(), InstantiationConstraintLexiconSchema

getNewJoke

public JokeStructure getNewJoke(Topic t)
Attempts to generate a new joke that satisfies the current user profile's constraints and uses at least one Lexeme 'about' the given Topic in its answer. A Lexeme is considered to be about a given Topic if it appears in the Topic's set of Lexemes or any of its subTopics' set of Lexemes.

Currently this does not support generating a joke where the Lexeme appears in the question of the joke.

Parameters:
t - the Topic that at least one Lexeme appearing in the joke's answer should be about
Returns:
a new joke if successfully generated, null otherwise.
See Also:
buildUserProfileConstraints(), InstantiationConstraintLexiconSchema, Topic.getAllKeywords()

getOldJokes

public JokeSet getOldJokes(JokeConstraints constraints)
Returns the subset of the current user's previously generated jokes that satisfy the given JokeConstraints.

Parameters:
constraints - the constraints that the returned jokes must satisfy
Returns:
a JokeSet of previously generated jokes that satisfy constraints
See Also:
ProfileJokeGeneration.getGeneratedJokeLog(), JokeSet.getJokes(JokeConstraints)

getOldJokes

public JokeSet getOldJokes()
Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints.

To get the entire generated joke log, regardless of the user's constraints, call Backend.getProfile().getGeneratedJokeLog() instead.

Returns:
a JokeSet of previously generated jokes that satisfy the current user profile's constraints
See Also:
buildUserProfileConstraints(), ProfileJokeGeneration.getGeneratedJokeLog()

getOldJokes

public JokeSet getOldJokes(JokeType t)
Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and is of the given JokeType.

Parameters:
t - the type of joke requested
Returns:
a JokeSet of previously generated jokes that satisfies the conditions above
See Also:
buildUserProfileConstraints(), JokeConstraintType

getOldJokes

public JokeSet getOldJokes(Lexeme k)
Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and use the given Lexeme in either their question or answer.

Note the current asymmetry with getNewJoke(Lexeme), which only generates a joke where the Lexeme appears in the answer.

Parameters:
k - the Lexeme to appear in the jokes
Returns:
a JokeSet of previously generated jokes that satisfies the conditions above
See Also:
buildUserProfileConstraints(), InstantiationConstraintLexeme

getOldJokes

public JokeSet getOldJokes(LexemeSet lxs)
Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and use at least one Lexeme from the given LexemeSet in their question or answer.

Note the current asymmetry with getNewJoke(LexemeSet), which only generates a joke where a member Lexeme appears in the answer.

Parameters:
lxs - the set of which at least one member Lexeme appears in the jokes
Returns:
a JokeSet of previously generated jokes that satisfies the conditions above
See Also:
buildUserProfileConstraints(), InstantiationConstraintLexicon

getOldJokes

public JokeSet getOldJokes(Topic t)
Returns the subset of the current user's previously generated jokes that satisfy the current user profile's constraints and use at least one Lexeme 'about' the given Topic in their question or answer. A Lexeme is considered to be about a given Topic if it appears in the Topic's set of Lexemes or any of its subTopics' set of Lexemes.

Note the current asymmetry with getNewJoke(Topic), which only generates a joke where a relevant Lexeme appears in the answer.

Parameters:
t - the Topic that at least one Lexeme appearing in the joke should be about
Returns:
a JokeSet of previously generated jokes that satisfies the conditions above
See Also:
buildUserProfileConstraints(), InstantiationConstraintLexicon, Topic.getAllKeywords()

getFavouriteJokes

public JokeSet getFavouriteJokes()
Returns the subset of the current user's favourite jokes that satisfy the current user's joke constraints.

To get the entire favourite jokes, regardless of the user's constraints, call Backend.getProfile().getFavourites() instead.

Returns:
the current user's favourite jokes.
See Also:
buildUserProfileConstraints(), ProfileJokeGeneration.getFavourites()

addToFavourites

public void addToFavourites(JokeStructure j)
Adds the given JokeStructure to the current user's favourite jokes collection.

Parameters:
j - the joke to be added

getGenerator

public Generator getGenerator()
Returns the Generator instance.

Returns:

getLexicalComponents

public LexicalComponents getLexicalComponents()
Returns the LexicalComponents instance.

Returns:

getCurrentUserUsedJokeTypes

public JokeTypeSet getCurrentUserUsedJokeTypes()
Returns a JokeTypeSet containing the current user's used JokeTypes.

Returns:
See Also:
OptionsJokeGeneration.getUsedJokeTypes(JokeTypeSet)

getRootTopic

public Topic getRootTopic()
Currently returns the first found root topic as specified by LexicalComponents#getRootTopicLabels().

Returns:

createProfile

public boolean createProfile(File directory)
Creates a default profile in the given directory. It uses the default OptionsJokeGeneration contained within the OptionsPackage returned by ProfileManager.getDefaultOptionsPackage().

Specified by:
createProfile in interface Profileable
Parameters:
directory - the directory where the profile is to be created
Returns:
true if the profile was successfully created, false otherwise

getProfile

public ProfileJokeGeneration getProfile()
Returns the ProfileJokeGeneration currently being used by this Backend.

Specified by:
getProfile in interface Profileable
Returns:
the currently used ProfileJokeGeneration

getProfileFilename

public String getProfileFilename()
Returns the filename for joke generation profiles. Currently this is 'jokegeneration.profile'.

Specified by:
getProfileFilename in interface Profileable
Returns:
"jokegeneration.profile"

loadProfile

public ProfileJokeGeneration loadProfile(File directory)
Loads the joke generation profile from the specified directory and returns it.

Specified by:
loadProfile in interface Profileable
Parameters:
directory - The directory containing the joke generation profile
Returns:
the instantiated ProfileJokeGeneration

setProfile

public boolean setProfile(Profile profile,
                          boolean repeat)
Sets this Backend to use the specified joke generation profile.

Specified by:
setProfile in interface Profileable
Parameters:
profile - the Profile to be used
repeat - whether this is the first time a profile is being set for the current user during this session
Returns:
true if successful, false otherwise (e.g. if the given profile was not an instance of ProfileJokeGeneration.