forked from EllaVator/EllaVator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module file for Ella that is connection between Domain_Opendial_Ella.…
…xml and the elevator (ElevatorController.java file)
- Loading branch information
lf
authored and
lf
committed
Aug 31, 2015
1 parent
6fb7416
commit c491225
Showing
1 changed file
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// ================================================================= | ||
// Copyright (C) 2011-2015 Pierre Lison (plison@ifi.uio.no) | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the "Software"), to deal in the Software without restriction, | ||
// including without limitation the rights to use, copy, modify, merge, | ||
// publish, distribute, sublicense, and/or sell copies of the Software, | ||
// and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// ================================================================= | ||
|
||
|
||
import java.util.logging.*; | ||
import java.awt.Color; | ||
import java.awt.GridLayout; | ||
import java.util.Collection; | ||
|
||
import javax.swing.BorderFactory; | ||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
|
||
import opendial.DialogueState; | ||
import opendial.DialogueSystem; | ||
import opendial.modules.Module; | ||
|
||
/** | ||
* Simple example of a synchronous module for the domain specified in | ||
* domains/examples/example-step-by-step.xml. | ||
* | ||
* <p> | ||
* The example creates a visual grid of size GRID_SIZE and updates the position of | ||
* the agent in accordance with the movements. | ||
* | ||
* @author Pierre Lison (plison@ifi.uio.no) | ||
*/ | ||
public class ModuleElla implements Module { | ||
|
||
// logger | ||
final static Logger log = Logger.getLogger("OpenDial"); | ||
|
||
|
||
//create controller to speak with the elevator via serial port | ||
final static ElevatorControllerInterface controller= new ElevatorController(); | ||
|
||
//number of floors (used for JFrame output) and the JFrame itself | ||
public static int FLOORS = 6; | ||
JFrame frame; | ||
|
||
boolean paused = true; | ||
|
||
// the controller is commented out at the moment because it is not attached and it crashes if it is not attached | ||
// the current floor we are on | ||
//int currentPosition = controller.getCurrentFloor(); | ||
int currentPosition = 0; | ||
|
||
public ModuleElla(DialogueSystem system) { | ||
} | ||
|
||
/** | ||
* Creates a simple visual grid of size FLOORS and puts the elevator where it is / zero-th floor. | ||
*/ | ||
@Override | ||
public void start() { | ||
frame = new JFrame(); | ||
|
||
frame.setLayout(new GridLayout(FLOORS, 1)); | ||
for (int i = 0; i < FLOORS; i++) { | ||
JLabel label = new JLabel(" "); | ||
label.setBorder(BorderFactory.createLineBorder(Color.BLACK)); | ||
frame.add(label); | ||
} | ||
((JLabel) frame.getContentPane().getComponent(FLOORS - currentPosition - 1 )) | ||
.setText(" HERE"); | ||
frame.setSize(500, 500); | ||
frame.setVisible(true); | ||
paused = false; | ||
} | ||
|
||
/** | ||
* If the updated variables contain the system action "a_m" and the action is a | ||
* movement, updates the visual grid and the elevator itself in accordance with the movement. | ||
*/ | ||
@Override | ||
public void trigger(DialogueState state, Collection<String> updatedVars) { | ||
if (updatedVars.contains("a_m") && state.hasChanceNode("a_m") && !paused) { | ||
String actionValue = state.queryProb("a_m").getBest().toString(); | ||
//same as in ModuleExample1 until here | ||
//System.out.println prints into the console while running | ||
System.out.println("EllaModule actionValue: \"" + actionValue + "\""); | ||
if (actionValue.startsWith("Movefloor(")) { | ||
String togoFloor = | ||
//actionValue.substring(10 <-- to not take into account the first 10 elements of string, -1 <-- to ignore the closing brackets | ||
actionValue.substring(10, actionValue.length() - 1); | ||
//System.out.println prints into the console while running | ||
System.out.println("EllaModule: go to floor: " + togoFloor); | ||
changePosition(togoFloor); | ||
} | ||
//the restrooms have to be done seperately, because the command is not "Movefloor(" | ||
if (actionValue.equals("restrooms")) { | ||
changePosition("restrooms"); | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Changes the position of the elevator and its visual grid depending on the specified floor. | ||
* | ||
* @param direction the direction, as a string. | ||
*/ | ||
private void changePosition(String togoFloor) { | ||
int newFloor = 0; | ||
//convert string to internal floor number | ||
if (togoFloor.equals("zero")) { | ||
newFloor = 0; | ||
} | ||
else if (togoFloor.equals("restrooms")) { | ||
newFloor = 1; | ||
} | ||
else if (togoFloor.equals("one")) { | ||
newFloor = 2; | ||
} | ||
else if (togoFloor.equals("one point five")) { | ||
newFloor = 3; | ||
} | ||
else if (togoFloor.equals("two")) { | ||
newFloor = 4; | ||
} | ||
else if (togoFloor.equals("three")) { | ||
newFloor = 5; | ||
} else { | ||
System.out.println("EllaModule unknown floor: \"" + togoFloor + "\""); | ||
} | ||
|
||
System.out.println("EllaModule new floor: " + newFloor); | ||
//controller commented out again because it is not attached | ||
//move elevator | ||
//controller.pushButton(newFloor); | ||
|
||
//change display | ||
if (newFloor >= 0 && newFloor < FLOORS) { | ||
((JLabel) frame.getContentPane().getComponent(FLOORS - currentPosition - 1 )) | ||
.setText(" "); | ||
currentPosition = newFloor; | ||
((JLabel) frame.getContentPane().getComponent(FLOORS - currentPosition - 1 )) | ||
.setText(" HERE"); | ||
} | ||
} | ||
|
||
/** | ||
* Pauses the module. | ||
*/ | ||
@Override | ||
public void pause(boolean toPause) { | ||
paused = toPause; | ||
} | ||
|
||
/** | ||
* Returns true is the module is not paused, and false otherwise. | ||
*/ | ||
@Override | ||
public boolean isRunning() { | ||
return !paused; | ||
} | ||
|
||
} | ||
|
||
|