Merge branch 'feature/scenario_impl' into develop

This commit is contained in:
Nikita Iryupin 2020-12-25 18:50:31 +01:00
commit 43dd1adb91
20 changed files with 187 additions and 24 deletions

View File

@ -4,7 +4,7 @@ import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
public class FoodChain {
private static FactoryManager factoryManager;
private static FactoryManager factoryManager = new FactoryManager();
public static void main(String[] args) {

View File

@ -3,13 +3,9 @@ package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class BuyerNodeBase extends NodeBase implements IBuyerNode {
@Override
public void sellProduct(Product product) {
}
@Override
public void addMargin(Product product) {
public void buyProduct(Product product) {
}
}

View File

@ -1,4 +1,11 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import java.util.Random;
public class Customer extends BuyerNodeBase {
public void consumeProduct() {
int i = new Random().nextInt(getStorage().size());
getStorage().remove(i);
}
}

View File

@ -1,4 +1,12 @@
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 void generateProduct(Product product) {
storeProduct(product);
createRequirement(TransactionType.SALE, product.getProductType(), product.getName());
}
}

View File

@ -4,7 +4,5 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
public interface IBuyerNode {
void sellProduct(Product product);
void addMargin(Product product);
void buyProduct(Product product);
}

View File

@ -4,5 +4,7 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
public interface ISellerNode {
void buyProduct(Product product);
void sellProduct(Product product);
void addMargin(Product product);
}

View File

@ -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) {
@ -85,7 +91,7 @@ public class NodeBase implements ChannelObserver {
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);
}
public List<Product> getStorage() {
return products;
}
public Signature generateSignature(Transaction transaction) {
return SignatureManager.signTransaction(transaction);

View File

@ -4,7 +4,12 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class SellerNodeBase extends NodeBase implements ISellerNode {
@Override
public void buyProduct(Product product) {
public void sellProduct(Product product) {
}
@Override
public void addMargin(Product product) {
}
}

View File

@ -10,7 +10,7 @@ public class Action extends OperationBase {
private final ProductOperationType type;
private int duration;
Action(NodeBase node, Product product, ProductOperationType type) {
public Action(NodeBase node, Product product, ProductOperationType type) {
super(node);
this.product = product;
this.type = type;

View File

@ -28,13 +28,13 @@ public class ConfigurationParser {
NodeList elements = activeDocument.getElementsByTagName(tag);
ParserResult resultList = new ParserResult();
for (int i = 0; i < elements.getLength(); i++) {
Node tmp_node = elements.item(i);
Node tmpNode = elements.item(i);
ParserResultElement element = new ParserResultElement();
for (int j = 0; j < tmp_node.getAttributes().getLength(); j++) {
element.addAttribute(tmp_node.getAttributes().item(i).getNodeName(), tmp_node.getAttributes().item(i).getTextContent());
for (int j = 0; j < tmpNode.getAttributes().getLength(); j++) {
element.addAttribute(tmpNode.getAttributes().item(i).getNodeName(), tmpNode.getAttributes().item(i).getTextContent());
}
for (int j = 0; j < tmp_node.getChildNodes().getLength(); j++) {
element.addChild(tmp_node.getChildNodes().item(i).getNodeName(), tmp_node.getChildNodes().item(i).getTextContent());
for (int j = 0; j < tmpNode.getChildNodes().getLength(); j++) {
element.addChild(tmpNode.getChildNodes().item(i).getNodeName(), tmpNode.getChildNodes().item(i).getTextContent());
}
resultList.addElement(element);
}

View File

@ -1,12 +1,11 @@
package cz.cvut.fel.sit.omo.foodchain.parser;
import org.w3c.dom.Element;
import java.util.LinkedList;
import java.util.List;
public class ParserResult {
private LinkedList<ParserResultElement> elements;
private final List<ParserResultElement> elements;
public ParserResult() {
elements = new LinkedList<>();
@ -15,4 +14,9 @@ public class ParserResult {
public void addElement(ParserResultElement element) {
elements.add(element);
}
public List<ParserResultElement> getElements() {
return elements;
}
}

View File

@ -1,6 +1,11 @@
package cz.cvut.fel.sit.omo.foodchain.pool;
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 ChannelBase findByType(ProductType type) {
return findAll().stream().filter(ch -> ch.getProductType().equals(type)).findFirst().orElse(null);
}
}

View File

@ -1,6 +1,5 @@
package cz.cvut.fel.sit.omo.foodchain.pool;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
@ -11,7 +10,7 @@ public abstract class PoolListBase<T> {
pool = new LinkedList<T>();
}
public Collection<T> findAll() {
public List<T> findAll() {
return pool;
}

View File

@ -9,12 +9,14 @@ public class Requirement {
private final NodeBase creator;
private final TransactionType transactionType;
private final ProductType productType;
private final String productName;
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.transactionType = transactionType;
this.productType = productType;
this.productName = productName;
TTL = 2;
}
@ -34,6 +36,10 @@ public class Requirement {
return productType;
}
public String getProductName() {
return productName;
}
public int getTTL() {
return TTL;
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -1,4 +1,26 @@
package cz.cvut.fel.sit.omo.foodchain.scenario;
import java.util.Iterator;
import java.util.List;
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();
}
}

View File

@ -0,0 +1,5 @@
package cz.cvut.fel.sit.omo.foodchain.scenario;
public abstract class ScenarioStep {
abstract void execute();
}

View File

@ -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);
}
}