public void resolve(
      String identifier,
      TableReference container,
      EReference reference,
      int position,
      boolean resolveFuzzy,
      final ISqlReferenceResolveResult<TableDefinition> result) {

    SQLScript sqlScript = (SQLScript) EcoreUtil.getRootContainer(container);

    String catalogName = container.getCatalogName();
    Predicate<TableDefinition> filter =
        !Helper.isEmpty(catalogName)
            ? table -> catalogName.equals(table.getSchemaQualifiedName().getCatalogName())
            : table -> true;

    String schemaName = container.getSchemaName();
    Predicate<TableDefinition> filter2 =
        !Helper.isEmpty(schemaName)
            ? table -> schemaName.equals(table.getSchemaQualifiedName().getSchemaName())
            : table -> true;

    Stream<TableDefinition> tables =
        sqlScript
            .getStatements()
            .stream()
            .filter(stmt -> stmt instanceof TableDefinition)
            .map(table -> (TableDefinition) table)
            .filter(filter.and(filter2));

    Consumer<TableDefinition> addMapping =
        table -> result.addMapping(table.getSchemaQualifiedName().getName(), table);

    if (resolveFuzzy) {
      tables
          .filter(
              table ->
                  table.getSchemaQualifiedName() != null
                      && table.getSchemaQualifiedName().getName() != null
                      && table
                          .getSchemaQualifiedName()
                          .getName()
                          .toUpperCase()
                          .startsWith(identifier.toUpperCase()))
          .forEach(addMapping);
    } else {
      tables
          .filter(
              table ->
                  table.getSchemaQualifiedName() != null
                      && table.getSchemaQualifiedName().getName() != null
                      && table.getSchemaQualifiedName().getName().equals(identifier))
          .findFirst()
          .ifPresent(addMapping);
    }
  }
Exemple #2
0
 /**
  * This file can be an osm xml (.osm), a compressed xml (.osm.zip or .osm.gz) or a protobuf file
  * (.pbf).
  */
 public GraphHopper setOSMFile(String osmFileStr) {
   if (Helper.isEmpty(osmFileStr)) {
     throw new IllegalArgumentException("OSM file cannot be empty.");
   }
   osmFile = osmFileStr;
   return this;
 }
Exemple #3
0
 /**
  * Formats the json content and print it
  *
  * @param json the json content
  */
 @Override
 public void json(String json) {
   if (Helper.isEmpty(json)) {
     d("Empty/Null json content");
     return;
   }
   try {
     json = json.trim();
     if (json.startsWith("{")) {
       JSONObject jsonObject = new JSONObject(json);
       String message = jsonObject.toString(JSON_INDENT);
       d(message);
       return;
     }
     if (json.startsWith("[")) {
       JSONArray jsonArray = new JSONArray(json);
       String message = jsonArray.toString(JSON_INDENT);
       d(message);
       return;
     }
     e("Invalid Json");
   } catch (JSONException e) {
     e("Invalid Json");
   }
 }
Exemple #4
0
 /**
  * Formats the json content and print it
  *
  * @param xml the xml content
  */
 @Override
 public void xml(String xml) {
   if (Helper.isEmpty(xml)) {
     d("Empty/Null xml content");
     return;
   }
   try {
     Source xmlInput = new StreamSource(new StringReader(xml));
     StreamResult xmlOutput = new StreamResult(new StringWriter());
     Transformer transformer = TransformerFactory.newInstance().newTransformer();
     transformer.setOutputProperty(OutputKeys.INDENT, "yes");
     transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
     transformer.transform(xmlInput, xmlOutput);
     d(xmlOutput.getWriter().toString().replaceFirst(">", ">\n"));
   } catch (TransformerException e) {
     e("Invalid xml");
   }
 }
Exemple #5
0
  @Override
  public synchronized void log(int priority, String tag, String message, Throwable throwable) {
    if (settings.getLogLevel() == LogLevel.NONE) {
      return;
    }
    if (throwable != null && message != null) {
      message += " : " + Helper.getStackTraceString(throwable);
    }
    if (throwable != null && message == null) {
      message = Helper.getStackTraceString(throwable);
    }
    if (message == null) {
      message = "No message/exception is set";
    }
    int methodCount = getMethodCount();
    if (Helper.isEmpty(message)) {
      message = "Empty/NULL log message";
    }

    logTopBorder(priority, tag);
    logHeaderContent(priority, tag, methodCount);

    // get bytes of message with system's default charset (which is UTF-8 for Android)
    byte[] bytes = message.getBytes();
    int length = bytes.length;
    if (length <= CHUNK_SIZE) {
      if (methodCount > 0) {
        logDivider(priority, tag);
      }
      logContent(priority, tag, message);
      logBottomBorder(priority, tag);
      return;
    }
    if (methodCount > 0) {
      logDivider(priority, tag);
    }
    for (int i = 0; i < length; i += CHUNK_SIZE) {
      int count = Math.min(length - i, CHUNK_SIZE);
      // create a new String with system's default charset (which is UTF-8 for Android)
      logContent(priority, tag, new String(bytes, i, count));
    }
    logBottomBorder(priority, tag);
  }
Exemple #6
0
  /**
   * Opens or creates a graph. The specified args need a property 'graph' (a folder) and if no such
   * folder exist it'll create a graph from the provided osm file (property 'osm'). A property
   * 'size' is used to preinstantiate a datastructure/graph to avoid over-memory allocation or
   * reallocation (default is 5mio)
   *
   * <p>
   *
   * @param graphHopperFolder is the folder containing graphhopper files (which can be compressed
   *     too)
   */
  @Override
  public boolean load(String graphHopperFolder) {
    if (Helper.isEmpty(graphHopperFolder))
      throw new IllegalStateException("graphHopperLocation is not specified. call init before");

    if (graph != null) throw new IllegalStateException("graph is already loaded");

    if (graphHopperFolder.endsWith("-gh")) {
      // do nothing
    } else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml")) {
      throw new IllegalArgumentException("To import an osm file you need to use importOrLoad");
    } else if (graphHopperFolder.indexOf(".") < 0) {
      if (new File(graphHopperFolder + "-gh").exists()) graphHopperFolder += "-gh";
    } else {
      File compressed = new File(graphHopperFolder + ".ghz");
      if (compressed.exists() && !compressed.isDirectory()) {
        try {
          new Unzipper().unzip(compressed.getAbsolutePath(), graphHopperFolder, removeZipped);
        } catch (IOException ex) {
          throw new RuntimeException(
              "Couldn't extract file " + compressed.getAbsolutePath() + " to " + graphHopperFolder,
              ex);
        }
      }
    }
    setGraphHopperLocation(graphHopperFolder);

    GHDirectory dir = new GHDirectory(ghLocation, dataAccessType);
    if (chUsage) graph = new LevelGraphStorage(dir, encodingManager);
    else graph = new GraphStorage(dir, encodingManager);

    graph.setSegmentSize(defaultSegmentSize);
    if (!graph.loadExisting()) return false;

    postProcessing();
    initIndex();
    return true;
  }
Exemple #7
0
  public GraphHopper init(CmdArgs args) throws IOException {
    if (!Helper.isEmpty(args.get("config", ""))) {
      CmdArgs tmp = CmdArgs.readFromConfig(args.get("config", ""), "graphhopper.config");
      // command line configuration overwrites the ones in the config file
      tmp.merge(args);
      args = tmp;
    }

    String tmpOsmFile = args.get("osmreader.osm", "");
    if (!Helper.isEmpty(tmpOsmFile)) osmFile = tmpOsmFile;

    String graphHopperFolder = args.get("graph.location", "");
    if (Helper.isEmpty(graphHopperFolder) && Helper.isEmpty(ghLocation)) {
      if (Helper.isEmpty(osmFile))
        throw new IllegalArgumentException("You need to specify an OSM file.");

      graphHopperFolder = Helper.pruneFileEnd(osmFile) + "-gh";
    }

    // graph
    setGraphHopperLocation(graphHopperFolder);
    expectedCapacity = args.getLong("graph.expectedCapacity", expectedCapacity);
    defaultSegmentSize = args.getInt("graph.dataaccess.segmentSize", defaultSegmentSize);
    String dataAccess = args.get("graph.dataaccess", "RAM_STORE").toUpperCase();
    if (dataAccess.contains("MMAP")) {
      setMemoryMapped();
    } else {
      if (dataAccess.contains("SAVE") || dataAccess.contains("INMEMORY"))
        throw new IllegalStateException(
            "configuration names for dataAccess changed. Use eg. RAM or RAM_STORE");

      if (dataAccess.contains("RAM_STORE")) setInMemory(true, true);
      else setInMemory(true, false);
    }

    if (dataAccess.contains("SYNC")) dataAccessType = new DAType(dataAccessType, true);

    sortGraph = args.getBool("graph.doSort", sortGraph);
    removeZipped = args.getBool("graph.removeZipped", removeZipped);

    // prepare
    doPrepare = args.getBool("prepare.doPrepare", doPrepare);
    String chShortcuts = args.get("prepare.chShortcuts", "no");
    boolean levelGraph =
        "true".equals(chShortcuts)
            || "fastest".equals(chShortcuts)
            || "shortest".equals(chShortcuts);
    if (levelGraph) setCHShortcuts(true, !"shortest".equals(chShortcuts));

    if (args.has("prepare.updates.periodic"))
      periodicUpdates = args.getInt("prepare.updates.periodic", periodicUpdates);

    if (args.has("prepare.updates.lazy"))
      lazyUpdates = args.getInt("prepare.updates.lazy", lazyUpdates);

    if (args.has("prepare.updates.neighbor"))
      neighborUpdates = args.getInt("prepare.updates.neighbor", neighborUpdates);

    // routing
    defaultAlgorithm = args.get("routing.defaultAlgorithm", defaultAlgorithm);

    // osm import
    wayPointMaxDistance = args.getDouble("osmreader.wayPointMaxDistance", wayPointMaxDistance);
    String type = args.get("osmreader.acceptWay", "CAR");
    encodingManager = new EncodingManager(type);
    workerThreads = args.getInt("osmreader.workerThreads", workerThreads);
    enableInstructions = args.getBool("osmreader.instructions", enableInstructions);

    // index
    preciseIndexResolution = args.getInt("index.highResolution", preciseIndexResolution);
    return this;
  }
Exemple #8
0
 private String formatTag(String tag) {
   if (!Helper.isEmpty(tag) && !Helper.equals(this.tag, tag)) {
     return this.tag + "-" + tag;
   }
   return this.tag;
 }