Ejemplo n.º 1
0
Expr* Lower_expr_flow::post_bin_op(Bin_op* in)
{
	Conditional_expr* result;
	if(*in->op->value == ",")
	{
		pieces->push_back(new Eval_expr(in->left));
		return in->right;
	}
	else if(*in->op->value == "||" || *in->op->value == "or")
	{
		Variable* temp = dynamic_cast<Variable*>(eval(in->left));
		assert(temp);
		result = new Conditional_expr(
			temp->clone(), 
			temp->clone(),
			in->right);
	}
	else if(*in->op->value == "&&" || *in->op->value == "and")
	{
		Variable* temp = dynamic_cast<Variable*>(eval(in->left));
		assert(temp);
		result = new Conditional_expr (
			temp->clone(), 
			in->right,
			temp->clone());
	}
	else 
		return in;

	// && and || automatically turn their parameters into bools
	return new Cast (new CAST (s("bool")), post_conditional_expr (result));
}
Ejemplo n.º 2
0
Expr* Lower_expr_flow::post_conditional_expr(Conditional_expr* in)
{
	Variable* temp = fresh_var("TEF");

	pieces->push_back(new If (
		in->cond, 
			new Statement_list (
				new Eval_expr (
					new Assignment (
						temp->clone (), 
						false, 
						in->iftrue))),
			new Statement_list (
				new Eval_expr (
					new Assignment (
						temp->clone (),
						false,
						in->iffalse)))));
	
	return temp;
}
Ejemplo n.º 3
0
/*** Execute this node ***/
Outcome For::execute()
{
	// Check for the for value
	if(forValue == 0)
		throw Excep(getLineNumber(), getColumnNumber(), "No variable specified in for loop.");

	if(forValue->getType() != OP_VARIABLE)
		throw Excep(getLineNumber(), getColumnNumber(), "For loop must be given a valid variable.");

	// Store the for value variable
	Variable* forVar = static_cast<Variable*>(forValue);

	// If a from value was given, set the counter to this initial value
	if(from != 0)
	{
		// Evaluate the 
		std::auto_ptr<Object> fromEval(from->evaluate());
		if(fromEval->getType() != OBJ_INTEGER && fromEval->getType() != OBJ_REAL)
			throw InvalidTypeException(getLineNumber(), getColumnNumber(), OBJ_INTEGER | OBJ_REAL, fromEval->getType());
		Assign(forVar->clone(), fromEval.release()).execute();
	}
	else
		Assign(forVar->clone(), new Integer(1)).execute();

	// Create an Outcome object to store the result
	Outcome result(S_SUCCESS);

	do
	{
		// Check the while condition, if present
		if(whileCond != 0)
		{
			std::auto_ptr<Object> whileCondEval(whileCond->evaluate());
			if(whileCondEval->getType() != OBJ_LOGICAL)
				throw InvalidTypeException(getLineNumber(), getColumnNumber(), OBJ_LOGICAL, whileCondEval->getType());
			bool whileCondResult = static_cast<Logical*>(whileCondEval.get())->getValue();

			if(!whileCondResult)
				break;
		}

		// Check the to value
		if(to != 0)
		{
			std::auto_ptr<Object> toEval(to->evaluate());

			// Store the step value evaluated
			std::auto_ptr<Object> stepEval;
			if(step == 0)
				stepEval.reset(new Integer(1));
			else
				stepEval.reset(step->evaluate());

			// Confirm that the step is an integer
			if(stepEval->getType() != OBJ_INTEGER && stepEval->getType() != OBJ_REAL)
				throw InvalidTypeException(getLineNumber(), getColumnNumber(), OBJ_INTEGER | OBJ_REAL, stepEval->getType());

			std::auto_ptr<Real> castStep;
			// Cast the step value to a real
			if(stepEval->getType() == OBJ_INTEGER)
				castStep.reset(new Real(static_cast<Integer*>(stepEval.get())->getValue()));
			else
				castStep.reset(static_cast<Real*>(stepEval.release()));

			bool toCondition = false;

			if(castStep->getValue() > 0)
			{
				std::auto_ptr<Object> operation(Greater(forVar->clone(), toEval.release()).evaluate());
				toCondition = static_cast<Logical*>(operation.get())->getValue();
			}
			if(castStep->getValue() < 0)
			{
				std::auto_ptr<Object> operation(Less(forVar->clone(), toEval.release()).evaluate());
				toCondition = static_cast<Logical*>(operation.get())->getValue();
			}

			if(toCondition)
				break;
		}

		// Execute the NodeList of statements
		result = list->execute();

		// Modify forVar based on step
		if(step == 0)
		{
			std::auto_ptr<Object> addOperation(Add(forVar->clone(), new Integer(1)).evaluate());
			Assign(forVar->clone(), addOperation.release()).execute();
		}
		else
		{
			std::auto_ptr<Object> stepEval(step->evaluate());
			if(stepEval->getType() != OBJ_INTEGER && stepEval->getType() != OBJ_REAL)
				throw InvalidTypeException(getLineNumber(), getColumnNumber(), OBJ_INTEGER | OBJ_REAL, stepEval->getType());
			std::auto_ptr<Object> addOperation(Add(forVar->clone(), stepEval.release()).evaluate());
			Assign(forVar->clone(), addOperation.release()).execute();
		}

		// Check the until condition, if present
		if(untilCond != 0)
		{
			std::auto_ptr<Object> untilEval(untilCond->evaluate());
			if(untilEval->getType() != OBJ_LOGICAL)
				throw InvalidTypeException(getLineNumber(), getColumnNumber(), OBJ_LOGICAL, untilEval->getType());
			bool untilResult = static_cast<Logical*>(untilEval.get())->getValue();

			if(untilResult)
				break;
		}
	}
	while(result.getStatus() == S_SUCCESS);

	return result;
}
Ejemplo n.º 4
0
MOOptVector *Optimization::evaluate(QList<ModModelPlus*> models, Variables *optimVariables, ScannedVariables *scannedVariables, bool &ok)
{
    ok = true;
    bool useScan = (scannedVariables->size()>0);
    bool usePoints = false;
    MOOptVector* resultVariables = new MOOptVector(true,useScan,usePoints);

    for(int iM=0;iM<models.size();iM++)
    {
        if(ok)
        {
            /************************************
            Creating a new OneSimulation
            ************************************/
            QString modelName = models.at(iM)->modModelName();
            OneSimulation *oneSim = new OneSimulation(_omProject,models.at(iM));
            oneSim->_filesToCopy = this->_filesToCopy;
            oneSim->setCtrls(*this->ctrls(modelName));


            int nbVar = optimVariables->size();
            Variable* curVar;

            // set optim variables in one simulation
            for(int i=0;i<nbVar;i++)
            {
                curVar = optimVariables->at(i);
                if(curVar->model()==modelName)
                {
                    oneSim->overwritedVariables()->addItem(curVar->clone());
                }
            }

            // add overwrited variables of optimization in simulation
            for(int i=0;i<overwritedVariables()->size();i++)
            {
                curVar = overwritedVariables()->at(i);
                if(curVar->model()==modelName)
                {
                    oneSim->overwritedVariables()->addItem(curVar->clone());
                }
            }

            // copying relevant scanned variables
            for(int iS=0;iS<scannedVariables->size();iS++)
            {
                if(scannedVariables->at(iS)->model()==modelName)
                {
                    oneSim->scannedVariables()->addItem(scannedVariables->at(iS)->clone());
                }
            }


            /************************************
            Launch OneSimulation
            ************************************/
            ProblemConfig config();
            OneSimResult *result =  dynamic_cast<OneSimResult*>(oneSim->launch(config));
            ok = ok && result->isSuccess();
            resultVariables->addItems(result->finalVariables(),true);

            // free memory
            delete oneSim;
            delete result;
        }
    }

    return resultVariables;
}