Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import natty to support natural language #36

Merged
merged 4 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ allprojects {
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonDataTypeVersion"
compile "com.google.guava:guava:$guavaVersion"
compile group: 'com.joestelmach', name: 'natty', version: '0.12'
compile group: 'org.slf4j', name: 'slf4j-nop', version: '1.7.10'

testCompile "junit:junit:$junitVersion"
testCompile "org.testfx:testfx-core:$testFxVersion"
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package seedu.address;

import com.google.common.eventbus.Subscribe;
import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
Expand All @@ -23,6 +26,8 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
Expand Down Expand Up @@ -198,6 +203,8 @@ public void handleStoragePathChangedEvent(StoragePathChangedEvent event) {
}

public static void main(String[] args) {
launch(args);
}
launch(args);

}
}

73 changes: 43 additions & 30 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import seedu.address.model.tag.UniqueTagList;
import seedu.address.model.task.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -15,65 +19,74 @@ public class AddCommand extends Command {

public static final String COMMAND_WORD = "add";

public static final String MESSAGE_USAGE = COMMAND_WORD
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Add an event with a starting and ending date or a task (with or without deadline) to the task manager.\n"
+ "Parameters: EVENT_NAME s/START_DATE e/END_DATE [t/TAG]... or TASK_NAME [d/DEADLINE] [t/TAG]...\n"// [p/PRIORITY_LEVEL]
+ "Example: " + COMMAND_WORD
+ " Lecture s/7.10.2016-14 e/7.10.2016-16 t/CS2103, add Project Deadline d/14.10.2016 t/CS2103"; //p/3
+ " Lecture s/7.10.2016-14 e/7.10.2016-16 t/CS2103, add Project Deadline d/14.10.2016 t/CS2103"; // p/3

public static final String MESSAGE_EVENT_SUCCESS = "New event added: %1$s";
public static final String MESSAGE_TASK_SUCCESS = "New task added: %1$s";
public static final String MESSAGE_DUPLICATE_TASK = "It's already exists in the task manager";
public static final String DATE_VALIDATION_REGEX = "^[0-3]?[0-9].[0-1]?[0-9].([0-9]{4})(-[0-2]?[0-9]?)?";

private final Task toAdd;

/**
* Convenience constructor using raw values.
*
* @throws IllegalValueException if any of the raw values are invalid
* @throws IllegalValueException
* if any of the raw values are invalid
*/
public AddCommand(String name, String deadline, Set<String> tags,String freq)
throws IllegalValueException {
public AddCommand(String name, String deadline, Set<String> tags, String freq) throws IllegalValueException {
final Set<Tag> tagSet = new HashSet<>();
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
if (!deadline.matches(DATE_VALIDATION_REGEX) && !deadline.equals("")) {
List<Date> date = new com.joestelmach.natty.Parser().parse(deadline).get(0).getDates();
deadline = dateFormat.format(date.get(0)).toString() + "-" + date.get(0).toString().substring(11, 13);
}
for (String tagName : tags) {
tagSet.add(new Tag(tagName));
}
if(deadline!=""&&freq!=""){
this.toAdd = new Task(
new Name(name),
new Deadline(deadline),
new UniqueTagList(tagSet),
new Recurring(freq)
);
}else if(deadline!=""){
this.toAdd=new Task(new Name(name),new Deadline(deadline),new UniqueTagList(tagSet));
}else{
this.toAdd=new Task(new Name(name),new UniqueTagList(tagSet));
if (deadline != "" && freq != "") {
this.toAdd = new Task(new Name(name), new Deadline(deadline), new UniqueTagList(tagSet),
new Recurring(freq));
} else if (deadline != "") {
this.toAdd = new Task(new Name(name), new Deadline(deadline), new UniqueTagList(tagSet));
} else {
this.toAdd = new Task(new Name(name), new UniqueTagList(tagSet));
}
}

/**
* Convenience constructor using raw values.
*
* @throws IllegalValueException if any of the raw values are invalid
* @throws IllegalValueException
* if any of the raw values are invalid
*/
public AddCommand(String name, String startDate, String endDate, Set<String> tags,String freq)
public AddCommand(String name, String startDate, String endDate, Set<String> tags, String freq)
throws IllegalValueException {
final Set<Tag> tagSet = new HashSet<>();
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
List<Date> date;
if (!startDate.matches(DATE_VALIDATION_REGEX) && !startDate.equals("")) {
date = new com.joestelmach.natty.Parser().parse(startDate).get(0).getDates();
startDate = dateFormat.format(date.get(0)).toString() + "-" + date.get(0).toString().substring(11, 13);
}
if (!endDate.matches(DATE_VALIDATION_REGEX) && !endDate.equals("")) {
date = new com.joestelmach.natty.Parser().parse(endDate).get(0).getDates();
endDate = dateFormat.format(date.get(0)).toString() + "-" + date.get(0).toString().substring(11, 13);
}
for (String tagName : tags) {
tagSet.add(new Tag(tagName));
}
if(freq!=""&&startDate!=""){
this.toAdd = new Task(
new Name(name),
new EventDate(startDate, endDate),
new UniqueTagList(tagSet),
new Recurring(freq)
);
}else if(startDate!=""){
this.toAdd=new Task(new Name(name),new EventDate(startDate,endDate),new UniqueTagList(tagSet));
}else{
this.toAdd=new Task(new Name(name),new UniqueTagList(tagSet));
if (freq != "" && startDate != "") {
this.toAdd = new Task(new Name(name), new EventDate(startDate, endDate), new UniqueTagList(tagSet),
new Recurring(freq));
} else if (startDate != "") {
this.toAdd = new Task(new Name(name), new EventDate(startDate, endDate), new UniqueTagList(tagSet));
} else {
this.toAdd = new Task(new Name(name), new UniqueTagList(tagSet));
}
}

Expand All @@ -89,7 +102,7 @@ public CommandResult execute() {
return new CommandResult(MESSAGE_DUPLICATE_TASK);
}
}

public static String getSuccessMessage(Task toAdd) {
if (toAdd.isEvent()) {
return MESSAGE_EVENT_SUCCESS;
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/seedu/address/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class Parser {
Pattern.compile("(?<name>[^/]+)" + "s/(?<startDate>[^/]+)" + "e/(?<endDate>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of
// tags
private static final String MESSAGE_INVALID_DATE = "Date format entered is invalid";

public static final Prefix deadlinePrefix = new Prefix("d/");
public static final Prefix tagPrefix = new Prefix("t/");
Expand Down Expand Up @@ -107,7 +108,7 @@ public Command parseCommand(String userInput) {

case DoneCommand.COMMAND_WORD:
return prepareMarkAsDone(arguments);

case RefreshCommand.COMMAND_WORD:
return new RefreshCommand();

Expand Down Expand Up @@ -161,12 +162,13 @@ private Command prepareAdd(String args) {
argsTokenizer.tokenize(args);
try {
if (argsTokenizer.getTokenizedArguments().containsKey(namePrefix)) {

if (!argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix)
&& !argsTokenizer.getTokenizedArguments().containsKey(deadlinePrefix)) {
// non-recurring task
return new AddCommand(argsTokenizer.getValue(namePrefix).get(), "",
toSet(argsTokenizer.getAllValues(tagPrefix)), "");}

if (!argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix)
&& !argsTokenizer.getTokenizedArguments().containsKey(deadlinePrefix)) {
// non-recurring task
return new AddCommand(argsTokenizer.getValue(namePrefix).get(), "",
toSet(argsTokenizer.getAllValues(tagPrefix)), "");
}
// check if task is recurring floating task
if (argsTokenizer.getTokenizedArguments().containsKey(deadlinePrefix)
&& argsTokenizer.getTokenizedArguments().containsKey(recurringPrefix)) {
Expand All @@ -178,8 +180,7 @@ private Command prepareAdd(String args) {
return new AddCommand(argsTokenizer.getValue(namePrefix).get(),
argsTokenizer.getValue(deadlinePrefix).get(), toSet(argsTokenizer.getAllValues(tagPrefix)),
"");
}
else if (argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix)
} else if (argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix)
&& argsTokenizer.getTokenizedArguments().containsKey(endDatePrefix)) {
if (!argsTokenizer.getTokenizedArguments().containsKey(recurringPrefix))
// non-recurring event
Expand All @@ -199,6 +200,8 @@ else if (argsTokenizer.getTokenizedArguments().containsKey(startDatePrefix)
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
} catch (IllegalValueException ive) {
return new IncorrectCommand(ive.getMessage());
} catch (Exception e) {
return new IncorrectCommand(MESSAGE_INVALID_DATE);
}

}
Expand Down
33 changes: 17 additions & 16 deletions src/test/java/guitests/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,52 @@ public class AddCommandTest extends TaskManagerGuiTest {

@Test
public void add() {
//add one person
// add one person
TestTask[] currentList = td.getTypicalTasks();
TestTask taskToAdd = td.project;
assertAddSuccess(taskToAdd, currentList);
currentList = TestUtil.addTasksToList(currentList, taskToAdd);

//add another person
// add another person
taskToAdd = td.workshop;
//currentList = TestUtil.addTasksToList(currentList, taskToAdd);
// currentList = TestUtil.addTasksToList(currentList, taskToAdd);
assertAddSuccess(taskToAdd, currentList);
//add to empty list

// add to empty list
commandBox.runCommand("clear");
assertAddSuccess(td.friend);

//add project to current list with flexi command
taskToAdd=td.project;
assertFlexiAddSuccess(taskToAdd,td.friend);


//invalid command
// add project to current list with flexi command
taskToAdd = td.project;
assertFlexiAddSuccess(taskToAdd, td.friend);

// invalid command
commandBox.runCommand("adds Johnny");
assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
}

private void assertAddSuccess(TestTask personToAdd, TestTask... currentList) {
commandBox.runCommand(personToAdd.getAddCommand());

//confirm the new card contains the right data
// confirm the new card contains the right data
TaskCardHandle addedCard = taskListPanel.navigateToTask(personToAdd.getName().taskName);
assertMatching(personToAdd, addedCard);

//confirm the list now contains all previous persons plus the new person
// confirm the list now contains all previous persons plus the new
// person
TestTask[] expectedList = TestUtil.addTasksToList(currentList, personToAdd);
assertTrue(taskListPanel.isListMatching(expectedList));
}

private void assertFlexiAddSuccess(TestTask personToAdd, TestTask... currentList) {
commandBox.runCommand(personToAdd.getFlexiAddCommand());

//confirm the new card contains the right data
// confirm the new card contains the right data
TaskCardHandle addedCard = taskListPanel.navigateToTask(personToAdd.getName().taskName);
assertMatching(personToAdd, addedCard);

//confirm the list now contains all previous persons plus the new person
// confirm the list now contains all previous persons plus the new
// person
TestTask[] expectedList = TestUtil.addTasksToList(currentList, personToAdd);
assertTrue(taskListPanel.isListMatching(expectedList));
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ public void execute_add_invalidArgsFormat() throws Exception {
public void execute_add_invalidTaskData() throws Exception {
assertCommandBehavior(
"add n/[]\\[;] d/11.12.2016", Name.MESSAGE_NAME_CONSTRAINTS);
assertCommandBehavior(
"add n/Valid Name d/not_numbers", Deadline.MESSAGE_DEADLINE_CONSTRAINTS);
assertCommandBehavior(
"add n/Valid Name d/11.12.2016-14 t/invalid_-[.tag", Tag.MESSAGE_TAG_CONSTRAINTS);

Expand Down