void
GLPolyFitDialog::Receive
	(
	JBroadcaster* 	sender, 
	const Message&	message
	)
{
	if (sender == itsHelpButton && message.Is(JXButton::kPushed))
		{
		}
	else if (message.Is(JXCheckbox::kPushed))
		{
		JString fStr;
		JBoolean started	= kJFalse;
		const JSize count	= 10;
		for (JIndex i = 1; i <= count; i++)
			{
			if (itsCB[i-1]->IsChecked())
				{
				if (started)
					{
					fStr += " + ";
					}
				else
					{
					started	= kJTrue;
					}
				JString parm	= "a" + JString(i - 1, JString::kBase10);
				JString xTerm	= " * x";
				if (i > 2)
					{
					xTerm += "^" + JString(i - 1, JString::kBase10);
					}
				fStr += parm;
				if (i > 1)
					{
					fStr += xTerm;
					}
				}
			}
		if (fStr.IsEmpty())
			{
			itsFn->Hide();
			}
		else
			{
			itsFn->Show();
			JFunction* f;
			if (JParseFunction(fStr, itsVarList, &f))
				{
				itsFn->SetFunction(itsVarList, f);
				}
			}
		}
	else
		{
		JXDialogDirector::Receive(sender, message);
		}
}
JFunction*
JFunction::Copy
	(
	const JVariableList* newVariableList
	)
	const
{
	const JString expr = Print();
	JFunction* theFunction;
	const JBoolean ok = JParseFunction(expr, newVariableList, &theFunction);
	assert( ok );
	return theFunction;
}
SCCircuitVarList::SCCircuitVarList()
	:
	JVariableList()
{
	SCCircuitVarListX();

	// we always need the frequency variables f,w,s

	JBoolean ok = AddVariable("f", 0.0, kJTrue);
	assert( ok );

	JFunction* f;
	ok = JParseFunction("2*pi*f", this, &f);
	assert( ok );
	ok = AddFunction("`w", *f, kJFalse);
	assert( ok );
	delete f;

	ok = JParseFunction("j*`w", this, &f);
	assert( ok );
	ok = AddFunction("s", *f, kJFalse);
	assert( ok );
	delete f;
}
JBoolean
GXTransformFunctionDialog::OKToDeactivate()
{
	if (Cancelled())
		{
		return kJTrue;
		}
	JFunction* f = NULL;
	if (JParseFunction(itsFunctionString->GetText(), itsList, &f))
		{
		jdelete f;
		return kJTrue;
		}
	return kJFalse;
}
JFunction*
JFunction::StreamIn
	(
	istream&				input,
	const JVariableList*	theVariableList,
	const JBoolean			allowUIF
	)
{
	JString expr;
	input >> expr;

	JFunction* theFunction;
	const JBoolean ok =
		JParseFunction(expr, theVariableList, &theFunction, allowUIF);
	assert( ok );
	return theFunction;
}
void
SCFeedbackDirector::HandleExtraMenu
(
    const JIndex item
)
{
    if (item != itsBuildH0Index && item != itsBuildHinfIndex)
    {
        return;
    }

    JString s;
    if (item == itsBuildH0Index)
    {
        s  = "(";
        s += (itsH0->GetFunction())->Print();
        s += ") * ((1 + (";
        s += (itsTn->GetFunction())->Print();
        s += "))/(1 + (";
        s += (itsT->GetFunction())->Print();
        s += ")))";
    }
    else
    {
        assert( item == itsBuildHinfIndex );

        s  = "(";
        s += (itsHinf->GetFunction())->Print();
        s += ") * ((1 + (1/(";
        s += (itsTn->GetFunction())->Print();
        s += ")))/(1 + (1/(";
        s += (itsT->GetFunction())->Print();
        s += "))))";
    }

    const SCCircuitVarList* varList = GetVarList();
    JFunction* f;
    const JBoolean ok = JParseFunction(s, varList, &f, kJTrue);
    assert( ok );
    itsScratchFn->SetFunction(varList, f);
}
void
SCXferFnDirector::EvaluateTransferFunction()
{
	if (!itsXferFn->EndEditing())
		{
		return;
		}
	else if (itsXferFn->ContainsUIF())
		{
		(JGetUserNotification())->ReportError("Please finish entering the function.");
		itsXferFn->Focus();
		return;
		}

	(GetCircuitDocument())->DataModified();

	itsResult->ClearFunction();

	const SCCircuit* circuit = GetCircuit();
	if (!circuit->IsLinear())
		{
		(JGetUserNotification())->ReportError("The circuit is not linear.");
		return;
		}

	const SCCircuitVarList* varList = GetVarList();

	const JFunction* fn  = itsXferFn->GetFunction();
	const JString textFn = (SCGetSymbolicMath())->Print(*fn);
	JString result;
	JFunction* f;
	if (circuit->Evaluate(textFn, &result) &&
		JParseFunction(result, varList, &f))
		{
		itsResult->SetFunction(varList, f);
		}
}
SCCircuitVarList::SCCircuitVarList
	(
	istream&			input,
	const JFileVersion	vers
	)
	:
	JVariableList()
{
JIndex i;

	SCCircuitVarListX();

	JSize varCount;
	input >> varCount;

	JArray<JIndex> misfitIndexList;
	JPtrArray<JString> misfitFnList(JPtrArrayT::kDeleteAll);
	for (i=1; i<=varCount; i++)
		{
		VarInfo info;

		info.name = new JString;
		assert( info.name != NULL );
		input >> *(info.name);

		input >> info.value;

		JBoolean isFunction;
		input >> isFunction;
		if (isFunction)
			{
			JString* fStr = new JString;
			assert( fStr != NULL );
			input >> *fStr;
			(JGetUserNotification())->SetSilent(kJTrue);	// complain the second time
			if (JParseFunction(*fStr, this, &(info.f)))
				{
				delete fStr;
				}
			else
				{
				info.f = NULL;
				misfitIndexList.AppendElement(i);
				misfitFnList.Append(fStr);
				}
			(JGetUserNotification())->SetSilent(kJFalse);
			}

		input >> info.visible;

		itsVars->AppendElement(info);
		}

	const JSize misfitCount = misfitIndexList.GetElementCount();
	for (i=1; i<=misfitCount; i++)
		{
		const JIndex j      = misfitIndexList.GetElement(i);
		VarInfo info        = itsVars->GetElement(j);
		const JString* fStr = misfitFnList.NthElement(i);
		if (JParseFunction(*fStr, this, &(info.f)))
			{
			itsVars->SetElement(j, info);
			}
		}
}
void
GLFitDirector::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsFitMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandleFitMenu(selection->GetIndex());
		}
	else if (sender == itsFitMenu && message.Is(JXMenu::kNeedsUpdate))
		{
		UpdateFitMenu();
		}
	else if (sender == itsPrefsMenu && message.Is(JXMenu::kItemSelected))
		{
		 const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandlePrefsMenu(selection->GetIndex());
		}
	else if (sender == itsHelpMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandleHelpMenu(selection->GetIndex());
		}
	else if (sender == itsCurveList && message.Is(GLCurveNameList::kCurveSelected))
		{
		const GLCurveNameList::CurveSelected* info = 
			dynamic_cast<const GLCurveNameList::CurveSelected*>(&message);
		assert(info != NULL);
		JPlotDataBase& curve = itsPlot->GetCurve(info->GetIndex());

		RemoveFit();
		RemoveCurves();

		itsParameterTable->ClearValues();
		itsChiSq->SetText("");

		// add new curve.
		itsFitPlot->AddCurve(&curve, kJFalse, itsPlot->GetCurveName(info->GetIndex()));
		itsFitPlot->ProtectCurve(1, kJTrue);
		}
	else if (sender == itsFitList && message.Is(GLFitDescriptionList::kFitSelected))
		{
		const GLFitDescriptionList::FitSelected* info = 
			dynamic_cast<const GLFitDescriptionList::FitSelected*>(&message);
		assert(info != NULL);
		const GLFitDescription& fd	= GetFitManager()->GetFitDescription(info->GetIndex());
		itsParameterTable->SetFitDescription(fd);
		RemoveFit();
		itsChiSq->SetText("");
		JFunction* f;
		if (JParseFunction(fd.GetFitFunctionString(), const_cast<GLFitDescription&>(fd).GetVarList(), &f))
			{
			itsExprWidget->SetFunction(const_cast<GLFitDescription&>(fd).GetVarList(), f);
			itsExprWidget->Show();
			}		
		}
	else if (sender == itsFitList && message.Is(GLFitDescriptionList::kFitInitiated))
		{
		const GLFitDescriptionList::FitInitiated* info = 
			dynamic_cast<const GLFitDescriptionList::FitInitiated*>(&message);
		assert(info != NULL);
		const GLFitDescription& fd	= GetFitManager()->GetFitDescription(info->GetIndex());
		if (!itsParameterTable->BeginEditingStartValues())
			{
			Fit();
			AddHistoryText();
			}
		}
	else if (sender == itsParameterTable && message.Is(GLFitParameterTable::kValueChanged))
		{
		const GLFitParameterTable::ValueChanged* info =
			dynamic_cast<const GLFitParameterTable::ValueChanged*>(&message);
		assert(info != NULL);
		JIndex index;
		JBoolean ok	= itsFitList->GetCurrentFitIndex(&index);
		assert(ok);
		GLFitDescription& fd	= GetFitManager()->GetFitDescription(index);
		const JArray<JFloat>& parms	= itsParameterTable->GetStartValues();
		const JSize count	= parms.GetElementCount();
		for (JIndex i = 1; i <= count; i++)
			{
			fd.GetVarList()->SetValue(i + 1, parms.GetElement(i));
			}
		GetWindow()->Refresh();
		}
	else if (sender == itsNLFitDialog && message.Is(JXDialogDirector::kDeactivated))
		{
		const JXDialogDirector::Deactivated* info =
			dynamic_cast<const JXDialogDirector::Deactivated*>(&message);
		assert(info != NULL);
		if (info->Successful())
			{
			GLNonLinearFitDescription* fit	= 
				new GLNonLinearFitDescription(itsNLFitDialog->GetFitName(), 
					itsNLFitDialog->GetFunctionString(), 
					itsNLFitDialog->GetDerivativeString(),
					itsNLFitDialog->GetVarList().GetVariables());
			assert(fit != NULL);
			GetFitManager()->AddFitDescription(fit);
			}
		itsNLFitDialog	= NULL;	
		}
	else if (sender == itsPolyFitDialog && message.Is(JXDialogDirector::kDeactivated))
		{
		const JXDialogDirector::Deactivated* info =
			dynamic_cast<const JXDialogDirector::Deactivated*>(&message);
		assert(info != NULL);
		if (info->Successful())
			{
			JArray<JIndex> powers;
			itsPolyFitDialog->GetPowers(&powers);
			GLPolyFitDescription* fit	= 
				new GLPolyFitDescription(itsPolyFitDialog->GetFitName(), 
					powers);
			assert(fit != NULL);
			GetFitManager()->AddFitDescription(fit);
			}
		itsPolyFitDialog	= NULL;	
		}
	else if (sender == itsPrinter &&
			 message.Is(JPrinter::kPrintSetupFinished))
		{
		const JPrinter::PrintSetupFinished* info =
			dynamic_cast<const JPrinter::PrintSetupFinished*>(&message);
		assert(info != NULL);
		if (info->Successful())
			{
			Print();
			}
		}
	else
		{
		JXWindowDirector::Receive(sender, message);
		}
}
Exemplo n.º 10
0
void
PlotDir::Receive
	(
	JBroadcaster* sender,
	const JBroadcaster::Message& message
	)
{
	if (sender == itsPlot && message.Is(J2DPlotWidget::kTitleChanged))
		{
		JString title = itsFileName + ":  " + itsPlot->GetTitle();
		GetWindow()->SetTitle(title);
		JString sessiontitle = "Glove session  -  " + title;
		(itsSessionDir->GetWindow())->SetTitle(sessiontitle);
		}

	else if (sender == itsPlot && message.Is(J2DPlotWidget::kPlotChanged))
		{
		itsSupervisor->DataModified();
		}

	else if (sender == itsSessionDir && message.Is(GloveHistoryDir::kSessionChanged))
		{
		itsSupervisor->DataModified();
		}

	else if (sender == itsPlot && message.Is(J2DPlotWidget::kIsEmpty))
		{
		if (!itsPlotIsClosing)
			{
			GLClosePlotDirTask* task = jnew GLClosePlotDirTask(this);
			assert(task != NULL);
			task->Go();
			}
		}

	else if (sender == itsPlot && message.Is(J2DPlotWidget::kCurveAdded))
		{
		HandleCurveAdded();
		}

	else if (sender == itsPlot && message.Is(J2DPlotWidget::kCurveRemoved))
		{
		const J2DPlotWidget::CurveRemoved* info =
			dynamic_cast<const J2DPlotWidget::CurveRemoved*>(&message);
		assert( info != NULL );
		HandleCurveRemoved(info->GetIndex());
		}

	else if (sender == itsPlotMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandlePlotMenu(selection->GetIndex());
		}
	else if (sender == itsAnalysisMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandleAnalysisMenu(selection->GetIndex());
		}
	else if (sender == itsHelpMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandleHelpMenu(selection->GetIndex());
		}

	else if (sender == itsFits &&
		( 	message.Is(JOrderedSetT::kElementsInserted) ||
			message.Is(JOrderedSetT::kElementsRemoved)))
		{
		UpdateFitParmsMenu();
		}

	else if (sender == itsDiffDirs &&
		( 	message.Is(JOrderedSetT::kElementsInserted) ||
			message.Is(JOrderedSetT::kElementsRemoved)))
		{
		UpdateDiffMenu();
		}


	else if (sender == itsFitParmsMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		itsFitParmsDir->Activate();
		itsFitParmsDir->ShowFit(selection->GetIndex());
		}

	else if (sender == itsDiffMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		PlotDir* dir = itsDiffDirs->NthElement(selection->GetIndex());
		dir->Activate();
//		(itsDiffDirs->NthElement(selection->GetIndex()))->Activate();
		}


	else if (sender == itsFunctionDialog && message.Is(JXDialogDirector::kDeactivated))
		{
		const JXDialogDirector::Deactivated* info =
			dynamic_cast<const JXDialogDirector::Deactivated*>(&message);
		assert( info != NULL );
		if (info->Successful())
			{
			JString fnStr = itsFunctionDialog->GetFunctionString();
			JFunction* f;
			if (JParseFunction(fnStr, itsVarList, &f))
				{
				PlotFunction(f);
				}
			}
		itsFunctionDialog = NULL;
		}

	else if (sender == itsFitModuleDialog && message.Is(JXDialogDirector::kDeactivated))
		{
		const JXDialogDirector::Deactivated* info =
			dynamic_cast<const JXDialogDirector::Deactivated*>(&message);
		assert( info != NULL );
		if (info->Successful())
			{
			JIndex index = itsFitModuleDialog->GetFilterIndex();
			JPlotDataBase* data = &(itsPlot->GetCurve(itsCurveForFit));
			FitModule* fm;
			JString modName;
			(GLGetApplication())->GetFitModulePath(index, &modName);
			JBoolean success = FitModule::Create(&fm, this, data, modName);
			if (!success)
				{
				JGetUserNotification()->ReportError("Unknown module error.");
				}
			}
		itsFitModuleDialog = NULL;
		}

	else if (sender == itsPrinter &&
			 message.Is(JPrinter::kPageSetupFinished))
		{
		const JPrinter::PageSetupFinished* info =
			dynamic_cast<const JPrinter::PageSetupFinished*>(&message);
		assert(info != NULL);
		if (info->Changed())
			{
			itsSupervisor->DataModified();
			// for WYSISYG: adjust display to match new paper size
			}
		}

	else if (sender == itsPrinter &&
			 message.Is(JPrinter::kPrintSetupFinished))
		{
		const JPrinter::PrintSetupFinished* info =
			dynamic_cast<const JPrinter::PrintSetupFinished*>(&message);
		assert(info != NULL);
		if (info->Successful())
			{
			itsPlot->Print(*itsPrinter);
			if (itsIsPrintAll)
				{
				itsFitParmsDir->SendAllToSession();
				itsSessionDir->Print();
				}
			}
		}
	else
		{
		JXWindowDirector::Receive(sender, message);
		}
}
void
SCFeedbackDirector::EvaluateFeedbackParameters()
{
    if (!itsOutputFn->EndEditing())
    {
        return;
    }
    else if (itsOutputFn->ContainsUIF())
    {
        (JGetUserNotification())->ReportError("Please finish entering the output function.");
        itsOutputFn->Focus();
        return;
    }

    (GetCircuitDocument())->DataModified();

    itsH0->ClearFunction();
    itsHinf->ClearFunction();
    itsT->ClearFunction();
    itsTn->ClearFunction();
    itsScratchFn->ClearFunction();

    const SCCircuit* circuit = GetCircuit();
    if (!circuit->IsLinear())
    {
        (JGetUserNotification())->ReportError("The circuit is not linear.");
        return;
    }

    JIndex indepSourceIndex;
    if (!itsInputSource->GetCompIndex(&indepSourceIndex))
    {
        (JGetUserNotification())->ReportError(
            "There are no independent sources in this circuit.");
        return;
    }

    JIndex depSourceIndex;
    if (!itsDepSource->GetCompIndex(&depSourceIndex))
    {
        (JGetUserNotification())->ReportError(
            "There are no dependent sources in this circuit.");
        return;
    }

    const SCCircuitVarList* varList = GetVarList();

    const JFunction* fn  = itsOutputFn->GetFunction();
    const JString textFn = (SCGetSymbolicMath())->Print(*fn);
    JString H0, Hinf, T, Tn;
    if (!circuit->GetFeedbackParameters(indepSourceIndex, textFn, depSourceIndex,
                                        &H0, &Hinf, &T, &Tn))
    {
        return;
    }

    JFunction* f;
    if (JParseFunction(H0, varList, &f))
    {
        itsH0->SetFunction(varList, f);
    }
    if (JParseFunction(Hinf, varList, &f))
    {
        itsHinf->SetFunction(varList, f);
    }
    if (JParseFunction(T, varList, &f))
    {
        itsT->SetFunction(varList, f);
    }
    if (JParseFunction(Tn, varList, &f))
    {
        itsTn->SetFunction(varList, f);
    }
}
JPlotFitProxy::JPlotFitProxy
	(
	J2DPlotWidget* 		plot, 
	JPlotDataBase* 		fitData,
	istream& 			is
	)
	:
	JPlotFitFunction(plot, fitData, 0, 0),
	itsErrors(NULL)
{
	is >> itsHasGOF;
	if (itsHasGOF)
		{
		is >> itsGOFName;
		is >> itsGOF;
		}

	itsParms	= new GVarList();
	assert(itsParms != NULL);
	itsParms->AddVariable("x", 0);

	JSize count;
	is >> count;
	for (JIndex i = 1; i <= count; i++)
		{
		JString name;
		is >> name;
		JFloat value;
		is >> value;
		itsParms->AddVariable(name, value);
		}

	SetParameterCount(count);
		
	JBoolean hasParameterErrors;
	is >> hasParameterErrors;
	
	if (hasParameterErrors)
		{
		SetHasParameterErrors(kJTrue);
		itsErrors	= new JArray<JFloat>;
		assert(itsErrors != NULL);
		for (JIndex i = 1; i <= count; i++)
			{
			JFloat value;
			is >> value;
			itsErrors->AppendElement(value);
			}
		}
	
	JFloat xMin;
	JFloat xMax;

	is >> xMin;
	is >> xMax;

	SetXRange(xMin, xMax);

	is >> itsFnString;
	
	itsFn	= NULL;
	JParseFunction(itsFnString, itsParms, &itsFn);
	SetHasGoodnessOfFit(itsHasGOF);

	GenerateDiffData();
}
JPlotFitProxy::JPlotFitProxy
	(
	JPlotFitFunction*	fit,
	J2DPlotWidget* 		plot, 
	JPlotDataBase* 		fitData
	)
	:
	JPlotFitFunction(plot, fitData, 0, 0),
	itsErrors(NULL),
	itsHasGOF(fit->HasParameterErrors()),
	itsGOFName(fit->HasGoodnessOfFit()),
	itsFnString(fit->GetFitFunctionString())
{
	if (itsHasGOF)
		{
		fit->GetGoodnessOfFitName(&itsGOFName);
		fit->GetGoodnessOfFit(&itsGOF);
		}
	itsParms	= new GVarList();
	assert(itsParms != NULL);
	itsParms->AddVariable("x", 0);
	const JSize count	= fit->GetParameterCount();
	SetParameterCount(count);
	for (JIndex i = 1; i <= count; i++)
		{
		JString name;
		JBoolean ok	= fit->GetParameterName(i, &name);
		assert(ok);
		JFloat value;
		ok	= fit->GetParameter(i, &value);
		assert(ok);
		itsParms->AddVariable(name, value);
		}

	if (fit->HasParameterErrors())
		{
		SetHasParameterErrors(kJTrue);
		itsErrors	= new JArray<JFloat>;
		assert(itsErrors != NULL);
		for (JIndex i = 1; i <= count; i++)
			{
			JFloat value;
			JBoolean ok	= fit->GetParameterError(i, &value);
			assert(ok);
			itsErrors->AppendElement(value);
			}
		}

	J2DPlotData* diff	= fit->GetDiffData();
	J2DPlotData* data;
	if (J2DPlotData::Create(&data, diff->GetXData(), diff->GetYData(), kJFalse))
		{
		const JArray<JFloat>* xerrors;
		if (diff->GetXPErrorData(&xerrors))
			{
			data->SetXErrors(*xerrors);
			}
		const JArray<JFloat>* yerrors;
		if (diff->GetYPErrorData(&yerrors))
			{
			data->SetYErrors(*yerrors);
			}
		SetDiffData(data);
		}
	JFloat xMin;
	JFloat xMax;
	fit->GetXRange(&xMin, &xMax);
	SetXRange(xMin, xMax);
	itsFn	= NULL;
	JParseFunction(itsFnString, itsParms, &itsFn);
	SetHasGoodnessOfFit(itsHasGOF);
}