Skip to content
Open
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
Empty file added .gitignore
Empty file.
3 changes: 3 additions & 0 deletions smartHome/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 5 true
1 8 true
1 10 false
67 changes: 67 additions & 0 deletions smartHome/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cnu2016</groupId>
<artifactId>com.cnu2016.smartHome</artifactId>
<version>0.7.1.201405082137</version>
<packaging>jar</packaging>
<name>JaCoCo Maven plug-in example</name>
<url>http://www.eclemma.org/jacoco</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.1.201405082137</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<!-- implementation is needed only for Maven 2 -->
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- implementation is needed only for Maven 2 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
34 changes: 34 additions & 0 deletions smartHome/src/main/java/com/cnu2016/assignment02/Appliance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cnu2016.assignment02;

public class Appliance {
public enum Type {
WATERHEATER,
AIRCONDITIONER,
COOKINGOVEN
};
private Type type;
private boolean state;
private int ID;
static int cnt = 0;
public Appliance(Type type) {
this.state = false;
this.ID = cnt++;
this.type = type;
}
public boolean getState() {
return state;
}
public void toggle() {
state ^= true;
}
public int getID() {
return ID;
}
public Type getType() {
return type;
}
@Override
public String toString() {
return ID + " " + state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cnu2016.assignment02;

public class BadScheduleException extends Exception {
public BadScheduleException(String msg) {
super(msg);
}
}
10 changes: 10 additions & 0 deletions smartHome/src/main/java/com/cnu2016/assignment02/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.cnu2016.assignment02;

public class Event {
int timeStamp;
Appliance appliance;
public Event(int timeStamp, Appliance appliance) {
this.timeStamp = timeStamp;
this.appliance = appliance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.cnu2016.assignment02;
import java.util.*;
import java.io.*;
public class EventGenerator {
static class Job {
int ID, timeStamp;
boolean state;
public Job (int ID, int timeStamp, boolean state) {
this.ID = ID;
this.timeStamp = timeStamp;
this.state = state;
}
};
public static ArrayList<Job> getSortedJobs(String filename) throws IOException, BadScheduleException {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different methods should be used for different use-cases
use different methods for reading and sorting, say readEvents() and processEvents()

ArrayList<Job> jobs = new ArrayList<Job>();
Scanner in = new Scanner(new FileReader(filename));
while(in.hasNext()) {
int ID = in.nextInt();
int startTime = in.nextInt();
boolean newState = in.nextBoolean();
jobs.add(new Job(ID, startTime, newState));
}
in.close();
Collections.sort(jobs, new Comparator<Job>() {
public int compare(Job a, Job b) {
return a.timeStamp - b.timeStamp;
}
});
return jobs;
}
public static ArrayList<Event> readData(String filename, ArrayList<Appliance> appliances) throws BadScheduleException, IOException {

boolean state[] = new boolean[appliances.size()];
int tim[] = new int[appliances.size()];

ArrayList<Event> events = new ArrayList<Event>();
ArrayList<Job> jobs = getSortedJobs(filename);
for (Job j : jobs) {
int ID = j.ID;
int startTime = j.timeStamp;
boolean newState = j.state;
if (state[ID] == newState) {
throw new BadScheduleException("Please enter a valid schedule");
}
else {
state[ID] = newState;
tim[ID] = startTime;
}
events.add(new Event(startTime, appliances.get(ID)));
}
//Explicity shut off all water heaters which were left on
for (int i = 0; i < appliances.size(); ++i) {
if (appliances.get(i).getType() == Appliance.Type.WATERHEATER && state[i] == true) {
events.add(new Event(tim[i] + 2, appliances.get(i)));
}
}
Collections.sort(events, new Comparator<Event>() {
public int compare(Event a, Event b) {
return a.timeStamp - b.timeStamp;
}
});
return events;
}
}
34 changes: 34 additions & 0 deletions smartHome/src/main/java/com/cnu2016/assignment02/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cnu2016.assignment02;
import java.io.*;
import java.util.*;
public class Main {
public static ArrayList<Appliance> populate() {
ArrayList<Appliance> appliances = new ArrayList<Appliance>();
appliances.add(new Appliance(Appliance.Type.WATERHEATER));
appliances.add(new Appliance(Appliance.Type.AIRCONDITIONER));
appliances.add(new Appliance(Appliance.Type.COOKINGOVEN));
appliances.add(new Appliance(Appliance.Type.WATERHEATER));
return appliances;
}
public static void scheduleAndRun(ArrayList<Event> events, ArrayList<Appliance> appliances) {
Timer timer = new Timer();
events.forEach( e -> {
timer.schedule(new TimerTask() {
@Override
public void run() {
e.appliance.toggle();
System.out.printf("At time %d\n", e.timeStamp);
appliances.forEach( a -> {
System.out.println(a);
});
}
}, e.timeStamp * 1000);
});
}
public static void main(String args[]) throws InterruptedException, IOException, BadScheduleException {
ArrayList<Appliance> appliances = populate();
ArrayList<Event> events = EventGenerator.readData("/projects/assignments/smartHome/input.txt", appliances);
scheduleAndRun(events, appliances);
Thread.sleep(10 * 1000);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cnu2016.assignment02;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ApplicanceTest {
@Test
public void testBasics() {
Appliance a = new Appliance(Appliance.Type.WATERHEATER);
Appliance b = new Appliance(Appliance.Type.COOKINGOVEN);
assertEquals(a.getID() + 1,b.getID());
assertEquals(a.getType(), Appliance.Type.WATERHEATER);
assertEquals(b.getType(), Appliance.Type.COOKINGOVEN);
assertEquals(a.getState(), false);
a.toggle();
assertEquals(a.getState(), true);
a.toggle();
assertEquals(a.getState(), false);
boolean check = a.toString().contains("false");
assertEquals(check, true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.cnu2016.assignment02;
import java.util.*;
import java.io.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EventGeneratorTest {
@Test
public void testWaterHeaterAutoShutOff() throws IOException, BadScheduleException {
ArrayList<Appliance> appliances = new ArrayList<Appliance>();
appliances.add(new Appliance(Appliance.Type.WATERHEATER));
String filename = "/projects/assignments/smartHome/src/test/java/testData/testWaterHeaterAutoShutOff.txt";
ArrayList<Event> ret = EventGenerator.readData(filename, appliances);
assertEquals(ret.size(), 2);
assertEquals(ret.get(0).timeStamp, 2);
assertEquals(ret.get(1).timeStamp, 4);
}
@Test
public void testBadSchedule() throws IOException, BadScheduleException {
ArrayList<Appliance> appliances = new ArrayList<Appliance>();
appliances.add(new Appliance(Appliance.Type.WATERHEATER));
String filename = "/projects/assignments/smartHome/src/test/java/testData/testBadScheduleException.txt";
try {
ArrayList<Event> ret = EventGenerator.readData(filename, appliances);
assertEquals(true, false);
}
catch (Exception e) {
boolean check = (e instanceof BadScheduleException);
assertEquals(check, true);
}
}
@Test
public void testGoodSchedule() throws IOException, BadScheduleException {
ArrayList<Appliance> appliances = new ArrayList<Appliance>();
appliances.add(new Appliance(Appliance.Type.WATERHEATER));
appliances.add(new Appliance(Appliance.Type.COOKINGOVEN));
appliances.add(new Appliance(Appliance.Type.AIRCONDITIONER));
String filename = "/projects/assignments/smartHome/src/test/java/testData/testGoodScheduleException.txt";
try {
ArrayList<Event> ret = EventGenerator.readData(filename, appliances);
}
catch (Exception e) {
assertEquals(true, false);
}
}
}
12 changes: 12 additions & 0 deletions smartHome/src/test/java/com/cnu2016/assignment02/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cnu2016.assignment02;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EventTest {
@Test
public void testBasics() {
Appliance a = new Appliance(Appliance.Type.WATERHEATER);
Event e = new Event(23, a);
assertEquals(e.timeStamp, 23);
assertEquals(e.appliance.getID(), a.getID());
}
}
30 changes: 30 additions & 0 deletions smartHome/src/test/java/com/cnu2016/assignment02/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cnu2016.assignment02;
import java.util.*;
import java.io.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MainTest {
@Test
public void testPopulate() {
ArrayList<Appliance> ret = Main.populate();
assertEquals(ret.size(), 4);
assertEquals(ret.get(0).getType(), Appliance.Type.WATERHEATER);
assertEquals(ret.get(1).getType(), Appliance.Type.AIRCONDITIONER);
assertEquals(ret.get(2).getType(), Appliance.Type.COOKINGOVEN);
assertEquals(ret.get(3).getType(), Appliance.Type.WATERHEATER);
}
@Test
public void testScheduleAndRun() throws IOException, BadScheduleException {
ArrayList<Appliance> appliances = Main.populate();
String filename = "/projects/assignments/smartHome/src/test/java/testData/testGoodScheduleException.txt";
ArrayList<Event> events = EventGenerator.readData(filename, appliances);
try {
Main.scheduleAndRun(events, appliances);
Thread.sleep(10 * 1000);
assertEquals(true, true);
}
catch (Exception e) {
assertEquals(true, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 2 false
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0 2 true
0 3 false
0 5 true
1 7 true
2 9 true
0 10 false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 2 true