Commit c96d2bd5 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add 'Dimension' and 'Slot' mechanism

the new dimension and slot abstraction are used to map input (data)
values to configuration values
parent b5b9dddd
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -27,7 +27,9 @@ add_library(plotfxlib STATIC
    elements/legend.cc
    common/config_helpers.cc
    common/data_frame.cc
    common/dimension.cc
    common/domain.cc
    common/document.cc
    common/format.cc
    common/plist/plist.cc
    common/plist/plist_parser.cc
@@ -48,7 +50,6 @@ add_library(plotfxlib STATIC
    common/graphics/png.cc
    common/graphics/font_lookup.cc
    common/element_factory.cc
    common/document.cc
    common/utils/random.cc
    common/utils/csv.cc
    common/utils/bufferutil.cc
+20 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include <graphics/color.h>
#include "utils/return_code.h"
#include "common/data_frame.h"
#include "common/dimension.h"

namespace plotfx {

@@ -85,6 +86,24 @@ ReturnCode parse_classlike(
    const std::string& fn,
    std::vector<std::string>* args);

template <typename T>
struct Slot {
  std::string key;
  std::optional<T> constant;
};

template <typename T>
ReturnCode resolve_slot(
    const Slot<T>& slot,
    std::function<ReturnCode (const DimensionConfig&, const Value&, T*)> map,
    const DimensionMap& dimensions,
    const DataFrame& data,
    size_t data_idx,
    T* val);

template <typename T>
ParserFn configure_slot(DimensionMap* dimensions, Slot<T>* slot);

ParserFn configure_multiprop(const std::vector<ParserFn>& parsers);

ReturnCode configure_measure_rel(
@@ -115,3 +134,4 @@ ReturnCode configure_data_frame(

} // namespace plotfx

#include "config_helpers_impl.h"
+84 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
namespace plotfx {

template <typename T>
ReturnCode resolve_slot(
    const Slot<T>& slot,
    std::function<ReturnCode (const DimensionConfig&, const Value&, T*)> map,
    const DimensionMap& dimensions,
    const DataFrame& data,
    size_t data_idx,
    T* val) {
  if (slot.constant) {
    *val = *slot.constant;
    return OK;
  }

  if (!slot.key.empty()) {
    auto dimension = dimension_find(dimensions, slot.key);
    if (!dimension) {
      return ReturnCode::errorf("EARG", "dimension not found: $0", slot.key);
    }

    const auto& data_val = data_lookup(data, slot.key, data_idx);
    if (auto rc = map(*dimension, data_val, val); !rc) {
      return rc;
    }

    return OK;
  }

  return OK;
}

template <typename T>
ReturnCode configure_slot(
    const plist::Property& prop,
    DimensionMap* dimensions,
    Slot<T>* slot) {
  if (plist::is_value(prop) && prop.value.size() > 0 && prop.value[0] == '$') {
    slot->key = prop.value.substr(1);
    dimension_add(dimensions, slot->key);
  }

  return OK;
}

template <typename T>
ParserFn configure_slot(DimensionMap* dimensions, Slot<T>* slot) {
  return [dimensions, slot] (const plist::Property& prop) {
    return configure_slot(prop, dimensions, slot);
  };
}

} // namespace plotfx
+36 −1
Original line number Diff line number Diff line
@@ -46,10 +46,45 @@ ReturnCode column_find(
  return ReturnCode::errorf("EARG", "column not found: $0", column_name);
}

Value data_lookup(
    const DataFrame& data,
    const std::string& key,
    size_t idx) {
  const DataColumn* column;
  if (auto rc = column_find(data, key, &column); !rc) {
    return {};
  }

  if (idx < column->data.size()) {
    return column->data[idx];
  } else {
    return {};
  }
}

size_t series_len(const Series& s) {
  return s.size();
}

bool series_is_numeric(const Series& s) {
  for (const auto& v : s) {
    if (!StringUtil::isNumber(v)) {
      size_t idx;
      try {
        std::stod(v, &idx);
      } catch (...) {
        return false;
      }

      if (idx != v.size()) {
        return false;
      }
    }
  }

  return true;
}

std::vector<double> series_to_float(const Series& s) {
  std::vector<double> sf;

@@ -83,7 +118,7 @@ std::vector<DataGroup> column_group(const DataColumn& col) {
    g.end = idx;

    while (g.end < col.data.size() && col.data[g.end] == col.data[g.begin]) {
      g.end = idx++;
      g.end = ++idx;
    }

    groups.emplace_back(g);
+7 −0
Original line number Diff line number Diff line
@@ -58,10 +58,17 @@ struct DataGroup {
  size_t end;
};

Value data_lookup(
    const DataFrame& data,
    const std::string& key,
    size_t idx);

std::vector<DataGroup> column_group(const DataColumn& col);

size_t series_len(const Series& s);

bool series_is_numeric(const Series& s);

std::vector<double> series_to_float(const Series& s);

double value_to_float(const Value&);
Loading