Changed project structure, fixed some stuff

This commit is contained in:
Tomáš Musil 2021-01-05 18:43:47 +01:00
parent 5ca75548bc
commit 80a52752e0
47 changed files with 292 additions and 268 deletions

View File

@ -24,7 +24,7 @@
***
**Nefunkční požadavky:**
- Aplikace nepožaduje autentizaci ani autorizaci.
- Do konzole se při každém ticku vypisují provedené úkony. Z toho pak vzniká PartiesReport.
- Do konzole se při každém ticku vypisují provedené úkony. Z toho pak vzniká TransactionReport.
- V systému není centrální autorita, jednotlivé nody na sebe nevidí a jediná třída přes kterou procházejí všechny transakce je SignatureManager, který je v tomto směru naprosto neutrální. Aplikace běží pouze v jednom threadu.
- Všechny proměnné jsou private nebo protected. Většina proměnných je final, abychom se vyhnuli např. změtným úpravám transakcí.
- SignatureManager přidává na transakce Signature, které následně i validuje. Node zahazuje (ukládá jako neplatné) všechny nevalidní transakce.
@ -33,6 +33,6 @@
***
**Požadované výstupy:**
- Diskrétní kroky jsou realizovány pomocí třídy Ticker resp. metody tick() na jednotlivých třídách.
- Diagramy jsou ve složce resources v souboru FoodChain.eap. BahavioralDiagram byl náš prvotní návrh a není aktuální. Ostatní reporty odpovídají realitě.
- Diagramy jsou ve složce resources v souboru FoodChain.eap.
- Javadoc je ve složce resources v souboru FoodChain.html.
***

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE scenarios [
<!ELEMENT scenarios (scenario)*>
<!ELEMENT scenario (steps)*>
<!ATTLIST scenario
type CDATA #REQUIRED>
<!ELEMENT steps (#PCDATA)>
]>
<!--
generate
consume
doaction <product-operation-type>
doaction <product-operation-type> <product-name>
sell <product-type> <product-name>
buy <product-type> <produt-name> <product-price>
pass
-->
<scenarios>
<scenario type="Farmer">
<steps>
generate
pass
pass
sell FRUIT jablko
</steps>
</scenario>
</scenarios>

View File

@ -1,31 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE nodes [
<!ELEMENT nodes (node)*>
<!ELEMENT node (money|channels)*>
<!ELEMENT node (money|margin|discount|channels)*>
<!ATTLIST node
type CDATA #REQUIRED>
<!ELEMENT money (#PCDATA)>
<!ELEMENT margin (#PCDATA)>
<!ELEMENT discount (#PCDATA)>
<!ELEMENT channels (#PCDATA)>
]>
<nodes>
<node type = "Farmer">
<money>100</money>
<margin>1.2</margin>
<discount>1</discount>
<channels>MEAT PURCHASE;VEGETABLE PURCHASE;FRUIT PURCHASE</channels>
</node>
<node type = "Processor">
<money>10</money>
<money>200</money>
<margin>1.3</margin>
<discount>0.9</discount>
<channels>MEAT SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
</node>
<node type = "Seller">
<money>1500</money>
<margin>1.5</margin>
<discount>0.8</discount>
<channels>MEAT SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
</node>
<node type = "Distributor">
<money>1000</money>
<margin>1.2</margin>
<discount>1</discount>
<channels>MEAT SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
</node>
<node type = "Customer">
<money>250</money>
<money>750</money>
<margin>1</margin>
<discount>1</discount>
<channels>MEAT SALE;VEGETABLE SALE;FRUIT SALE</channels>
</node>
</nodes>

View File

@ -19,7 +19,55 @@ pass
<scenario type="Farmer">
<steps>
generate
generate
generate
generate
generate
generate
generate
generate
generate
</steps>
</scenario>
<scenario type="Processor">
<steps>
pass
pass
doaction DRY_FRUIT jablko
sell FRUIT hruška
sell FRUIT banán
sell VEGETABLE okurka
doaction STORE_VEGETABLE paprika
sell FRUIT jablko
doaction STORE_VEGETABLE rajče
doaction BOIL_MEAT vepřové
sell VEGETABLE paprika
doaction GRILL_MEAT hovězí
sell VEGETABLE rajče
sell MEAT vepřové
sell MEAT kuřecí
sell MEAT hovězí
</steps>
</scenario>
<scenario type="Seller">
<steps>
pass
pass
</steps>
</scenario>
<scenario type="Distributor">
<steps>
pass
pass
pass
</steps>
</scenario>
<scenario type="Customer">
<steps>
pass
pass
pass
pass
</steps>
</scenario>
</scenarios>

View File

@ -2,7 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.parser.ConfigurationParser;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResult;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
@ -39,14 +39,23 @@ public class FoodChain
rootLogger.addHandler(new ReportLogger());
if(args.length < 4)
if(args.length < 1)
{
parseChannels("conf/channels.xml");
parseNodes("conf/nodes.xml");
parseProducts("conf/products.xml");
parseScenarios("conf/scenarios.xml");
rootLogger.log(Level.CONFIG,"Parsed default scenarios");
rootLogger.log(Level.INFO,"Parsed default scenarios");
}
else if (args.length < 2)
{
parseChannels("conf/channels.xml");
parseNodes("conf/nodes.xml");
parseProducts("conf/products.xml");
parseScenarios(args[0]);
rootLogger.log(Level.INFO,"Parsed non-default scenarios");
}
else
{
@ -55,29 +64,18 @@ public class FoodChain
parseProducts(args[3]);
parseScenarios(args[4]);
rootLogger.log(Level.CONFIG,"Parsed non-default scenarios");
rootLogger.log(Level.INFO,"Parsed non-default scenarios");
}
// getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType()));
// getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().findAll().stream().forEach(ch -> System.out.println(ch));
// getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType() + " " + ch.getName()));
// getFactoryManager().getPoolDatabaseInstance().getScenarioPoolInstance().findAll().stream().forEach(ch -> ch.getSteps().stream().forEach(s -> System.out.println(s)));
Ticker.start();
while (Ticker.getTimer().isRunning())
{
;
}
while (Ticker.getTimer().isRunning()) {}
ReportingManager reportingManager = new ReportingManager();
System.out.println(reportingManager.generateReport());
}
public static FactoryManager getFactoryManager()
{
return factoryManager;
}
public static FactoryManager getFactoryManager() { return factoryManager; }
/**
* Provides connection between FoodChain and ConfigurationParser.

View File

@ -1,15 +1,17 @@
package cz.cvut.fel.sit.omo.foodchain.channel;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
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.product.ProductType;
import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
import cz.cvut.fel.sit.omo.foodchain.util.Observable;
import java.util.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
/**

View File

@ -1,7 +1,5 @@
package cz.cvut.fel.sit.omo.foodchain.channel;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
/**
* Channel for transporting other products
*/

View File

@ -2,7 +2,6 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.channel.*;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;

View File

@ -3,8 +3,6 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType;
import cz.cvut.fel.sit.omo.foodchain.node.*;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
@ -30,6 +28,8 @@ public class NodeFactory
NodeType nodeType = null;
String subsChannels = "";
double startMoney = 0;
double margin = 0;
double acceptableDiscount = 0;
try
{
@ -37,6 +37,8 @@ public class NodeFactory
subsChannels = input.getChild("channels");
startMoney = Double.parseDouble(input.getChild("money"));
margin = Double.parseDouble(input.getChild("margin"));
acceptableDiscount = Double.parseDouble(input.getChild("discount"));
}
catch (IllegalArgumentException e)
{
@ -50,19 +52,19 @@ public class NodeFactory
switch (Objects.requireNonNull(nodeType))
{
case Farmer:
n = new Farmer(startMoney);
n = new Farmer(startMoney, margin, acceptableDiscount);
break;
case Processor:
n = new Processor(startMoney);
n = new Processor(startMoney, margin, acceptableDiscount);
break;
case Distributor:
n = new Distributor(startMoney);
n = new Distributor(startMoney, margin, acceptableDiscount);
break;
case Seller:
n = new Seller(startMoney);
n = new Seller(startMoney, margin, acceptableDiscount);
break;
case Customer:
n = new Customer(startMoney);
n = new Customer(startMoney, margin, acceptableDiscount);
break;
default:
}

View File

@ -2,7 +2,6 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
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.ProductState;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
import java.util.logging.Level;

View File

@ -3,7 +3,7 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.node.Customer;
import cz.cvut.fel.sit.omo.foodchain.node.Farmer;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
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;
@ -60,9 +60,7 @@ public class ScenarioFactory
}
else
{
Product p = FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().stream()
.filter(pr -> pr.getName().equals(parts[2])).findFirst().orElse(null);
scenarioStep = new DoActionStep(node, ProductOperationType.valueOf(parts[1]), p);
scenarioStep = new DoActionStep(node, ProductOperationType.valueOf(parts[1]), parts[2]);
}
break;
case "sell":

View File

@ -1,17 +1,14 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.BuyerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import java.util.Random;
/**
* Customer node
*/
public class Customer extends BuyerNodeBase {
public class Customer extends NodeBase {
public Customer(double startMoney) {
super(startMoney);
public Customer(double startMoney, double margin, double acceptableDiscount) {
super(startMoney, margin, acceptableDiscount);
allowedSeller = NodeType.Distributor;
allowedPurchaser = NodeType.None;
nodeType = NodeType.Customer;

View File

@ -1,15 +1,12 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.RetailerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
/**
* Distributor node
*/
public class Distributor extends RetailerNodeBase {
public class Distributor extends NodeBase {
public Distributor(double startMoney) {
super(startMoney);
public Distributor(double startMoney, double margin, double acceptableDiscount) {
super(startMoney, margin, acceptableDiscount);
allowedSeller = NodeType.Seller;
allowedPurchaser = NodeType.Customer;
this.nodeType = NodeType.Distributor;

View File

@ -1,7 +1,5 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.SellerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
@ -11,10 +9,10 @@ import java.util.logging.Level;
/**
* Farmer node
*/
public class Farmer extends SellerNodeBase {
public class Farmer extends NodeBase {
public Farmer(double startMoney) {
super(startMoney);
public Farmer(double startMoney, double margin, double acceptableDiscount) {
super(startMoney, margin, acceptableDiscount);
allowedSeller = NodeType.None;
allowedPurchaser = NodeType.Processor;
nodeType = NodeType.Farmer;

View File

@ -1,9 +1,8 @@
package cz.cvut.fel.sit.omo.foodchain.node.base;
package cz.cvut.fel.sit.omo.foodchain.node;
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.node.Storage;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.node.helper.RefusedRequirement;
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.ProductState;
@ -16,7 +15,10 @@ import org.javamoney.moneta.Money;
import javax.money.MonetaryAmount;
import javax.naming.InsufficientResourcesException;
import java.util.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -32,6 +34,8 @@ public class NodeBase implements ChannelObserver {
private final List<RefusedRequirement> refusedRequirements = new LinkedList<>();
private MonetaryAmount money;
private final double margin;
private final double acceptableDiscount;
protected final Storage storage;
protected NodeType nodeType;
@ -42,12 +46,16 @@ public class NodeBase implements ChannelObserver {
this.nodeType = NodeType.None;
this.storage = new Storage(this);
this.money = Money.of(0, "USD");
this.margin = 1.2;
this.acceptableDiscount = 0.9;
}
public NodeBase(double m) {
public NodeBase(double m, double margin, double acceptableDiscount) {
this.nodeType = NodeType.None;
this.storage = new Storage(this);
this.money = Money.of(m, "USD");
this.margin = margin;
this.acceptableDiscount = acceptableDiscount;
}
@ -111,10 +119,11 @@ public class NodeBase implements ChannelObserver {
*/
private void receiveTransaction(ProductTransaction transaction) {
if (validateSignature(transaction)) {
transaction.getProduct().setStateToReadyForReception();
Product p = transaction.getProduct();
p.setStateToReadyForReception();
transaction.endTransaction();
sendTransaction(transaction.getProduct().getPrice(), transaction.getNodeCreator(), transaction);
sendTransaction(p.getPrice(), transaction.getNodeCreator(), transaction);
p.setPrice(p.getPrice().multiply(margin));
} else refuseTransaction(transaction);
}
@ -164,7 +173,7 @@ public class NodeBase implements ChannelObserver {
price = storage.getProduct(pName, pType).getPrice();
} catch (NullPointerException e) {
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
throw new InsufficientResourcesException("product unavailable");
throw new InsufficientResourcesException(this.getClass().getSimpleName() + " tried to sell unavailable product " + pName);
}
}
if (tType == TransactionType.SALE) {
@ -172,7 +181,7 @@ public class NodeBase implements ChannelObserver {
storage.getProduct(pName, pType).setStateToInRequirement();
} catch (NullPointerException | IllegalStateException e) {
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
throw new InsufficientResourcesException("product unavailable");
throw new InsufficientResourcesException(this.getClass().getSimpleName() + " tried to sell unavailable product " + pName);
}
}
Requirement req = new Requirement(this, tType, pType, pName, price);
@ -192,7 +201,7 @@ public class NodeBase implements ChannelObserver {
if (req.getTransactionType() == TransactionType.SALE) {
createRequirement(TransactionType.PURCHASE, req.getProductType(), req.getProductName(), req.getPrice());
} else {
if (req.getPrice().isGreaterThanOrEqualTo(storage.getProduct(req.getProductName(), req.getProductType()).getPrice().multiply(0.9))) {
if (req.getPrice().isGreaterThanOrEqualTo(storage.getProduct(req.getProductName(), req.getProductType()).getPrice().multiply(acceptableDiscount))) {
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());
@ -226,10 +235,10 @@ 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
* @param name product to proceed
*/
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException {
this.storage.doAction(action, product);
public void doAction(ProductOperationType action, String name) throws IllegalCallerException, InsufficientResourcesException {
this.storage.doAction(action, name);
}
/**
@ -270,6 +279,9 @@ public class NodeBase implements ChannelObserver {
return allowedPurchaser;
}
public double getMargin() { return margin; }
public double getAcceptableDiscount() { return acceptableDiscount; }
/**
* Adds amount to current assets
@ -366,24 +378,11 @@ public class NodeBase implements ChannelObserver {
}
/**
* Receives transactions.
* Receives transactions and passes tick to storage.
*/
public void tick() {
receiveTransactions();
}
private static class RefusedRequirement {
public final TransactionType tType;
public final ProductType pType;
public final String pName;
public final MonetaryAmount price;
public RefusedRequirement(TransactionType tType, ProductType pType, String pName, MonetaryAmount price) {
this.tType = tType;
this.pType = pType;
this.pName = pName;
this.price = price;
}
storage.tick();
}
public String accept(ReportTextVisitor visitor) {

View File

@ -1,4 +1,4 @@
package cz.cvut.fel.sit.omo.foodchain.node.enums;
package cz.cvut.fel.sit.omo.foodchain.node;
/**
* Enum class for all nodes

View File

@ -1,15 +1,12 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.RetailerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
/**
* Processor node
*/
public class Processor extends RetailerNodeBase {
public class Processor extends NodeBase {
public Processor(double startMoney) {
super(startMoney);
public Processor(double startMoney, double margin, double acceptableDiscount) {
super(startMoney, margin, acceptableDiscount);
allowedSeller = NodeType.Farmer;
allowedPurchaser = NodeType.Seller;
this.nodeType = NodeType.Processor;

View File

@ -1,15 +1,12 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.RetailerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
/**
* Seller node
*/
public class Seller extends RetailerNodeBase {
public class Seller extends NodeBase {
public Seller(double startMoney) {
super(startMoney);
public Seller(double startMoney, double margin, double acceptableDiscount) {
super(startMoney, margin, acceptableDiscount);
allowedSeller = NodeType.Processor;
allowedPurchaser = NodeType.Distributor;
this.nodeType = NodeType.Seller;

View File

@ -1,13 +1,15 @@
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.node.helper.ProductCache;
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 javax.naming.InsufficientResourcesException;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Storage sub-node for storing products of other nodes
@ -16,6 +18,7 @@ public class Storage
{
private final NodeBase storageOwner;
private final List<Product> products = new LinkedList<>();
private final List<ProductCache> cache = new LinkedList<>();
public Storage(NodeBase owner)
{
@ -29,6 +32,7 @@ public class Storage
public void storeProduct(Product product)
{
this.products.add(product);
this.cache.add(new ProductCache(product.getName(), product.getProductType()));
}
/**
@ -38,6 +42,7 @@ public class Storage
public void storeProducts(List<Product> products)
{
this.products.addAll(products);
this.cache.addAll(products.stream().map(p -> new ProductCache(p.getName(), p.getProductType())).collect(Collectors.toList()));
}
/**
@ -56,6 +61,8 @@ public class Storage
return products;
}
public List<ProductCache> getCache() { return cache; }
/**
* Returns number of products in storage.
* @return
@ -99,15 +106,16 @@ public class Storage
/**
* Proceed specific type of action on provided product if its possible.
* @param action type of product operation to do
* @param product product to proceed
* @param productName product to process
*/
public void doAction(ProductOperationType action, Product product) throws IllegalCallerException
{
if (action.getNode().equals(this.storageOwner.getNodeType()))
if (action.getProduct().equals(product.getProductType()) || action.getProduct().equals(ProductType.ALL))
public void doAction(ProductOperationType action, String productName) throws IllegalCallerException, InsufficientResourcesException {
if (!containsProduct(productName, action.getProductType())) throw new InsufficientResourcesException(storageOwner.getClass().getSimpleName() + " tried to do action " + action.toString() + " to unavailable product " + productName);
Product product = getProduct(productName, action.getProductType());
if (action.getNode().equals(this.storageOwner.getNodeType()) || action.getNode().equals(NodeType.All))
if (action.getProductType().equals(product.getProductType()) || action.getProductType().equals(ProductType.ALL))
new Action(this.storageOwner, product, action);
else
throw new IllegalCallerException("! IllegalCallerException in NodeBase. " + product.getProductType().toString() + " cannot be processed by action " + action.name() + " only for " + action.getProduct());
throw new IllegalCallerException("! IllegalCallerException in NodeBase. " + product.getProductType().toString() + " cannot be processed by action " + action.name() + " only for " + action.getProductType());
else
throw new IllegalCallerException("! IllegalCallerException in NodeBase. " + this.storageOwner.getNodeType().toString() + " cannot do action available only for " + action.getNode());
}
@ -122,4 +130,7 @@ public class Storage
}
public void tick() {
cache.stream().filter(p -> containsProduct(p.getName(), p.getType())).forEach(ProductCache::addToDuration);
}
}

View File

@ -1,19 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.IBuyerNode;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
/**
* Base class for all buyer nodes
*/
public class BuyerNodeBase extends NodeBase implements IBuyerNode {
public BuyerNodeBase(double m){
super(m);
}
@Override
public void buyProduct(Product product) {
}
}

View File

@ -1,29 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.IBuyerNode;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.ISellerNode;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
/**
* Base class for all retailer nodes
*/
public class RetailerNodeBase extends NodeBase implements IBuyerNode, ISellerNode {
public RetailerNodeBase(double startMoney) {
super(startMoney);
}
@Override
public void sellProduct(Product product) {
}
@Override
public void addMargin(Product product) {
}
@Override
public void buyProduct(Product product) {
}
}

View File

@ -1,23 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.ISellerNode;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
/**
* Base class for all seller nodes
*/
public class SellerNodeBase extends NodeBase implements ISellerNode {
public SellerNodeBase(double startMoney) {
super(startMoney);
}
@Override
public void sellProduct(Product product) {
}
@Override
public void addMargin(Product product) {
}
}

View File

@ -0,0 +1,23 @@
package cz.cvut.fel.sit.omo.foodchain.node.helper;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
public class ProductCache {
private final String name;
private final ProductType type;
private int duration;
public ProductCache(String name, ProductType type) {
this.name = name;
this.type = type;
this.duration = 0;
}
public String getName() { return name; }
public ProductType getType() { return type; }
public int getDuration() { return duration; }
public void addToDuration() { duration++; }
}

View File

@ -0,0 +1,29 @@
package cz.cvut.fel.sit.omo.foodchain.node.helper;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
import javax.money.MonetaryAmount;
public class RefusedRequirement {
private final TransactionType transactionType;
private final ProductType productType;
private final String productName;
private final MonetaryAmount price;
public RefusedRequirement(TransactionType tType, ProductType pType, String pName, MonetaryAmount price) {
this.transactionType = tType;
this.productType = pType;
this.productName = pName;
this.price = price;
}
public TransactionType getTransactionType() { return transactionType; }
public ProductType getProductType() { return productType; }
public String getProductName() { return productName; }
public MonetaryAmount getPrice() { return price; }
}

View File

@ -1,11 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.node.interfaces;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
/**
* Interface for buyer nodes
*/
public interface IBuyerNode {
void buyProduct(Product product);
}

View File

@ -1,13 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.node.interfaces;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
/**
* Interface for seller nodes
*/
public interface ISellerNode {
void sellProduct(Product product);
void addMargin(Product product);
}

View File

@ -1,11 +1,10 @@
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.node.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;
/**
* Operation for processing products in storage
@ -39,11 +38,11 @@ public class Action extends OperationBase {
* @return action is in progress
*/
public boolean tick() {
duration--;
if (duration == 0) {
endAction();
return false;
}
else duration--;
return true;
}
@ -58,9 +57,10 @@ public class Action extends OperationBase {
@Override
public String toString() {
return "Action{" +
", product=" + product +
"product=" + product.getName() +
", type=" + type +
", duration=" + duration +
", temperature=" + type.getTemperature() +
'}';
}

View File

@ -1,6 +1,6 @@
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.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import javax.money.MonetaryAmount;

View File

@ -1,7 +1,6 @@
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.reporting.PartiesReportVisitor;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import java.time.LocalDateTime;

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.node.NodeType;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
/**
@ -8,32 +8,32 @@ import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
*/
public enum ProductOperationType
{
STORE_MEAT(NodeType.All, ProductType.MEAT, 20, -8),
STORE_FRUIT(NodeType.All, ProductType.FRUIT, 8, 2),
STORE_VEGETABLE(NodeType.All, ProductType.VEGETABLE, 10, 2),
STORE_OTHER(NodeType.All, ProductType.OTHER, 30, 21),
STORE_MEAT(NodeType.All, ProductType.MEAT, 3, -8),
STORE_FRUIT(NodeType.All, ProductType.FRUIT, 3, 2),
STORE_VEGETABLE(NodeType.All, ProductType.VEGETABLE, 3, 2),
STORE_OTHER(NodeType.All, ProductType.OTHER, 3, 21),
WRAP_INTO_GIFT_PACKAGE(NodeType.Seller, ProductType.ALL, 1, -8),
UNWRAP(NodeType.All, ProductType.ALL, 1, 21),
CUT_TO_PIECES(NodeType.All, ProductType.ALL, 1, 21),
WRAP_INTO_GIFT_PACKAGE(NodeType.Seller, ProductType.ALL, 1, 21),
UNWRAP(NodeType.Customer, ProductType.ALL, 1, 21),
CUT_TO_PIECES(NodeType.Customer, ProductType.ALL, 1, 21),
BOIL_MEAT(NodeType.All, ProductType.MEAT, 1, 21),
GRILL_MEAT(NodeType.All, ProductType.MEAT, 1, 21),
SMOKE_MEAT(NodeType.All, ProductType.MEAT, 1, 21),
DRY_MEAT(NodeType.All, ProductType.MEAT, 1, 21),
BOIL_MEAT(NodeType.Processor, ProductType.MEAT, 2, 100),
GRILL_MEAT(NodeType.Processor, ProductType.MEAT, 2, 200),
SMOKE_MEAT(NodeType.Processor, ProductType.MEAT, 7, 60),
DRY_MEAT(NodeType.Processor, ProductType.MEAT, 5, 55),
DRY_FRUIT(NodeType.All, ProductType.FRUIT, 1, 21),
FREEZE_DRY_FRUIT(NodeType.All, ProductType.FRUIT, 1, 21);
DRY_FRUIT(NodeType.Processor, ProductType.FRUIT, 4, 45),
FREEZE_DRY_FRUIT(NodeType.Processor, ProductType.FRUIT, 3, -195);
private final NodeType node;
private final ProductType product;
private final ProductType productType;
private final int duration;
private final int temperature;
ProductOperationType(NodeType node, ProductType product, int duration, int temperature)
{
this.node = node;
this.product = product;
this.productType = product;
this.duration = duration;
this.temperature = temperature;
}
@ -53,5 +53,5 @@ public enum ProductOperationType
return node;
}
public ProductType getProduct() { return product; }
public ProductType getProductType() { return productType; }
}

View File

@ -1,6 +1,6 @@
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.node.NodeBase;
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.reporting.ReportTextVisitor;
@ -20,7 +20,7 @@ public class ProductTransaction extends Transaction {
super(creator, receiver, transactionType, product.getCurrentOperation());
this.product = product;
type = null;
if (product == null || product.getProductState() != ProductState.IN_REQUIREMENT) throw new InsufficientResourcesException("product unavailable");
if (product == null || product.getProductState() != ProductState.IN_REQUIREMENT) throw new InsufficientResourcesException(creator.getClass().getSimpleName() + " tried to sell unavailable product " + product.getName() + " to " + receiver.getClass().getSimpleName());
product.setCurrentOperation(this);
product.setStateToReadyForDispatch();
creator.getStorage().removeProduct(product);

View File

@ -1,6 +1,6 @@
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.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.reporting.ReportTextVisitor;
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;

View File

@ -1,7 +0,0 @@
package cz.cvut.fel.sit.omo.foodchain.parser;
/**
* Parser for console input
*/
public class ConsoleParser {
}

View File

@ -1,7 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.parser;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.pool;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
/**
*Pool for storing all nodes

View File

@ -16,6 +16,7 @@ public class Product
private ProductType productType;
private OperationBase currentOperation;
private MonetaryAmount price;
private final MonetaryAmount startPrice;
private String name;
@ -25,6 +26,7 @@ public class Product
this.productType = productType;
this.productState = ProductState.AVAILABLE;
this.price = Money.of(m, "USD");
this.startPrice = Money.of(m, "USD");
}
/**
@ -151,6 +153,8 @@ public class Product
public void setPrice(MonetaryAmount price) { this.price = price; }
public MonetaryAmount getStartPrice() { return startPrice; }
public String accept(ReportTextVisitor visitor){
return visitor.visit(this);
}

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
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;

View File

@ -1,8 +1,10 @@
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.*;
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.OperationBase;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductTransaction;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import java.util.LinkedList;

View File

@ -1,8 +1,7 @@
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.node.NodeBase;
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;

View File

@ -1,8 +1,9 @@
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.*;
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 {

View File

@ -1,14 +1,11 @@
package cz.cvut.fel.sit.omo.foodchain.requirement;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
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.ProductState;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
import cz.cvut.fel.sit.omo.foodchain.util.Ticker;
import org.javamoney.moneta.Money;
import javax.money.MonetaryAmount;
import javax.naming.InsufficientResourcesException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -33,7 +30,7 @@ public class Requirement {
this.productType = productType;
this.productName = productName;
this.price = price;
this.TTL = 3;
this.TTL = 5;
this.id = counter++;
LOGGER.log(Level.INFO, toString());

View File

@ -1,9 +1,10 @@
package cz.cvut.fel.sit.omo.foodchain.scenario;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
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;
/**
@ -12,12 +13,12 @@ import java.util.logging.Level;
public class DoActionStep extends ScenarioStep {
private final NodeBase nodeBase;
private final ProductOperationType productOperationType;
private final Product product;
private final String productName;
public DoActionStep(NodeBase node, ProductOperationType productOperationType, Product product){
public DoActionStep(NodeBase node, ProductOperationType productOperationType, String name){
this.nodeBase = node;
this.productOperationType = productOperationType;
this.product = product;
this.productName = name;
}
public DoActionStep(NodeBase node, ProductOperationType productOperationType){
@ -31,14 +32,14 @@ public class DoActionStep extends ScenarioStep {
@Override
void execute() {
try {
if (product != null) {
nodeBase.doAction(productOperationType, product);
if (productName != null) {
nodeBase.doAction(productOperationType, productName);
}
else {
nodeBase.doAction(productOperationType);
}
}
catch (IllegalCallerException e) {
catch (IllegalCallerException | InsufficientResourcesException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
}
}

View File

@ -1,7 +1,5 @@
package cz.cvut.fel.sit.omo.foodchain.scenario;
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
import java.util.logging.Logger;
/**

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.scenario;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
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;
import org.javamoney.moneta.Money;

View File

@ -16,11 +16,7 @@ public class ReportLogger extends Handler
@Override
public void publish(LogRecord record) {
StringBuilder sb = new StringBuilder();
sb.append(record.getSourceClassName())
.append("#")
.append(record.getSourceMethodName())
.append(" - ")
.append(record.getMessage());
sb.append(record.getMessage());
if(record.getLevel().equals(Level.WARNING) || record.getLevel().equals(Level.SEVERE))
{

View File

@ -2,7 +2,6 @@ package cz.cvut.fel.sit.omo.foodchain.util;
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.pool.PoolDatabase;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
@ -29,8 +28,8 @@ public class Ticker {
PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
boolean productBool = db.getProductPoolInstance().findAll().stream().filter(Product::tick).count() == 0; // change duration of action
boolean channelBool = db.getChannelPoolInstance().findAll().stream().filter(ChannelBase::tick).count() == 0; // change TTL of requirements
boolean scenarioBool = db.getScenarioPoolInstance().findAll().stream().filter(Scenario::tick).count() == 0; // do next step in scenario
db.getNodePoolInstance().findAll().forEach(n -> n.tick()); // receive transactions
boolean scenarioBool = db.getScenarioPoolInstance().findAll().stream().filter(Scenario::tick).count() == 0; // do next step in scenario
if (channelBool && productBool && scenarioBool) stop();
});
timer.start();