Merge branch 'develop' into feature/factory_impl
This commit is contained in:
commit
a6d4a423e0
@ -1,7 +1,16 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain;
|
package cz.cvut.fel.sit.omo.foodchain;
|
||||||
|
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
|
||||||
|
|
||||||
public class FoodChain {
|
public class FoodChain {
|
||||||
|
|
||||||
|
private static FactoryManager factoryManager;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static FactoryManager getFactoryManager() {
|
||||||
|
return factoryManager;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,24 +6,30 @@ 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.product.ProductType;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
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.Signature;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||||
|
|
||||||
import javax.money.MonetaryAmount;
|
import javax.money.MonetaryAmount;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class NodeBase {
|
public class NodeBase implements ChannelObserver {
|
||||||
private final List<ChannelBase> subscribedChannels;
|
private final List<ChannelBase> subscribedChannels;
|
||||||
private final List<Product> products;
|
private final List<Product> products;
|
||||||
|
private MonetaryAmount money;
|
||||||
|
|
||||||
|
private final List<Transaction> refusedTranslations;
|
||||||
|
|
||||||
public NodeBase() {
|
public NodeBase() {
|
||||||
subscribedChannels = new LinkedList<>();
|
subscribedChannels = new LinkedList<>();
|
||||||
products = new LinkedList<>();
|
products = new LinkedList<>();
|
||||||
|
refusedTranslations = new LinkedList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void sendTransaction(Product product, NodeBase node) {
|
public void sendTransaction(Product product, NodeBase node) {
|
||||||
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product); //TODO wrong constructor
|
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product);
|
||||||
|
|
||||||
subscribedChannels.stream()
|
subscribedChannels.stream()
|
||||||
.filter(ch -> ch.getProductType().equals(product.getProductType()))
|
.filter(ch -> ch.getProductType().equals(product.getProductType()))
|
||||||
@ -31,8 +37,12 @@ public class NodeBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void sendTransaction(MonetaryAmount amount, NodeBase node) {
|
public void sendTransaction(MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction) {
|
||||||
|
Transaction transaction = new FinancialTransaction(this, node, TransactionType.PURCHASE, amount, prevTransaction);
|
||||||
|
|
||||||
|
subscribedChannels.stream()
|
||||||
|
.filter(ch -> ch.getProductType().equals(prevTransaction.getProduct().getProductType()))
|
||||||
|
.findAny().ifPresent(channel -> channel.addTransaction(transaction));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -46,26 +56,18 @@ public class NodeBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void refuseTransaction(ProductTransaction transaction) {
|
public void refuseTransaction(Transaction transaction) {
|
||||||
|
transaction.endTransaction();
|
||||||
|
refusedTranslations.add(transaction); //TODO maybe need more work on it
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void refuseTransaction(FinancialTransaction transaction) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO another way to read transaction
|
|
||||||
|
|
||||||
public Transaction readNextTransaction(ChannelBase channel) {
|
public Transaction readNextTransaction(ChannelBase channel) {
|
||||||
|
|
||||||
|
|
||||||
return channel.getNextTransaction();
|
return channel.getNextTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Requirement readNextRequirement(ChannelBase channel) {
|
|
||||||
return channel.getNextRequirement();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void acceptRequirement(Requirement requirement) {
|
public void acceptRequirement(Requirement requirement) {
|
||||||
|
|
||||||
@ -83,7 +85,7 @@ public class NodeBase {
|
|||||||
|
|
||||||
|
|
||||||
public void doAction(ProductOperationType action, Product product) {
|
public void doAction(ProductOperationType action, Product product) {
|
||||||
//create new action
|
Action actionProcess = new Action(this, product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -93,26 +95,32 @@ public class NodeBase {
|
|||||||
|
|
||||||
|
|
||||||
public void storeProduct(Product product) {
|
public void storeProduct(Product product) {
|
||||||
|
this.products.add(product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void storeProducts(List<Product> products) {
|
public void storeProducts(List<Product> products) {
|
||||||
|
this.products.addAll(products);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Product getProductFromStorage(String name, ProductType type) {
|
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 Signature generateSignature(Transaction transaction) {
|
public Signature generateSignature(Transaction transaction) {
|
||||||
return null;
|
return SignatureManager.signTransaction(transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean validateSignature(Signature signature) {
|
public boolean validateSignature(Transaction transaction) {
|
||||||
return false;
|
return SignatureManager.validateSignature(transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Requirement requirement) {
|
||||||
|
if (!requirement.getCreator().equals(this))
|
||||||
|
acceptRequirement(requirement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,21 +8,28 @@ public class Action extends OperationBase {
|
|||||||
private final OperationBase lastOperation;
|
private final OperationBase lastOperation;
|
||||||
private final Product product;
|
private final Product product;
|
||||||
private final ProductOperationType type;
|
private final ProductOperationType type;
|
||||||
|
private int duration;
|
||||||
|
|
||||||
Action(NodeBase node, Product product) {
|
Action(NodeBase node, Product product, ProductOperationType type) {
|
||||||
super(node);
|
super(node);
|
||||||
this.product = product;
|
this.product = product;
|
||||||
type = null;
|
this.type = type;
|
||||||
lastOperation = product.getCurrentOperation();
|
lastOperation = product.getCurrentOperation();
|
||||||
|
duration = type.getDuration();
|
||||||
product.setCurrentOperation(this);
|
product.setCurrentOperation(this);
|
||||||
product.setStateToReadyForDispatch();
|
product.setStateToReadyForDispatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endAction() {
|
private void endAction() {
|
||||||
this.setTimeEnd();
|
this.setTimeEnd();
|
||||||
product.setStateToAvailable();
|
product.setStateToAvailable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
if (duration == 0) endAction();
|
||||||
|
else duration--;
|
||||||
|
}
|
||||||
|
|
||||||
public OperationBase getLastOperation() {
|
public OperationBase getLastOperation() {
|
||||||
return lastOperation;
|
return lastOperation;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public class FinancialTransaction extends Transaction {
|
|||||||
private final MonetaryAmount amount;
|
private final MonetaryAmount amount;
|
||||||
private final ProductTransaction transactionPaid;
|
private final ProductTransaction transactionPaid;
|
||||||
|
|
||||||
FinancialTransaction(NodeBase creator, NodeBase receiver, TransactionType type, MonetaryAmount amount, ProductTransaction transactionPaid) {
|
public FinancialTransaction(NodeBase creator, NodeBase receiver, TransactionType type, MonetaryAmount amount, ProductTransaction transactionPaid) {
|
||||||
super(creator, receiver, type);
|
super(creator, receiver, type);
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
this.transactionPaid = transactionPaid;
|
this.transactionPaid = transactionPaid;
|
||||||
|
|||||||
@ -1,4 +1,22 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.operation;
|
package cz.cvut.fel.sit.omo.foodchain.operation;
|
||||||
|
|
||||||
public enum ProductOperationType {
|
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,43 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.parser;
|
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 {
|
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 tmp_node = elements.item(i);
|
||||||
|
ParserResultElement element = new ParserResultElement();
|
||||||
|
for (int j = 0; j < tmp_node.getAttributes().getLength(); j++) {
|
||||||
|
element.addAttribute(tmp_node.getAttributes().item(i).getNodeName(), tmp_node.getAttributes().item(i).getTextContent());
|
||||||
|
}
|
||||||
|
for (int j = 0; j < tmp_node.getChildNodes().getLength(); j++) {
|
||||||
|
element.addChild(tmp_node.getChildNodes().item(i).getNodeName(), tmp_node.getChildNodes().item(i).getTextContent());
|
||||||
|
}
|
||||||
|
resultList.addElement(element);
|
||||||
|
}
|
||||||
|
return resultList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,18 @@
|
|||||||
|
package cz.cvut.fel.sit.omo.foodchain.parser;
|
||||||
|
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class ParserResult {
|
||||||
|
|
||||||
|
private LinkedList<ParserResultElement> elements;
|
||||||
|
|
||||||
|
public ParserResult() {
|
||||||
|
elements = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addElement(ParserResultElement element) {
|
||||||
|
elements.add(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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,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);
|
|
||||||
}
|
|
||||||
@ -3,4 +3,5 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
|
|||||||
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
|
||||||
|
|
||||||
public class NodePool extends PoolListBase<NodeBase> {
|
public class NodePool extends PoolListBase<NodeBase> {
|
||||||
|
//TODO Tomas Angelo
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import java.util.Collection;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class PoolListBase<T> implements IPool<T> {
|
public abstract class PoolListBase<T> {
|
||||||
List<T> pool;
|
List<T> pool;
|
||||||
|
|
||||||
public PoolListBase() {
|
public PoolListBase() {
|
||||||
|
|||||||
@ -1,4 +1,40 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.requirement;
|
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 {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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