Exemplo n.º 1
0
  @Test
  public void testSaveModelSettings() throws ConfigurationException, IOException {
    PivotModel model = getPivotModel();
    model.setMdx(getTestQuery());
    model.initialize();

    model.setSorting(true);
    model.setTopBottomCount(3);
    model.setSortCriteria(SortCriteria.BOTTOMCOUNT);

    CellSet cellSet = model.getCellSet();
    CellSetAxis axis = cellSet.getAxes().get(Axis.COLUMNS.axisOrdinal());

    model.sort(axis, axis.getPositions().get(0));

    String mdx = model.getCurrentMdx();

    XMLConfiguration configuration = new XMLConfiguration();
    configuration.setDelimiterParsingDisabled(true);

    model.saveSettings(configuration);

    Logger logger = LoggerFactory.getLogger(getClass());
    if (logger.isDebugEnabled()) {
      StringWriter writer = new StringWriter();
      configuration.save(writer);
      writer.flush();
      writer.close();

      logger.debug("Loading report content :" + System.getProperty("line.separator"));
      logger.debug(writer.getBuffer().toString());
    }

    PivotModel newModel = new PivotModelImpl(getDataSource());
    newModel.restoreSettings(configuration);

    newModel.getCellSet();

    String newMdx = newModel.getCurrentMdx();
    if (newMdx != null) {
      // Currently the parser treats every number as double value.
      // It's inevitable now and does not impact the result.
      newMdx = newMdx.replaceAll("3\\.0", "3");
    }

    assertEquals("MDX has been changed after the state restoration", mdx, newMdx);
    assertTrue(
        "Property 'sorting' has been changed after the state restoration", newModel.isSorting());
    assertEquals(
        "Property 'topBottomCount' has been changed after the state restoration",
        3,
        newModel.getTopBottomCount());
    assertEquals(
        "Property 'sortMode' has been changed after the state restoration",
        SortCriteria.BOTTOMCOUNT,
        newModel.getSortCriteria());
  }
Exemplo n.º 2
0
  public void read(String filename) {
    logger.info("read vrp from file " + filename);
    XMLConfiguration xmlConfig = new XMLConfiguration();
    xmlConfig.setFileName(filename);
    xmlConfig.setAttributeSplittingDisabled(true);
    xmlConfig.setDelimiterParsingDisabled(true);

    if (schemaValidation) {
      final InputStream resource = Resource.getAsInputStream("vrp_xml_schema.xsd");
      if (resource != null) {
        EntityResolver resolver =
            new EntityResolver() {

              @Override
              public InputSource resolveEntity(String publicId, String systemId)
                  throws SAXException, IOException {
                {
                  InputSource is = new InputSource(resource);
                  return is;
                }
              }
            };
        xmlConfig.setEntityResolver(resolver);
        xmlConfig.setSchemaValidation(true);
        logger.info("validating " + filename + " with xsd-schema");
      } else {
        logger.warn(
            "cannot find schema-xsd file (vrp_xml_schema.xsd). try to read xml without xml-file-validation.");
      }
    }
    try {
      xmlConfig.load();
    } catch (ConfigurationException e) {
      logger.error(e);
      e.printStackTrace();
      System.exit(1);
    }
    readProblemType(xmlConfig);
    readVehiclesAndTheirTypes(xmlConfig);

    readShipments(xmlConfig);
    readServices(xmlConfig);

    readInitialRoutes(xmlConfig);
    readSolutions(xmlConfig);

    addJobsAndTheirLocationsToVrp();
  }
  /** 静态读入属性文件到Properties p变量中. */
  protected static void init(String propertyFileName) {
    InputStream in = null;
    try {
      in = ConfigurableConstants.class.getClassLoader().getResourceAsStream(propertyFileName);
      if (in != null) {
        p.load(in);
        String path = p.getProperty("config_file");
        if ("".equals(StringUtils.trimToEmpty(path))) {
          // 引用当前文件
          URL url = ConfigurableConstants.class.getClassLoader().getResource(propertyFileName);
          path = url.getPath();
        }

        File f = new File(path);
        pc = new XMLConfiguration();
        pc.setDelimiterParsingDisabled(true);

        // 注册 修改生新加载
        pc.setReloadingStrategy(new FileChangedReloadingStrategy());

        pc.load(f);
      }
    } catch (IOException e) {
      logger.error("load " + propertyFileName + " into Constants error!");
    } catch (ConfigurationException e) {
      e.printStackTrace();
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          logger.error("close " + propertyFileName + " error!");
        }
      }
    }
  }
Exemplo n.º 4
0
  public void processQueries(String instanceName, String xmlFile) {

    XMLConfiguration config = null;
    /** clear'em out */
    if (collectionQueries.get(instanceName) != null) {
      collectionQueries.get(instanceName).clear();
    }
    if (relationshipQueries.get(instanceName) != null) {
      relationshipQueries.get(instanceName).clear();
    }

    if (jdbcDriver != null) {
      jdbcDriver = null;
    }

    if (validationQuery != null) {
      validationQuery = null;
    }

    if (url != null) {
      url = null;
    }

    try {
      /**
       * use strange ethiopic unicode character so that it never delimits the properties
       *
       * <p>This is a workaround becuase config.setDelimiterParsingDisabled(true) doesn't disable
       * parsing
       */
      AbstractConfiguration.setDefaultListDelimiter('\u12BF');
      config = new XMLConfiguration(xmlFile);
      config.setDelimiterParsingDisabled(true);
      Map<String, String> instanceCollectionQueryMap = new TreeMap<String, String>();
      Map<String, String> instanceRelationshipQueryMap = new TreeMap<String, String>();
      Map<String, String> instanceAttributeQueryMap = new TreeMap<String, String>();

      @SuppressWarnings("unchecked")
      List<HierarchicalConfiguration> params = config.configurationsAt("configuration");
      for (HierarchicalConfiguration param : params) {
        this.jdbcDriver = param.getString("driver");
        this.validationQuery = param.getString("validationquery");
        this.url = param.getString("url");
        this.cycleTimeOffset = param.getInt("cycletimeoffset");
        if (log.isDebugEnabled()) {
          log.debug("jdbcDriver:" + param.getString("driver"));
          log.debug("validationQuery:" + param.getString("validationquery"));
          log.debug("url:" + param.getString("url"));
          log.debug("cycleTimeOffset:" + param.getInt("cycletimeoffset"));
        }
      }

      @SuppressWarnings("unchecked")
      List<HierarchicalConfiguration> queries = config.configurationsAt("queries.query");
      for (HierarchicalConfiguration sub : queries) {
        String name = sub.getString("name");
        String type = sub.getString("type");
        String sql = sub.getString("sql");

        if (type.equalsIgnoreCase("collection")) {
          instanceCollectionQueryMap.put(name, sql);
        } else if (type.equalsIgnoreCase("relationship")) {
          instanceRelationshipQueryMap.put(name, sql);
        } else if (type.equalsIgnoreCase("attribute")) {
          instanceAttributeQueryMap.put(name, sql);
        }
      }

      collectionQueries.put(instanceName, instanceCollectionQueryMap);
      relationshipQueries.put(instanceName, instanceRelationshipQueryMap);
      attributeQueries.put(instanceName, instanceAttributeQueryMap);

    } catch (ConfigurationException e) {
      log.error(e);
    }
  }