Finished food chain and parties report

This commit is contained in:
Nikita Iryupin 2021-01-05 20:04:36 +01:00
parent 80a52752e0
commit f1e66fc4b7
8 changed files with 96 additions and 71 deletions

View File

@ -7,7 +7,6 @@ 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.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
import cz.cvut.fel.sit.omo.foodchain.scenario.*;

View File

@ -6,6 +6,9 @@ import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import org.javamoney.moneta.Money;
import javax.money.MonetaryAmount;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Product class
@ -155,6 +158,19 @@ public class Product
public MonetaryAmount getStartPrice() { return startPrice; }
public List<OperationBase> getListOfOperations(){
OperationBase operation = this.getCurrentOperation();
List<OperationBase> operations = new LinkedList<>();
while (operation != null) {
operations.add(operation);
operation = operation.getLastOperation();
}
Collections.reverse(operations);
return operations;
}
public String accept(ReportTextVisitor visitor){
return visitor.visit(this);
}

View File

@ -2,34 +2,45 @@ package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.node.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;
public class FoodChainReportVisitor implements ReportTextVisitor {
@Override
public String visit(NodeBase node) {
return null;
return node.getNodeType().toString();
}
@Override
public String visit(Product product) {
return null;
StringBuilder builder = new StringBuilder();
builder.append("-----PRODUCT-----").append(ln)
.append("Name: ").append(product.getName()).append(ln)
.append("Type: ").append(product.getProductType()).append(ln)
.append("Started price: ").append(product.getStartPrice()).append(ln)
.append("Final price: ").append(product.getPrice()).append(ln)
.append("Road map: ").append(ln);
builder.append("Product pool");
db.getNodePoolInstance().findAll()
.stream().filter(n -> n.getStorage().getCache().stream().anyMatch(c -> c.getName().equals(product.getName())))
.forEach(n -> builder.append(" -> ").append(n.accept(this)));
builder.append(ln);
builder.append("Things happened to this product: ").append(ln);
product.getListOfOperations().forEach(o -> builder.append(o.accept(this)).append(" -> "));
builder.append("Reported").append(ln);
return builder.toString();
}
@Override
public String visit(ProductTransaction transaction) {
return null;
}
@Override
public String visit(FinancialTransaction transaction) {
return null;
return "Moves product from " + transaction.getNodeCreator().accept(this) + " to " + transaction.getNodeReceiver().accept(this);
}
@Override
public String visit(Action action) {
return null;
return "Action " + action.getType().toString() + " was done on " + action.getNodeCreator().accept(this);
}
}

View File

@ -1,17 +1,14 @@
package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.helper.ProductCache;
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.OperationBase;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductTransaction;
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) {
current = node;
@ -21,6 +18,7 @@ public class PartiesReportVisitor implements ReportTextVisitor {
.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("Applied margin: ").append(node.getMargin()).append(ln)
.append("Product sold and purchased, action provided: ").append(ln);
@ -32,19 +30,23 @@ public class PartiesReportVisitor implements ReportTextVisitor {
@Override
public String visit(Product product) {
if (current == null) return "";
ProductCache cache = current.getStorage().getCache()
.stream().filter(c -> c.getName().equals(product.getName()))
.findFirst().orElse(null);
if (current == null || cache == 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)));
builder.append("-----Product: ").append(product.getName()).append("-----").append(ln);
product.getListOfOperations().forEach(o -> {
String result = o.accept(this);
if (result != null)
builder.append(o.accept(this));
});
if (builder.toString().equals("")) return "";
builder.append(ln);
builder.append("Presence of product on Node (in ticks): ").append(cache.getDuration()).append(ln);
return builder.toString();
}
@ -55,25 +57,13 @@ public class PartiesReportVisitor implements ReportTextVisitor {
return "Sell " + transaction.getProduct().getName() + ln;
else if (transaction.getNodeReceiver() == current)
return "Get " + transaction.getProduct().getName() + ln;
return "";
}
@Override
public String visit(FinancialTransaction transaction) {
if (transaction.getNodeCreator() == current)
return "Send " + transaction.getAmount() + ln;
else if (transaction.getNodeReceiver() == current)
return "Get " + transaction.getAmount() + ln;
return "";
return null;
}
@Override
public String visit(Action action) {
if (action.getNodeCreator() == current)
return "Action " + action.getType() + " with " + action.getProduct().getName() + ln;
return "";
return "Action " + action.getType() + " was done with " + action.getProduct().getName() + ln;
return null;
}
}

View File

@ -5,43 +5,59 @@ import cz.cvut.fel.sit.omo.foodchain.pool.PoolDatabase;
public class ReportBuilder {
private final StringBuilder report;
private final String ln;
private final PoolDatabase db;
public ReportBuilder(){
public ReportBuilder() {
report = new StringBuilder();
db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
ln = System.lineSeparator();
}
public ReportBuilder addPartiesReport(){
public ReportBuilder addPartiesReport() {
ReportTextVisitor reportVisitor = new PartiesReportVisitor();
db.getNodePoolInstance().findAll().forEach(n -> report.append(reportVisitor.visit(n)));
report.append("--------------").append(ln)
.append("PARTIES REPORT").append(ln)
.append("--------------").append(ln).append(ln);
db.getNodePoolInstance().findAll().forEach(n -> report.append(n.accept(reportVisitor)));
report.append(ln);
return this;
}
public ReportBuilder addFoodChainReport(){
public ReportBuilder addFoodChainReport() {
ReportTextVisitor reportVisitor = new FoodChainReportVisitor();
db.getProductPoolInstance().findAll().forEach(p -> report.append(reportVisitor.visit(p)));
report.append("------------------").append(ln)
.append("FOOD CHAIN REPORT").append(ln)
.append("------------------").append(ln).append(ln);
db.getProductPoolInstance().findAll().forEach(p -> report.append(p.accept(reportVisitor)));
report.append(ln);
return this;
}
public ReportBuilder addSecurityReport(){
ReportTextVisitor reportVisitor = new FoodChainReportVisitor();
public ReportBuilder addSecurityReport() {
ReportTextVisitor reportVisitor = new SecurityReportVisitor();
db.getNodePoolInstance().findAll().forEach(n -> report.append(reportVisitor.visit(n)));
report.append("----------------").append(ln)
.append("SECURITY REPORT").append(ln)
.append("----------------").append(ln).append(ln);
db.getNodePoolInstance().findAll().forEach(n -> report.append(n.accept(reportVisitor)));
return this;
}
public ReportBuilder addTransactionReport(){
public ReportBuilder addTransactionReport() {
//TODO
return this;
}
public String build(){
public String build() {
return report.toString();
}
}

View File

@ -2,7 +2,10 @@ package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.*;
import cz.cvut.fel.sit.omo.foodchain.operation.Action;
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.pool.PoolDatabase;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
@ -16,25 +19,21 @@ public interface ReportTextVisitor {
String visit(ProductTransaction transaction);
String visit(FinancialTransaction transaction);
String visit(Action action);
default String visit(OperationBase operationBase){
try
{
default String visit(OperationBase operationBase) {
try {
return visit((Action) operationBase);
} catch (ClassCastException e){
} catch (ClassCastException e) {
return visit((Transaction) operationBase);
}
}
default String visit(Transaction transaction){
try
{
default String visit(Transaction transaction) {
try {
return visit((ProductTransaction) transaction);
} catch (ClassCastException e){
return visit((FinancialTransaction) transaction);
} catch (ClassCastException ignored) {
}
return null;
}
}

View File

@ -2,14 +2,14 @@ package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.node.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;
public class SecurityReportVisitor implements ReportTextVisitor {
@Override
public String visit(NodeBase node) {
return null;
return
;
}
@Override
@ -22,11 +22,6 @@ public class SecurityReportVisitor implements ReportTextVisitor {
return null;
}
@Override
public String visit(FinancialTransaction transaction) {
return null;
}
@Override
public String visit(Action action) { return null; }

View File

@ -2,7 +2,6 @@ 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;
import javax.naming.InsufficientResourcesException;
import java.util.logging.Level;