Added configuration and ChannelSubstcriptionType
This commit is contained in:
parent
8b47edd07b
commit
ba84dfefe4
5
conf/channels.xml
Normal file
5
conf/channels.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<channels>
|
||||
<channel type="MEET"></channel>
|
||||
<channel type="VEGETABLE"></channel>
|
||||
<channel type="FRUIT"></channel>
|
||||
</channels>
|
||||
18
conf/nodes.xml
Normal file
18
conf/nodes.xml
Normal file
@ -0,0 +1,18 @@
|
||||
<nodes>
|
||||
<node type = "Farmer">
|
||||
<money>100</money>
|
||||
<channels>MEET PURCHASE;VEGETABLE PURCHASE;FRUIT PURCHASE</channels>
|
||||
</node>
|
||||
<node type = "Processor">
|
||||
<money>0</money>
|
||||
<channels>MEET SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
|
||||
</node>
|
||||
<node type = "Distributor">
|
||||
<money>1500</money>
|
||||
<channels>MEET SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
|
||||
</node>
|
||||
<node type = "Customer">
|
||||
<money>250</money>
|
||||
<channels>MEET SALE;VEGETABLE SALE;FRUIT SALE</channels>
|
||||
</node>
|
||||
</nodes>
|
||||
47
conf/products.xml
Normal file
47
conf/products.xml
Normal file
@ -0,0 +1,47 @@
|
||||
<products>
|
||||
<product>
|
||||
<name>jablko</name>
|
||||
<type>FRUIT</type>
|
||||
<price>5</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>hruška</name>
|
||||
<type>FRUIT</type>
|
||||
<price>7</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>banán</name>
|
||||
<type>FRUIT</type>
|
||||
<price>10</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>okurka</name>
|
||||
<type>VEGETABLE</type>
|
||||
<price>3</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>paprika</name>
|
||||
<type>VEGETABLE</type>
|
||||
<price>5</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>rajče</name>
|
||||
<type>VEGETABLE</type>
|
||||
<price>3</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>vepřové</name>
|
||||
<type>MEET</type>
|
||||
<price>20</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>hovězí</name>
|
||||
<type>MEET</type>
|
||||
<price>50</price>
|
||||
</product>
|
||||
<product>
|
||||
<name>kuřecí</name>
|
||||
<type>MEET</type>
|
||||
<price>30</price>
|
||||
</product>
|
||||
</products>
|
||||
15
conf/scenarios.xml
Normal file
15
conf/scenarios.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<scenarios>
|
||||
<scenario type="Farmer">
|
||||
<steps>
|
||||
generate jablko
|
||||
generate hovězí
|
||||
generate hruška
|
||||
generate rajče
|
||||
generate paprika
|
||||
generate kuřecí
|
||||
generate vepřové
|
||||
generate banán
|
||||
generate okurka
|
||||
</steps>
|
||||
</scenario>
|
||||
</scenarios>
|
||||
7
pom.xml
7
pom.xml
@ -7,6 +7,13 @@
|
||||
<groupId>cz.cvut.fel.sit.omo.foodchain</groupId>
|
||||
<artifactId>FoodChain</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.money</groupId>
|
||||
<artifactId>money-api</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>12</maven.compiler.target>
|
||||
|
||||
@ -1,7 +1,16 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
|
||||
|
||||
public class FoodChain {
|
||||
|
||||
private static FactoryManager factoryManager = new FactoryManager();
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
public static FactoryManager getFactoryManager() {
|
||||
return factoryManager;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ChannelBase implements IChannel, Observable {
|
||||
public abstract class ChannelBase implements Observable {
|
||||
protected ProductType productType;
|
||||
protected Queue<Requirement> requirements;
|
||||
protected Queue<Transaction> transactions;
|
||||
@ -31,13 +31,11 @@ public abstract class ChannelBase implements IChannel, Observable {
|
||||
this.productType = ProductType.OTHER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queue<Transaction> getTransactions()
|
||||
{
|
||||
return this.transactions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queue<Requirement> getCurrentRequirements()
|
||||
{
|
||||
return this.requirements;
|
||||
@ -47,7 +45,6 @@ public abstract class ChannelBase implements IChannel, Observable {
|
||||
* Adds new requirement to the requirement Queue
|
||||
* @param requirement requirement
|
||||
*/
|
||||
@Override
|
||||
public void addRequirement(Requirement requirement)
|
||||
{
|
||||
this.requirements.add(requirement);
|
||||
@ -73,7 +70,6 @@ public abstract class ChannelBase implements IChannel, Observable {
|
||||
* Adds new transaction to the transaction Queue
|
||||
* @param transaction transaction
|
||||
*/
|
||||
@Override
|
||||
public void addTransaction(Transaction transaction)
|
||||
{
|
||||
this.transactions.add(transaction);
|
||||
@ -86,4 +82,8 @@ public abstract class ChannelBase implements IChannel, Observable {
|
||||
{
|
||||
this.requirements.clear();
|
||||
}
|
||||
|
||||
public ProductType getProductType() {
|
||||
return productType;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
public enum ChannelSubscriptionType {
|
||||
|
||||
SALE, PURCHASE, SALE_AND_PURCHASE
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
public interface IChannel
|
||||
{
|
||||
//TODO Daniel
|
||||
|
||||
//Transaction getNextTransaction();
|
||||
Queue<Transaction> getTransactions();
|
||||
|
||||
//public Requirement getNextRequirement();
|
||||
Queue<Requirement> getCurrentRequirements();
|
||||
|
||||
void addRequirement(Requirement requirement);
|
||||
|
||||
void addTransaction(Transaction transaction);
|
||||
|
||||
}
|
||||
@ -3,13 +3,9 @@ package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class BuyerNodeBase extends NodeBase implements IBuyerNode {
|
||||
@Override
|
||||
public void sellProduct(Product product) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMargin(Product product) {
|
||||
public void buyProduct(Product product) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,11 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Customer extends BuyerNodeBase {
|
||||
|
||||
public void consumeProduct() {
|
||||
int i = new Random().nextInt(getStorage().size());
|
||||
getStorage().remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class Farmer extends SellerNodeBase {
|
||||
|
||||
public void generateProduct(Product product) {
|
||||
storeProduct(product);
|
||||
createRequirement(TransactionType.SALE, product.getProductType(), product.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,5 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public interface IBuyerNode {
|
||||
|
||||
void sellProduct(Product product);
|
||||
|
||||
void addMargin(Product product);
|
||||
void buyProduct(Product product);
|
||||
}
|
||||
|
||||
@ -1,43 +0,0 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.IChannel;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Action;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
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.requirement.Requirement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface INode {
|
||||
void sendTransaction(Product product, INode INode);
|
||||
|
||||
void receiveTransaction(Transaction transaction);
|
||||
|
||||
void refuseTransaction(Transaction transaction);
|
||||
|
||||
Transaction readNextTransaction(IChannel channel);
|
||||
|
||||
Requirement readNextRequirement(IChannel channel);
|
||||
|
||||
void acceptRequirement(Requirement requirement);
|
||||
|
||||
void subscribeToChannel(IChannel channel);
|
||||
|
||||
void unsubscribeFromChannel(IChannel channel);
|
||||
|
||||
void doAction(Action action, Product product);
|
||||
|
||||
void doAction(Action action);
|
||||
|
||||
void storeProduct(Product product);
|
||||
|
||||
void storeProducts(List products);
|
||||
|
||||
Product getProductFromStorage(String name, ProductType type);
|
||||
|
||||
Signature generateSignature(Transaction transaction);
|
||||
|
||||
boolean validateSignature(Signature signature);
|
||||
}
|
||||
@ -4,5 +4,7 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public interface ISellerNode {
|
||||
|
||||
void buyProduct(Product product);
|
||||
void sellProduct(Product product);
|
||||
|
||||
void addMargin(Product product);
|
||||
}
|
||||
|
||||
@ -1,88 +1,147 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.IChannel;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Action;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
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.operation.*;
|
||||
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.requirement.Requirement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.money.Monetary;
|
||||
import javax.money.MonetaryAmount;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class NodeBase implements INode {
|
||||
@Override
|
||||
public void sendTransaction(Product product, INode node) {
|
||||
|
||||
public class NodeBase implements ChannelObserver {
|
||||
private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels;
|
||||
private final List<Product> products;
|
||||
private MonetaryAmount money;
|
||||
|
||||
private final List<Transaction> refusedTranslations;
|
||||
|
||||
public NodeBase() {
|
||||
subscribedChannels = new HashMap<>();
|
||||
products = new LinkedList<>();
|
||||
refusedTranslations = new LinkedList<>();
|
||||
money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(0).create();
|
||||
}
|
||||
|
||||
public NodeBase(int m) {
|
||||
subscribedChannels = new HashMap<>();
|
||||
products = new LinkedList<>();
|
||||
refusedTranslations = new LinkedList<>();
|
||||
money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(m).create();
|
||||
}
|
||||
|
||||
|
||||
public void sendTransaction(Product product, NodeBase node) {
|
||||
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product);
|
||||
|
||||
subscribedChannels.keySet().stream()
|
||||
.filter(ch -> ch.getProductType().equals(product.getProductType()))
|
||||
.findAny().ifPresent(channel -> channel.addTransaction(transaction));
|
||||
}
|
||||
|
||||
|
||||
public void sendTransaction(MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction) {
|
||||
Transaction transaction = new FinancialTransaction(this, node, TransactionType.PURCHASE, amount, prevTransaction);
|
||||
|
||||
subscribedChannels.keySet().stream()
|
||||
.filter(ch -> ch.getProductType().equals(prevTransaction.getProduct().getProductType()))
|
||||
.findAny().ifPresent(channel -> channel.addTransaction(transaction));
|
||||
}
|
||||
|
||||
|
||||
public void receiveTransaction(ProductTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveTransaction(Transaction transaction) {
|
||||
|
||||
public void receiveTransaction(FinancialTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void refuseTransaction(Transaction transaction) {
|
||||
|
||||
transaction.endTransaction();
|
||||
refusedTranslations.add(transaction); //TODO maybe need more work on it
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transaction readNextTransaction(IChannel channel) {
|
||||
return null;
|
||||
public Transaction readNextTransaction(ChannelBase channel) {
|
||||
|
||||
|
||||
return channel.getNextTransaction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement readNextRequirement(IChannel channel) {
|
||||
return null;
|
||||
|
||||
public void createRequirement(TransactionType tType, ProductType pType, String pName) {
|
||||
Requirement req = new Requirement(this, tType, pType, pName);
|
||||
subscribedChannels.keySet().stream()
|
||||
.filter(ch -> ch.getProductType().equals(pType))
|
||||
.findAny().ifPresent(channel -> channel.addRequirement(req));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptRequirement(Requirement requirement) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribeToChannel(IChannel channel) {
|
||||
|
||||
public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type) {
|
||||
subscribedChannels.put(channel, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribeFromChannel(IChannel channel) {
|
||||
|
||||
public void unsubscribeFromChannel(ChannelBase channel) {
|
||||
subscribedChannels.remove(channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAction(Action action, Product product) {
|
||||
|
||||
public void doAction(ProductOperationType action, Product product) {
|
||||
Action actionProcess = new Action(this, product, action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAction(Action action) {
|
||||
|
||||
public void doAction(ProductOperationType action) {
|
||||
//create new action
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void storeProduct(Product product) {
|
||||
|
||||
this.products.add(product);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeProducts(List products) {
|
||||
|
||||
public void storeProducts(List<Product> products) {
|
||||
this.products.addAll(products);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Product getProductFromStorage(String name, ProductType type) {
|
||||
return null;
|
||||
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
public List<Product> getStorage() {
|
||||
return products;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Signature generateSignature(Transaction transaction) {
|
||||
return null;
|
||||
return SignatureManager.signTransaction(transaction);
|
||||
}
|
||||
|
||||
|
||||
public boolean validateSignature(Transaction transaction) {
|
||||
return SignatureManager.validateSignature(transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateSignature(Signature signature) {
|
||||
return false;
|
||||
public void update(Requirement requirement) {
|
||||
if (!requirement.getCreator().equals(this))
|
||||
acceptRequirement(requirement);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,12 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class SellerNodeBase extends NodeBase implements ISellerNode {
|
||||
@Override
|
||||
public void buyProduct(Product product) {
|
||||
public void sellProduct(Product product) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMargin(Product product) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,44 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class Action extends OperationBase {
|
||||
|
||||
private final OperationBase lastOperation;
|
||||
private final Product product;
|
||||
private final ProductOperationType type;
|
||||
private int duration;
|
||||
|
||||
public Action(NodeBase node, Product product, ProductOperationType type) {
|
||||
super(node);
|
||||
this.product = product;
|
||||
this.type = type;
|
||||
lastOperation = product.getCurrentOperation();
|
||||
duration = type.getDuration();
|
||||
product.setCurrentOperation(this);
|
||||
product.setStateToReadyForDispatch();
|
||||
}
|
||||
|
||||
private void endAction() {
|
||||
this.setTimeEnd();
|
||||
product.setStateToAvailable();
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (duration == 0) endAction();
|
||||
else duration--;
|
||||
}
|
||||
|
||||
public OperationBase getLastOperation() {
|
||||
return lastOperation;
|
||||
}
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public ProductOperationType getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,25 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
|
||||
import javax.money.MonetaryAmount;
|
||||
|
||||
public class FinancialTransaction extends Transaction {
|
||||
|
||||
private final MonetaryAmount amount;
|
||||
private final ProductTransaction transactionPaid;
|
||||
|
||||
public FinancialTransaction(NodeBase creator, NodeBase receiver, TransactionType type, MonetaryAmount amount, ProductTransaction transactionPaid) {
|
||||
super(creator, receiver, type);
|
||||
this.amount = amount;
|
||||
this.transactionPaid = transactionPaid;
|
||||
}
|
||||
|
||||
public MonetaryAmount getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public ProductTransaction getTransactionPaid() {
|
||||
return transactionPaid;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
public interface IOperation {
|
||||
}
|
||||
@ -1,5 +1,34 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class OperationBase {
|
||||
IOperation lastOperation;
|
||||
|
||||
private final LocalDateTime timeStart;
|
||||
private LocalDateTime timeEnd;
|
||||
|
||||
private final NodeBase nodeCreator;
|
||||
|
||||
OperationBase(NodeBase node) {
|
||||
nodeCreator = node;
|
||||
timeStart = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public LocalDateTime getTimeStart() {
|
||||
return timeStart;
|
||||
}
|
||||
|
||||
public LocalDateTime getTimeEnd() {
|
||||
return timeEnd;
|
||||
}
|
||||
|
||||
public void setTimeEnd() {
|
||||
this.timeEnd = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public NodeBase getNodeCreator() {
|
||||
return nodeCreator;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,22 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
public enum ProductOperationType {
|
||||
|
||||
STORE_MEAT(10, -8);
|
||||
|
||||
private final int duration;
|
||||
private final int temperature;
|
||||
|
||||
ProductOperationType(int duration, int temperature) {
|
||||
this.duration = duration;
|
||||
this.temperature = temperature;
|
||||
}
|
||||
|
||||
public int getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public int getTemperature() {
|
||||
return temperature;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,38 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class ProductTransaction extends Transaction {
|
||||
|
||||
private final OperationBase lastOperation;
|
||||
private final Product product;
|
||||
private final ProductOperationType type;
|
||||
|
||||
public ProductTransaction(NodeBase creator, NodeBase receiver, TransactionType transactionType, Product product) {
|
||||
super(creator, receiver, transactionType);
|
||||
this.product = product;
|
||||
type = null;
|
||||
lastOperation = product.getCurrentOperation();
|
||||
product.setCurrentOperation(this);
|
||||
product.setStateToReadyForDispatch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endTransaction() {
|
||||
super.endTransaction();
|
||||
product.setStateToAvailable();
|
||||
}
|
||||
|
||||
public OperationBase getLastOperation() {
|
||||
return lastOperation;
|
||||
}
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public ProductOperationType getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,35 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
||||
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||
|
||||
public abstract class Transaction extends OperationBase {
|
||||
|
||||
private final TransactionType transactionType;
|
||||
private final Signature signature;
|
||||
private final NodeBase nodeReceiver;
|
||||
|
||||
Transaction(NodeBase creator, NodeBase receiver, TransactionType type) {
|
||||
super(creator);
|
||||
transactionType = type;
|
||||
nodeReceiver = receiver;
|
||||
signature = SignatureManager.signTransaction(this);
|
||||
}
|
||||
|
||||
public void endTransaction() {
|
||||
this.setTimeEnd();
|
||||
}
|
||||
|
||||
public TransactionType getTransactionType() {
|
||||
return transactionType;
|
||||
}
|
||||
|
||||
public Signature getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
public NodeBase getNodeReceiver() {
|
||||
return nodeReceiver;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
public enum TransactionType {
|
||||
|
||||
SALE, PURCHASE
|
||||
}
|
||||
|
||||
@ -1,4 +1,43 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ConfigurationParser {
|
||||
|
||||
private static Document activeDocument;
|
||||
|
||||
public static void parseDocument(String name) throws ParserConfigurationException, IOException, SAXException {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setValidating(true);
|
||||
factory.setIgnoringElementContentWhitespace(true);
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
File file = new File(name);
|
||||
activeDocument = builder.parse(file);
|
||||
}
|
||||
|
||||
public static ParserResult getElements(String tag) {
|
||||
NodeList elements = activeDocument.getElementsByTagName(tag);
|
||||
ParserResult resultList = new ParserResult();
|
||||
for (int i = 0; i < elements.getLength(); i++) {
|
||||
Node tmpNode = elements.item(i);
|
||||
ParserResultElement element = new ParserResultElement();
|
||||
for (int j = 0; j < tmpNode.getAttributes().getLength(); j++) {
|
||||
element.addAttribute(tmpNode.getAttributes().item(i).getNodeName(), tmpNode.getAttributes().item(i).getTextContent());
|
||||
}
|
||||
for (int j = 0; j < tmpNode.getChildNodes().getLength(); j++) {
|
||||
element.addChild(tmpNode.getChildNodes().item(i).getNodeName(), tmpNode.getChildNodes().item(i).getTextContent());
|
||||
}
|
||||
resultList.addElement(element);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ParserResult {
|
||||
|
||||
private final List<ParserResultElement> elements;
|
||||
|
||||
public ParserResult() {
|
||||
elements = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void addElement(ParserResultElement element) {
|
||||
elements.add(element);
|
||||
}
|
||||
|
||||
public List<ParserResultElement> getElements() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParserResultElement {
|
||||
|
||||
private String name;
|
||||
private Map<String, String> attributes;
|
||||
private Map<String, String> children;
|
||||
|
||||
public ParserResultElement() {
|
||||
attributes = new HashMap<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Map<String, String> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public void setAttributes(Map<String, String> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public void addAttribute(String key, String value) {
|
||||
attributes.put(key, value);
|
||||
}
|
||||
|
||||
public String getAttribute(String key) {
|
||||
return attributes.get(key);
|
||||
}
|
||||
|
||||
public Map<String, String> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(Map<String, String> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public void addChild(String key, String value) {
|
||||
children.put(key, value);
|
||||
}
|
||||
|
||||
public String getChild(String key) {
|
||||
return children.get(key);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,11 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.IChannel;
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class ChannelPool extends PoolListBase<IChannel> {
|
||||
public class ChannelPool extends PoolListBase<ChannelBase> {
|
||||
|
||||
public ChannelBase findByType(ProductType type) {
|
||||
return findAll().stream().filter(ch -> ch.getProductType().equals(type)).findFirst().orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface IPool<T> {
|
||||
Collection<T> findAll();
|
||||
|
||||
void add(T element);
|
||||
|
||||
void remove(T element);
|
||||
|
||||
void clear();
|
||||
|
||||
boolean contains(T element);
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.INode;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
|
||||
public class NodePool extends PoolListBase<INode> {
|
||||
public class NodePool extends PoolListBase<NodeBase> {
|
||||
//TODO Tomas Angelo
|
||||
}
|
||||
|
||||
@ -1,17 +1,16 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class PoolListBase<T> implements IPool<T> {
|
||||
public abstract class PoolListBase<T> {
|
||||
List<T> pool;
|
||||
|
||||
public PoolListBase() {
|
||||
pool = new LinkedList<T>();
|
||||
}
|
||||
|
||||
public Collection<T> findAll() {
|
||||
public List<T> findAll() {
|
||||
return pool;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.product;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.IOperation;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
|
||||
|
||||
public class Product
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.reporting;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.IChannel;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.INode;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.IOperation;
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
|
||||
|
||||
public class ReportTextFileVisitor {
|
||||
public void visit(INode node){
|
||||
public void visit(NodeBase node){
|
||||
|
||||
}
|
||||
|
||||
public void visit(IOperation operationBase){
|
||||
public void visit(OperationBase operationBase){
|
||||
|
||||
}
|
||||
|
||||
public void visit(IChannel channel){
|
||||
public void visit(ChannelBase channel){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,46 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.requirement;
|
||||
|
||||
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;
|
||||
|
||||
public class Requirement {
|
||||
|
||||
private final NodeBase creator;
|
||||
private final TransactionType transactionType;
|
||||
private final ProductType productType;
|
||||
private final String productName;
|
||||
private int TTL;
|
||||
|
||||
public Requirement(NodeBase creator, TransactionType transactionType, ProductType productType, String productName) {
|
||||
this.creator = creator;
|
||||
this.transactionType = transactionType;
|
||||
this.productType = productType;
|
||||
this.productName = productName;
|
||||
TTL = 2;
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
TTL--;
|
||||
}
|
||||
|
||||
public NodeBase getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
public TransactionType getTransactionType() {
|
||||
return transactionType;
|
||||
}
|
||||
|
||||
public ProductType getProductType() {
|
||||
return productType;
|
||||
}
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
|
||||
public int getTTL() {
|
||||
return TTL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.Customer;
|
||||
|
||||
public class ConsumeProductStep extends ScenarioStep {
|
||||
|
||||
private Customer node;
|
||||
|
||||
public ConsumeProductStep(Customer node) {
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
@Override
|
||||
void execute() {
|
||||
node.consumeProduct();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
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;
|
||||
|
||||
public class DoActionStep extends ScenarioStep {
|
||||
private final NodeBase nodeBase;
|
||||
private final ProductOperationType productOperationType;
|
||||
private final Product product;
|
||||
|
||||
public DoActionStep(NodeBase node, ProductOperationType productOperationType, Product product){
|
||||
this.nodeBase = node;
|
||||
this.productOperationType = productOperationType;
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
public DoActionStep(NodeBase node, ProductOperationType productOperationType){
|
||||
this(node, productOperationType, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
void execute() {
|
||||
if (product != null){
|
||||
nodeBase.doAction(productOperationType, product);
|
||||
}
|
||||
else {
|
||||
nodeBase.doAction(productOperationType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.Farmer;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class GenerateProductStep extends ScenarioStep {
|
||||
|
||||
private Farmer node;
|
||||
private Product product;
|
||||
|
||||
public GenerateProductStep(Farmer node) {
|
||||
this.node = node;
|
||||
product = FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll()
|
||||
.stream().findFirst().orElse(null);
|
||||
FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().remove(product);
|
||||
}
|
||||
|
||||
@Override
|
||||
void execute() {
|
||||
node.generateProduct(product);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,26 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Scenario {
|
||||
List<ScenarioStep> steps;
|
||||
Iterator<ScenarioStep> stepIterator;
|
||||
|
||||
public Scenario() {
|
||||
//steps = //TODO init
|
||||
stepIterator = steps.listIterator(0);
|
||||
}
|
||||
|
||||
public Scenario(List<ScenarioStep> steps) {
|
||||
this.steps = steps;
|
||||
stepIterator = steps.listIterator(0);
|
||||
}
|
||||
|
||||
void execute() {
|
||||
if (!stepIterator.hasNext()) {
|
||||
stepIterator = steps.listIterator(0);
|
||||
}
|
||||
stepIterator.next().execute();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
public abstract class ScenarioStep {
|
||||
abstract void execute();
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
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.TransactionType;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class SendRequirementStep extends ScenarioStep {
|
||||
private final NodeBase node;
|
||||
private final TransactionType tType;
|
||||
private final ProductType pType;
|
||||
private final String pName;
|
||||
|
||||
public SendRequirementStep(NodeBase node, TransactionType tType, ProductType pType, String pName) {
|
||||
this.node = node;
|
||||
this.tType = tType;
|
||||
this.pType = pType;
|
||||
this.pName = pName;
|
||||
}
|
||||
|
||||
@Override
|
||||
void execute() {
|
||||
node.createRequirement(tType, pType, pName);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,39 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.signature;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Signature {
|
||||
|
||||
private final String managerCode;
|
||||
private final Transaction transaction;
|
||||
private final LocalDateTime signedDate;
|
||||
private final UUID uuid;
|
||||
|
||||
public Signature(String code, Transaction transaction) {
|
||||
managerCode = code;
|
||||
this.transaction = transaction;
|
||||
signedDate = LocalDateTime.now();
|
||||
uuid = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public Transaction getTransaction(String code) {
|
||||
if (code.equals(managerCode))
|
||||
return transaction;
|
||||
return null;
|
||||
}
|
||||
|
||||
public LocalDateTime getSignedDate(String code) {
|
||||
if (code.equals(managerCode))
|
||||
return signedDate;
|
||||
return null;
|
||||
}
|
||||
|
||||
public UUID getUuid(String code) {
|
||||
if (code.equals(managerCode))
|
||||
return uuid;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,17 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.signature;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
|
||||
public class SignatureManager {
|
||||
|
||||
private static final String MANAGER_CODE = "iW91AJWNOCmX1pbpLlW0";
|
||||
|
||||
public static Signature signTransaction(Transaction transaction) {
|
||||
return new Signature(MANAGER_CODE, 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));
|
||||
}
|
||||
}
|
||||
|
||||
26
src/main/java/cz/cvut/fel/sit/omo/foodchain/util/Ticker.java
Normal file
26
src/main/java/cz/cvut/fel/sit/omo/foodchain/util/Ticker.java
Normal file
@ -0,0 +1,26 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
|
||||
import cz.cvut.fel.sit.omo.foodchain.pool.PoolDatabase;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class Ticker {
|
||||
|
||||
private static Timer timer;
|
||||
|
||||
public static void start() {
|
||||
timer = new Timer(500, actionEvent -> {
|
||||
PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
|
||||
db.getChannelPoolInstance().findAll().forEach(ch -> ch.tick()); // change TTL of requirements
|
||||
db.getNodePoolInstance().findAll().forEach(ch -> ch.tick()); // do next step in scenario
|
||||
db.getProductPoolInstance().findAll().forEach(ch -> ch.tick()); // change duration of action
|
||||
db.getScenarioPoolInstance().findAll().forEach(ch -> ch.tick()); // maybe unnecessary
|
||||
});
|
||||
timer.start();
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
timer.stop();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user