Archived
0

change xml store format

This commit is contained in:
iMoHax
2014-11-21 16:33:13 +03:00
parent fec9b84d37
commit 0dbd820c98
5 changed files with 217 additions and 16 deletions

View File

@@ -30,6 +30,7 @@ public class MarketDocHandler extends DefaultHandler {
protected final static String PRICE_ATTR = "price"; protected final static String PRICE_ATTR = "price";
protected final static String COUNT_ATTR = "count"; protected final static String COUNT_ATTR = "count";
protected final static String ITEM_ATTR = "item"; protected final static String ITEM_ATTR = "item";
protected final static String DISTANCE_ATTR = "distance";
protected final static String X_ATTR = "x"; protected final static String X_ATTR = "x";
protected final static String Y_ATTR = "y"; protected final static String Y_ATTR = "y";
protected final static String Z_ATTR = "z"; protected final static String Z_ATTR = "z";
@@ -91,8 +92,9 @@ public class MarketDocHandler extends DefaultHandler {
protected void parseVendor(Attributes attributes) throws SAXException { protected void parseVendor(Attributes attributes) throws SAXException {
String name = attributes.getValue(NAME_ATTR); String name = attributes.getValue(NAME_ATTR);
LOG.debug("parse vendor {} position ({};{};{})", name); String distance = attributes.getValue(DISTANCE_ATTR);
onVendor(name); LOG.debug("parse vendor {}, distance {}", name, distance);
onVendor(name, distance != null ? Double.valueOf(distance) : 0);
} }
protected void parseService(Attributes attributes) throws SAXException { protected void parseService(Attributes attributes) throws SAXException {
@@ -135,8 +137,9 @@ public class MarketDocHandler extends DefaultHandler {
curPlace = world.addPlace(name, x, y, z); curPlace = world.addPlace(name, x, y, z);
} }
protected void onVendor(String name){ protected void onVendor(String name, double distance){
curVendor = curPlace.addVendor(name); curVendor = curPlace.addVendor(name);
curVendor.setDistance(distance);
} }
protected void onService(SERVICE_TYPE type){ protected void onService(SERVICE_TYPE type){

View File

@@ -42,7 +42,7 @@ public class MarketStreamWriter {
protected void writeMarket() throws XMLStreamException { protected void writeMarket() throws XMLStreamException {
out.writeStartElement(MarketDocHandler.MARKET); out.writeStartElement(MarketDocHandler.MARKET);
writeItems(); writeItems();
writeVendors(); writePlaces();
out.writeEndElement(); out.writeEndElement();
} }
@@ -68,12 +68,22 @@ public class MarketStreamWriter {
out.writeAttribute(MarketDocHandler.ID_ATTR, id); out.writeAttribute(MarketDocHandler.ID_ATTR, id);
} }
protected void writeVendors() throws XMLStreamException { protected void writePlaces() throws XMLStreamException {
out.writeStartElement(MarketDocHandler.PLACES_LIST); out.writeStartElement(MarketDocHandler.PLACES_LIST);
for (Place place : market.get()) { for (Place place : market.get()) {
for (Vendor vendor : place.get()) { writePlace(place);
writeVendor(vendor); }
} out.writeEndElement();
}
protected void writePlace(Place place) throws XMLStreamException {
out.writeStartElement(MarketDocHandler.PLACE);
out.writeAttribute(MarketDocHandler.NAME_ATTR, place.getName());
out.writeAttribute(MarketDocHandler.X_ATTR, String.valueOf(place.getX()));
out.writeAttribute(MarketDocHandler.Y_ATTR, String.valueOf(place.getY()));
out.writeAttribute(MarketDocHandler.Z_ATTR, String.valueOf(place.getZ()));
for (Vendor vendor : place.get()) {
writeVendor(vendor);
} }
out.writeEndElement(); out.writeEndElement();
} }
@@ -81,10 +91,7 @@ public class MarketStreamWriter {
protected void writeVendor(Vendor vendor) throws XMLStreamException { protected void writeVendor(Vendor vendor) throws XMLStreamException {
out.writeStartElement(MarketDocHandler.VENDOR); out.writeStartElement(MarketDocHandler.VENDOR);
out.writeAttribute(MarketDocHandler.NAME_ATTR, vendor.getName()); out.writeAttribute(MarketDocHandler.NAME_ATTR, vendor.getName());
Place place = vendor.getPlace(); out.writeAttribute(MarketDocHandler.DISTANCE_ATTR, String.valueOf(vendor.getDistance()));
out.writeAttribute(MarketDocHandler.X_ATTR, String.valueOf(place.getX()));
out.writeAttribute(MarketDocHandler.Y_ATTR, String.valueOf(place.getY()));
out.writeAttribute(MarketDocHandler.Z_ATTR, String.valueOf(place.getZ()));
out.writeStartElement(MarketDocHandler.SERVICES_LIST); out.writeStartElement(MarketDocHandler.SERVICES_LIST);
for (SERVICE_TYPE service_type : vendor.getServices()) { for (SERVICE_TYPE service_type : vendor.getServices()) {
out.writeEmptyElement(MarketDocHandler.SERVICE); out.writeEmptyElement(MarketDocHandler.SERVICE);
@@ -105,6 +112,7 @@ public class MarketStreamWriter {
out.writeAttribute(MarketDocHandler.TYPE_ATTR, offer.getType().toString()); out.writeAttribute(MarketDocHandler.TYPE_ATTR, offer.getType().toString());
out.writeAttribute(MarketDocHandler.ITEM_ATTR, items.get(offer.getItem())); out.writeAttribute(MarketDocHandler.ITEM_ATTR, items.get(offer.getItem()));
out.writeAttribute(MarketDocHandler.PRICE_ATTR, String.valueOf(offer.getPrice())); out.writeAttribute(MarketDocHandler.PRICE_ATTR, String.valueOf(offer.getPrice()));
out.writeAttribute(MarketDocHandler.COUNT_ATTR, String.valueOf(offer.getCount()));
} }
protected void writeGroup(Group group) throws XMLStreamException { protected void writeGroup(Group group) throws XMLStreamException {

View File

@@ -66,6 +66,7 @@
<xs:element name="offer" type="offerType" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="offer" type="offerType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence> </xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="distance" type="xs:double"/>
</xs:complexType> </xs:complexType>
<xs:complexType name="serviceType"> <xs:complexType name="serviceType">

View File

@@ -5,12 +5,15 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import ru.trader.core.Market; import ru.trader.TestUtil;
import ru.trader.core.*;
import ru.trader.store.simple.SimpleMarket;
import ru.trader.store.simple.Store; import ru.trader.store.simple.Store;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import javax.xml.stream.XMLStreamException;
import java.io.InputStream; import java.io.*;
import java.util.Collection;
public class LoadTest extends Assert { public class LoadTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(LoadTest.class); private final static Logger LOG = LoggerFactory.getLogger(LoadTest.class);
@@ -28,4 +31,190 @@ public class LoadTest extends Assert {
assertNotNull(world); assertNotNull(world);
} }
private void assertGroup(Group group1, Group group2){
assertEquals(group1.getName(), group2.getName());
assertEquals(group1.getType(), group2.getType());
}
private void assertItem(Item item1, Item item2){
assertEquals(item1.getName(), item2.getName());
assertGroup(item1.getGroup(), item2.getGroup());
}
private void assertPlace(Place place1, Place place2){
assertEquals(place1.getName(), place2.getName());
assertEquals(place1.getX(), place2.getX(), 0.00001);
assertEquals(place1.getY(), place2.getY(), 0.00001);
assertEquals(place1.getZ(), place2.getZ(), 0.00001);
}
private void assertVendor(Vendor vendor1, Vendor vendor2){
assertEquals(vendor1.getName(), vendor2.getName());
assertEquals(vendor1.getDistance(), vendor2.getDistance(), 0.00001);
}
private void assertOffer(Offer offer1, Offer offer2){
assertEquals(offer1.getType(), offer2.getType());
assertItem(offer1.getItem(), offer2.getItem());
assertEquals(offer1.getPrice(), offer2.getPrice(), 0.000001);
assertEquals(offer1.getCount(), offer2.getCount());
}
@Test
public void testSave(){
LOG.info("Start world save test");
Market market = new SimpleMarket();
Group group1 = market.addGroup("Group 1", GROUP_TYPE.MARKET);
Group group2 = market.addGroup("Group 2", GROUP_TYPE.MARKET);
Group group3 = market.addGroup("Group 3", GROUP_TYPE.OUTFIT);
Item item1 = market.addItem("Item 1", group1);
Item item2 = market.addItem("Item 2", group1);
Item item3 = market.addItem("Item 3", group2);
Item item4 = market.addItem("Item 4", group2);
Item item5 = market.addItem("Item 5", group3);
Place place1 = market.addPlace("Place 1", 0, 1, 3);
Place place2 = market.addPlace("Place 2",4,0,5);
Place place3 = market.addPlace("Place 3",0,0,0);
Vendor vendor1 = place1.addVendor("Vendor 1");
Vendor vendor2 = place1.addVendor("Vendor 2");
Vendor vendor3 = place2.addVendor("Vendor 3");
vendor1.setDistance(10);
vendor1.add(SERVICE_TYPE.MARKET);
vendor1.add(SERVICE_TYPE.OUTFIT);
Offer offer1 = vendor1.addOffer(OFFER_TYPE.SELL, item1, 10,43);
Offer offer2 = vendor1.addOffer(OFFER_TYPE.BUY, item1, 12,1);
Offer offer3 = vendor1.addOffer(OFFER_TYPE.SELL, item2, 1012,1000);
Offer offer4 = vendor1.addOffer(OFFER_TYPE.SELL, item3, 110,0);
Offer offer5 = vendor1.addOffer(OFFER_TYPE.SELL, item4, 1112,12);
Offer offer6 = vendor1.addOffer(OFFER_TYPE.BUY, item5, 11,10);
vendor2.setDistance(100.4);
vendor3.setDistance(200000.4);
vendor3.add(SERVICE_TYPE.OUTFIT);
LOG.info("save market");
File xml = new File("save_load_test.xml");
try {
Store.saveToFile(market, xml);
} catch (FileNotFoundException | UnsupportedEncodingException | XMLStreamException e) {
throw new AssertionError(e);
}
LOG.info("load world");
Market world;
try {
world = Store.loadFromFile(xml);
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new AssertionError(e);
}
assertNotNull(world);
assertTrue(xml.delete());
LOG.info("check groups");
Collection<Group> groups = world.getGroups();
int i=0;
for (Group group : groups) {
if (group1.getName().equals(group.getName())) {assertGroup(group, group1);i++;}
if (group2.getName().equals(group.getName())) {assertGroup(group, group2);i++;}
if (group3.getName().equals(group.getName())) {assertGroup(group, group3);i++;}
}
assertEquals(3, i);
LOG.info("check items");
Collection<Item> items = world.getItems();
i=0;
for (Item item : items) {
if (item1.getName().equals(item.getName())) {assertItem(item, item1);i++;}
if (item2.getName().equals(item.getName())) {assertItem(item, item2);i++;}
if (item3.getName().equals(item.getName())) {assertItem(item, item3);i++;}
if (item4.getName().equals(item.getName())) {assertItem(item, item4);i++;}
if (item5.getName().equals(item.getName())) {assertItem(item, item5);i++;}
}
assertEquals(5, i);
LOG.info("check places");
Collection<Place> places = world.get();
i=0;
for (Place place : places) {
if (place1.getName().equals(place.getName())) {
assertPlace(place, place1);
i++;
Collection<Vendor> vendors = place.get();
int j=0;
for (Vendor vendor : vendors) {
if (vendor1.getName().equals(vendor.getName())) {
LOG.info("check vendor 1");
assertVendor(vendor, vendor1);j++;
Collection<Offer> offers = vendor.getAllSellOffers();
int o = 0;
for (Offer offer : offers) {
if (offer1.getItem().getName().equals(offer.getItem().getName())) {assertOffer(offer, offer1);o++;}
if (offer3.getItem().getName().equals(offer.getItem().getName())) {assertOffer(offer, offer3);o++;}
if (offer4.getItem().getName().equals(offer.getItem().getName())) {assertOffer(offer, offer4);o++;}
if (offer5.getItem().getName().equals(offer.getItem().getName())) {assertOffer(offer, offer5);o++;}
}
assertEquals(4, o);
offers = vendor.getAllBuyOffers();
o = 0;
for (Offer offer : offers) {
if (offer2.getItem().getName().equals(offer.getItem().getName())) {assertOffer(offer, offer2);o++;}
if (offer6.getItem().getName().equals(offer.getItem().getName())) {assertOffer(offer, offer6);o++;}
}
assertEquals(2, o);
assertTrue(vendor.has(SERVICE_TYPE.MARKET));
assertFalse(vendor.has(SERVICE_TYPE.BLACK_MARKET));
assertFalse(vendor.has(SERVICE_TYPE.REPAIR));
assertFalse(vendor.has(SERVICE_TYPE.MUNITION));
assertTrue(vendor.has(SERVICE_TYPE.OUTFIT));
assertFalse(vendor.has(SERVICE_TYPE.SHIPYARD));
}
if (vendor2.getName().equals(vendor.getName())) {
LOG.info("check vendor 2");
assertVendor(vendor, vendor2);j++;
assertTrue(vendor.getAllBuyOffers().isEmpty());
assertTrue(vendor.getAllSellOffers().isEmpty());
assertFalse(vendor.has(SERVICE_TYPE.MARKET));
assertFalse(vendor.has(SERVICE_TYPE.BLACK_MARKET));
assertFalse(vendor.has(SERVICE_TYPE.REPAIR));
assertFalse(vendor.has(SERVICE_TYPE.MUNITION));
assertFalse(vendor.has(SERVICE_TYPE.OUTFIT));
assertFalse(vendor.has(SERVICE_TYPE.SHIPYARD));
}
}
assertEquals(2, j);
assertTrue(place.canRefill());
}
if (place2.getName().equals(place.getName())) {
assertPlace(place, place2);
i++;
Collection<Vendor> vendors = place.get();
int j=0;
for (Vendor vendor : vendors) {
if (vendor3.getName().equals(vendor.getName())) {
LOG.info("check vendor 3");
assertVendor(vendor, vendor3);j++;
assertTrue(vendor.getAllBuyOffers().isEmpty());
assertTrue(vendor.getAllSellOffers().isEmpty());
assertFalse(vendor.has(SERVICE_TYPE.MARKET));
assertFalse(vendor.has(SERVICE_TYPE.BLACK_MARKET));
assertFalse(vendor.has(SERVICE_TYPE.REPAIR));
assertFalse(vendor.has(SERVICE_TYPE.MUNITION));
assertTrue(vendor.has(SERVICE_TYPE.OUTFIT));
assertFalse(vendor.has(SERVICE_TYPE.SHIPYARD));
}
}
assertEquals(1, j);
assertTrue(place.canRefill());
}
if (place3.getName().equals(place.getName())) {
assertPlace(place, place3);
i++;
assertTrue(place.get().isEmpty());
assertFalse(place.canRefill());
}
}
assertEquals(3, i);
}
} }

View File

@@ -10,7 +10,7 @@
</items> </items>
<places> <places>
<place name="Place 1"> <place name="Place 1">
<vendor name="Vendor 1"> <vendor name="Vendor 1" distance="10000.4">
<services><service type="MARKET"/></services> <services><service type="MARKET"/></services>
<offer item="i1" type="SELL" price="140" count="10"/> <offer item="i1" type="SELL" price="140" count="10"/>
</vendor> </vendor>