Implemented parties report

This commit is contained in:
Nikita Iryupin 2021-01-04 15:51:13 +01:00
parent 099dd81246
commit fa4ea75a21
10 changed files with 159 additions and 63 deletions

View File

@ -53,6 +53,7 @@ public class NodeBase implements ChannelObserver {
/**
* Sends product transaction to node through a channel based on its type.
*
* @param product product to be sent
* @param node receiver node
*/
@ -70,6 +71,7 @@ public class NodeBase implements ChannelObserver {
/**
* Sends financial transaction to node through a channel based on previous transaction type.
*
* @param amount amount to be sent
* @param node receiver node
* @param prevTransaction previous product transaction
@ -87,17 +89,16 @@ public class NodeBase implements ChannelObserver {
/**
* Receives (processes) transaction.
*
* @param transaction transaction to be received
*/
public void receiveTransaction(Transaction transaction) {
try {
receiveTransaction((ProductTransaction) transaction);
}
catch (Exception e) {
} catch (Exception e) {
try {
receiveTransaction((FinancialTransaction) transaction);
}
catch (Exception ex) {
} catch (Exception ex) {
LOGGER.log(Level.WARNING, e.getMessage());
}
}
@ -105,6 +106,7 @@ public class NodeBase implements ChannelObserver {
/**
* Receives (validates) product transaction and sends financial transaction as response.
*
* @param transaction transaction to be received
*/
public void receiveTransaction(ProductTransaction transaction) {
@ -113,12 +115,12 @@ public class NodeBase implements ChannelObserver {
transaction.endTransaction();
sendTransaction(transaction.getProduct().getPrice(), transaction.getNodeCreator(), transaction);
}
else refuseTransaction(transaction);
} else refuseTransaction(transaction);
}
/**
* Receives (validates) financial transaction.
*
* @param transaction transaction to be received
*/
public void receiveTransaction(FinancialTransaction transaction) {
@ -129,6 +131,7 @@ public class NodeBase implements ChannelObserver {
/**
* Adds transaction to refused transactions.
*
* @param transaction
*/
public void refuseTransaction(Transaction transaction) {
@ -140,11 +143,15 @@ public class NodeBase implements ChannelObserver {
* Receives all transactions for this node.
*/
public void receiveTransactions() {
subscribedChannels.keySet().forEach(ch -> ch.getTransactions(this).forEach(t -> {receiveTransaction(t); ch.disposeOf(t);}));
subscribedChannels.keySet().forEach(ch -> ch.getTransactions(this).forEach(t -> {
receiveTransaction(t);
ch.disposeOf(t);
}));
}
/**
* Creates requirement based on data provided.
*
* @param tType transaction type
* @param pType product type
* @param pName product name
@ -155,8 +162,7 @@ public class NodeBase implements ChannelObserver {
if (price == null) {
try {
price = storage.getProduct(pName, pType).getPrice();
}
catch (NullPointerException e) {
} catch (NullPointerException e) {
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
throw new InsufficientResourcesException("product unavailable");
}
@ -164,8 +170,7 @@ public class NodeBase implements ChannelObserver {
if (tType == TransactionType.SALE) {
try {
storage.getProduct(pName, pType).setStateToInRequirement();
}
catch (NullPointerException | IllegalStateException e) {
} catch (NullPointerException | IllegalStateException e) {
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
throw new InsufficientResourcesException("product unavailable");
}
@ -178,6 +183,7 @@ public class NodeBase implements ChannelObserver {
/**
* Accepts requirement and creates response.
*
* @param req requirement to be accepted
* @throws InsufficientResourcesException if product is unavailable
*/
@ -185,19 +191,18 @@ public class NodeBase implements ChannelObserver {
LOGGER.log(Level.INFO, this.nodeType.toString() + " accepted " + req.toString());
if (req.getTransactionType() == TransactionType.SALE) {
createRequirement(TransactionType.PURCHASE, req.getProductType(), req.getProductName(), req.getPrice());
}
else {
} else {
if (req.getPrice().isGreaterThanOrEqualTo(storage.getProduct(req.getProductName(), req.getProductType()).getPrice().multiply(0.9))) {
storage.getProduct(req.getProductName(), req.getProductType()).setPrice(req.getPrice());
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());
}
}
/**
* Adds channel to subscribed channels with correct type.
*
* @param channel channel to be subscribed
* @param type type of subscription
*/
@ -209,6 +214,7 @@ public class NodeBase implements ChannelObserver {
/**
* Removes channel from subscribed channels.
*
* @param channel
*/
public void unsubscribeFromChannel(ChannelBase channel) {
@ -218,20 +224,20 @@ public class NodeBase implements ChannelObserver {
/**
* Proceed specific type of action on provided product if its possible. (Propagated to its storage).
*
* @param action type of product operation to do
* @param product product to proceed
*/
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException
{
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException {
this.storage.doAction(action, product);
}
/**
* Calls specific ProductOperation on each product in stock (Propagated to its storage).
*
* @param action ProductOperationType
*/
public void doAction(ProductOperationType action)
{
public void doAction(ProductOperationType action) {
this.storage.doAction(action);
}
@ -247,7 +253,7 @@ public class NodeBase implements ChannelObserver {
return money;
}
<<<<<<< HEAD
public Map<ChannelBase, ChannelSubscriptionType> getSubscribedChannels() {
return subscribedChannels;
}
@ -264,18 +270,20 @@ public class NodeBase implements ChannelObserver {
return allowedPurchaser;
}
=======
/**
* Adds amount to current assets
*
* @param m amount to be added
*/
>>>>>>> develop
public void addMoney(MonetaryAmount m) {
this.money = this.money.add(m);
}
/**
* Subtracts amount from current assets.
*
* @param m amount to be subtracted
*/
public void subtractMoney(MonetaryAmount m) {
@ -284,6 +292,7 @@ public class NodeBase implements ChannelObserver {
/**
* Validates signature on transaction.
*
* @param transaction transaction to be validated
* @return signature is valid
*/
@ -293,6 +302,7 @@ public class NodeBase implements ChannelObserver {
/**
* Checks if requirement creator is an allowed purchaser or allowed seller.
*
* @param req requirement
* @return creator is valid
*/
@ -306,6 +316,7 @@ public class NodeBase implements ChannelObserver {
/**
* Checks if requirement type corresponds with channel subscription type.
*
* @param requirement requirement
* @param channel channel
* @return requirement type corresponds with channel subscription type
@ -325,6 +336,7 @@ public class NodeBase implements ChannelObserver {
/**
* Chacks if product is available or if node has enough money
*
* @param req requirement
* @return product is available or node has enough money
*/
@ -337,6 +349,7 @@ public class NodeBase implements ChannelObserver {
/**
* Chacks incomming requirement.
*
* @param requirement requirement
* @param channel channel
*/

View File

@ -2,6 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -10,16 +11,14 @@ import java.util.logging.Logger;
* Operation for processing products in storage
*/
public class Action extends OperationBase {
private final OperationBase lastOperation;
private final Product product;
private final ProductOperationType type;
private int duration;
public Action(NodeBase node, Product product, ProductOperationType type) {
super(node);
super(node, product.getCurrentOperation());
this.product = product;
this.type = type;
lastOperation = product.getCurrentOperation();
duration = type.getDuration();
product.setCurrentOperation(this);
product.setStateToInAction();
@ -48,10 +47,6 @@ public class Action extends OperationBase {
return true;
}
public OperationBase getLastOperation() {
return lastOperation;
}
public Product getProduct() {
return product;
}
@ -68,4 +63,8 @@ public class Action extends OperationBase {
", duration=" + duration +
'}';
}
public String accept(ReportTextVisitor visitor) {
return visitor.visit(this);
}
}

View File

@ -16,7 +16,7 @@ public class FinancialTransaction extends Transaction {
private final ProductTransaction transactionPaid;
public FinancialTransaction(NodeBase creator, NodeBase receiver, TransactionType type, MonetaryAmount amount, ProductTransaction transactionPaid) throws InsufficientResourcesException {
super(creator, receiver, type);
super(creator, receiver, type, transactionPaid);
this.amount = amount;
this.transactionPaid = transactionPaid;
if (creator.getMoney().isLessThan(amount)) throw new InsufficientResourcesException("not enough money");

View File

@ -1,7 +1,8 @@
package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.factory.ScenarioFactory;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.reporting.PartiesReportVisitor;
import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import java.time.LocalDateTime;
import java.util.logging.Logger;
@ -17,9 +18,11 @@ public abstract class OperationBase {
private LocalDateTime timeEnd;
private final NodeBase nodeCreator;
private final OperationBase lastOperation;
OperationBase(NodeBase node) {
OperationBase(NodeBase node, OperationBase lastOperation) {
nodeCreator = node;
this.lastOperation = lastOperation;
timeStart = LocalDateTime.now();
}
@ -38,4 +41,12 @@ public abstract class OperationBase {
public NodeBase getNodeCreator() {
return nodeCreator;
}
public OperationBase getLastOperation() {
return lastOperation;
}
public String accept(ReportTextVisitor reportVisitor){
return reportVisitor.visit(this);
}
}

View File

@ -13,15 +13,13 @@ import java.util.logging.Level;
*/
public class ProductTransaction extends Transaction {
private final OperationBase lastOperation;
private final Product product;
private final ProductOperationType type;
public ProductTransaction(NodeBase creator, NodeBase receiver, TransactionType transactionType, Product product) throws InsufficientResourcesException {
super(creator, receiver, transactionType);
super(creator, receiver, transactionType, product.getCurrentOperation());
this.product = product;
type = null;
lastOperation = product.getCurrentOperation();
if (product == null || product.getProductState() != ProductState.IN_REQUIREMENT) throw new InsufficientResourcesException("product unavailable");
product.setCurrentOperation(this);
product.setStateToReadyForDispatch();
@ -40,10 +38,6 @@ public class ProductTransaction extends Transaction {
product.setStateToAvailable();
}
public OperationBase getLastOperation() {
return lastOperation;
}
public Product getProduct() {
return product;
}

View File

@ -14,8 +14,8 @@ public abstract class Transaction extends OperationBase {
private final Signature signature;
private final NodeBase nodeReceiver;
Transaction(NodeBase creator, NodeBase receiver, TransactionType type) {
super(creator);
Transaction(NodeBase creator, NodeBase receiver, TransactionType type, OperationBase lastOperation) {
super(creator, lastOperation);
transactionType = type;
nodeReceiver = receiver;
signature = SignatureManager.signTransaction(this);

View File

@ -1,7 +1,7 @@
package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
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.FinancialTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductTransaction;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
@ -15,6 +15,7 @@ public class FoodChainReportVisitor implements ReportTextVisitor {
@Override
public String visit(Product product) {
return null;
}
@Override
@ -26,4 +27,9 @@ public class FoodChainReportVisitor implements ReportTextVisitor {
public String visit(FinancialTransaction transaction) {
return null;
}
@Override
public String visit(Action action) {
return null;
}
}

View File

@ -2,28 +2,76 @@ package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.FinancialTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.*;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import java.util.LinkedList;
import java.util.List;
public class PartiesReportVisitor implements ReportTextVisitor {
private NodeBase current;
@Override
public String visit(NodeBase node) {
return null;
current = node;
StringBuilder builder = new StringBuilder();
builder.append("------NODE------").append(ln)
.append("Node type: ").append(node.getNodeType()).append(ln)
.append("Allowed seller: ").append(node.getAllowedSeller()).append(ln)
.append("Allowed purchaser: ").append(node.getAllowedPurchaser()).append(ln)
.append("Product sold and purchased, action provided: ").append(ln);
db.getProductPoolInstance().findAll().forEach(p -> builder.append(p.accept(this)));
builder.append(ln);
return builder.toString();
}
@Override
public String visit(Product product) {
return null;
if (current == null) return "";
StringBuilder builder = new StringBuilder();
List<OperationBase> operations = new LinkedList<>();
OperationBase operation = product.getCurrentOperation();
while (operation != null){
operations.add(operation);
operation = operation.getLastOperation();
}
operations.forEach(o -> builder.append(o.accept(this)));
if (builder.toString().equals("")) return "";
builder.append(ln);
return builder.toString();
}
@Override
public String visit(ProductTransaction transaction) {
return null;
if (transaction.getNodeCreator() == current)
return "Sell " + transaction.getProduct().getName() + ln;
else if (transaction.getNodeReceiver() == current)
return "Get " + transaction.getProduct().getName() + ln;
return "";
}
@Override
public String visit(FinancialTransaction transaction) {
return null;
if (transaction.getNodeCreator() == current)
return "Send " + transaction.getAmount() + ln;
else if (transaction.getNodeReceiver() == current)
return "Get " + transaction.getAmount() + ln;
return "";
}
@Override
public String visit(Action action) {
if (action.getNodeCreator() == current)
return "Action " + action.getType() + " with " + action.getProduct().getName() + ln;
return "";
}
}

View File

@ -1,15 +1,15 @@
package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.FinancialTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
import cz.cvut.fel.sit.omo.foodchain.operation.*;
import cz.cvut.fel.sit.omo.foodchain.pool.PoolDatabase;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
public interface ReportTextVisitor {
String ln = System.lineSeparator();
PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
String visit(NodeBase node);
@ -18,4 +18,24 @@ public interface ReportTextVisitor {
String visit(ProductTransaction transaction);
String visit(FinancialTransaction transaction);
String visit(Action action);
default String visit(OperationBase operationBase){
try
{
return visit((Action) operationBase);
} catch (ClassCastException e){
return visit((Transaction) operationBase);
}
}
default String visit(Transaction transaction){
try
{
return visit((ProductTransaction) transaction);
} catch (ClassCastException e){
return visit((FinancialTransaction) transaction);
}
}
}

View File

@ -2,8 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.FinancialTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductTransaction;
import cz.cvut.fel.sit.omo.foodchain.operation.*;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class SecurityReportReportVisitor implements ReportTextVisitor {
@ -26,4 +25,10 @@ public class SecurityReportReportVisitor implements ReportTextVisitor {
public String visit(FinancialTransaction transaction) {
return null;
}
@Override
public String visit(Action action) {
return null;
}
}