Merge remote-tracking branch 'origin/feature/factory_impl' into feature/factory_impl

This commit is contained in:
Daniel Poustka 2020-12-26 11:39:00 +01:00
commit a459317c69
11 changed files with 104 additions and 25 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,3 +1,11 @@
<?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>

View File

@ -6,7 +6,8 @@ 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.pool.ChannelPool;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
@ -22,6 +23,11 @@ public class FoodChain
{
parseChannels();
parseNodes();
parseProducts();
parseScenarios();
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType()));
getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().findAll().stream().forEach(ch -> System.out.println(ch));
}
public static FactoryManager getFactoryManager()
@ -29,7 +35,6 @@ public class FoodChain
return factoryManager;
}
// todo maybe some refactoring needed?
// add other entities
public static void parseChannels()
@ -51,12 +56,8 @@ public class FoodChain
{
System.err.println(e.getMessage());
}
catch (Exception e)
{
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public static void parseNodes()
{
try
@ -76,7 +77,45 @@ public class FoodChain
{
System.err.println(e.getMessage());
}
catch (Exception e)
}
public static void parseProducts()
{
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

@ -33,7 +33,7 @@ public class NodeFactory
{
nodeType = NodeType.valueOf(input.getAttribute("type"));
subsChannels = input.getChild("subscribed_channels");
subsChannels = input.getChild("channels");
startMoney = Double.parseDouble(input.getChild("money"));
}
catch (IllegalArgumentException e)

View File

@ -21,7 +21,6 @@ public class ScenarioFactory
{
Scenario s = null;
return s;
}
}

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

@ -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();
}