Ejemplo n.º 1
0
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated NOT
   */
  @Override
  public EList<HistoricalValues> updateHistoryExtensions(SnapshotCollection snapshotCollection) {
    // long start = System.currentTimeMillis();
    // String typeName = ((EClass)getSEType()).getName();
    EList<HistoricalValues> histValsList = new BasicEList<HistoricalValues>();
    RepositoryRegistry rg = RepositoryRegistry.vmtMANAGER;
    // long start4 = System.currentTimeMillis();
    Set<VMTRootObject> vObj = rg.getInstances(getSEType());
    // logger.info("getting SE instances fo SE type "+typeName +" took:
    // "+(System.currentTimeMillis()-start4));

    EList<EObject> attrList = getAttribute();
    EList<ENamedElement> nes = new BasicEList<ENamedElement>();
    ServiceEntityHistoryExt histExt;

    // long start5 = System.currentTimeMillis();
    for (VMTRootObject obj : vObj) {
      // long start3 = System.currentTimeMillis();
      boolean constraintFailed = false;
      ServiceEntity se = (ServiceEntity) obj;
      // Check constraints + that the se participates in the main market and not a template
      if ((se.getServiceEntityOf() != null)
          && se.getServiceEntityOf().isMainMarket()
          && (se.getTemplateForMarket() == null)) {

        for (Constraint constraint : getMatchingCriteria()) {
          if (!constraint.match(obj)) {
            constraintFailed = true;
            break;
          }
        }
      } else {
        constraintFailed = true;
      }

      // and skip if any of the constraints do not match
      if (constraintFailed) continue;

      histExt =
          (ServiceEntityHistoryExt)
              se.createExtension(ReportingExtensionsPackage.eINSTANCE.getServiceEntityHistoryExt());
      histExt.setSnapshotCollection(snapshotCollection);
      if (scLogger.isTraceEnabled())
        scLogger.trace(String.format("%s: Updating ServiceEntityHistoryExt", se.toVMTString()));

      // Get the attribute value and create/add to the historical values
      if (attrList.size() == 1) {
        EStructuralFeature attribute = (EStructuralFeature) attrList.get(0);

        HistoricalValues vals;
        EList<HistoricalValues> valuesList = histExt.getHistoricalValues(attribute.getName());
        // if the attribute was not added to the extension yet create it
        if (valuesList == null || valuesList.isEmpty()) {
          vals = ReportingFactory.eINSTANCE.createHistoricalValues();
          vals.initValues(snapshotCollection);
          histExt.addHistoricalValues(attribute.getName(), vals);
          if (scLogger.isTraceEnabled())
            scLogger.trace(
                String.format(
                    "%s: Adding historicalValues for attribute %s",
                    se.toVMTString(), attribute.getName()));
        } else {
          // There cannot be more than 1 value for an attribute.
          vals = histExt.getHistoricalValues(attribute.getName()).get(0);
        }

        // Only add a value if the attribute has been set already - otherwise add no value
        if (obj.eIsSet(attribute)) {
          Object attributeValue = obj.eGet(attribute);
          vals.addValue((Float) attributeValue);
          histValsList.add(vals);
          if (scLogger.isTraceEnabled())
            scLogger.trace(
                String.format(
                    "%s: Adding value %f to historicalValues for attribute %s",
                    se.toVMTString(), attributeValue, attribute.getName()));
        } else {
          vals.addValue(HistoricalValuesImpl.NO_VALUE);
          if (scLogger.isTraceEnabled())
            scLogger.trace(
                String.format(
                    "%s: Adding no value to historicalValues for attribute %s",
                    se.toVMTString(), attribute.getName()));
        }
      }
      // Or get the list of attribute values and create/add to historical values for each
      else {
        nes.clear();
        /* previously we assumed a list of size 3 [reference, commodity, attribute], now we assume
         * the last entry is the attribute and the third to last entry is the commodity. */
        EStructuralFeature attribute = (EStructuralFeature) attrList.get(attrList.size() - 1);
        EReference reference = (EReference) attrList.get(attrList.size() - 3);

        // instead of: nes.addAll((Collection<? extends ENamedElement>) attrList.subList(0,
        // attrList.size()-1));
        for (int i = 0; i < attrList.size() - 1; i++) nes.add((ENamedElement) attrList.get(i));

        List<Object> matchedCommodities = obj.getValues(nes);
        for (Object commObj : matchedCommodities) {

          // long start2 = System.currentTimeMillis();

          if (commObj instanceof Commodity) {
            Commodity comm = (Commodity) commObj;

            // if a history extension already exists use it, otherwise create a new one and
            // initialize it.
            if (comm.getHistoryExtension() == null) {
              CommodityHistoryExt commHistExt =
                  (CommodityHistoryExt)
                      comm.createExtension(
                          ReportingExtensionsPackage.eINSTANCE.getCommodityHistoryExt());
              commHistExt.init(snapshotCollection, histExt);
              if (scLogger.isTraceEnabled())
                scLogger.trace(
                    String.format(
                        "%s: Updating CommodityHistoryExt for %s",
                        se.toVMTString(), comm.toVMTString()));
            }

            // update the snapshot collection accumulated used values
            snapshotCollection.updateOverallCommodityUsage(comm);

            try {
              // add the value to the historical ext - insert to the proper historical values
              HistoricalValues vals =
                  comm.getHistoryExtension().getFeatureHistory(attribute, reference);

              // Only add a value for the attributes that have been set already
              if (comm.eIsSet(attribute)) {
                vals.addValue((Float) comm.eGet(attribute));
                histValsList.add(vals);
                if (scLogger.isTraceEnabled())
                  scLogger.trace(
                      String.format(
                          "%s: Adding value %f to historicalValues of commodity %s for attribute %s",
                          se.toVMTString(),
                          comm.eGet(attribute),
                          comm.toVMTString(),
                          attribute.getName()));
              }
              // otherwise - set to NO VALUE
              else {
                vals.addValue(HistoricalValuesImpl.NO_VALUE);
                if (scLogger.isTraceEnabled())
                  scLogger.trace(
                      String.format(
                          "%s: Adding no value to historicalValues of commodity %s for attribute %s",
                          se.toVMTString(), comm.toVMTString(), attribute.getName()));
              }
            } catch (Exception e) {
              logger.error(
                  "Exception trying to update history extension for "
                      + comm.getName()
                      + "::"
                      + attribute.getName(),
                  e);
            }
          } else {
            logger.error(
                "Missconfigured MetaReacord. Penultimate argument should be a Commodity: "
                    + commObj);
          }

          // logger.info("updating a single commodity for SE type "+typeName +" took:
          // "+(System.currentTimeMillis()-start2));
        }
      }
      // logger.info("updating a single entity for SE type "+typeName +" took:
      // "+(System.currentTimeMillis()-start3));
    }
    // logger.info("iterating over all entities of type "+typeName +" took:
    // "+(System.currentTimeMillis()-start5));

    // logger.info("update history extension for SE type "+typeName +" took:
    // "+(System.currentTimeMillis()-start));
    return histValsList;
  }
Ejemplo n.º 2
0
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated NOT
   */
  @Override
  public EList<Record> getRecords() {
    EList<Record> recordList = new BasicEList<Record>();
    RepositoryRegistry rg = RepositoryRegistry.vmtMANAGER;
    Set<VMTRootObject> vObj = rg.getInstances(getSEType());

    EList<EObject> attrList = getAttribute();
    EList<ENamedElement> nes = new BasicEList<ENamedElement>();

    for (VMTRootObject obj : vObj) {
      boolean constraintfailed = false;
      ServiceEntity se = (ServiceEntity) obj;
      // Check constraints + that the se participates in the main market and not a template
      if ((se.getServiceEntityOf() != null)
          && se.getServiceEntityOf().isMainMarket()
          && (se.getTemplateForMarket() == null)) {

        for (Constraint constraint : getMatchingCriteria()) {
          if (!constraint.match(obj)) {
            constraintfailed = true;
            break;
          }
        }
      } else {
        constraintfailed = true;
      }

      // and skip if any of the constraints do not match
      if (constraintfailed) continue;

      // Get the attribute value and create a single record
      if (attrList.size() == 1) {
        EStructuralFeature attribute = (EStructuralFeature) attrList.get(0);
        // Only create a record if the attribute has been set already
        if (obj.eIsSet(attribute)) {
          Object attributeValue = obj.eGet(attribute);
          RecordFromMetaRecord rec = ReportingFactory.eINSTANCE.createRecordFromMetaRecord();
          rec.setObject(obj);
          rec.setFeature(attribute);
          if (trackSize && attributeValue instanceof List<?>) {
            rec.setValueObject(((List<?>) attributeValue).size());
          } else if (trackSize) {
            rec.setValueObject(1);
          } else {
            rec.setValueObject(attributeValue);
          }
          recordList.add(rec);
        }
        // Or get the list of attribute values and create a record for each
      } else {
        nes.clear();
        EReference reference = (EReference) attrList.get(attrList.size() - 3);
        ENamedElement commodity = (ENamedElement) attrList.get(attrList.size() - 2);
        EStructuralFeature attribute = (EStructuralFeature) attrList.get(attrList.size() - 1);

        for (EObject attrib : attrList) {
          nes.add((ENamedElement) attrib);
        }
        // Only creating a record for the attributes that has been set already, otherwise getValues
        // returns no elements in the list
        List<Object> attributeValueList = obj.getValues(nes);
        for (Object attributeValue : attributeValueList) {
          RecordFromMetaRecord2 rec = ReportingFactory.eINSTANCE.createRecordFromMetaRecord2();
          rec.setObject(obj);
          rec.setFeature(attribute);
          rec.setCommodity(commodity);
          rec.setReference(reference);
          if (trackSize && attributeValue instanceof List<?>) {
            rec.setValueObject(((List<?>) attributeValue).size());
          } else if (trackSize) {
            rec.setValueObject(1);
          } else {
            rec.setValueObject(attributeValue);
          }
          recordList.add(rec);
        }
      }
    }
    return recordList;
  }