Compare commits
24 Commits
master
...
feature/op
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42cc9d6ebb | ||
|
|
6f7c11a878 | ||
|
|
e0b53db736 | ||
|
|
371b47ac31 | ||
|
|
127bdd1270 | ||
|
|
8b47edd07b | ||
|
|
90ff7af47e | ||
|
|
d3378d37d4 | ||
|
|
32468b44f0 | ||
|
|
2b57aa8dfe | ||
|
|
bd653482e5 | ||
|
|
5f481a6e48 | ||
|
|
62fa46e6c2 | ||
|
|
9119ebb8ce | ||
|
|
5a6fc28b76 | ||
|
|
7815e0df70 | ||
|
|
c9c34d4f46 | ||
|
|
8a90a07ef7 | ||
|
|
5e4ea372d0 | ||
|
|
021c194839 | ||
|
|
1d04bb6dc7 | ||
|
|
2924024ae4 | ||
|
|
cdce93bce3 | ||
|
|
8f3848e895 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -0,0 +1,4 @@
|
||||
.idea
|
||||
target
|
||||
|
||||
*.idb
|
||||
BIN
EAR diagrams/FoodChain.eap
Normal file
BIN
EAR diagrams/FoodChain.eap
Normal file
Binary file not shown.
BIN
EAR diagrams/FoodChain.ldb
Normal file
BIN
EAR diagrams/FoodChain.ldb
Normal file
Binary file not shown.
2
FoodChain.iml
Normal file
2
FoodChain.iml
Normal file
@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
||||
23
pom.xml
Normal file
23
pom.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<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>
|
||||
<maven.compiler.source>12</maven.compiler.source>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
16
src/main/java/cz/cvut/fel/sit/omo/foodchain/FoodChain.java
Normal file
16
src/main/java/cz/cvut/fel/sit/omo/foodchain/FoodChain.java
Normal file
@ -0,0 +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;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
public static FactoryManager getFactoryManager() {
|
||||
return factoryManager;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
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.requirement.Requirement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.Observable;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ChannelBase implements Observable {
|
||||
protected ProductType productType;
|
||||
protected Queue<Requirement> requirements;
|
||||
protected Queue<Transaction> transactions;
|
||||
|
||||
private Set<ChannelObserver> subscribers;
|
||||
|
||||
public ChannelBase(ProductType type)
|
||||
{
|
||||
this.requirements = new PriorityQueue<>();
|
||||
this.transactions = new PriorityQueue<>();
|
||||
this.productType = type;
|
||||
}
|
||||
|
||||
public ChannelBase()
|
||||
{
|
||||
this.requirements = new PriorityQueue<>();
|
||||
this.transactions = new PriorityQueue<>();
|
||||
this.productType = ProductType.OTHER;
|
||||
}
|
||||
|
||||
public Queue<Transaction> getTransactions()
|
||||
{
|
||||
return this.transactions;
|
||||
}
|
||||
|
||||
public Queue<Requirement> getCurrentRequirements()
|
||||
{
|
||||
return this.requirements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new requirement to the requirement Queue
|
||||
* @param requirement requirement
|
||||
*/
|
||||
public void addRequirement(Requirement requirement)
|
||||
{
|
||||
this.requirements.add(requirement);
|
||||
notifyAllObservers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyAllObservers(){
|
||||
subscribers.forEach(subscriber -> subscriber.update(requirements.peek()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attach(ChannelObserver observer){
|
||||
subscribers.add(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detach(ChannelObserver observer){
|
||||
subscribers.remove(observer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new transaction to the transaction Queue
|
||||
* @param transaction transaction
|
||||
*/
|
||||
public void addTransaction(Transaction transaction)
|
||||
{
|
||||
this.transactions.add(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all requirements from this channel
|
||||
*/
|
||||
public void clearRequirements()
|
||||
{
|
||||
this.requirements.clear();
|
||||
}
|
||||
|
||||
public ProductType getProductType() {
|
||||
return productType;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class FruitChannel extends ChannelBase
|
||||
{
|
||||
public FruitChannel()
|
||||
{
|
||||
super(ProductType.FRUIT);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class MeatChannel extends ChannelBase
|
||||
{
|
||||
public MeatChannel()
|
||||
{
|
||||
super(ProductType.MEAT);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class OtherChannel extends ChannelBase
|
||||
{
|
||||
public OtherChannel()
|
||||
{
|
||||
super();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class VegetableChannel extends ChannelBase
|
||||
{
|
||||
public VegetableChannel()
|
||||
{
|
||||
super(ProductType.VEGETABLE);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class ChannelFactory {
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.pool.PoolDatabase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.Singleton;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class FactoryManager {
|
||||
|
||||
public NodeFactory getNodeFactoryInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(NodeFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ChannelFactory getChannelFactoryInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(ChannelFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ScenarioFactory getScenarioFactoryInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(ScenarioFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProductFactory getProductFactoryInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(ProductFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public PoolDatabase getPoolDatabaseInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(PoolDatabase.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class NodeFactory {
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class ProductFactory {
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class ScenarioFactory {
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
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) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
public class Customer extends BuyerNodeBase {
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
public class Distributor extends RetailerNodeBase {
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
public class Farmer extends SellerNodeBase {
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public interface IBuyerNode {
|
||||
|
||||
void sellProduct(Product product);
|
||||
|
||||
void addMargin(Product product);
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public interface ISellerNode {
|
||||
|
||||
void buyProduct(Product product);
|
||||
}
|
||||
118
src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java
Normal file
118
src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java
Normal file
@ -0,0 +1,118 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||
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 javax.money.MonetaryAmount;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class NodeBase {
|
||||
private final List<ChannelBase> subscribedChannels;
|
||||
private final List<Product> products;
|
||||
|
||||
public NodeBase(){
|
||||
subscribedChannels = new LinkedList<>();
|
||||
products = new LinkedList<>();
|
||||
}
|
||||
|
||||
|
||||
public void sendTransaction(Product product, NodeBase node) {
|
||||
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product); //TODO wrong constructor
|
||||
|
||||
subscribedChannels.stream()
|
||||
.filter(ch -> ch.getProductType().equals(product.getProductType()))
|
||||
.findAny().ifPresent(channel -> channel.addTransaction(transaction));
|
||||
}
|
||||
|
||||
|
||||
public void sendTransaction(MonetaryAmount amount, NodeBase node) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void receiveTransaction(ProductTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void receiveTransaction(FinancialTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void refuseTransaction(ProductTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void refuseTransaction(FinancialTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
//TODO another way to read transaction
|
||||
|
||||
public Transaction readNextTransaction(ChannelBase channel) {
|
||||
return channel.getNextTransaction();
|
||||
}
|
||||
|
||||
|
||||
public Requirement readNextRequirement(ChannelBase channel) {
|
||||
return channel.getNextRequirement();
|
||||
}
|
||||
|
||||
|
||||
public void acceptRequirement(Requirement requirement) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void subscribeToChannel(ChannelBase channel) {
|
||||
subscribedChannels.add(channel);
|
||||
}
|
||||
|
||||
|
||||
public void unsubscribeFromChannel(ChannelBase channel) {
|
||||
subscribedChannels.remove(channel);
|
||||
}
|
||||
|
||||
|
||||
public void doAction(ProductOperationType action, Product product) {
|
||||
//create new action
|
||||
}
|
||||
|
||||
|
||||
public void doAction(ProductOperationType action) {
|
||||
//create new action
|
||||
}
|
||||
|
||||
|
||||
public void storeProduct(Product product) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void storeProducts(List<Product> products) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Product getProductFromStorage(String name, ProductType type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public Signature generateSignature(Transaction transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public boolean validateSignature(Signature signature) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
public class Processor extends RetailerNodeBase {
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class RetailerNodeBase extends NodeBase implements IBuyerNode, ISellerNode {
|
||||
@Override
|
||||
public void sellProduct(Product product) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMargin(Product product) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buyProduct(Product product) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
public class Seller extends RetailerNodeBase {
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.node;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class SellerNodeBase extends NodeBase implements ISellerNode {
|
||||
@Override
|
||||
public void buyProduct(Product product) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +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 {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||
|
||||
public enum TransactionType {
|
||||
|
||||
SALE, PURCHASE
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
|
||||
public class ConfigurationParser {
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||
|
||||
public class ConsoleParser {
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||
|
||||
public class ChannelPool extends PoolListBase<ChannelBase> {
|
||||
}
|
||||
15
src/main/java/cz/cvut/fel/sit/omo/foodchain/pool/IPool.java
Normal file
15
src/main/java/cz/cvut/fel/sit/omo/foodchain/pool/IPool.java
Normal file
@ -0,0 +1,15 @@
|
||||
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);
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||
|
||||
public class NodePool extends PoolListBase<NodeBase> {
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.Singleton;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class PoolDatabase {
|
||||
|
||||
public ChannelPool getChannelPoolInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(ChannelPool.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public NodePool getNodePoolInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(NodePool.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProductPool getProductPoolInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(ProductPool.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ScenarioPool getScenarioPoolInstance() {
|
||||
try {
|
||||
return Singleton.getInstance(ScenarioPool.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
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> {
|
||||
List<T> pool;
|
||||
|
||||
public PoolListBase() {
|
||||
pool = new LinkedList<T>();
|
||||
}
|
||||
|
||||
public Collection<T> findAll() {
|
||||
return pool;
|
||||
}
|
||||
|
||||
public void add(T element) {
|
||||
pool.add(element);
|
||||
}
|
||||
|
||||
public void remove(T element) {
|
||||
pool.remove(element);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
pool.clear();
|
||||
}
|
||||
|
||||
public boolean contains(T element) {
|
||||
return pool.contains(element);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
|
||||
public class ProductPool extends PoolListBase<Product> {
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.pool;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
|
||||
|
||||
public class ScenarioPool extends PoolListBase<Scenario> {
|
||||
}
|
||||
124
src/main/java/cz/cvut/fel/sit/omo/foodchain/product/Product.java
Normal file
124
src/main/java/cz/cvut/fel/sit/omo/foodchain/product/Product.java
Normal file
@ -0,0 +1,124 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.product;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
|
||||
|
||||
public class Product
|
||||
{
|
||||
private ProductState productState;
|
||||
private ProductType productType;
|
||||
private OperationBase currentOperation;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Product
|
||||
* @param name name of product
|
||||
* @param productType type of product
|
||||
* @param productState state of product
|
||||
*/
|
||||
public Product(String name, ProductType productType, ProductState productState)
|
||||
{
|
||||
this.name = name;
|
||||
this.productType = productType;
|
||||
this.productState = productState;
|
||||
}
|
||||
|
||||
// "State machine methods"
|
||||
|
||||
/**
|
||||
* Set current product state to AVAILABLE
|
||||
*/
|
||||
public void setStateToAvailable()
|
||||
{
|
||||
this.productState = ProductState.AVAILABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current product state to READY_FOR_DISPATCH. Throws IllegalStateException if the product is not AVAILABLE.
|
||||
*/
|
||||
public void setStateToReadyForDispatch()
|
||||
{
|
||||
if(this.productState.equals(ProductState.AVAILABLE))
|
||||
this.productState = ProductState.READY_FOR_DISPATCH;
|
||||
else
|
||||
throw new IllegalStateException("Cannot set state READY_FOR_DISPATCH. Product is not AVAILABLE!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current product state to IN_TRANSIT. Throws IllegalStateException if the product is not READY_FOR_DISPATCH.
|
||||
*/
|
||||
public void setStateToInTransit()
|
||||
{
|
||||
if(this.productState.equals(ProductState.READY_FOR_DISPATCH))
|
||||
this.productState = ProductState.IN_TRANSIT;
|
||||
else
|
||||
throw new IllegalStateException("Cannot set state IN_TRANSIT. Product is not READY_FOR_DISPATCH!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current product state to READY_FOR_RECEPTION. Throws IllegalStateException if the product is not READY_FOR_RECEPTION.
|
||||
*/
|
||||
public void setStateToReadyForReception()
|
||||
{
|
||||
if(this.productState.equals(ProductState.IN_TRANSIT))
|
||||
this.productState = ProductState.READY_FOR_RECEPTION;
|
||||
else
|
||||
throw new IllegalStateException("Cannot set state READY_FOR_RECEPTION. Product has not been in transit!");
|
||||
}
|
||||
|
||||
/*
|
||||
public void setProductState(ProductState productState)
|
||||
{
|
||||
this.productState = productState;
|
||||
}*/
|
||||
|
||||
|
||||
//Getters & Setters
|
||||
|
||||
public ProductState getProductState()
|
||||
{
|
||||
return productState;
|
||||
}
|
||||
|
||||
public ProductType getProductType()
|
||||
{
|
||||
return productType;
|
||||
}
|
||||
|
||||
public void setProductType(ProductType productType)
|
||||
{
|
||||
this.productType = productType;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OperationBase getCurrentOperation()
|
||||
{
|
||||
return currentOperation;
|
||||
}
|
||||
|
||||
public void setCurrentOperation(OperationBase currentOperation)
|
||||
{
|
||||
this.currentOperation = currentOperation;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.product;
|
||||
|
||||
public enum ProductState
|
||||
{
|
||||
AVAILABLE, READY_FOR_DISPATCH, IN_TRANSIT, READY_FOR_RECEPTION;
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.product;
|
||||
|
||||
public enum ProductType
|
||||
{
|
||||
MEAT, FRUIT, VEGETABLE, OTHER;
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
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.NodeBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
|
||||
|
||||
public class ReportTextFileVisitor {
|
||||
public void visit(NodeBase node){
|
||||
|
||||
}
|
||||
|
||||
public void visit(OperationBase operationBase){
|
||||
|
||||
}
|
||||
|
||||
public void visit(ChannelBase channel){
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.reporting;
|
||||
|
||||
public class ReportingManager {
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
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 int TTL;
|
||||
|
||||
public Requirement(NodeBase creator, TransactionType transactionType, ProductType productType) {
|
||||
this.creator = creator;
|
||||
this.transactionType = transactionType;
|
||||
this.productType = productType;
|
||||
TTL = 2;
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
TTL--;
|
||||
}
|
||||
|
||||
public NodeBase getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
public TransactionType getTransactionType() {
|
||||
return transactionType;
|
||||
}
|
||||
|
||||
public ProductType getProductType() {
|
||||
return productType;
|
||||
}
|
||||
|
||||
public int getTTL() {
|
||||
return TTL;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
public class Scenario {
|
||||
}
|
||||
@ -0,0 +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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +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));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||
|
||||
public interface ChannelObserver {
|
||||
void update(Requirement requirement);
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
public interface Observable {
|
||||
void attach(ChannelObserver channelObserver);
|
||||
void detach(ChannelObserver channelObserver);
|
||||
void notifyAllObservers();
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Singleton {
|
||||
|
||||
private static final Singleton instance = new Singleton();
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private final Map<Class, Object> mapHolder = new HashMap<>();
|
||||
|
||||
private Singleton() {
|
||||
}
|
||||
|
||||
public static <T> T getInstance(Class<T> tClass) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
|
||||
if (!instance.mapHolder.containsKey(tClass)) {
|
||||
Constructor<T> cons = tClass.getConstructor();
|
||||
Object object = cons.newInstance();
|
||||
instance.mapHolder.put(tClass, object);
|
||||
}
|
||||
|
||||
return (T) instance.mapHolder.get(tClass);
|
||||
}
|
||||
}
|
||||
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