Merge branch 'feature/node_impl' into develop
This commit is contained in:
commit
bfde92e92c
@ -56,7 +56,7 @@ public abstract class ChannelBase implements Observable {
|
|||||||
public boolean tick() {
|
public boolean tick() {
|
||||||
requirements.forEach(Requirement::tick);
|
requirements.forEach(Requirement::tick);
|
||||||
try {
|
try {
|
||||||
requirements.stream().filter(r -> r.getTTL() == 0).forEach(re -> re.getCreator().getProductFromStorage(re.getProductName(), re.getProductType()).setStateToAvailable());
|
requirements.stream().filter(r -> r.getTTL() == 0).forEach(re -> re.getCreator().getStorage().getProduct(re.getProductName(), re.getProductType()).setStateToAvailable());
|
||||||
} catch (Exception ignored) {}
|
} catch (Exception ignored) {}
|
||||||
requirements = requirements.stream().filter(r -> r.getTTL() > 0).collect(Collectors.toCollection(LinkedList::new));
|
requirements = requirements.stream().filter(r -> r.getTTL() > 0).collect(Collectors.toCollection(LinkedList::new));
|
||||||
notifyAllObservers();
|
notifyAllObservers();
|
||||||
|
|||||||
@ -11,11 +11,11 @@ public class Customer extends BuyerNodeBase {
|
|||||||
super(startMoney);
|
super(startMoney);
|
||||||
allowedSeller = NodeType.Distributor;
|
allowedSeller = NodeType.Distributor;
|
||||||
allowedPurchaser = NodeType.None;
|
allowedPurchaser = NodeType.None;
|
||||||
this.nodeType = NodeType.Customer;
|
nodeType = NodeType.Customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void consumeProduct() {
|
public void consumeProduct() {
|
||||||
int i = new Random().nextInt(getStorage().size());
|
int i = new Random().nextInt(getStorage().getNumberOfStoredProducts());
|
||||||
getStorage().remove(i);
|
getStorage().getProducts().remove(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,11 +13,11 @@ public class Farmer extends SellerNodeBase {
|
|||||||
super(startMoney);
|
super(startMoney);
|
||||||
allowedSeller = NodeType.None;
|
allowedSeller = NodeType.None;
|
||||||
allowedPurchaser = NodeType.Processor;
|
allowedPurchaser = NodeType.Processor;
|
||||||
this.nodeType = NodeType.Farmer;
|
nodeType = NodeType.Farmer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateProduct(Product product) {
|
public void generateProduct(Product product) {
|
||||||
storeProduct(product);
|
this.storage.storeProduct(product);
|
||||||
try {
|
try {
|
||||||
createRequirement(TransactionType.SALE, product.getProductType(), product.getName(), product.getPrice());
|
createRequirement(TransactionType.SALE, product.getProductType(), product.getName(), product.getPrice());
|
||||||
} catch (InsufficientResourcesException e) {
|
} catch (InsufficientResourcesException e) {
|
||||||
|
|||||||
113
src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Storage.java
Normal file
113
src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Storage.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.operation.Action;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Storage
|
||||||
|
{
|
||||||
|
private final NodeBase storageOwner;
|
||||||
|
private final List<Product> products = new LinkedList<>();
|
||||||
|
|
||||||
|
public Storage(NodeBase owner)
|
||||||
|
{
|
||||||
|
this.storageOwner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds provided product into the storage
|
||||||
|
* @param product
|
||||||
|
*/
|
||||||
|
public void storeProduct(Product product)
|
||||||
|
{
|
||||||
|
this.products.add(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds all of the provided products into the storage
|
||||||
|
* @param products
|
||||||
|
*/
|
||||||
|
public void storeProducts(List<Product> products)
|
||||||
|
{
|
||||||
|
this.products.addAll(products);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a product (first if there is multiple of them) based on its name and type
|
||||||
|
* @param name name of product to find
|
||||||
|
* @param type type of product to find
|
||||||
|
* @return product if found, null if product is not in storage
|
||||||
|
*/
|
||||||
|
public Product getProduct(String name, ProductType type)
|
||||||
|
{
|
||||||
|
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns List of all products in storage
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Product> getProducts()
|
||||||
|
{
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns number of products in storage
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int getNumberOfStoredProducts()
|
||||||
|
{
|
||||||
|
return this.products.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether storage contains provided product
|
||||||
|
* @param product product to check
|
||||||
|
* @return true if contains, false if not
|
||||||
|
*/
|
||||||
|
public boolean containsProduct(Product product)
|
||||||
|
{
|
||||||
|
return products.contains(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes provided product from storage if storage contains this product. (Removes only first found)
|
||||||
|
* @param product product to remove
|
||||||
|
*/
|
||||||
|
public void removeProduct(Product product)
|
||||||
|
{
|
||||||
|
if (containsProduct(product))
|
||||||
|
this.products.remove(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Proceed specific type of action on provided product if its possible.
|
||||||
|
* @param action type of product operation to do
|
||||||
|
* @param product product to proceed
|
||||||
|
*/
|
||||||
|
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException
|
||||||
|
{
|
||||||
|
if (action.getNode().equals(this.storageOwner.getNodeType()))
|
||||||
|
new Action(this.storageOwner, this.getProduct(product.getName(), product.getProductType()), action);
|
||||||
|
//new Action(this.storageOwner, product, action); //todo má se zpracovat produkt co přichází nebo na jeho základě najít tento produkt ve skladu a ten zpracovat???
|
||||||
|
else
|
||||||
|
throw new IllegalCallerException("! IllegalCallerException in NodeBase. " + this.storageOwner.getNodeType().toString() + " cannot do action available only for " + action.getNode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls specific ProductOperation on each product in stock
|
||||||
|
* @param action ProductOperationType
|
||||||
|
*/
|
||||||
|
public void doAction(ProductOperationType action)
|
||||||
|
{
|
||||||
|
this.products.forEach(t -> new Action(this.storageOwner, t, action));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -2,13 +2,13 @@ package cz.cvut.fel.sit.omo.foodchain.node.base;
|
|||||||
|
|
||||||
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.channel.ChannelSubscriptionType;
|
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.node.Storage;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
|
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.operation.*;
|
import cz.cvut.fel.sit.omo.foodchain.operation.*;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductState;
|
import cz.cvut.fel.sit.omo.foodchain.product.ProductState;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
|
||||||
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||||
import org.javamoney.moneta.Money;
|
import org.javamoney.moneta.Money;
|
||||||
@ -19,31 +19,26 @@ import java.util.*;
|
|||||||
|
|
||||||
|
|
||||||
public class NodeBase implements ChannelObserver {
|
public class NodeBase implements ChannelObserver {
|
||||||
private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels;
|
private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels = new HashMap<>();
|
||||||
private final List<Product> products;
|
private final List<Transaction> refusedTransactions = new LinkedList<>();
|
||||||
private final List<Transaction> refusedTransactions;
|
private final List<RefusedRequirement> refusedRequirements = new LinkedList<>();
|
||||||
private final List<RefusedRequirement> refusedRequirements;
|
|
||||||
|
|
||||||
private MonetaryAmount money;
|
private MonetaryAmount money;
|
||||||
|
|
||||||
|
protected final Storage storage;
|
||||||
protected NodeType nodeType;
|
protected NodeType nodeType;
|
||||||
protected NodeType allowedSeller;
|
protected NodeType allowedSeller;
|
||||||
protected NodeType allowedPurchaser;
|
protected NodeType allowedPurchaser;
|
||||||
|
|
||||||
public NodeBase() {
|
public NodeBase() {
|
||||||
this.nodeType = NodeType.None;
|
this.nodeType = NodeType.None;
|
||||||
this.subscribedChannels = new HashMap<>();
|
this.storage = new Storage(this);
|
||||||
this.products = new LinkedList<>();
|
|
||||||
this.refusedTransactions = new LinkedList<>();
|
|
||||||
this.refusedRequirements = new LinkedList<>();
|
|
||||||
this.money = Money.of(0, "USD");
|
this.money = Money.of(0, "USD");
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodeBase(double m) {
|
public NodeBase(double m) {
|
||||||
this.subscribedChannels = new HashMap<>();
|
this.nodeType = NodeType.None;
|
||||||
this.products = new LinkedList<>();
|
this.storage = new Storage(this);
|
||||||
this.refusedTransactions = new LinkedList<>();
|
|
||||||
this.refusedRequirements = new LinkedList<>();
|
|
||||||
this.money = Money.of(m, "USD");
|
this.money = Money.of(m, "USD");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +116,7 @@ public class NodeBase implements ChannelObserver {
|
|||||||
public void createRequirement(TransactionType tType, ProductType pType, String pName, MonetaryAmount price) throws InsufficientResourcesException {
|
public void createRequirement(TransactionType tType, ProductType pType, String pName, MonetaryAmount price) throws InsufficientResourcesException {
|
||||||
if (price == null) {
|
if (price == null) {
|
||||||
try {
|
try {
|
||||||
price = getProductFromStorage(pName, pType).getPrice();
|
price = storage.getProduct(pName, pType).getPrice();
|
||||||
}
|
}
|
||||||
catch (NullPointerException e) {
|
catch (NullPointerException e) {
|
||||||
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
|
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
|
||||||
@ -130,7 +125,7 @@ public class NodeBase implements ChannelObserver {
|
|||||||
}
|
}
|
||||||
if (tType == TransactionType.SALE) {
|
if (tType == TransactionType.SALE) {
|
||||||
try {
|
try {
|
||||||
getProductFromStorage(pName, pType).setStateToInRequirement();
|
storage.getProduct(pName, pType).setStateToInRequirement();
|
||||||
}
|
}
|
||||||
catch (NullPointerException | IllegalStateException e) {
|
catch (NullPointerException | IllegalStateException e) {
|
||||||
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
|
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
|
||||||
@ -149,9 +144,9 @@ public class NodeBase implements ChannelObserver {
|
|||||||
createRequirement(TransactionType.PURCHASE, req.getProductType(), req.getProductName(), req.getPrice());
|
createRequirement(TransactionType.PURCHASE, req.getProductType(), req.getProductName(), req.getPrice());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (req.getPrice().isGreaterThanOrEqualTo(getProductFromStorage(req.getProductName(), req.getProductType()).getPrice().multiply(0.9))) {
|
if (req.getPrice().isGreaterThanOrEqualTo(storage.getProduct(req.getProductName(), req.getProductType()).getPrice().multiply(0.9))) {
|
||||||
getProductFromStorage(req.getProductName(), req.getProductType()).setPrice(req.getPrice());
|
storage.getProduct(req.getProductName(), req.getProductType()).setPrice(req.getPrice());
|
||||||
sendTransaction(getProductFromStorage(req.getProductName(), req.getProductType()), req.getCreator());
|
sendTransaction(storage.getProduct(req.getProductName(), req.getProductType()), req.getCreator());
|
||||||
}
|
}
|
||||||
else createRequirement(TransactionType.SALE, req.getProductType(), req.getProductName(), req.getPrice());
|
else createRequirement(TransactionType.SALE, req.getProductType(), req.getProductName(), req.getPrice());
|
||||||
}
|
}
|
||||||
@ -170,45 +165,30 @@ public class NodeBase implements ChannelObserver {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proceed specific type of action on provided product if its possible.
|
* Proceed specific type of action on provided product if its possible. (Propagated to its storage)
|
||||||
* @param action type of product operation to do
|
* @param action type of product operation to do
|
||||||
* @param product product to proceed
|
* @param product product to proceed
|
||||||
*/
|
*/
|
||||||
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException
|
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException
|
||||||
{
|
{
|
||||||
if(action.getNode().equals(this.nodeType))
|
this.storage.doAction(action,product);
|
||||||
new Action(this, product, action);
|
|
||||||
else
|
|
||||||
throw new IllegalCallerException("! IllegalCallerException in NodeBase. " + this.nodeType.toString() + " cannot do action available only for "+action.getNode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls specific ProductOperation on each product in stock
|
* Calls specific ProductOperation on each product in stock (Propagated to its storage)
|
||||||
* @param action ProductOperationType
|
* @param action ProductOperationType
|
||||||
*/
|
*/
|
||||||
public void doAction(ProductOperationType action)
|
public void doAction(ProductOperationType action)
|
||||||
{
|
{
|
||||||
this.products.forEach(t -> new Action(this, t, action));
|
this.storage.doAction(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Storage getStorage() {
|
||||||
public void storeProduct(Product product) {
|
return storage;
|
||||||
this.products.add(product);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NodeType getNodeType() {
|
||||||
public void storeProducts(List<Product> products) {
|
return nodeType;
|
||||||
this.products.addAll(products);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Product getProductFromStorage(String name, ProductType type) {
|
|
||||||
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Product> getStorage() {
|
|
||||||
return products;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MonetaryAmount getMoney() {
|
public MonetaryAmount getMoney() {
|
||||||
@ -249,7 +229,7 @@ public class NodeBase implements ChannelObserver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean requirementCheck(Requirement req) {
|
boolean requirementCheck(Requirement req) {
|
||||||
Product product = getProductFromStorage(req.getProductName(), req.getProductType());
|
Product product = storage.getProduct(req.getProductName(), req.getProductType());
|
||||||
return (req.getTransactionType() == TransactionType.SALE && money.isGreaterThanOrEqualTo(req.getPrice())) ||
|
return (req.getTransactionType() == TransactionType.SALE && money.isGreaterThanOrEqualTo(req.getPrice())) ||
|
||||||
(req.getTransactionType() == TransactionType.PURCHASE && product != null &&
|
(req.getTransactionType() == TransactionType.PURCHASE && product != null &&
|
||||||
((product.getProductState() == ProductState.AVAILABLE) || product.getProductState() == ProductState.IN_REQUIREMENT));
|
((product.getProductState() == ProductState.AVAILABLE) || product.getProductState() == ProductState.IN_REQUIREMENT));
|
||||||
@ -271,9 +251,8 @@ public class NodeBase implements ChannelObserver {
|
|||||||
receiveTransactions();
|
receiveTransactions();
|
||||||
//System.out.println(this.getClass().getSimpleName() + ": " + money.getNumber());
|
//System.out.println(this.getClass().getSimpleName() + ": " + money.getNumber());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class RefusedRequirement {
|
private static class RefusedRequirement {
|
||||||
public final TransactionType tType;
|
public final TransactionType tType;
|
||||||
public final ProductType pType;
|
public final ProductType pType;
|
||||||
public final String pName;
|
public final String pName;
|
||||||
@ -286,3 +265,4 @@ class RefusedRequirement {
|
|||||||
this.price = price;
|
this.price = price;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -20,14 +20,14 @@ public class ProductTransaction extends Transaction {
|
|||||||
if (product == null || product.getProductState() != ProductState.IN_REQUIREMENT) throw new InsufficientResourcesException("product unavailable");
|
if (product == null || product.getProductState() != ProductState.IN_REQUIREMENT) throw new InsufficientResourcesException("product unavailable");
|
||||||
product.setCurrentOperation(this);
|
product.setCurrentOperation(this);
|
||||||
product.setStateToReadyForDispatch();
|
product.setStateToReadyForDispatch();
|
||||||
creator.getStorage().remove(product);
|
creator.getStorage().removeProduct(product);
|
||||||
System.out.println(toString());
|
System.out.println(toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endTransaction() {
|
public void endTransaction() {
|
||||||
super.endTransaction();
|
super.endTransaction();
|
||||||
getNodeReceiver().storeProduct(product);
|
getNodeReceiver().getStorage().storeProduct(product);
|
||||||
product.setStateToAvailable();
|
product.setStateToAvailable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user