Beispiel #1
0
QString XSLTransformer::transform(const QString &xml, QHash<QString, QString> const &params) const
{
    // Read XML data intro an xmlDoc.
    QByteArray xmlData(xml.toUtf8());
    QScopedPointer<xmlDoc, XmlDocDeleter> doc(
        xmlReadMemory(xmlData.constData(), xmlData.size(), 0, 0, 0));

    if (!doc)
        throw std::runtime_error("XSLTransformer::transform: Could not open XML data");

    // Hmpf, data conversions.
    char const **cParams = new char const *[params.size() * 2 + 1];
    int i = 0;
    for (QHash<QString, QString>::const_iterator iter = params.constBegin();
        iter != params.constEnd(); ++iter)
    {
        QByteArray keyData(iter.key().toUtf8());
        QByteArray valueData(iter.value().toUtf8());

        char const *cKey = strdup(keyData.constData());
        char const *cValue = strdup(valueData.constData());

        cParams[i] = cKey;
        cParams[i + 1] = cValue;

        i += 2;
    }

    cParams[params.size() * 2] = 0; // Terminator

    QScopedPointer<xsltTransformContext, XsltTransformContextDeleter> ctx(
        xsltNewTransformContext(d_xslPtr.data(), doc.data()));
    xsltSetCtxtParseOptions(ctx.data(), XSLT_PARSE_OPTIONS);

    // Transform...
    QScopedPointer<xmlDoc, XmlDocDeleter> res(
        xsltApplyStylesheetUser(d_xslPtr.data(), doc.data(), cParams, NULL, NULL, ctx.data()));

    if (!res)
        throw std::runtime_error("XSLTransformer::transform: Could not apply transformation!");
    else if (ctx->state != XSLT_STATE_OK)
        throw std::runtime_error("XSLTransformer::transform: Transformation error, check your query!");

    xmlChar *outputBare = 0;
    int outputLen = -1;
    xsltSaveResultToString(&outputBare, &outputLen, res.data(), d_xslPtr.data());
    QScopedPointer<xmlChar, XmlDeleter> output(outputBare);

    if (!output)
        throw std::runtime_error("Could not apply stylesheet!");

    QString result(QString::fromUtf8(reinterpret_cast<char const *>(output.data())));

    // Deallocate parameter memory
    for (int i = 0; i < params.size() * 2; ++i)
        free(const_cast<char *>(cParams[i]));
    delete[] cParams;

    return result;
}
Beispiel #2
0
    /*static*/ ValuePtr Value::Create(const NDShape& sampleShape, const std::vector<std::vector<ElementType>>& sequences, const DeviceDescriptor& device, bool readOnly/* = false*/)
    {
        size_t sampleSize = sampleShape.TotalSize();
        NDMaskPtr deviceValueMask = CreateMask(sampleSize, sequences, device);
        size_t maxSequenceLength = (deviceValueMask == nullptr) ? sequences[0].size() : deviceValueMask->Shape()[0];

        size_t numSequences = sequences.size();
        NDShape valueDataShape = sampleShape.AppendShape({ maxSequenceLength, numSequences });
        NDArrayViewPtr valueData(new NDArrayView(AsDataType<ElementType>(), valueDataShape, DeviceDescriptor::CPUDevice()), [](ReferenceCount* ptr) { delete ptr; });
        ElementType* dataBuffer = valueData->WritableDataBuffer<ElementType>();
        for (size_t i = 0; i < numSequences; ++i)
            std::copy(sequences[i].data(), sequences[i].data() + sequences[i].size(), dataBuffer + (maxSequenceLength * i * sampleSize));

        NDArrayViewPtr deviceValueData;
        if (device == DeviceDescriptor::CPUDevice())
        {
            if (readOnly)
                deviceValueData = valueData->Alias(true);
            else
                deviceValueData = valueData;
        }
        else
        {
            deviceValueData = NDArrayViewPtr(new NDArrayView(AsDataType<ElementType>(), valueDataShape, device), [](ReferenceCount* ptr) { delete ptr; });
            deviceValueData->CopyFrom(*valueData);
            if (readOnly)
                deviceValueData = deviceValueData->Alias(true);
        }

        return ValuePtr(new Value(deviceValueData, deviceValueMask), [](ReferenceCount* ptr) { delete ptr; });
    }
Beispiel #3
0
TimedShadowVariator circullarStormPathVariator(double factor1, double factor2)
{
    TimedShadowVariator variator = [=](const ShadowStructure& structure, int time)
    {
        // Lift 'Safe' concept to monads.
        SafeShadowStructureAction action;
        // Presentation tip: power of combinatorial pattern.
        // You can combine lambdas and regular functions to achieve simplicity.
        action = safeTimedEvalOverElements(time,
            { { Element::Water,  sinElementVariator(factor1) }
            , { Element::Ground, cosElementVariator(-factor1) }
            , { Element::Sky,    elementVariator(factor2,  variators::SinElementVariator) } // N.B., strategy passed.
            , { Element::Air,    elementVariator(-factor2, variators::CosElementVariator) }
            });

        SafeShadowStructure value = runSafe(action, structure);
        if (isFail(value))
        {
            // TODO - fail tolerance, error reporting
            return structure;
        }

        return valueData(value);
    };
    return variator;
}
// virtual
QVariant CQTaskMethodParametersDM::data(const QModelIndex & index, int role) const
{
  CCopasiParameter * pNode = nodeFromIndex(index);

  if (pNode == NULL) return QVariant();

  switch (index.column())
    {
      case COL_NAME:
        return nameData(pNode, role);
        break;

      case COL_VALUE:
        return valueData(pNode, role);
        break;

      case COL_TYPE:
        return typeData(pNode, role);
        break;
    }

  return QVariant();
}
void ccHistogramWindow::refreshBars()
{
	if (	m_histogram
		&&	m_colorScheme == USE_SF_SCALE
		&&	m_associatedSF
		&&	m_associatedSF->getColorScale())
	{
		int histoSize = static_cast<int>(m_histoValues.size());

		//DGM: the bars will be redrawn only if we delete and recreate the graph?!
		m_histogram->clearData();

		QVector<double> keyData(histoSize);
		QVector<double> valueData(histoSize);
		QVector<QColor> colors(histoSize);

		for (int i=0; i<histoSize; ++i)
		{
			//we take the 'normalized' value at the middle of the class
			double normVal = (static_cast<double>(i)+0.5) / histoSize;

			keyData[i] = m_minVal + normVal * (m_maxVal - m_minVal);
			valueData[i] = m_histoValues[i];

			const ColorCompType* col= m_associatedSF->getColor(static_cast<ScalarType>(keyData[i]));
			if (!col) //hidden values may have no associated color!
				col = ccColor::lightGrey.rgba;
			colors[i] = QColor(col[0],col[1],col[2]);
		}

		m_histogram->setData(keyData, valueData, colors);

		//rescaleAxes();
	}

	replot(QCustomPlot::rpImmediate);
}
void ccHistogramWindow::refresh()
{
	// set ranges appropriate to show data
	double minVal = m_minVal;
	double maxVal = m_maxVal;
	if (m_sfInteractionMode && m_associatedSF)
	{
		double minSat = m_associatedSF->saturationRange().min();
		double maxSat = m_associatedSF->saturationRange().max();
		minVal = std::min(minVal,minSat);
		maxVal = std::max(maxVal,maxSat);
	}
	xAxis->setRange(minVal, maxVal);
	yAxis->setRange(0, m_maxHistoVal);

	if (!m_titleStr.isEmpty())
	{
		// add title layout element
		if (!m_titlePlot)
		{
			//add a row for the title
			plotLayout()->insertRow(0);
		}
		else
		{
			//remove previous title
			plotLayout()->remove(m_titlePlot);
			m_titlePlot = 0;
		}
		m_titlePlot = new QCPPlotTitle(this, QString("%0 [%1 classes]").arg(m_titleStr).arg(m_histoValues.size()));
		//title font
		m_renderingFont.setPointSize(ccGui::Parameters().defaultFontSize);
		m_titlePlot->setFont(m_renderingFont);
		plotLayout()->addElement(0, 0, m_titlePlot);
	}

	//clear previous display
	m_histogram		= 0;
	m_vertBar		= 0;
	m_overlayCurve	= 0;
	m_areaLeft		= 0;
	m_areaRight		= 0;
	m_arrowLeft		= 0;
	m_arrowRight	= 0;
	this->clearGraphs();
	this->clearPlottables();

	if (m_histoValues.empty())
		return;

	//default color scale to be used for display
	ccColorScale::Shared colorScale = (m_colorScale ? m_colorScale : ccColorScalesManager::GetDefaultScale());

	//histogram
	int histoSize = static_cast<int>(m_histoValues.size());
	double totalSum = 0;
	double partialSum = 0;
	if (histoSize > 0)
	{
		m_histogram = new QCPColoredBars(xAxis, yAxis);
		addPlottable(m_histogram);
		// now we can modify properties of myBars:
		m_histogram->setWidth((m_maxVal - m_minVal) / histoSize);
		m_histogram->setAntialiasedFill(false);
		QVector<double> keyData(histoSize);
		QVector<double> valueData(histoSize);

		HISTOGRAM_COLOR_SCHEME colorScheme = m_colorScheme;
		switch(colorScheme)
		{
		case USE_SOLID_COLOR:
			m_histogram->setBrush(QBrush(m_solidColor,Qt::SolidPattern));
			m_histogram->setPen(QPen(m_solidColor));
			break;
		case USE_CUSTOM_COLOR_SCALE:
			//nothing to do
			break;
		case USE_SF_SCALE:
			if (m_associatedSF && m_associatedSF->getColorScale())
			{
				//we use the SF's color scale
				colorScale = m_associatedSF->getColorScale();
			}
			else
			{
				//we'll use the default one...
				assert(false);
				colorScheme = USE_CUSTOM_COLOR_SCALE;
			}
			break;
		default:
			assert(false);
			colorScheme = USE_CUSTOM_COLOR_SCALE;
			break;
		}

		QVector<QColor> colors;
		if (colorScheme != USE_SOLID_COLOR)
		{
			colors.resize(histoSize);
		}

		for (int i=0; i<histoSize; ++i)
		{
			//we take the 'normalized' value at the middle of the class
			double normVal = (static_cast<double>(i)+0.5) / histoSize;

			totalSum += m_histoValues[i];
			if (normVal < m_verticalIndicatorPositionPercent)
				partialSum += m_histoValues[i];

			keyData[i] = m_minVal + normVal * (m_maxVal - m_minVal);
			valueData[i] = m_histoValues[i];

			//import color for the current bin
			if (colorScheme != USE_SOLID_COLOR)
			{
				const ColorCompType* col = 0;
				if (colorScheme == USE_SF_SCALE)
				{
					//equivalent SF value
					assert(m_associatedSF);
					col = m_associatedSF->getColor(static_cast<ScalarType>(keyData[i]));
				}
				else if (colorScheme == USE_CUSTOM_COLOR_SCALE)
				{
					//use default gradient
					assert(colorScale);
					col = colorScale->getColorByRelativePos(normVal);
				}
				if (!col) //hidden values may have no associated color!
					col = ccColor::lightGrey.rgba;
				colors[i] = QColor(col[0],col[1],col[2]);
			}
		}

		if (!colors.isEmpty())
			m_histogram->setData(keyData, valueData, colors);
		else
			m_histogram->setData(keyData, valueData);
	}

	//overlay curve?
	int curveSize = static_cast<int>(m_curveValues.size());
	if (curveSize > 1)
	{
		QVector<double> x(curveSize), y(curveSize);
		
		double step = (m_maxVal - m_minVal) / (curveSize-1);
		for (int i=0; i<curveSize; ++i)
		{
			x[i] = m_minVal + (static_cast<double>(i)/*+0.5*/) * step;
			y[i] = m_curveValues[i];
		}

		// create graph and assign data to it:
		m_overlayCurve = addGraph();
		m_overlayCurve->setData(x, y);
		m_overlayCurve->setName("OverlayCurve");

		//set pen color
		const ccColor::Rgba& col = ccColor::darkGrey;
		QPen pen(QColor(col.r,col.g,col.b));
		m_overlayCurve->setPen(pen);

		//set width
		updateOverlayCurveWidth(rect().width(),rect().height());
	}
	
	//sf interaction mode
	if (m_sfInteractionMode && m_associatedSF)
	{
		const ccScalarField::Range& dispRange = m_associatedSF->displayRange();

		m_areaLeft = new QCPHiddenArea(true,xAxis, yAxis);
		m_areaLeft->setRange(dispRange.min(),dispRange.max());
		m_areaLeft->setCurrentVal(dispRange.start());
		addPlottable(m_areaLeft);

		m_areaRight = new QCPHiddenArea(false,xAxis, yAxis);
		m_areaRight->setRange(dispRange.min(),dispRange.max());
		m_areaRight->setCurrentVal(dispRange.stop());
		addPlottable(m_areaRight);

		const ccScalarField::Range& satRange = m_associatedSF->saturationRange();

		m_arrowLeft = new QCPArrow(xAxis, yAxis);
		m_arrowLeft->setRange(satRange.min(),satRange.max());
		m_arrowLeft->setCurrentVal(satRange.start());
		if (colorScale)
		{
			const ColorCompType* col = colorScale->getColorByRelativePos(m_associatedSF->symmetricalScale() ? 0.5 : 0,m_associatedSF->getColorRampSteps());
			if (col)
				m_arrowLeft->setColor(col[0],col[1],col[2]);
		}
		addPlottable(m_arrowLeft);

		m_arrowRight = new QCPArrow(xAxis, yAxis);
		m_arrowRight->setRange(satRange.min(),satRange.max());
		m_arrowRight->setCurrentVal(satRange.stop());
		if (colorScale)
		{
			const ColorCompType* col = colorScale->getColorByRelativePos(1.0,m_associatedSF->getColorRampSteps());
			if (col)
				m_arrowRight->setColor(col[0],col[1],col[2]);
		}
		addPlottable(m_arrowRight);
	}
	else if (m_drawVerticalIndicator) //vertical hint
	{
		m_vertBar = new QCPBarsWithText(xAxis, yAxis);
		addPlottable(m_vertBar);
		
		// now we can modify properties of vertBar
		m_vertBar->setName("VertLine");
		m_vertBar->setWidth(0/*(m_maxVal - m_minVal) / histoSize*/);
		m_vertBar->setBrush(QBrush(Qt::red));
		m_vertBar->setPen(QPen(Qt::red));
		m_vertBar->setAntialiasedFill(false);
		QVector<double> keyData(1);
		QVector<double> valueData(1);

		//horizontal position
		keyData[0] = m_minVal + (m_maxVal-m_minVal) * m_verticalIndicatorPositionPercent;
		valueData[0] = m_maxHistoVal;

		m_vertBar->setData(keyData,valueData);

		//precision (same as color scale)
		int precision = static_cast<int>(ccGui::Parameters().displayedNumPrecision);
		unsigned bin = static_cast<unsigned>(m_verticalIndicatorPositionPercent * m_histoValues.size());
		QString valueStr = QString("bin %0").arg(bin);
		m_vertBar->setText(valueStr);
		valueStr = QString("< %0 %").arg(100.0*static_cast<double>(partialSum)/static_cast<double>(totalSum),0,'f',3);
		m_vertBar->appendText(valueStr);
		valueStr = QString("val = %0").arg(m_minVal+(m_maxVal-m_minVal)*m_verticalIndicatorPositionPercent,0,'f',precision);
		m_vertBar->appendText(valueStr);
		m_vertBar->setTextAlignment(m_verticalIndicatorPositionPercent > 0.5);
	}

	//rescaleAxes();

	// redraw
	replot();
}