Update generated style

This commit is contained in:
Victor Shcherb 2015-05-01 15:18:56 +02:00
parent 9877df8ca0
commit 96fea08063
5 changed files with 36647 additions and 10 deletions

File diff suppressed because it is too large Load diff

View file

@ -33,7 +33,7 @@ public class RenderingRuleProperty {
protected String[] possibleValues;
protected String category;
private RenderingRuleProperty(String attrName, int type, boolean input){
protected RenderingRuleProperty(String attrName, int type, boolean input){
this.attrName = attrName;
this.type = type;
this.input = input;

View file

@ -25,6 +25,7 @@ import org.xmlpull.v1.XmlPullParserException;
public class RenderingRulesStorage {
private final static Log log = PlatformUtil.getLog(RenderingRulesStorage.class);
static boolean STORE_ATTTRIBUTES = false;
// keep sync !
// keep sync ! not change values
@ -34,7 +35,7 @@ public class RenderingRulesStorage {
public final static int POLYGON_RULES = 3;
public final static int TEXT_RULES = 4;
public final static int ORDER_RULES = 5;
private final static int LENGTH_RULES = 6;
public final static int LENGTH_RULES = 6;
private final static int SHIFT_TAG_VAL = 16;
@ -50,8 +51,8 @@ public class RenderingRulesStorage {
protected Map<String, RenderingRule> renderingAttributes = new LinkedHashMap<String, RenderingRule>();
protected Map<String, String> renderingConstants = new LinkedHashMap<String, String>();
private String renderingName;
private String internalRenderingName;
protected String renderingName;
protected String internalRenderingName;
public static interface RenderingRulesStorageResolver {
@ -233,7 +234,7 @@ public class RenderingRulesStorage {
boolean top = stack.size() == 0 || isTopCase();
parseAttributes(attrsMap);
RenderingRule renderingRule = new RenderingRule(attrsMap, isSwitch, RenderingRulesStorage.this);
if(top){
if(top || STORE_ATTTRIBUTES){
renderingRule.storeAttributes(attrsMap);
}
if (stack.size() > 0 && stack.peek() instanceof RenderingRule) {
@ -245,6 +246,9 @@ public class RenderingRulesStorage {
attrsMap.clear();
parseAttributes(attrsMap);
RenderingRule renderingRule = new RenderingRule(attrsMap, false, RenderingRulesStorage.this);
if(STORE_ATTTRIBUTES) {
renderingRule.storeAttributes(attrsMap);
}
if (stack.size() > 0 && stack.peek() instanceof RenderingRule) {
((RenderingRule) stack.peek()).addIfChildren(renderingRule);
} else {
@ -460,6 +464,7 @@ public class RenderingRulesStorage {
public static void main(String[] args) throws XmlPullParserException, IOException {
STORE_ATTTRIBUTES = true;
// InputStream is = RenderingRulesStorage.class.getResourceAsStream("default.render.xml");
String file = "/Users/victorshcherb/osmand/repos/resources/rendering_styles/default.render.xml";
Map<String, String> renderingConstants = new LinkedHashMap<String, String>();
@ -495,12 +500,19 @@ public class RenderingRulesStorage {
return depends;
}
};
storage.parseRulesFromXmlInputStream(is, resolver);
// storage.parseRulesFromXmlInputStream(is, resolver);
printAllRules(storage);
storage = new RenderingRulesStorage("", null);
new DefaultRenderingRulesStorage().createStyle(storage);
// printAllRules(storage);
testSearch(storage);
}
protected static void testSearch(RenderingRulesStorage storage) {
// long tm = System.nanoTime();
// int count = 100000;
@ -520,7 +532,7 @@ public class RenderingRulesStorage {
// searchRequest.setStringFilter(customProp, "");
// }
// }
searchRequest.setBooleanFilter(storage.PROPS.get("noPolygons"), true);
// searchRequest.setBooleanFilter(storage.PROPS.get("noPolygons"), true);
boolean res = searchRequest.search(LINE_RULES);
System.out.println("Result " + res);
printResult(searchRequest, System.out);

View file

@ -0,0 +1,255 @@
package net.osmand.render;
import gnu.trove.iterator.TIntObjectIterator;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.osmand.PlatformUtil;
import net.osmand.render.RenderingRulesStorage.RenderingRulesStorageResolver;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class RenderingRulesStoragePrinter {
public static void main(String[] args) throws XmlPullParserException, IOException {
RenderingRulesStorage.STORE_ATTTRIBUTES = true;
// InputStream is = RenderingRulesStorage.class.getResourceAsStream("default.render.xml");
String file = "/Users/victorshcherb/osmand/repos/resources/rendering_styles/default.render.xml";
Map<String, String> renderingConstants = new LinkedHashMap<String, String>();
InputStream is = new FileInputStream(file);
if(args != null && args.length > 0) {
is = new FileInputStream(args[0]);
}
try {
XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(is, "UTF-8");
int tok;
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (tok == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if (tagName.equals("renderingConstant")) {
if (!renderingConstants.containsKey(parser.getAttributeValue("", "name"))) {
renderingConstants.put(parser.getAttributeValue("", "name"),
parser.getAttributeValue("", "value"));
}
}
}
}
} finally {
is.close();
}
is = new FileInputStream(file);
RenderingRulesStorage storage = new RenderingRulesStorage("default", renderingConstants);
final RenderingRulesStorageResolver resolver = new RenderingRulesStorageResolver() {
@Override
public RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws XmlPullParserException, IOException {
RenderingRulesStorage depends = new RenderingRulesStorage(name, null);
depends.parseRulesFromXmlInputStream(RenderingRulesStorage.class.getResourceAsStream(name + ".render.xml"), ref);
return depends;
}
};
storage.parseRulesFromXmlInputStream(is, resolver);
new RenderingRulesStoragePrinter().printJavaFile(storage);
}
protected void printJavaFile(RenderingRulesStorage storage) throws IOException {
PrintStream out = System.out;
out = new PrintStream(
new File(
"/Users/victorshcherb/osmand/repos/android/OsmAnd-java/src/net/osmand/render/DefaultRenderingRulesStorage.java"));
out.println("\n\npackage net.osmand.render;\n\npublic class DefaultRenderingRulesStorage {");
String defindent = "\t";
String indent = defindent;
out.println(""+indent + defindent +"RenderingRulesStorage storage;");
out.println(
"\tprivate java.util.Map<String, String> createMap(int... attrs) {\n" +
"\t java.util.Map<String, String> mp = new java.util.HashMap<String, String>();\n" +
"\t for(int i = 0; i< attrs.length; i+=2) {\n" +
"\t mp.put(storage.getStringValue(attrs[i]), storage.getStringValue(attrs[i+1]));\n" +
"\t }\n"+
"\t return mp;\n"+
"\t}");
out.println(
"\tprivate java.util.Map<String, String> createMap(String... attrs) {\n" +
"\t java.util.Map<String, String> mp = new java.util.HashMap<String, String>();\n" +
"\t for(int i = 0; i< attrs.length; i+=2) {\n" +
"\t mp.put(attrs[i], attrs[i+1]);\n" +
"\t }\n"+
"\t return mp;\n"+
"\t}");
out.println("\n" + indent + "public void createStyle(RenderingRulesStorage storage) {");
out.println(""+indent + defindent +"this.storage=storage;");
out.println(""+indent + defindent +"storage.renderingName="+javaString(storage.renderingName)+";");
out.println(""+indent + defindent +"storage.internalRenderingName="+javaString(storage.internalRenderingName)+";");
// init dictionary must be first here
out.println(""+indent + defindent +"initDictionary();");
out.println(""+indent + defindent +"initProperties();");
out.println(""+indent + defindent +"initConstants();");
out.println(""+indent + defindent +"initAttributes();");
out.println(""+indent + defindent +"initRules();");
out.println(""+indent +"}");
printJavaInitConstants(storage, out, indent, defindent);
printJavaInitProperties(storage, out, indent, defindent);
printJavaInitRules(storage, out, indent, defindent);
printJavaInitAttributes(storage, out, indent, defindent);
// PRINT last one in order to initialize storage properly
printJavaInitDictionary(storage, out, indent, defindent);
out.println("\n\n}");
}
private String javaString(String s) {
return "\""+s+"\"";
}
private void printJavaInitDictionary(RenderingRulesStorage storage, PrintStream out, String indent, String ti) {
out.println("\n" + indent + "public void initDictionary() {");
int i = 0;
for(String s : storage.dictionary) {
out.println(""+indent + ti +"storage.getDictionaryValue("+ javaString(s) + "); // " + i++);
}
out.println(""+indent +"}");
}
private void printJavaInitProperties(RenderingRulesStorage storage, PrintStream out, String indent, String ti) {
out.println("\n" + indent + "public void initProperties() {");
out.println("" + indent + ti + "RenderingRuleProperty prop = null;");
for(RenderingRuleProperty p : storage.PROPS.customRules) {
out.println("" + indent + ti + "prop = new RenderingRuleProperty("+javaString(p.attrName)+
"," +p.type+", "+p.input+");");
out.println("" + indent + ti + "prop.setDescription("+javaString(p.description)+");");
out.println("" + indent + ti + "prop.setCategory("+javaString(p.category)+");");
out.println("" + indent + ti + "prop.setName("+javaString(p.name)+");");
if(p.possibleValues != null && !p.isBoolean()) {
String mp = "";
for (String s : p.possibleValues) {
if (mp.length() > 0) {
mp += ", ";
}
mp += javaString(s);
}
out.println("" + indent + ti + "prop.setPossibleValues(new String[]{"+mp+"});");
}
out.println("" + indent + ti + "storage.PROPS.registerRule(prop);");
}
out.println(""+indent +"}");
}
private void printJavaInitAttributes(RenderingRulesStorage storage, PrintStream out, String indent, String ti) {
out.println("\n" + indent + "public void initAttributes() {");
for (int i = 0; i < 15; i++) {
out.println("" + indent + ti + "RenderingRule rule" + i + " = null;");
}
for (String s : storage.renderingAttributes.keySet()) {
generateRenderingRule(storage, out, indent + ti, "rule", 0, storage.renderingAttributes.get(s));
out.println("" + indent + ti + "storage.renderingAttributes.put(" + javaString(s) + ", rule0);");
}
out.println(""+indent +"}");
}
private void printJavaInitRules(RenderingRulesStorage storage, PrintStream out, String indent, String ti) {
int javaFunctions = 0;
boolean initNewSection = true;
for (int rulesSection = 0; rulesSection < RenderingRulesStorage.LENGTH_RULES; rulesSection++) {
initNewSection = true;
if(storage.tagValueGlobalRules[rulesSection] == null) {
continue;
}
TIntObjectIterator<RenderingRule> iterator = storage.tagValueGlobalRules[rulesSection].iterator();
int rulesInSection = 0;
while(iterator.hasNext()) {
iterator.advance();
if (initNewSection) {
if (javaFunctions > 0) {
out.println("" + indent + "}\n");
}
out.println("\n" + indent + "public void initRules"+javaFunctions+"() {");
for (int k = 0; k < 15; k++) {
out.println("" + indent + ti + "RenderingRule rule" + k + " = null;");
}
initNewSection = false;
javaFunctions++;
}
if(rulesInSection > 50) {
rulesInSection = 0;
initNewSection = true;
}
rulesInSection += generateRenderingRule(storage, out, indent + ti, "rule", 0, iterator.value());
out.println("" + indent + ti + "storage.tagValueGlobalRules["+rulesSection+"].put("+iterator.key()+", rule0);");
}
}
if (javaFunctions > 0) {
out.println("" + indent + "}\n");
}
out.println("\n" + indent + "public void initRules() {");
for (int k = 0; k < RenderingRulesStorage.LENGTH_RULES; k++) {
if(storage.tagValueGlobalRules[k] == null) {
continue;
}
out.println("" + indent + ti + "storage.tagValueGlobalRules["+k+"] = new gnu.trove.map.hash.TIntObjectHashMap();");
}
for(int i = 0; i < javaFunctions; i++) {
out.println("" + indent + ti + "initRules"+i+"();");
}
out.println(""+indent +"}");
}
private int generateRenderingRule(RenderingRulesStorage storage, PrintStream out, String indent, String name, int ind, RenderingRule key) {
int cnt = 1;
String mp = "";
Iterator<Entry<String, String>> it = key.getAttributes().entrySet().iterator();
while(it.hasNext()) {
Entry<String, String> e = it.next();
// mp += javaString(e.getKey()) + ", " + javaString(e.getValue());
int kk = storage.getDictionaryValue(e.getKey());
int vv = storage.getDictionaryValue(e.getValue());
mp += kk +", " +vv;
if(it.hasNext()) {
mp+=", ";
}
}
if(mp.equals("")) {
mp = "java.util.Collections.EMPTY_MAP";
} else {
mp = "createMap(" +mp +")";
}
out.println("" + indent + name + ind +" = new RenderingRule("+mp +", "+ key.isGroup() + ", storage);");
for (RenderingRule k : key.getIfChildren()) {
generateRenderingRule(storage, out, indent + "\t", name, ind + 1, k);
out.println("" + indent + name + ind + ".addIfChildren(" + name + (ind + 1) + ");");
cnt++;
}
for (RenderingRule k : key.getIfElseChildren()) {
generateRenderingRule(storage, out, indent + "\t", name, ind + 1, k);
out.println("" + indent + name + ind + ".addIfElseChildren(" + name + (ind + 1) + ");");
cnt++;
}
return cnt;
}
private void printJavaInitConstants(RenderingRulesStorage storage, PrintStream out, String indent, String ti) {
out.println("\n" + indent + "public void initConstants() {");
for (String s : storage.renderingConstants.keySet()) {
out.println("" + indent + ti + "storage.renderingConstants.put(" + javaString(s) + ", "
+ javaString(storage.renderingConstants.get(s)) + ");");
}
out.println(""+indent +"}");
}
}

View file

@ -15,6 +15,7 @@ import net.osmand.IProgress;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.render.DefaultRenderingRulesStorage;
import net.osmand.render.RenderingRulesStorage;
import net.osmand.render.RenderingRulesStorage.RenderingRulesStorageResolver;
import net.osmand.util.Algorithms;
@ -29,6 +30,7 @@ public class RendererRegistry {
private final static Log log = PlatformUtil.getLog(RendererRegistry.class);
public final static String DEFAULT_RENDER = "OsmAnd"; //$NON-NLS-1$
public final static String DEFAULT_RENDER_FILE_PATH = "default.render.xml";
public final static String TOURING_VIEW = "Touring view (contrast and details)"; //$NON-NLS-1$
public final static String WINTER_SKI_RENDER = "Winter and ski"; //$NON-NLS-1$
public final static String NAUTICAL_RENDER = "Nautical"; //$NON-NLS-1$
@ -51,7 +53,7 @@ public class RendererRegistry {
public RendererRegistry(OsmandApplication app){
this.app = app;
internalRenderers.put(DEFAULT_RENDER, "default.render.xml");
internalRenderers.put(DEFAULT_RENDER, DEFAULT_RENDER_FILE_PATH);
internalRenderers.put(TOURING_VIEW, "Touring-view_(more-contrast-and-details)" +".render.xml");
internalRenderers.put("UniRS", "UniRS" + ".render.xml");
internalRenderers.put("LightRS", "LightRS" + ".render.xml");
@ -74,7 +76,9 @@ public class RendererRegistry {
return null;
}
try {
log.info("INIT start loading style");
RenderingRulesStorage r = loadRenderer(name, new LinkedHashMap<String, RenderingRulesStorage>(), new LinkedHashMap<String, String>());
log.info("INIT finish loading style");
renderers.put(name, r);
return r;
} catch (IOException e) {
@ -91,6 +95,12 @@ public class RendererRegistry {
private RenderingRulesStorage loadRenderer(String name, final Map<String, RenderingRulesStorage> loadedRenderers,
final Map<String, String> renderingConstants) throws IOException, XmlPullParserException {
// if (name.equals(DEFAULT_RENDER) || name.equalsIgnoreCase("default")) {
// RenderingRulesStorage rrs = new RenderingRulesStorage("", null);
// new DefaultRenderingRulesStorage().createStyle(rrs);
// log.info("INIT rendering from class");
// return rrs;
// }
InputStream is = getInputStream(name);
if(is == null) {
return null;