com.freedomotic.persistence.ReactionConverter Maven / Gradle / Ivy
/**
*
* Copyright (c) 2009-2014 Freedomotic team http://freedomotic.com
*
* This file is part of Freedomotic
*
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Freedomotic; see the file COPYING. If not, see
* .
*/
package com.freedomotic.persistence;
import com.freedomotic.rules.Statement;
import com.freedomotic.core.Condition;
import com.freedomotic.reactions.Command;
import com.freedomotic.reactions.CommandPersistence;
import com.freedomotic.reactions.Reaction;
import com.freedomotic.reactions.Trigger;
import com.freedomotic.reactions.TriggerPersistence;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Enrico
*/
class ReactionConverter implements Converter {
/**
*
* @param o
* @param writer
* @param mc
*/
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
Reaction rea = (Reaction) o;
writer.startNode("trigger");
writer.setValue(rea.getTrigger().getName());
writer.endNode();
//start conditions
if (rea.getConditions() != null && !rea.getConditions().isEmpty()) {
writer.startNode("conditions");
for (Condition c : rea.getConditions()) {
if (c != null) {
//start condition
writer.startNode("condition");
//start target
writer.startNode("target");
writer.setValue(c.getTarget());
writer.endNode();
//end target
//start statement
Statement statement = c.getStatement();
writer.startNode("statement");
writer.startNode("logical");
writer.setValue(statement.getLogical());
writer.endNode(); //
writer.startNode("attribute");
writer.setValue(statement.getAttribute());
writer.endNode(); //
writer.startNode("operand");
writer.setValue(statement.getOperand());
writer.endNode(); //
writer.startNode("value");
writer.setValue(statement.getValue());
writer.endNode(); //
writer.endNode();
//end statement
writer.endNode();
//end condition
}
}
writer.endNode();
//end conditions
}
writer.startNode("sequence");
for (Command c : rea.getCommands()) {
if (c != null) {
writer.startNode("command");
writer.setValue(c.getName());
writer.endNode(); //end command
}
}
writer.endNode(); //end sequence
}
/**
*
* @param reader
* @param uc
* @return
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
Trigger t;
List conditions = new ArrayList();
ArrayList list = new ArrayList();
reader.moveDown(); //goes down to
String triggerName = reader.getValue();
t = TriggerPersistence.getTrigger(triggerName.trim());
reader.moveUp(); //up to root
//go down to conditions or sequence
reader.moveDown();
if (reader.getNodeName().equalsIgnoreCase("conditions")) {
while (reader.hasMoreChildren()) {
//read a single condition
reader.moveDown(); //move down to condition
reader.moveDown(); //move down to target
Condition condition = new Condition();
condition.setTarget(reader.getValue().trim());
reader.moveUp(); //move up to condition
//parse statement
ArrayList statementValues = new ArrayList();
reader.moveDown(); //move down to statement
while (reader.hasMoreChildren()) { //childs of statement (logical, attribute, ...)
reader.moveDown(); //move down to statement property
System.out.println(" " + reader.getValue().trim());
statementValues.add(reader.getValue());
reader.moveUp(); //move up to statement
}
reader.moveUp();//move up to condition
Statement stm = new Statement();
stm.create(statementValues.get(0),
statementValues.get(1),
statementValues.get(2),
statementValues.get(3));
condition.setStatement(stm);
conditions.add(condition);
reader.moveUp(); //move up to conditions
}
reader.moveUp(); //move up to root
reader.moveDown();//move down to sequence
}
while (reader.hasMoreChildren()) {
reader.moveDown(); //move down to command
Command c = CommandPersistence.getCommand(reader.getValue().trim());
if (c != null) {
list.add(c);
}
reader.moveUp(); //move up to sequence
}
reader.moveUp(); //move uo to root
return new Reaction(t, conditions, list);
}
/**
*
* @param type
* @return
*/
@Override
public boolean canConvert(Class type) {
return (type == Reaction.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy