use of jmri.JmriException in project JMRI by JMRI.
the class DefaultConditional method parseCalculate.
/**
* Parses and computes one parenthesis level of a boolean statement.
* <p>
* Recursively calls inner parentheses levels. Note that all logic operators
* are detected by the parsing, therefore the internal negation of a
* variable is washed.
*
* @param s The expression to be parsed
* @param variableList ConditionalVariables for R1, R2, etc
* @return a data pair consisting of the truth value of the level a count of
* the indices consumed to parse the level and a bitmap of the
* variable indices used.
* @throws jmri.JmriException if unable to compute the logic
*/
DataPair parseCalculate(String s, ArrayList<ConditionalVariable> variableList) throws JmriException {
// for simplicity, we force the string to upper case before scanning
s = s.toUpperCase();
BitSet argsUsed = new BitSet(_variableList.size());
DataPair dp = null;
boolean leftArg = false;
boolean rightArg = false;
int oper = OPERATOR_NONE;
int k = -1;
// index of String s
int i = 0;
//int numArgs = 0;
if (s.charAt(i) == '(') {
dp = parseCalculate(s.substring(++i), variableList);
leftArg = dp.result;
i += dp.indexCount;
argsUsed.or(dp.argsUsed);
} else // cannot be '('. must be either leftArg or notleftArg
{
if (s.charAt(i) == 'R') {
//NOI18N
try {
k = Integer.parseInt(String.valueOf(s.substring(i + 1, i + 3)));
i += 2;
} catch (NumberFormatException | IndexOutOfBoundsException nfe) {
k = Integer.parseInt(String.valueOf(s.charAt(++i)));
}
leftArg = variableList.get(k - 1).evaluate();
if (variableList.get(k - 1).isNegated()) {
leftArg = !leftArg;
}
i++;
argsUsed.set(k - 1);
} else if (Bundle.getMessage("LogicNOT").equals(s.substring(i, i + (Bundle.getMessage("LogicNOT").length())))) {
// compare the right length after i18n
// was: 3;
i += Bundle.getMessage("LogicNOT").length();
//not leftArg
if (s.charAt(i) == '(') {
dp = parseCalculate(s.substring(++i), variableList);
leftArg = dp.result;
i += dp.indexCount;
argsUsed.or(dp.argsUsed);
} else if (s.charAt(i) == 'R') {
//NOI18N
try {
k = Integer.parseInt(String.valueOf(s.substring(i + 1, i + 3)));
i += 2;
} catch (NumberFormatException | IndexOutOfBoundsException nfe) {
k = Integer.parseInt(String.valueOf(s.charAt(++i)));
}
leftArg = variableList.get(k - 1).evaluate();
if (variableList.get(k - 1).isNegated()) {
leftArg = !leftArg;
}
i++;
argsUsed.set(k - 1);
} else {
throw new JmriException(java.text.MessageFormat.format(rbx.getString("ParseError1"), new Object[] { s.substring(i) }));
}
leftArg = !leftArg;
} else {
throw new JmriException(java.text.MessageFormat.format(rbx.getString("ParseError9"), new Object[] { s }));
}
}
// crank away to the right until a matching parent is reached
while (i < s.length()) {
if (s.charAt(i) != ')') {
// must be either AND or OR
if (Bundle.getMessage("LogicAND").equals(s.substring(i, i + (Bundle.getMessage("LogicAND").length())))) {
// compare the right length after i18n
// EN AND: 3;
i += Bundle.getMessage("LogicAND").length();
oper = OPERATOR_AND;
} else if (Bundle.getMessage("LogicOR").equals(s.substring(i, i + (Bundle.getMessage("LogicOR").length())))) {
// compare the right length after i18n
// EN OR: 2;
i += Bundle.getMessage("LogicOR").length();
oper = OPERATOR_OR;
} else {
throw new JmriException(java.text.MessageFormat.format(rbx.getString("ParseError2"), new Object[] { s.substring(i) }));
}
if (s.charAt(i) == '(') {
dp = parseCalculate(s.substring(++i), variableList);
rightArg = dp.result;
i += dp.indexCount;
argsUsed.or(dp.argsUsed);
} else // cannot be '('. must be either rightArg or notRightArg
{
if (s.charAt(i) == 'R') {
//NOI18N
try {
k = Integer.parseInt(String.valueOf(s.substring(i + 1, i + 3)));
i += 2;
} catch (NumberFormatException | IndexOutOfBoundsException nfe) {
k = Integer.parseInt(String.valueOf(s.charAt(++i)));
}
rightArg = variableList.get(k - 1).evaluate();
if (variableList.get(k - 1).isNegated()) {
rightArg = !rightArg;
}
i++;
argsUsed.set(k - 1);
} else if ((i + 3) < s.length() && Bundle.getMessage("LogicNOT").equals(s.substring(i, i + (Bundle.getMessage("LogicNOT").length())))) {
// compare the right length after i18n
// EN NOT: 3;
i += Bundle.getMessage("LogicNOT").length();
//not rightArg
if (s.charAt(i) == '(') {
dp = parseCalculate(s.substring(++i), variableList);
rightArg = dp.result;
i += dp.indexCount;
argsUsed.or(dp.argsUsed);
} else if (s.charAt(i) == 'R') {
//NOI18N
try {
k = Integer.parseInt(String.valueOf(s.substring(i + 1, i + 3)));
i += 2;
} catch (NumberFormatException | IndexOutOfBoundsException nfe) {
k = Integer.parseInt(String.valueOf(s.charAt(++i)));
}
rightArg = variableList.get(k - 1).evaluate();
if (variableList.get(k - 1).isNegated()) {
rightArg = !rightArg;
}
i++;
argsUsed.set(k - 1);
} else {
throw new JmriException(java.text.MessageFormat.format(rbx.getString("ParseError3"), new Object[] { s.substring(i) }));
}
rightArg = !rightArg;
} else {
throw new JmriException(java.text.MessageFormat.format(rbx.getString("ParseError9"), new Object[] { s.substring(i) }));
}
}
if (oper == OPERATOR_AND) {
leftArg = (leftArg && rightArg);
} else if (oper == OPERATOR_OR) {
leftArg = (leftArg || rightArg);
}
} else {
// This level done, pop recursion
i++;
break;
}
}
dp = new DataPair();
dp.result = leftArg;
dp.indexCount = i;
dp.argsUsed = argsUsed;
return dp;
}
use of jmri.JmriException in project JMRI by JMRI.
the class AbstractSensorServer method setSensorInactive.
public void setSensorInactive(String sensorName) {
Sensor sensor;
try {
addSensorToList(sensorName);
sensor = InstanceManager.sensorManagerInstance().getSensor(sensorName);
if (sensor == null) {
log.error("Sensor " + sensorName + " is not available");
} else {
if (sensor.getKnownState() != Sensor.INACTIVE) {
// set state to INACTIVE
log.debug("changing sensor '{}' to InActive ({}->{})", sensorName, sensor.getKnownState(), Sensor.INACTIVE);
sensor.setKnownState(Sensor.INACTIVE);
} else {
// just notify the client.
log.debug("not changing sensor '{}', already InActive ({})", sensorName, sensor.getKnownState());
try {
sendStatus(sensorName, Sensor.INACTIVE);
} catch (IOException ie) {
log.error("Error Sending Status");
}
}
}
} catch (JmriException ex) {
log.error("set sensor inactive", ex);
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class SimpleOperationsServer method parseStatus.
/**
* Parse operation commands. They all start with "OPERATIONS" followed by a
* command like "LOCATIONS". A command like "TRAINLENGTH" requires a train
* name. The delimiter is the tab character.
*
*/
@Override
public void parseStatus(String statusString) throws JmriException, IOException {
ArrayList<Attribute> contents = parseOperationsMessage(statusString);
ArrayList<Attribute> response = new ArrayList<Attribute>();
String trainName = null;
String tag;
String value;
for (Attribute field : contents) {
tag = field.getName();
if (TRAIN.equals(tag)) {
trainName = (String) field.getValue();
response.add(field);
} else if (LOCATIONS.equals(tag)) {
sendLocationList();
} else if (TRAINS.equals(tag)) {
sendTrainList();
} else if (trainName != null) {
if (TRAINLENGTH.equals(tag)) {
value = constructTrainLength(trainName);
if (value != null) {
response.add(new Attribute(TRAINLENGTH, value));
}
} else if (TRAINWEIGHT.equals(tag)) {
value = constructTrainWeight(trainName);
if (value != null) {
response.add(new Attribute(TRAINWEIGHT, value));
}
} else if (TRAINCARS.equals(tag)) {
value = constructTrainNumberOfCars(trainName);
if (value != null) {
response.add(new Attribute(TRAINCARS, value));
}
} else if (TRAINLEADLOCO.equals(tag)) {
value = constructTrainLeadLoco(trainName);
if (value != null) {
response.add(new Attribute(TRAINLEADLOCO, value));
}
} else if (TRAINCABOOSE.equals(tag)) {
value = constructTrainCaboose(trainName);
if (value != null) {
response.add(new Attribute(TRAINCABOOSE, value));
}
} else if (TRAINSTATUS.equals(tag)) {
value = constructTrainStatus(trainName);
if (value != null) {
response.add(new Attribute(TRAINSTATUS, value));
}
} else if (TERMINATE.equals(tag)) {
value = terminateTrain(trainName);
if (value != null) {
response.add(new Attribute(TERMINATE, value));
}
} else if (TRAINLOCATION.equals(tag)) {
if (field.getValue() == null) {
value = constructTrainLocation(trainName);
} else {
value = setTrainLocation(trainName, (String) field.getValue());
}
if (value != null) {
response.add(new Attribute(TRAINLOCATION, value));
}
} else {
throw new jmri.JmriException();
}
} else {
throw new jmri.JmriException();
}
}
if (response.size() > 1) {
// something more than just a train ID
sendMessage(response);
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class BeanTableDataModel method renameBean.
public void renameBean(int row, int column) {
NamedBean nBean = getBySystemName(sysNameList.get(row));
String oldName = nBean.getUserName();
JTextField _newName = new JTextField(20);
_newName.setText(oldName);
Object[] renameBeanOption = { Bundle.getMessage("ButtonCancel"), Bundle.getMessage("ButtonOK"), _newName };
int retval = JOptionPane.showOptionDialog(null, Bundle.getMessage("RenameFrom", oldName), Bundle.getMessage("RenameTitle", getBeanType()), 0, JOptionPane.INFORMATION_MESSAGE, null, renameBeanOption, renameBeanOption[2]);
if (retval != 1) {
return;
}
// N11N
String value = _newName.getText().trim();
if (value.equals(oldName)) {
//name not changed.
return;
} else {
NamedBean nB = getByUserName(value);
if (nB != null) {
log.error("User name is not unique " + value);
String msg = Bundle.getMessage("WarningUserName", new Object[] { ("" + value) });
JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
}
nBean.setUserName(value);
fireTableRowsUpdated(row, row);
if (!value.equals("")) {
if (oldName == null || oldName.equals("")) {
if (!nbMan.inUse(sysNameList.get(row), nBean)) {
return;
}
String msg = Bundle.getMessage("UpdateToUserName", new Object[] { getBeanType(), value, sysNameList.get(row) });
int optionPane = JOptionPane.showConfirmDialog(null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
if (optionPane == JOptionPane.YES_OPTION) {
//This will update the bean reference from the systemName to the userName
try {
nbMan.updateBeanFromSystemToUser(nBean);
} catch (JmriException ex) {
//We should never get an exception here as we already check that the username is not valid
}
}
} else {
nbMan.renameBean(oldName, value, nBean);
}
} else {
//This will update the bean reference from the old userName to the SystemName
nbMan.updateBeanFromUserToSystem(nBean);
}
}
use of jmri.JmriException in project JMRI by JMRI.
the class BeanTableDataModel method moveBean.
public void moveBean(int row, int column) {
final NamedBean t = getBySystemName(sysNameList.get(row));
String currentName = t.getUserName();
NamedBean oldNameBean = getBySystemName(sysNameList.get(row));
if ((currentName == null) || currentName.equals("")) {
JOptionPane.showMessageDialog(null, "Can not move an empty UserName");
return;
}
JComboBox<String> box = new JComboBox<>();
List<String> nameList = getManager().getSystemNameList();
for (int i = 0; i < nameList.size(); i++) {
NamedBean nb = getBySystemName(nameList.get(i));
//Only add items that do not have a username assigned.
if (nb.getDisplayName().equals(nameList.get(i))) {
box.addItem(nameList.get(i));
}
}
int retval = JOptionPane.showOptionDialog(null, "Move " + getBeanType() + " " + currentName + " from " + oldNameBean.getSystemName(), "Move UserName", 0, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { Bundle.getMessage("ButtonCancel"), Bundle.getMessage("ButtonOK"), box }, // TODO I18N
null);
log.debug("Dialog value " + retval + " selected " + box.getSelectedIndex() + ":" + box.getSelectedItem());
if (retval != 1) {
return;
}
String entry = (String) box.getSelectedItem();
NamedBean newNameBean = getBySystemName(entry);
if (oldNameBean != newNameBean) {
oldNameBean.setUserName("");
newNameBean.setUserName(currentName);
InstanceManager.getDefault(NamedBeanHandleManager.class).moveBean(oldNameBean, newNameBean, currentName);
if (nbMan.inUse(newNameBean.getSystemName(), newNameBean)) {
String msg = Bundle.getMessage("UpdateToUserName", new Object[] { getBeanType(), currentName, sysNameList.get(row) });
int optionPane = JOptionPane.showConfirmDialog(null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
if (optionPane == JOptionPane.YES_OPTION) {
try {
nbMan.updateBeanFromSystemToUser(newNameBean);
} catch (JmriException ex) {
//We should never get an exception here as we already check that the username is not valid
}
}
}
fireTableRowsUpdated(row, row);
InstanceManager.getDefault(UserPreferencesManager.class).showInfoMessage("Reminder", getBeanType() + " " + Bundle.getMessage("UpdateComplete"), getMasterClassName(), "remindSaveReLoad");
//JOptionPane.showMessageDialog(null, getBeanType() + " " + Bundle.getMessage("UpdateComplete"));
}
}
Aggregations