Merge branch 'feature/factory_impl' into develop

# Conflicts:
#	src/main/java/cz/cvut/fel/sit/omo/foodchain/product/Product.java
This commit is contained in:
Nikita Iryupin 2020-12-26 12:43:37 +01:00
commit 624b18bd79
19 changed files with 235 additions and 52 deletions

View File

@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE channels [
<!ELEMENT channels (channel)*>
<!ELEMENT channel (#PCDATA)>
<!ATTLIST channel
type CDATA #REQUIRED>
]>
<channels>
<channel type="MEET"></channel>
<channel type="MEAT"></channel>
<channel type="VEGETABLE"></channel>
<channel type="FRUIT"></channel>
</channels>

View File

@ -1,18 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE nodes [
<!ELEMENT nodes (node)*>
<!ELEMENT node (money|channels)*>
<!ATTLIST node
type CDATA #REQUIRED>
<!ELEMENT money (#PCDATA)>
<!ELEMENT channels (#PCDATA)>
]>
<nodes>
<node type = "Farmer">
<money>100</money>
<channels>MEET PURCHASE;VEGETABLE PURCHASE;FRUIT PURCHASE</channels>
<channels>MEAT 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>
<channels>MEAT 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>
<channels>MEAT 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>
<channels>MEAT SALE;VEGETABLE SALE;FRUIT SALE</channels>
</node>
</nodes>

View File

@ -1,3 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE products [
<!ELEMENT products (product)*>
<!ELEMENT product (name|type|price)*>
<!ELEMENT name (#PCDATA)>
<!ELEMENT type (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<products>
<product>
<name>jablko</name>
@ -31,17 +39,17 @@
</product>
<product>
<name>vepřové</name>
<type>MEET</type>
<type>MEAT</type>
<price>20</price>
</product>
<product>
<name>hovězí</name>
<type>MEET</type>
<type>MEAT</type>
<price>50</price>
</product>
<product>
<name>kuřecí</name>
<type>MEET</type>
<type>MEAT</type>
<price>30</price>
</product>
</products>

View File

@ -1,15 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE scenarios [
<!ELEMENT scenarios (scenario)*>
<!ELEMENT scenario (steps)*>
<!ATTLIST scenario
type CDATA #REQUIRED>
<!ELEMENT steps (#PCDATA)>
]>
<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
generate
generate
generate
generate
generate
generate
generate
generate
generate
</steps>
</scenario>
</scenarios>

View File

@ -1,11 +1,14 @@
package cz.cvut.fel.sit.omo.foodchain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.factory.ChannelFactory;
import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.parser.ConfigurationParser;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResult;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
import cz.cvut.fel.sit.omo.foodchain.util.Ticker;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
@ -18,18 +21,39 @@ public class FoodChain
private static FactoryManager factoryManager = new FactoryManager();
public static void main(String[] args)
{
parseChannels();
parseNodes();
parseProducts();
parseScenarios();
Ticker.start();
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType()));
getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().findAll().stream().forEach(ch -> System.out.println(ch));
getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType() + " " + ch.getName()));
getFactoryManager().getPoolDatabaseInstance().getScenarioPoolInstance().findAll().stream().forEach(ch -> ch.getSteps().stream().forEach(s -> System.out.println(s)));
}
public static FactoryManager getFactoryManager()
{
return factoryManager;
}
// todo maybe some refactoring needed?
// add other entities
public static void parseChannels()
{
try
{
// parse channel configuration
ConfigurationParser.parseDocument("channels");
ParserResult parserResult = ConfigurationParser.getElements("channels");
//todo need getter for elemtens...
ConfigurationParser.parseDocument("conf/channels.xml");
ParserResult parserResult = ConfigurationParser.getElements("channel");
List<ParserResultElement> elements = parserResult.getElements();
for (ParserResultElement el : elements)
{
ChannelBase c = getFactoryManager().getChannelFactoryInstance().makeChannel(el);
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().add(c);
ChannelBase res = getFactoryManager().getChannelFactoryInstance().makeChannel(el);
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().add(res);
}
}
@ -37,16 +61,68 @@ public class FoodChain
{
System.err.println(e.getMessage());
}
catch (Exception e)
}
public static void parseNodes()
{
try
{
// parse channel configuration
ConfigurationParser.parseDocument("conf/nodes.xml");
ParserResult parserResult = ConfigurationParser.getElements("node");
List<ParserResultElement> elements = parserResult.getElements();
for (ParserResultElement el : elements)
{
NodeBase res = getFactoryManager().getNodeFactoryInstance().makeNode(el);
getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().add(res);
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
System.err.println(e.getMessage());
}
}
public static FactoryManager getFactoryManager()
public static void parseProducts()
{
return factoryManager;
try
{
// parse channel configuration
ConfigurationParser.parseDocument("conf/products.xml");
ParserResult parserResult = ConfigurationParser.getElements("product");
List<ParserResultElement> elements = parserResult.getElements();
for (ParserResultElement el : elements)
{
Product res = getFactoryManager().getProductFactoryInstance().makeProduct(el);
getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().add(res);
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
System.err.println(e.getMessage());
}
}
public static void parseScenarios()
{
try
{
// parse channel configuration
ConfigurationParser.parseDocument("conf/scenarios.xml");
ParserResult parserResult = ConfigurationParser.getElements("scenario");
List<ParserResultElement> elements = parserResult.getElements();
for (ParserResultElement el : elements)
{
Scenario res = getFactoryManager().getScenarioFactoryInstance().makeScenario(el);
getFactoryManager().getPoolDatabaseInstance().getScenarioPoolInstance().add(res);
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
System.err.println(e.getMessage());
}
}
}

View File

@ -1,16 +1,18 @@
package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType;
import cz.cvut.fel.sit.omo.foodchain.node.*;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
import java.util.Objects;
public class NodeFactory
{
private enum NodeType
enum NodeType
{
Farmer, Processor, Distributor, Seller, Customer;
}
@ -25,14 +27,14 @@ public class NodeFactory
NodeBase n = null;
NodeType nodeType = null;
String subsChannels = "";
int startMoney = 0;
double startMoney = 0;
try
{
nodeType = NodeType.valueOf(input.getAttribute("type"));
subsChannels = input.getChild("subscribed_channels");
startMoney = Integer.getInteger(input.getChild("money"));
subsChannels = input.getChild("channels");
startMoney = Double.parseDouble(input.getChild("money"));
}
catch (IllegalArgumentException e)
{
@ -63,12 +65,13 @@ public class NodeFactory
}
String[] subs = subsChannels.split(";");
//todo Node subscription to channels by parser data
for (String s : subs)
{
String[] el = s.split(" ");
ChannelPool temp = FoodChain.getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance();
// n.subscribeToChannel(temp.findByType(s)); //TODO String to enum
n.subscribeToChannel(temp.findByType(ProductType.valueOf(el[0])), ChannelSubscriptionType.valueOf(el[1]));
}
return n;

View File

@ -18,10 +18,10 @@ public class ProductFactory
Product p = null;
try
{
String name = input.getAttribute("name");
String name = input.getChild("name");
ProductType productType = ProductType.valueOf(input.getChild("type"));
//ProductState productState = ProductState.valueOf(input.getChild("state"));
int price = Integer.getInteger(input.getChild("price"));
double price = Double.parseDouble(input.getChild("price"));
//p = new Product(name, price, productType, productState);
p = new Product(name, productType, price);

View File

@ -2,16 +2,24 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.node.*;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
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.scenario.*;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.util.Objects;
public class ScenarioFactory
{
/**
* Creates Scenario based on input ParserResultElement
* @param input data from Scenario is created
@ -20,7 +28,52 @@ public class ScenarioFactory
public Scenario makeScenario(ParserResultElement input)
{
Scenario s = null;
NodeBase node = null;
try
{
String nodeType = input.getAttribute("type");
node = FoodChain.getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().findAll().stream()
.filter(ch -> ch.getClass().getSimpleName().equals(nodeType)).findFirst().orElse(null);
String[] steps = Arrays.stream(input.getChild("steps").split("\n")).filter(st -> !st.strip().equals("")).toArray(String[]::new);
List<ScenarioStep> scenarioSteps = new LinkedList<>();
ScenarioStep scenarioStep = null;
for (String step : steps) {
String[] parts = step.strip().split(" ");
switch (parts[0]) {
case "generate":
scenarioStep = new GenerateProductStep((Farmer) node);
break;
case "consume":
scenarioStep = new ConsumeProductStep((Customer) node);
break;
case "doaction":
if (parts.length == 2) {
scenarioStep = new DoActionStep(node, ProductOperationType.valueOf(parts[1]));
}
else {
Product p = FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().stream()
.filter(pr -> pr.getName().equals(parts[2])).findFirst().orElse(null);
scenarioStep = new DoActionStep(node, ProductOperationType.valueOf(parts[1]), p);
}
break;
case "sendreq":
scenarioStep = new SendRequirementStep(node, TransactionType.valueOf(parts[1]), ProductType.valueOf(parts[2]), parts[3]);
break;
}
scenarioSteps.add(scenarioStep);
}
s = new Scenario(scenarioSteps);
}
catch (IllegalArgumentException e)
{
System.err.println("! Wrong type of enum in ProductFactory: " + e);
}
catch (Exception e)
{
System.err.println("! Fatal error in ProductFactory: " + e);
}
return s;
}

View File

@ -4,7 +4,7 @@ import java.util.Random;
public class Customer extends BuyerNodeBase {
public Customer(int startMoney) {
public Customer(double startMoney) {
super();
}

View File

@ -2,7 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain.node;
public class Distributor extends RetailerNodeBase {
public Distributor(int startMoney) {
public Distributor(double startMoney) {
super();
}
}

View File

@ -5,7 +5,7 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class Farmer extends SellerNodeBase {
public Farmer(int startMoney) {
public Farmer(double startMoney) {
super();
}

View File

@ -2,7 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain.node;
public class Processor extends RetailerNodeBase {
public Processor(int startMoney) {
public Processor(double startMoney) {
super();
}
}

View File

@ -2,7 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain.node;
public class Seller extends RetailerNodeBase {
public Seller(int startMoney) {
public Seller(double startMoney) {
super();
}
}

View File

@ -31,10 +31,19 @@ public class ConfigurationParser {
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());
element
.addAttribute(
tmpNode
.getAttributes()
.item(j)
.getNodeName(),
tmpNode
.getAttributes()
.item(j)
.getTextContent());
}
for (int j = 0; j < tmpNode.getChildNodes().getLength(); j++) {
element.addChild(tmpNode.getChildNodes().item(i).getNodeName(), tmpNode.getChildNodes().item(i).getTextContent());
element.addChild(tmpNode.getChildNodes().item(j).getNodeName(), tmpNode.getChildNodes().item(j).getTextContent());
}
resultList.addElement(element);
}

View File

@ -11,6 +11,7 @@ public class ParserResultElement {
private Map<String, String> children;
public ParserResultElement() {
children = new HashMap<>();
attributes = new HashMap<>();
}

View File

@ -3,5 +3,4 @@ package cz.cvut.fel.sit.omo.foodchain.pool;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
public class NodePool extends PoolListBase<NodeBase> {
//TODO Tomas Angelo
}

View File

@ -9,12 +9,18 @@ public class GenerateProductStep extends ScenarioStep {
private final Farmer node;
private final Product product;
private static int counter = 0;
public GenerateProductStep(Farmer node) {
this.node = node;
product = FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll()
.stream().findFirst().orElse(null);
FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().remove(product);
if (counter < FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().size()) {
product = FoodChain.getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().get(counter);
counter++;
}
else {
product = null;
System.err.println("Insufficient products in pool");
}
}
@Override

View File

@ -23,4 +23,8 @@ public class Scenario {
}
stepIterator.next().execute();
}
public List<ScenarioStep> getSteps() {
return steps;
}
}

View File

@ -12,10 +12,10 @@ public class Ticker {
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
// 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();
}