Merge branch 'feature/scenario_impl' into develop
This commit is contained in:
commit
43dd1adb91
@ -4,7 +4,7 @@ import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
|
|||||||
|
|
||||||
public class FoodChain {
|
public class FoodChain {
|
||||||
|
|
||||||
private static FactoryManager factoryManager;
|
private static FactoryManager factoryManager = new FactoryManager();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|||||||
@ -3,13 +3,9 @@ package cz.cvut.fel.sit.omo.foodchain.node;
|
|||||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||||
|
|
||||||
public class BuyerNodeBase extends NodeBase implements IBuyerNode {
|
public class BuyerNodeBase extends NodeBase implements IBuyerNode {
|
||||||
@Override
|
|
||||||
public void sellProduct(Product product) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addMargin(Product product) {
|
public void buyProduct(Product product) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,11 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class Customer extends BuyerNodeBase {
|
public class Customer extends BuyerNodeBase {
|
||||||
|
|
||||||
|
public void consumeProduct() {
|
||||||
|
int i = new Random().nextInt(getStorage().size());
|
||||||
|
getStorage().remove(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,12 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||||
|
|
||||||
public class Farmer extends SellerNodeBase {
|
public class Farmer extends SellerNodeBase {
|
||||||
|
|
||||||
|
public void generateProduct(Product product) {
|
||||||
|
storeProduct(product);
|
||||||
|
createRequirement(TransactionType.SALE, product.getProductType(), product.getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,5 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
|||||||
|
|
||||||
public interface IBuyerNode {
|
public interface IBuyerNode {
|
||||||
|
|
||||||
void sellProduct(Product product);
|
void buyProduct(Product product);
|
||||||
|
|
||||||
void addMargin(Product product);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,5 +4,7 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
|||||||
|
|
||||||
public interface ISellerNode {
|
public interface ISellerNode {
|
||||||
|
|
||||||
void buyProduct(Product product);
|
void sellProduct(Product product);
|
||||||
|
|
||||||
|
void addMargin(Product product);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,6 +68,12 @@ public class NodeBase implements ChannelObserver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void createRequirement(TransactionType tType, ProductType pType, String pName) {
|
||||||
|
Requirement req = new Requirement(this, tType, pType, pName);
|
||||||
|
subscribedChannels.stream()
|
||||||
|
.filter(ch -> ch.getProductType().equals(pType))
|
||||||
|
.findAny().ifPresent(channel -> channel.addRequirement(req));
|
||||||
|
}
|
||||||
|
|
||||||
public void acceptRequirement(Requirement requirement) {
|
public void acceptRequirement(Requirement requirement) {
|
||||||
|
|
||||||
@ -85,7 +91,7 @@ public class NodeBase implements ChannelObserver {
|
|||||||
|
|
||||||
|
|
||||||
public void doAction(ProductOperationType action, Product product) {
|
public void doAction(ProductOperationType action, Product product) {
|
||||||
Action actionProcess = new Action(this, product);
|
Action actionProcess = new Action(this, product, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -108,6 +114,9 @@ public class NodeBase implements ChannelObserver {
|
|||||||
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
|
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Product> getStorage() {
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
|
||||||
public Signature generateSignature(Transaction transaction) {
|
public Signature generateSignature(Transaction transaction) {
|
||||||
return SignatureManager.signTransaction(transaction);
|
return SignatureManager.signTransaction(transaction);
|
||||||
|
|||||||
@ -4,7 +4,12 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
|||||||
|
|
||||||
public class SellerNodeBase extends NodeBase implements ISellerNode {
|
public class SellerNodeBase extends NodeBase implements ISellerNode {
|
||||||
@Override
|
@Override
|
||||||
public void buyProduct(Product product) {
|
public void sellProduct(Product product) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addMargin(Product product) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ public class Action extends OperationBase {
|
|||||||
private final ProductOperationType type;
|
private final ProductOperationType type;
|
||||||
private int duration;
|
private int duration;
|
||||||
|
|
||||||
Action(NodeBase node, Product product, ProductOperationType type) {
|
public Action(NodeBase node, Product product, ProductOperationType type) {
|
||||||
super(node);
|
super(node);
|
||||||
this.product = product;
|
this.product = product;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
|||||||
@ -28,13 +28,13 @@ public class ConfigurationParser {
|
|||||||
NodeList elements = activeDocument.getElementsByTagName(tag);
|
NodeList elements = activeDocument.getElementsByTagName(tag);
|
||||||
ParserResult resultList = new ParserResult();
|
ParserResult resultList = new ParserResult();
|
||||||
for (int i = 0; i < elements.getLength(); i++) {
|
for (int i = 0; i < elements.getLength(); i++) {
|
||||||
Node tmp_node = elements.item(i);
|
Node tmpNode = elements.item(i);
|
||||||
ParserResultElement element = new ParserResultElement();
|
ParserResultElement element = new ParserResultElement();
|
||||||
for (int j = 0; j < tmp_node.getAttributes().getLength(); j++) {
|
for (int j = 0; j < tmpNode.getAttributes().getLength(); j++) {
|
||||||
element.addAttribute(tmp_node.getAttributes().item(i).getNodeName(), tmp_node.getAttributes().item(i).getTextContent());
|
element.addAttribute(tmpNode.getAttributes().item(i).getNodeName(), tmpNode.getAttributes().item(i).getTextContent());
|
||||||
}
|
}
|
||||||
for (int j = 0; j < tmp_node.getChildNodes().getLength(); j++) {
|
for (int j = 0; j < tmpNode.getChildNodes().getLength(); j++) {
|
||||||
element.addChild(tmp_node.getChildNodes().item(i).getNodeName(), tmp_node.getChildNodes().item(i).getTextContent());
|
element.addChild(tmpNode.getChildNodes().item(i).getNodeName(), tmpNode.getChildNodes().item(i).getTextContent());
|
||||||
}
|
}
|
||||||
resultList.addElement(element);
|
resultList.addElement(element);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||||
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ParserResult {
|
public class ParserResult {
|
||||||
|
|
||||||
private LinkedList<ParserResultElement> elements;
|
private final List<ParserResultElement> elements;
|
||||||
|
|
||||||
public ParserResult() {
|
public ParserResult() {
|
||||||
elements = new LinkedList<>();
|
elements = new LinkedList<>();
|
||||||
@ -15,4 +14,9 @@ public class ParserResult {
|
|||||||
public void addElement(ParserResultElement element) {
|
public void addElement(ParserResultElement element) {
|
||||||
elements.add(element);
|
elements.add(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ParserResultElement> getElements() {
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,11 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||||
|
|
||||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||||
|
|
||||||
public class ChannelPool extends PoolListBase<ChannelBase> {
|
public class ChannelPool extends PoolListBase<ChannelBase> {
|
||||||
|
|
||||||
|
public ChannelBase findByType(ProductType type) {
|
||||||
|
return findAll().stream().filter(ch -> ch.getProductType().equals(type)).findFirst().orElse(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -11,7 +10,7 @@ public abstract class PoolListBase<T> {
|
|||||||
pool = new LinkedList<T>();
|
pool = new LinkedList<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<T> findAll() {
|
public List<T> findAll() {
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,12 +9,14 @@ public class Requirement {
|
|||||||
private final NodeBase creator;
|
private final NodeBase creator;
|
||||||
private final TransactionType transactionType;
|
private final TransactionType transactionType;
|
||||||
private final ProductType productType;
|
private final ProductType productType;
|
||||||
|
private final String productName;
|
||||||
private int TTL;
|
private int TTL;
|
||||||
|
|
||||||
public Requirement(NodeBase creator, TransactionType transactionType, ProductType productType) {
|
public Requirement(NodeBase creator, TransactionType transactionType, ProductType productType, String productName) {
|
||||||
this.creator = creator;
|
this.creator = creator;
|
||||||
this.transactionType = transactionType;
|
this.transactionType = transactionType;
|
||||||
this.productType = productType;
|
this.productType = productType;
|
||||||
|
this.productName = productName;
|
||||||
TTL = 2;
|
TTL = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +36,10 @@ public class Requirement {
|
|||||||
return productType;
|
return productType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getProductName() {
|
||||||
|
return productName;
|
||||||
|
}
|
||||||
|
|
||||||
public int getTTL() {
|
public int getTTL() {
|
||||||
return TTL;
|
return TTL;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,17 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.Customer;
|
||||||
|
|
||||||
|
public class ConsumeProductStep extends ScenarioStep {
|
||||||
|
|
||||||
|
private Customer node;
|
||||||
|
|
||||||
|
public ConsumeProductStep(Customer node) {
|
||||||
|
this.node = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void execute() {
|
||||||
|
node.consumeProduct();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,32 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||||
|
|
||||||
|
public class DoActionStep extends ScenarioStep {
|
||||||
|
private final NodeBase nodeBase;
|
||||||
|
private final ProductOperationType productOperationType;
|
||||||
|
private final Product product;
|
||||||
|
|
||||||
|
public DoActionStep(NodeBase node, ProductOperationType productOperationType, Product product){
|
||||||
|
this.nodeBase = node;
|
||||||
|
this.productOperationType = productOperationType;
|
||||||
|
this.product = product;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoActionStep(NodeBase node, ProductOperationType productOperationType){
|
||||||
|
this(node, productOperationType, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void execute() {
|
||||||
|
if (product != null){
|
||||||
|
nodeBase.doAction(productOperationType, product);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
nodeBase.doAction(productOperationType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.Farmer;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||||
|
|
||||||
|
public class GenerateProductStep extends ScenarioStep {
|
||||||
|
|
||||||
|
private Farmer node;
|
||||||
|
private Product product;
|
||||||
|
|
||||||
|
public GenerateProductStep(Farmer node) {
|
||||||
|
this.node = node;
|
||||||
|
product = FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll()
|
||||||
|
.stream().findFirst().orElse(null);
|
||||||
|
FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().remove(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void execute() {
|
||||||
|
node.generateProduct(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,26 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Scenario {
|
public class Scenario {
|
||||||
|
List<ScenarioStep> steps;
|
||||||
|
Iterator<ScenarioStep> stepIterator;
|
||||||
|
|
||||||
|
public Scenario() {
|
||||||
|
//steps = //TODO init
|
||||||
|
stepIterator = steps.listIterator(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scenario(List<ScenarioStep> steps) {
|
||||||
|
this.steps = steps;
|
||||||
|
stepIterator = steps.listIterator(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void execute() {
|
||||||
|
if (!stepIterator.hasNext()) {
|
||||||
|
stepIterator = steps.listIterator(0);
|
||||||
|
}
|
||||||
|
stepIterator.next().execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,5 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||||
|
|
||||||
|
public abstract class ScenarioStep {
|
||||||
|
abstract void execute();
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||||
|
|
||||||
|
public class SendRequirementStep extends ScenarioStep {
|
||||||
|
private final NodeBase node;
|
||||||
|
private final TransactionType tType;
|
||||||
|
private final ProductType pType;
|
||||||
|
private final String pName;
|
||||||
|
|
||||||
|
public SendRequirementStep(NodeBase node, TransactionType tType, ProductType pType, String pName) {
|
||||||
|
this.node = node;
|
||||||
|
this.tType = tType;
|
||||||
|
this.pType = pType;
|
||||||
|
this.pName = pName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void execute() {
|
||||||
|
node.createRequirement(tType, pType, pName);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user