Added most of Javadoc
This commit is contained in:
parent
a2b5c2bc92
commit
e1ff0508ea
@ -11,6 +11,9 @@ import cz.cvut.fel.sit.omo.foodchain.util.Observable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Base class for all channels
|
||||
*/
|
||||
public abstract class ChannelBase implements Observable {
|
||||
protected ProductType productType;
|
||||
protected Queue<Requirement> requirements;
|
||||
@ -45,7 +48,7 @@ public abstract class ChannelBase implements Observable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new requirement to the requirement Queue
|
||||
* Adds new requirement to the requirement Queue.
|
||||
* @param requirement requirement
|
||||
*/
|
||||
public void addRequirement(Requirement requirement)
|
||||
@ -53,6 +56,10 @@ public abstract class ChannelBase implements Observable {
|
||||
this.requirements.add(requirement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes tick onto every requirement in channel, if requirement has TTL= 0 deletes it, notifies all nodes with current requirements.
|
||||
* @return if there are any requirements
|
||||
*/
|
||||
public boolean tick() {
|
||||
requirements.forEach(Requirement::tick);
|
||||
try {
|
||||
@ -63,23 +70,32 @@ public abstract class ChannelBase implements Observable {
|
||||
return requirements.size() != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies all nodes apart from creator about a requirement.
|
||||
*/
|
||||
@Override
|
||||
public void notifyAllObservers(){
|
||||
subscribers.forEach(subscriber -> new LinkedList<>(requirements).forEach(r -> subscriber.update(r, this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds observer to list of subscribed nodes.
|
||||
* @param observer channelobserver
|
||||
*/
|
||||
@Override
|
||||
public void attach(ChannelObserver observer){
|
||||
subscribers.add(observer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes observer from list of subscribed nodes.
|
||||
* @param observer channelobserver
|
||||
*/
|
||||
@Override
|
||||
public void detach(ChannelObserver observer){
|
||||
subscribers.remove(observer);
|
||||
}
|
||||
public void detach(ChannelObserver observer){ subscribers.remove(observer); }
|
||||
|
||||
/**
|
||||
* Adds new transaction to the transaction Queue
|
||||
* Adds new transaction to the transaction Queue.
|
||||
* @param transaction transaction
|
||||
*/
|
||||
public void addTransaction(Transaction transaction)
|
||||
@ -91,12 +107,20 @@ public abstract class ChannelBase implements Observable {
|
||||
catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes requirement from list of active requirements.
|
||||
* @param requirement requirement
|
||||
*/
|
||||
public void disposeOf(Requirement requirement) { this.requirements.remove(requirement); }
|
||||
|
||||
/**
|
||||
* Removes transaction from list of unprocessed transactions.
|
||||
* @param transaction transaction
|
||||
*/
|
||||
public void disposeOf(Transaction transaction) { this.transactions.remove(transaction); }
|
||||
|
||||
/**
|
||||
* Clears all requirements from this channel
|
||||
* Clears all active requirements from this channel.
|
||||
*/
|
||||
public void clearRequirements()
|
||||
{
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
/**
|
||||
* Enum class for different subscription types
|
||||
*/
|
||||
public enum ChannelSubscriptionType {
|
||||
|
||||
SALE, PURCHASE, SALE_AND_PURCHASE
|
||||
|
||||
@ -2,6 +2,9 @@ package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
/**
|
||||
* Channel for transporting fruit
|
||||
*/
|
||||
public class FruitChannel extends ChannelBase
|
||||
{
|
||||
public FruitChannel()
|
||||
|
||||
@ -2,6 +2,9 @@ package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
/**
|
||||
* Channel for transporting meat
|
||||
*/
|
||||
public class MeatChannel extends ChannelBase
|
||||
{
|
||||
public MeatChannel()
|
||||
|
||||
@ -2,6 +2,9 @@ package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
/**
|
||||
* Channel for transporting other products
|
||||
*/
|
||||
public class OtherChannel extends ChannelBase
|
||||
{
|
||||
public OtherChannel()
|
||||
|
||||
@ -2,6 +2,9 @@ package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
/**
|
||||
* Channel for transporting vegetable
|
||||
*/
|
||||
public class VegetableChannel extends ChannelBase
|
||||
{
|
||||
public VegetableChannel()
|
||||
|
||||
@ -5,11 +5,14 @@ import cz.cvut.fel.sit.omo.foodchain.channel.*;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
/**
|
||||
* Factory for creating channels
|
||||
*/
|
||||
public class ChannelFactory
|
||||
{
|
||||
/**
|
||||
* Creates channel based on channel type (Fruit, Vegetable, Meat, Other)
|
||||
* @param input data from channel is created
|
||||
* Creates channel based on channel type (Fruit, Vegetable, Meat, Other).
|
||||
* @param input data from channel that is created
|
||||
* @return created Channel (FruitChannel, VegetableChannel, MeatChannel, OtherChannel)
|
||||
*/
|
||||
public ChannelBase makeChannel(ParserResultElement input)
|
||||
@ -32,6 +35,11 @@ public class ChannelFactory
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates channel based on channel type (Fruit, Vegetable, Meat, Other).
|
||||
* @param typeOfChannel channel type to be created
|
||||
* @return created Channel (FruitChannel, VegetableChannel, MeatChannel, OtherChannel)
|
||||
*/
|
||||
private ChannelBase makeChannelFromChannelType(ProductType typeOfChannel)
|
||||
{
|
||||
switch (typeOfChannel)
|
||||
|
||||
@ -5,6 +5,9 @@ import cz.cvut.fel.sit.omo.foodchain.util.Singleton;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Manager class for all factories and pool database
|
||||
*/
|
||||
public class FactoryManager
|
||||
{
|
||||
public NodeFactory getNodeFactoryInstance()
|
||||
|
||||
@ -11,12 +11,15 @@ import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Factory for creating nodes
|
||||
*/
|
||||
public class NodeFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates Node based on input ParserResultElement
|
||||
* @param input data from Node is created
|
||||
* Creates Node based on input ParserResultElement.
|
||||
* @param input data from Node that is created
|
||||
* @return created Node
|
||||
*/
|
||||
public NodeBase makeNode(ParserResultElement input)
|
||||
|
||||
@ -6,11 +6,14 @@ import cz.cvut.fel.sit.omo.foodchain.product.ProductState;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
|
||||
/**
|
||||
* Factory for creating products
|
||||
*/
|
||||
public class ProductFactory
|
||||
{
|
||||
/**
|
||||
* Creates Product based on input ParserResultElement
|
||||
* @param input data from Product is created
|
||||
* Creates Product based on input ParserResultElement.
|
||||
* @param input data from Product that is created
|
||||
* @return created Product
|
||||
*/
|
||||
public Product makeProduct(ParserResultElement input)
|
||||
|
||||
@ -15,12 +15,15 @@ import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Factory for creating scenarios
|
||||
*/
|
||||
public class ScenarioFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates Scenario based on input ParserResultElement
|
||||
* @param input data from Scenario is created
|
||||
* Creates Scenario based on input ParserResultElement.
|
||||
* @param input data from Scenario that is created
|
||||
* @return created Scenario
|
||||
*/
|
||||
public Scenario makeScenario(ParserResultElement input)
|
||||
|
||||
@ -5,6 +5,9 @@ import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Customer node
|
||||
*/
|
||||
public class Customer extends BuyerNodeBase {
|
||||
|
||||
public Customer(double startMoney) {
|
||||
|
||||
@ -3,6 +3,9 @@ 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 Distributor(double startMoney) {
|
||||
|
||||
@ -7,6 +7,9 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
import javax.naming.InsufficientResourcesException;
|
||||
|
||||
/**
|
||||
* Farmer node
|
||||
*/
|
||||
public class Farmer extends SellerNodeBase {
|
||||
|
||||
public Farmer(double startMoney) {
|
||||
|
||||
@ -3,6 +3,9 @@ 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 Processor(double startMoney) {
|
||||
|
||||
@ -3,6 +3,9 @@ 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 Seller(double startMoney) {
|
||||
|
||||
@ -9,6 +9,9 @@ import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Storage sub-node for storing products of other nodes
|
||||
*/
|
||||
public class Storage
|
||||
{
|
||||
private final NodeBase storageOwner;
|
||||
@ -20,7 +23,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds provided product into the storage
|
||||
* Adds provided product into the storage.
|
||||
* @param product
|
||||
*/
|
||||
public void storeProduct(Product product)
|
||||
@ -29,7 +32,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all of the provided products into the storage
|
||||
* Adds all of the provided products into the storage.
|
||||
* @param products
|
||||
*/
|
||||
public void storeProducts(List<Product> products)
|
||||
@ -38,7 +41,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a product (first if there is multiple of them) based on its name and type
|
||||
* 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
|
||||
@ -48,17 +51,13 @@ public class Storage
|
||||
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
|
||||
* Returns number of products in storage.
|
||||
* @return
|
||||
*/
|
||||
public int getNumberOfStoredProducts()
|
||||
@ -67,7 +66,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether storage contains provided product
|
||||
* Checks whether storage contains provided product.
|
||||
* @param product product to check
|
||||
* @return true if contains, false if not
|
||||
*/
|
||||
@ -77,7 +76,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether storage contains provided product
|
||||
* Checks whether storage contains provided product.
|
||||
* @param name name of product to check
|
||||
* @param type type of product to check
|
||||
* @return true if contains, false if not
|
||||
@ -88,7 +87,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes provided product from storage if storage contains this product. (Removes only first found)
|
||||
* Removes provided product from storage if storage contains this product. (Removes only first found).
|
||||
* @param product product to remove
|
||||
*/
|
||||
public void removeProduct(Product product)
|
||||
@ -114,7 +113,7 @@ public class Storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls specific ProductOperation on each product in stock
|
||||
* Calls specific ProductOperation on each product in stock.
|
||||
* @param action ProductOperationType
|
||||
*/
|
||||
public void doAction(ProductOperationType action)
|
||||
|
||||
@ -3,6 +3,9 @@ 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);
|
||||
|
||||
@ -18,6 +18,9 @@ import javax.naming.InsufficientResourcesException;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for all nodes
|
||||
*/
|
||||
public class NodeBase implements ChannelObserver {
|
||||
private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels = new HashMap<>();
|
||||
private final List<Transaction> refusedTransactions = new LinkedList<>();
|
||||
@ -43,6 +46,11 @@ 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
|
||||
*/
|
||||
public void sendTransaction(Product product, NodeBase node) {
|
||||
try {
|
||||
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product);
|
||||
@ -55,6 +63,12 @@ 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
|
||||
*/
|
||||
public void sendTransaction(MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction) {
|
||||
try {
|
||||
Transaction transaction = new FinancialTransaction(this, node, TransactionType.PURCHASE, amount, prevTransaction);
|
||||
@ -66,6 +80,10 @@ public class NodeBase implements ChannelObserver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives (processes) transaction.
|
||||
* @param transaction transaction to be received
|
||||
*/
|
||||
public void receiveTransaction(Transaction transaction) {
|
||||
try {
|
||||
receiveTransaction((ProductTransaction) transaction);
|
||||
@ -80,6 +98,10 @@ 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) {
|
||||
if (validateSignature(transaction)) {
|
||||
transaction.getProduct().setStateToReadyForReception();
|
||||
@ -90,29 +112,40 @@ public class NodeBase implements ChannelObserver {
|
||||
else refuseTransaction(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives (validates) financial transaction.
|
||||
* @param transaction transaction to be received
|
||||
*/
|
||||
public void receiveTransaction(FinancialTransaction transaction) {
|
||||
if (validateSignature(transaction)) transaction.endTransaction();
|
||||
else refuseTransaction(transaction);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds transaction to refused transactions.
|
||||
* @param transaction
|
||||
*/
|
||||
public void refuseTransaction(Transaction transaction) {
|
||||
transaction.endTransaction();
|
||||
refusedTransactions.add(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives all transactions for this node.
|
||||
*/
|
||||
public void receiveTransactions() {
|
||||
subscribedChannels.keySet().forEach(ch -> ch.getTransactions(this).forEach(t -> {receiveTransaction(t); ch.disposeOf(t);}));
|
||||
}
|
||||
|
||||
|
||||
public void createRequirement(TransactionType tType, ProductType pType, String pName, double price) {
|
||||
Requirement req = new Requirement(this, tType, pType, pName, price);
|
||||
subscribedChannels.keySet().stream()
|
||||
.filter(ch -> ch.getProductType().equals(pType))
|
||||
.findAny().ifPresent(channel -> channel.addRequirement(req));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates requirement based on data provided.
|
||||
* @param tType transaction type
|
||||
* @param pType product type
|
||||
* @param pName product name
|
||||
* @param price product price
|
||||
* @throws InsufficientResourcesException if product is unavailable
|
||||
*/
|
||||
public void createRequirement(TransactionType tType, ProductType pType, String pName, MonetaryAmount price) throws InsufficientResourcesException {
|
||||
if (price == null) {
|
||||
try {
|
||||
@ -138,6 +171,11 @@ public class NodeBase implements ChannelObserver {
|
||||
.findAny().ifPresent(channel -> channel.addRequirement(req));
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts requirement and creates response.
|
||||
* @param req requirement to be accepted
|
||||
* @throws InsufficientResourcesException if product is unavailable
|
||||
*/
|
||||
public void acceptRequirement(Requirement req) throws InsufficientResourcesException {
|
||||
System.out.println(this.nodeType.toString() + " accepted " + req.toString());
|
||||
if (req.getTransactionType() == TransactionType.SALE) {
|
||||
@ -153,19 +191,28 @@ public class NodeBase implements ChannelObserver {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds channel to subscribed channels with correct type.
|
||||
* @param channel channel to be subscribed
|
||||
* @param type type of subscription
|
||||
*/
|
||||
public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type) {
|
||||
subscribedChannels.put(channel, type);
|
||||
channel.attach(this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes channel from subscribed channels.
|
||||
* @param channel
|
||||
*/
|
||||
public void unsubscribeFromChannel(ChannelBase channel) {
|
||||
subscribedChannels.remove(channel);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Proceed specific type of action on provided product if its possible. (Propagated to its storage)
|
||||
* 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
|
||||
*/
|
||||
@ -175,7 +222,7 @@ public class NodeBase implements ChannelObserver {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls specific ProductOperation on each product in stock (Propagated to its storage)
|
||||
* Calls specific ProductOperation on each product in stock (Propagated to its storage).
|
||||
* @param action ProductOperationType
|
||||
*/
|
||||
public void doAction(ProductOperationType action)
|
||||
@ -195,18 +242,36 @@ public class NodeBase implements ChannelObserver {
|
||||
return money;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds amount to current assets
|
||||
* @param m amount to be added
|
||||
*/
|
||||
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) {
|
||||
this.money = this.money.subtract(m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates signature on transaction.
|
||||
* @param transaction transaction to be validated
|
||||
* @return signature is valid
|
||||
*/
|
||||
public boolean validateSignature(Transaction transaction) {
|
||||
return SignatureManager.validateSignature(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if requirement creator is an allowed purchaser or allowed seller.
|
||||
* @param req requirement
|
||||
* @return creator is valid
|
||||
*/
|
||||
boolean senderPurchaserCheck(Requirement req) {
|
||||
NodeType creatorType = req.getCreator().nodeType;
|
||||
|
||||
@ -215,6 +280,12 @@ public class NodeBase implements ChannelObserver {
|
||||
((allowedPurchaser == creatorType || allowedPurchaser == NodeType.All) && req.getTransactionType() == TransactionType.PURCHASE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if requirement type corresponds with channel subscription type.
|
||||
* @param requirement requirement
|
||||
* @param channel channel
|
||||
* @return requirement type corresponds with channel subscription type
|
||||
*/
|
||||
boolean transactionSubscriptionCheck(Requirement requirement, ChannelBase channel) {
|
||||
switch (subscribedChannels.get(channel)) {
|
||||
case SALE:
|
||||
@ -228,6 +299,11 @@ public class NodeBase implements ChannelObserver {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chacks if product is available or if node has enough money
|
||||
* @param req requirement
|
||||
* @return product is available or node has enough money
|
||||
*/
|
||||
boolean requirementCheck(Requirement req) {
|
||||
Product product = storage.getProduct(req.getProductName(), req.getProductType());
|
||||
return (req.getTransactionType() == TransactionType.SALE && money.isGreaterThanOrEqualTo(req.getPrice())) ||
|
||||
@ -235,6 +311,11 @@ public class NodeBase implements ChannelObserver {
|
||||
((product.getProductState() == ProductState.AVAILABLE) || product.getProductState() == ProductState.IN_REQUIREMENT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Chacks incomming requirement.
|
||||
* @param requirement requirement
|
||||
* @param channel channel
|
||||
*/
|
||||
@Override
|
||||
public void update(Requirement requirement, ChannelBase channel) {
|
||||
if (senderPurchaserCheck(requirement) && transactionSubscriptionCheck(requirement, channel) && requirementCheck(requirement)) {
|
||||
@ -247,9 +328,11 @@ public class NodeBase implements ChannelObserver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives transactions.
|
||||
*/
|
||||
public void tick() {
|
||||
receiveTransactions();
|
||||
//System.out.println(this.getClass().getSimpleName() + ": " + money.getNumber());
|
||||
}
|
||||
|
||||
private static class RefusedRequirement {
|
||||
|
||||
@ -4,6 +4,9 @@ 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);
|
||||
|
||||
@ -3,6 +3,9 @@ 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);
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node.enums;
|
||||
|
||||
/**
|
||||
* Enum class for all nodes
|
||||
*/
|
||||
public enum NodeType {
|
||||
Farmer, Processor, Distributor, Seller, Customer, None, All
|
||||
}
|
||||
|
||||
@ -2,6 +2,9 @@ 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);
|
||||
|
||||
@ -2,6 +2,9 @@ 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);
|
||||
|
||||
@ -3,6 +3,9 @@ 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;
|
||||
|
||||
/**
|
||||
* Operation for processing products in storage
|
||||
*/
|
||||
public class Action extends OperationBase {
|
||||
|
||||
private final OperationBase lastOperation;
|
||||
@ -21,11 +24,18 @@ public class Action extends OperationBase {
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends this action and changes products type to available.
|
||||
*/
|
||||
private void endAction() {
|
||||
this.setTimeEnd();
|
||||
product.setStateToAvailable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes actions duration or ends action.
|
||||
* @return action is in progress
|
||||
*/
|
||||
public boolean tick() {
|
||||
if (duration == 0) {
|
||||
endAction();
|
||||
|
||||
@ -5,6 +5,9 @@ import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
|
||||
import javax.money.MonetaryAmount;
|
||||
import javax.naming.InsufficientResourcesException;
|
||||
|
||||
/**
|
||||
* Operation for transporting money
|
||||
*/
|
||||
public class FinancialTransaction extends Transaction {
|
||||
|
||||
private final MonetaryAmount amount;
|
||||
@ -19,6 +22,9 @@ public class FinancialTransaction extends Transaction {
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends transaction and adds amount to receiver node.
|
||||
*/
|
||||
@Override
|
||||
public void endTransaction() {
|
||||
super.endTransaction();
|
||||
|
||||
@ -4,7 +4,10 @@ import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class OperationBase {
|
||||
/**
|
||||
* Base class for all operations
|
||||
*/
|
||||
public abstract class OperationBase {
|
||||
|
||||
private final LocalDateTime timeStart;
|
||||
private LocalDateTime timeEnd;
|
||||
|
||||
@ -3,6 +3,9 @@ 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.product.ProductType;
|
||||
|
||||
/**
|
||||
* Enum class for product operations types with parameters
|
||||
*/
|
||||
public enum ProductOperationType
|
||||
{
|
||||
STORE_MEAT(NodeType.All, ProductType.MEAT, 20, -8),
|
||||
|
||||
@ -6,6 +6,9 @@ import cz.cvut.fel.sit.omo.foodchain.product.ProductState;
|
||||
|
||||
import javax.naming.InsufficientResourcesException;
|
||||
|
||||
/**
|
||||
* Operation for transporting products
|
||||
*/
|
||||
public class ProductTransaction extends Transaction {
|
||||
|
||||
private final OperationBase lastOperation;
|
||||
@ -24,6 +27,9 @@ public class ProductTransaction extends Transaction {
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends transaction and stores product in receivers storage.
|
||||
*/
|
||||
@Override
|
||||
public void endTransaction() {
|
||||
super.endTransaction();
|
||||
|
||||
@ -4,6 +4,9 @@ import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||
|
||||
/**
|
||||
* Operation for transporting things between nodes
|
||||
*/
|
||||
public abstract class Transaction extends OperationBase {
|
||||
|
||||
private final TransactionType transactionType;
|
||||
@ -17,6 +20,9 @@ public abstract class Transaction extends OperationBase {
|
||||
signature = SignatureManager.signTransaction(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets time end to current time.
|
||||
*/
|
||||
public void endTransaction() {
|
||||
this.setTimeEnd();
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
/**
|
||||
* Enum class for transaction types
|
||||
*/
|
||||
public enum TransactionType {
|
||||
|
||||
SALE, PURCHASE
|
||||
|
||||
@ -11,10 +11,20 @@ import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Parser for XML configuration
|
||||
*/
|
||||
public class ConfigurationParser {
|
||||
|
||||
private static Document activeDocument;
|
||||
|
||||
/**
|
||||
* Parses xml file to document.
|
||||
* @param name file name
|
||||
* @throws ParserConfigurationException ParserConfigurationException
|
||||
* @throws IOException IOException
|
||||
* @throws SAXException SAXException
|
||||
*/
|
||||
public static void parseDocument(String name) throws ParserConfigurationException, IOException, SAXException {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setValidating(true);
|
||||
@ -24,6 +34,11 @@ public class ConfigurationParser {
|
||||
activeDocument = builder.parse(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns parsed result with all elements having provided tag.
|
||||
* @param tag element tag
|
||||
* @return parsed result element
|
||||
*/
|
||||
public static ParserResult getElements(String tag) {
|
||||
NodeList elements = activeDocument.getElementsByTagName(tag);
|
||||
ParserResult resultList = new ParserResult();
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
|
||||
/**
|
||||
* Parser for console input
|
||||
*/
|
||||
public class ConsoleParser {
|
||||
}
|
||||
|
||||
@ -3,6 +3,9 @@ package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Wrapper class for parser result elements
|
||||
*/
|
||||
public class ParserResult {
|
||||
|
||||
private final List<ParserResultElement> elements;
|
||||
@ -11,6 +14,10 @@ public class ParserResult {
|
||||
elements = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds element to list.
|
||||
* @param element element
|
||||
*/
|
||||
public void addElement(ParserResultElement element) {
|
||||
elements.add(element);
|
||||
}
|
||||
|
||||
@ -4,6 +4,9 @@ import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class for storing all information given in a xml tag
|
||||
*/
|
||||
public class ParserResultElement {
|
||||
|
||||
private String name;
|
||||
|
||||
@ -3,8 +3,16 @@ 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;
|
||||
|
||||
/**
|
||||
* Pool for storing all channels
|
||||
*/
|
||||
public class ChannelPool extends PoolListBase<ChannelBase> {
|
||||
|
||||
/**
|
||||
* Finds channel by its type.
|
||||
* @param type type of channel
|
||||
* @return channel
|
||||
*/
|
||||
public ChannelBase findByType(ProductType type) {
|
||||
return findAll().stream().filter(ch -> ch.getProductType().equals(type)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@ -2,5 +2,8 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
|
||||
|
||||
/**
|
||||
*Pool for storing all nodes
|
||||
*/
|
||||
public class NodePool extends PoolListBase<NodeBase> {
|
||||
}
|
||||
|
||||
@ -4,6 +4,9 @@ import cz.cvut.fel.sit.omo.foodchain.util.Singleton;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Database class for storing all pools
|
||||
*/
|
||||
public class PoolDatabase {
|
||||
|
||||
public ChannelPool getChannelPoolInstance() {
|
||||
|
||||
@ -3,6 +3,10 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Base class for all pools
|
||||
* @param <T> pool type
|
||||
*/
|
||||
public abstract class PoolListBase<T> {
|
||||
List<T> pool;
|
||||
|
||||
|
||||
@ -2,5 +2,8 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
/**
|
||||
* Pool for storing all products
|
||||
*/
|
||||
public class ProductPool extends PoolListBase<Product> {
|
||||
}
|
||||
|
||||
@ -2,5 +2,8 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
|
||||
|
||||
/**
|
||||
* Pool for storing all scenarios
|
||||
*/
|
||||
public class ScenarioPool extends PoolListBase<Scenario> {
|
||||
}
|
||||
|
||||
@ -7,6 +7,9 @@ import org.javamoney.moneta.Money;
|
||||
import javax.money.Monetary;
|
||||
import javax.money.MonetaryAmount;
|
||||
|
||||
/**
|
||||
* Product class
|
||||
*/
|
||||
public class Product
|
||||
{
|
||||
private ProductState productState;
|
||||
@ -16,19 +19,6 @@ public class Product
|
||||
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Creates Product with default state AVAILABLE
|
||||
* @param name name of product
|
||||
* @param productType type of product
|
||||
*/
|
||||
public Product(String name, ProductType productType)
|
||||
{
|
||||
this.name = name;
|
||||
this.productType = productType;
|
||||
this.productState = ProductState.AVAILABLE;
|
||||
this.price = Money.of(0, "USD");
|
||||
}
|
||||
|
||||
public Product(String name, ProductType productType, double m)
|
||||
{
|
||||
this.name = name;
|
||||
@ -38,27 +28,9 @@ public class Product
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Product
|
||||
* @param name name of product
|
||||
* @param productType type of product
|
||||
* @param productState state of product
|
||||
* Passes tick onto current operation
|
||||
* @return operation is active
|
||||
*/
|
||||
public Product(String name, ProductType productType, ProductState productState)
|
||||
{
|
||||
this.name = name;
|
||||
this.productType = productType;
|
||||
this.productState = productState;
|
||||
this.price = Money.of(0, "USD");
|
||||
}
|
||||
|
||||
public Product(String name, ProductType productType, ProductState productState, double m)
|
||||
{
|
||||
this.name = name;
|
||||
this.productType = productType;
|
||||
this.productState = productState;
|
||||
this.price = Money.of(m, "USD");
|
||||
}
|
||||
|
||||
public boolean tick() {
|
||||
try {
|
||||
return ((Action) currentOperation).tick();
|
||||
@ -138,13 +110,6 @@ public class Product
|
||||
throw new IllegalStateException("Cannot set state IN_REQUIREMENT. Product is not AVAILABLE!");
|
||||
}
|
||||
|
||||
/*
|
||||
public void setProductState(ProductState productState)
|
||||
{
|
||||
this.productState = productState;
|
||||
}*/
|
||||
|
||||
|
||||
//Getters & Setters
|
||||
|
||||
public ProductState getProductState()
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.product;
|
||||
|
||||
/**
|
||||
* Enum class for product states
|
||||
*/
|
||||
public enum ProductState
|
||||
{
|
||||
AVAILABLE, READY_FOR_DISPATCH, IN_TRANSIT, READY_FOR_RECEPTION, IN_ACTION, IN_REQUIREMENT
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.product;
|
||||
|
||||
/**
|
||||
* Enum class for product types
|
||||
*/
|
||||
public enum ProductType
|
||||
{
|
||||
MEAT, FRUIT, VEGETABLE, OTHER, ALL // ALL is used only for actions
|
||||
|
||||
@ -9,6 +9,9 @@ import org.javamoney.moneta.Money;
|
||||
import javax.money.MonetaryAmount;
|
||||
import javax.naming.InsufficientResourcesException;
|
||||
|
||||
/**
|
||||
* Requirement class
|
||||
*/
|
||||
public class Requirement {
|
||||
|
||||
private final NodeBase creator;
|
||||
@ -20,17 +23,6 @@ public class Requirement {
|
||||
private static int counter;
|
||||
private int id;
|
||||
|
||||
public Requirement(NodeBase creator, TransactionType transactionType, ProductType productType, String productName, double price) {
|
||||
this.creator = creator;
|
||||
this.transactionType = transactionType;
|
||||
this.productType = productType;
|
||||
this.productName = productName;
|
||||
this.price = Money.of(price, "USD");
|
||||
this.TTL = 3;
|
||||
this.id = counter++;
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
public Requirement(NodeBase creator, TransactionType transactionType, ProductType productType, String productName, MonetaryAmount price) {
|
||||
this.creator = creator;
|
||||
this.transactionType = transactionType;
|
||||
@ -42,6 +34,9 @@ public class Requirement {
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces TTL.
|
||||
*/
|
||||
public void tick() {
|
||||
TTL--;
|
||||
}
|
||||
|
||||
@ -2,6 +2,9 @@ package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.Customer;
|
||||
|
||||
/**
|
||||
* Step for consuming random product from storage
|
||||
*/
|
||||
public class ConsumeProductStep extends ScenarioStep {
|
||||
|
||||
private final Customer node;
|
||||
@ -10,6 +13,9 @@ public class ConsumeProductStep extends ScenarioStep {
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes random product.
|
||||
*/
|
||||
@Override
|
||||
void execute() {
|
||||
node.consumeProduct();
|
||||
|
||||
@ -4,6 +4,9 @@ import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
/**
|
||||
* Step for starting action on product(s)
|
||||
*/
|
||||
public class DoActionStep extends ScenarioStep {
|
||||
private final NodeBase nodeBase;
|
||||
private final ProductOperationType productOperationType;
|
||||
@ -20,6 +23,9 @@ public class DoActionStep extends ScenarioStep {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts action.
|
||||
*/
|
||||
@Override
|
||||
void execute() {
|
||||
try {
|
||||
|
||||
@ -4,6 +4,9 @@ import cz.cvut.fel.sit.omo.foodchain.FoodChain;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.Farmer;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
/**
|
||||
* Step for generating product from pool
|
||||
*/
|
||||
public class GenerateProductStep extends ScenarioStep {
|
||||
|
||||
private final Farmer node;
|
||||
@ -22,6 +25,9 @@ public class GenerateProductStep extends ScenarioStep {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates product from pool.
|
||||
*/
|
||||
@Override
|
||||
void execute() {
|
||||
node.generateProduct(product);
|
||||
|
||||
@ -1,7 +1,13 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
/**
|
||||
* Step for skipping
|
||||
*/
|
||||
public class PassStep extends ScenarioStep {
|
||||
|
||||
/**
|
||||
* Does nothing.
|
||||
*/
|
||||
@Override
|
||||
void execute() {}
|
||||
}
|
||||
|
||||
@ -3,28 +3,34 @@ package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Scenatio class fpr holding and executing steps
|
||||
*/
|
||||
public class Scenario {
|
||||
List<ScenarioStep> steps;
|
||||
Iterator<ScenarioStep> stepIterator;
|
||||
|
||||
public Scenario() {
|
||||
stepIterator = steps.listIterator(0);
|
||||
}
|
||||
|
||||
public Scenario(List<ScenarioStep> steps) {
|
||||
this.steps = steps;
|
||||
stepIterator = steps.listIterator(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes next step.
|
||||
* @return scenario has steps
|
||||
*/
|
||||
boolean execute() {
|
||||
if (!stepIterator.hasNext()) {
|
||||
// stepIterator = steps.listIterator(0);
|
||||
return false;
|
||||
}
|
||||
stepIterator.next().execute();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes next step.
|
||||
* @return scenario has steps
|
||||
*/
|
||||
public boolean tick() {
|
||||
return execute();
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
/**
|
||||
* Scenario step class
|
||||
*/
|
||||
public abstract class ScenarioStep {
|
||||
abstract void execute();
|
||||
}
|
||||
|
||||
@ -8,6 +8,9 @@ import org.javamoney.moneta.Money;
|
||||
import javax.money.MonetaryAmount;
|
||||
import javax.naming.InsufficientResourcesException;
|
||||
|
||||
/**
|
||||
* Step for sending requirements
|
||||
*/
|
||||
public class SendRequirementStep extends ScenarioStep {
|
||||
private final NodeBase node;
|
||||
private final TransactionType tType;
|
||||
@ -33,6 +36,9 @@ public class SendRequirementStep extends ScenarioStep {
|
||||
this.pPrice = Money.of(price, "USD");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates requirement.
|
||||
*/
|
||||
@Override
|
||||
void execute() {
|
||||
try {
|
||||
|
||||
@ -5,6 +5,9 @@ import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Signature class validating its transaction
|
||||
*/
|
||||
public class Signature {
|
||||
|
||||
private final String managerCode;
|
||||
|
||||
@ -2,14 +2,27 @@ package cz.cvut.fel.sit.omo.foodchain.signature;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
|
||||
/**
|
||||
* Manager class for creating and validating signatures
|
||||
*/
|
||||
public class SignatureManager {
|
||||
|
||||
private static final String MANAGER_CODE = "iW91AJWNOCmX1pbpLlW0";
|
||||
|
||||
/**
|
||||
* Creates signature for transaction.
|
||||
* @param transaction transaction
|
||||
* @return signature
|
||||
*/
|
||||
public static Signature signTransaction(Transaction transaction) {
|
||||
return new Signature(MANAGER_CODE, transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if signature corresponds with its transaction.
|
||||
* @param transaction transaction
|
||||
* @return signature corresponds with its transaction
|
||||
*/
|
||||
public static boolean validateSignature(Transaction transaction) {
|
||||
return transaction.equals(transaction.getSignature().getTransaction(MANAGER_CODE))
|
||||
&& transaction.getSignature().getUuid(MANAGER_CODE).equals(transaction.getSignature().getTransaction(MANAGER_CODE).getSignature().getUuid(MANAGER_CODE));
|
||||
|
||||
@ -3,6 +3,9 @@ package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||
|
||||
/**
|
||||
* Interface for nodes that receives requirements
|
||||
*/
|
||||
public interface ChannelObserver {
|
||||
void update(Requirement requirement, ChannelBase channel);
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
/**
|
||||
* Interface for channels to work with observers
|
||||
*/
|
||||
public interface Observable {
|
||||
void attach(ChannelObserver channelObserver);
|
||||
void detach(ChannelObserver channelObserver);
|
||||
|
||||
@ -5,6 +5,9 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Singleton implementation 1 of 3
|
||||
*/
|
||||
public class Singleton {
|
||||
|
||||
private static final Singleton instance = new Singleton();
|
||||
|
||||
@ -8,10 +8,16 @@ import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* Creates artificial time for application
|
||||
*/
|
||||
public class Ticker {
|
||||
|
||||
private static Timer timer;
|
||||
|
||||
/**
|
||||
* Starts timer.
|
||||
*/
|
||||
public static void start() {
|
||||
timer = new Timer(1000, actionEvent -> {
|
||||
System.out.println("-----tick-----");
|
||||
@ -25,6 +31,9 @@ public class Ticker {
|
||||
timer.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops timer (ends application).
|
||||
*/
|
||||
public static void stop() {
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user