Esempio n. 1
0
  private static List<Node> getAllClasses(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> finalClasses = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Class"))
        .forEach(a -> finalClasses.add(a));
    tx.success();
    tx.close();

    return finalClasses.stream().map(a -> a).collect(Collectors.toList());
  }
Esempio n. 2
0
  public List<RoadLink> getRoadLinks(Point2D pos, boolean isGrid) {
    int row =
        (int)
            ((pos.getX() - fcdSettings.getMap().getxOffset())
                / fcdSettings.getMap().getGridLength());
    int col =
        (int)
            ((pos.getY() - fcdSettings.getMap().getyOffset())
                / fcdSettings.getMap().getGridLength());

    Set<String> links = new HashSet();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        String grid = row + "+" + col;
        Optional<MapGrid> mapGrid = Optional.fromNullable(gridNet.get(grid));
        if (mapGrid.isPresent()) {
          links.addAll(mapGrid.get().union());
        }
      }
    }

    //        logger.debug("---->{}", links);

    List<RoadLink> roads = new ArrayList();

    for (String linkId : links) {
      //            logger.debug("=>{}", linkId);

      Optional<RoadLink> roadLink = Optional.fromNullable(roadNet.get(Integer.valueOf(linkId)));
      if (roadLink.isPresent()) {
        RoadLink link = roadLink.get();
        if (link.xmin < pos.getX() + 50
            && link.ymin < pos.getY() + 50
            && link.xmax > pos.getX() - 50
            && link.ymax > pos.getY() - 50) {
          roads.add(link);
        }
      }
    }

    return roads;
  }
Esempio n. 3
0
  private static List<Integer> getFeatureIndexList(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> patterns = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Pattern"))
        .forEach(a -> patterns.add(a));

    Collections.sort(
        patterns,
        (a, b) ->
            ((Integer) b.getProperty("threshold"))
                .compareTo(((Integer) a.getProperty("threshold"))));

    List<Integer> patternIds =
        patterns.stream().map(a -> ((Long) a.getId()).intValue()).collect(Collectors.toList());

    tx.success();
    tx.close();

    return patternIds;
  }
Esempio n. 4
0
    private Object toJsonCompatible(Object value) {
      if (value instanceof Node) {
        final Node node = (Node) value;
        final Map<String, Object> result = SubGraph.toMap((PropertyContainer) node);
        result.put("_id", node.getId());

        final List<String> labelNames = SubGraph.getLabelNames(node);
        if (!labelNames.isEmpty()) result.put("_labels", labelNames);
        return result;
      }
      if (value instanceof Relationship) {
        final Relationship relationship = (Relationship) value;
        final Map<String, Object> result = SubGraph.toMap((PropertyContainer) relationship);
        result.put("_id", relationship.getId());
        result.put("_start", relationship.getStartNode().getId());
        result.put("_end", relationship.getEndNode().getId());
        result.put("_type", relationship.getType().name());
        return result;
      }
      if (value instanceof Map) {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) value;
        final Map<String, Object> result = new LinkedHashMap<>(map.size());
        for (Map.Entry<String, Object> entry : map.entrySet()) {
          result.put(entry.getKey(), toJsonCompatible(entry.getValue()));
        }
        return result;
      }
      if (value instanceof Iterable) {
        final List<Object> result = new ArrayList<>();
        for (Object inner : (Iterable) value) {
          result.add(toJsonCompatible(inner));
        }
        return result;
      }
      return value;
    }
Esempio n. 5
0
  public List<RoadLink> loadToDatabase(String mif, String mid, boolean saved) throws IOException {
    CSVReader reader = new CSVReader(new FileReader(mid), ',', '"', 0);
    String[] row;
    List<RoadLink> links = new ArrayList();

    while ((row = reader.readNext()) != null) {

      RoadLink link = new RoadLink();
      link.no = Integer.valueOf(row[0]);
      link.fromNode = Integer.valueOf(row[1]);
      link.toNode = Integer.valueOf(row[2]);
      link.name = row[3];
      link.length = Double.valueOf(row[4]);
      link.ft = row[5];
      link.tf = row[6];
      link.combine = row[7];
      link.overhead = row[8];
      link.xmin = Double.valueOf(row[9]);
      link.ymin = Double.valueOf(row[10]);
      link.xmax = Double.valueOf(row[11]);
      link.ymax = Double.valueOf(row[12]);

      links.add(link);

      //            logger.debug("{}",link);
    }

    Charset charset = Charset.forName("UTF-8");
    BufferedReader mifReader = Files.newBufferedReader(Paths.get(mif), charset);
    String line;
    int index = -1;
    int pos = -1;
    int segs = -1;
    Point2D prePoint = null;

    while ((line = mifReader.readLine()) != null) {
      List<String> cols =
          Lists.newArrayList(
              Splitter.on(CharMatcher.BREAKING_WHITESPACE)
                  .trimResults()
                  .omitEmptyStrings()
                  .split(line));

      //            logger.debug("{}", line);

      if (cols.get(0).compareTo("PLINE") == 0) {
        index++;
        segs = Integer.valueOf(cols.get(1));
        pos = 0;
        prePoint = null;
      } else if (cols.get(0).compareTo("LINE") == 0) {
        index++;
        RoadLink link = links.get(index);

        Segment seg = new Segment();
        seg.start = new Point2D.Double(Double.valueOf(cols.get(1)), Double.valueOf(cols.get(2)));
        seg.end = new Point2D.Double(Double.valueOf(cols.get(3)), Double.valueOf(cols.get(4)));
        seg.length = seg.start.distance(seg.end);

        link.segments.add(seg);
      } else if (index >= 0) {
        if (prePoint != null) {
          prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1)));
        } else {
          Segment seg = new Segment();
          seg.start = prePoint;
          prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1)));
          seg.end = prePoint;
          seg.length = seg.start.distance(seg.end);

          RoadLink link = links.get(index);
          link.segments.add(seg);
        }
      }
    }

    if (saved) {
      for (final RoadLink link : links) {
        logger.debug("---->{}", link);
        jdbcTemplate.update(
            sql1,
            link.no,
            link.fromNode,
            link.toNode,
            link.name,
            link.length,
            link.ft,
            link.tf,
            link.combine,
            link.overhead,
            link.xmin,
            link.ymin,
            link.xmax,
            link.ymax);

        //                jdbcTemplate.batchUpdate(sql2,
        //                        new BatchPreparedStatementSetter() {
        //                            @Override
        //                            public int getBatchSize() {
        //                                return link.segments.size();
        //                            }
        //
        //                            @Override
        //                            public void setValues(PreparedStatement ps, int i)
        //                                    throws SQLException {
        //                                Point2D segment = link.segments.get(i);
        //                                ps.setInt(1, link.no);
        //                                ps.setInt(2, i);
        //                                ps.setDouble(3, segment.getX());
        //                                ps.setDouble(4, segment.getY());
        //                            }
        //                        });
      }
    }

    //        logger.debug("=>{},{}", links.size(), index);

    return links;
  }
Esempio n. 6
0
  public List<RoadLink> loadToDatabase(String mif, String mid) throws IOException {
    logger.debug("loading {}...", mid);

    CSVReader reader = new CSVReader(new FileReader(mid), ',', '"', 0);
    String[] row;
    List<RoadLink> links = new ArrayList();

    while ((row = reader.readNext()) != null) {
      RoadLink link = new RoadLink();
      link.no = Integer.valueOf(row[0]);
      link.fromNode = Integer.valueOf(row[1]);
      link.toNode = Integer.valueOf(row[2]);
      link.name = row[3];
      link.length = Double.valueOf(row[4]);
      link.ft = row[5];
      link.tf = row[6];
      link.combine = row[7];
      link.overhead = row[8];
      link.xmin = Double.valueOf(row[9]);
      link.ymin = Double.valueOf(row[10]);
      link.xmax = Double.valueOf(row[11]);
      link.ymax = Double.valueOf(row[12]);
      links.add(link);

      roadNet.put(link.no, link);
    }

    for (final RoadLink link : links) {
      jdbcTemplate.update(
          sql1,
          link.no,
          link.fromNode,
          link.toNode,
          link.name,
          link.length,
          link.ft,
          link.tf,
          link.combine,
          link.overhead,
          link.xmin,
          link.ymin,
          link.xmax,
          link.ymax);
    }

    Charset charset = Charset.forName("UTF-8");
    BufferedReader mifReader = Files.newBufferedReader(Paths.get(mif), charset);
    String line;
    int index = -1;
    int pos = -1;
    int segs = -1;
    Point2D prePoint = null;

    while ((line = mifReader.readLine()) != null) {
      List<String> cols =
          Lists.newArrayList(
              Splitter.on(CharMatcher.BREAKING_WHITESPACE)
                  .trimResults()
                  .omitEmptyStrings()
                  .split(line));

      //            logger.debug("{}", line);

      if (cols.get(0).compareTo("PLINE") == 0) {
        index++;
        segs = Integer.valueOf(cols.get(1));
        pos = 0;
        prePoint = null;
      } else if (cols.get(0).compareTo("LINE") == 0) {
        index++;
        RoadLink link = links.get(index);

        //                link.segments.add(new Point2D.Double(Double.valueOf(cols.get(1)),
        // Double.valueOf(cols.get(2))));
        //                link.segments.add(new Point2D.Double(Double.valueOf(cols.get(3)),
        // Double.valueOf(cols.get(4))));

        Segment seg = new Segment();
        seg.start = new Point2D.Double(Double.valueOf(cols.get(1)), Double.valueOf(cols.get(2)));
        seg.end = new Point2D.Double(Double.valueOf(cols.get(3)), Double.valueOf(cols.get(4)));
        seg.length = seg.start.distance(seg.end);

        link.segments.add(seg);

      } else if (index >= 0) {

        if (prePoint == null) {
          prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1)));
        } else {
          Segment seg = new Segment();
          seg.start = prePoint;
          prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1)));
          seg.end = prePoint;
          seg.length = seg.start.distance(seg.end);

          RoadLink link = links.get(index);
          link.segments.add(seg);
        }

        //                RoadLink link = links.get(index);
        //                link.segments.add(new Point2D.Double(Double.valueOf(cols.get(0)),
        // Double.valueOf(cols.get(1))));
      }
    }

    return links;
  }
Esempio n. 7
0
  public static Map<String, Object> getCosineSimilarityVector(GraphDatabaseService db) {
    Map<String, List<LinkedHashMap<String, Object>>> documents = getFeaturesForAllClasses(db);
    Map<String, List<LinkedHashMap<String, Object>>> results = new HashMap<>();
    List<Integer> featureIndexList = getFeatureIndexList(db);

    List<String> documentList = documents.keySet().stream().collect(Collectors.toList());

    Collections.sort(documentList, (a, b) -> a.compareToIgnoreCase(b));

    for (String key : documentList) {
      List<LinkedHashMap<String, Object>> resultList = new ArrayList<>();
      LinkedHashMap<String, Double> classMap = new LinkedHashMap<>();

      List<Double> v1 =
          featureIndexList
              .stream()
              .map(i -> documents.get(key).contains(i) ? featureIndexList.indexOf(i) : 0.0)
              .collect(Collectors.toList());
      documents
          .keySet()
          .stream()
          .forEach(
              otherKey -> {
                List<Double> v2 =
                    featureIndexList
                        .stream()
                        .map(
                            i ->
                                documents.get(otherKey).contains(i)
                                    ? featureIndexList.indexOf(i)
                                    : 0.0)
                        .collect(Collectors.toList());
                classMap.put(otherKey, cosineSimilarity(v1, v2));
              });

      final List<LinkedHashMap<String, Object>> finalResultList = resultList;
      classMap
          .keySet()
          .forEach(
              ks -> {
                LinkedHashMap<String, Object> localMap = new LinkedHashMap<>();
                localMap.put("class", ks);
                localMap.put("similarity", classMap.get(ks));
                finalResultList.add(localMap);
              });

      Collections.sort(
          finalResultList,
          (a, b) -> ((String) a.get("class")).compareToIgnoreCase((String) b.get("class")));
      results.put(key, finalResultList);
    }

    List<LinkedHashMap<String, Object>> similarityVector = new ArrayList<>();

    for (String key : results.keySet()) {
      List<Double> cosineVector;
      cosineVector =
          results
              .get(key)
              .stream()
              .map(a -> Convert.toDouble(Math.round(100000 * (Double) a.get("similarity"))))
              .collect(Collectors.toList());
      LinkedHashMap<String, Object> row = new LinkedHashMap<>();
      row.put("class", key);
      row.put("vector", cosineVector);
      similarityVector.add(row);
    }

    Collections.sort(
        similarityVector,
        (a, b) -> ((String) a.get("class")).compareToIgnoreCase((String) b.get("class")));

    Map<String, Object> vectorMap = new LinkedHashMap<>();

    List<ArrayList<Double>> vectors = new ArrayList<>();
    List<String> classNames = new ArrayList<>();

    for (LinkedHashMap<String, Object> val : similarityVector) {
      vectors.add((ArrayList<Double>) val.get("vector"));
      classNames.add((String) val.get("class"));
    }

    vectorMap.put("classes", classNames);
    vectorMap.put("vectors", vectors);

    return vectorMap;
  }