Beispiel #1
0
EvaluationResult At::evaluate(const EvaluationContext& params) const {
    const EvaluationResult evaluatedIndex = index->evaluate(params);
    const EvaluationResult evaluatedInput = input->evaluate(params);
    if (!evaluatedIndex) {
        return evaluatedIndex.error();
    }
    if (!evaluatedInput) {
        return evaluatedInput.error();
    }
    
    const auto i = evaluatedIndex->get<double>();
    const auto inputArray = evaluatedInput->get<std::vector<Value>>();
    
    if (i < 0 || i >= inputArray.size()) {
        return EvaluationError {
            "Array index out of bounds: " + stringify(i) +
            " > " + util::toString(inputArray.size()) + "."
        };
    }
    if (i != std::floor(i)) {
        return EvaluationError {
            "Array index must be an integer, but found " + stringify(i) + " instead."
        };
    }
    return inputArray[static_cast<std::size_t>(i)];
}
EvaluationResult CompositeExpression::EvaluateOr(IEvaluationContext* scope)
{
  if (fExpressions.size() == 0)
    return EvaluationResult::TRUE_EVAL;
  EvaluationResult result = EvaluationResult::FALSE_EVAL;
  std::vector<Expression::Pointer>::iterator iter;
  for (iter= fExpressions.begin(); iter != fExpressions.end(); ++iter)
  {
    result = result.Or((*iter)->Evaluate(scope));
    if (result == EvaluationResult::TRUE_EVAL)
      return result;
  }
  return result;
}
EvaluationResult CompositeExpression::EvaluateAnd(IEvaluationContext* scope)
{
  if (fExpressions.size() == 0)
    return EvaluationResult::TRUE_EVAL;
  EvaluationResult result = EvaluationResult::TRUE_EVAL;
  std::vector<Expression::Pointer>::iterator iter;
  for (iter= fExpressions.begin(); iter != fExpressions.end(); ++iter)
  {
    result = result.And((*iter)->Evaluate(scope));
    // keep iterating even if we have a not loaded found. It can be
    // that we find a FALSE_EVAL which will result in a better result.
    if (result == EvaluationResult::FALSE_EVAL)
      return result;
  }
  return result;
}
StatementReversal SgWhileStmt_Handler::generateReverseAST(SgStatement* stmt, const EvaluationResult& eval_result)
{
	ROSE_ASSERT(eval_result.getChildResults().size() == 1);
    SgWhileStmt* while_stmt = isSgWhileStmt(stmt);
    ROSE_ASSERT(while_stmt);

    StatementReversal body_result = eval_result.getChildResults()[0].generateReverseStatement();

	SgStatement* fwd_cond = copyStatement(while_stmt->get_condition());
	SgStatement* fwd_stmt = buildWhileStmt(fwd_cond, body_result.forwardStatement);
	fwd_stmt = assembleLoopCounter(fwd_stmt);
	
	SgStatement* rvs_stmt = buildForLoop(body_result.reverseStatement);

    return StatementReversal(fwd_stmt, rvs_stmt);
}
Beispiel #5
0
template<> EvaluationResult Match<std::string>::evaluate(const EvaluationContext& params) const {
    const EvaluationResult inputValue = input->evaluate(params);
    if (!inputValue) {
        return inputValue.error();
    }

    if (!inputValue->is<std::string>()) {
        return otherwise->evaluate(params);
    }

    auto it = branches.find(inputValue->get<std::string>());
    if (it != branches.end()) {
        return (*it).second->evaluate(params);
    }

    return otherwise->evaluate(params);
}
Beispiel #6
0
template<> EvaluationResult Match<int64_t>::evaluate(const EvaluationContext& params) const {
    const EvaluationResult inputValue = input->evaluate(params);
    if (!inputValue) {
        return inputValue.error();
    }

    if (!inputValue->is<double>()) {
        return otherwise->evaluate(params);
    }

    const auto numeric = inputValue->get<double>();
    int64_t rounded = std::floor(numeric);
    if (numeric == rounded) {
        auto it = branches.find(rounded);
        if (it != branches.end()) {
            return (*it).second->evaluate(params);
        }
    }
    
    return otherwise->evaluate(params);
}
// =================================================================
//	Evalulates the constant value of this node.
// =================================================================
EvaluationResult CComparisonExpressionASTNode::Evaluate(CTranslationUnit* unit)
{
	EvaluationResult leftResult  = LeftValue->Evaluate(unit);
	EvaluationResult rightResult = RightValue->Evaluate(unit);

	if (dynamic_cast<CBoolDataType*>(CompareResultType) != NULL)
	{
	}
	else if (dynamic_cast<CIntDataType*>(CompareResultType) != NULL)
	{
		switch (Token.Type)
		{		
			case TokenIdentifier::OP_EQUAL:			return EvaluationResult(leftResult.GetInt() == rightResult.GetInt()); 
			case TokenIdentifier::OP_NOT_EQUAL:		return EvaluationResult(leftResult.GetInt() != rightResult.GetInt());  
			case TokenIdentifier::OP_GREATER:		return EvaluationResult(leftResult.GetInt() >  rightResult.GetInt());
			case TokenIdentifier::OP_LESS:			return EvaluationResult(leftResult.GetInt() <  rightResult.GetInt());   
			case TokenIdentifier::OP_GREATER_EQUAL:	return EvaluationResult(leftResult.GetInt() >= rightResult.GetInt()); 
			case TokenIdentifier::OP_LESS_EQUAL:	return EvaluationResult(leftResult.GetInt() <= rightResult.GetInt());  
		}
	}
	else if (dynamic_cast<CFloatDataType*>(CompareResultType) != NULL)
	{
		switch (Token.Type)
		{		
			case TokenIdentifier::OP_EQUAL:			return EvaluationResult(leftResult.GetFloat() == rightResult.GetFloat()); 
			case TokenIdentifier::OP_NOT_EQUAL:		return EvaluationResult(leftResult.GetFloat() != rightResult.GetFloat());  
			case TokenIdentifier::OP_GREATER:		return EvaluationResult(leftResult.GetFloat() >  rightResult.GetFloat());
			case TokenIdentifier::OP_LESS:			return EvaluationResult(leftResult.GetFloat() <  rightResult.GetFloat());   
			case TokenIdentifier::OP_GREATER_EQUAL:	return EvaluationResult(leftResult.GetFloat() >= rightResult.GetFloat()); 
			case TokenIdentifier::OP_LESS_EQUAL:	return EvaluationResult(leftResult.GetFloat() <= rightResult.GetFloat());  
		}
	}
	else if (dynamic_cast<CStringDataType*>(CompareResultType) != NULL)
	{
		switch (Token.Type)
		{		
			case TokenIdentifier::OP_EQUAL:			return EvaluationResult(leftResult.GetString() == rightResult.GetString()); 
			case TokenIdentifier::OP_NOT_EQUAL:		return EvaluationResult(leftResult.GetString() != rightResult.GetString());  
			case TokenIdentifier::OP_GREATER:		return EvaluationResult(leftResult.GetString() >  rightResult.GetString());
			case TokenIdentifier::OP_LESS:			return EvaluationResult(leftResult.GetString() <  rightResult.GetString());   
			case TokenIdentifier::OP_GREATER_EQUAL:	return EvaluationResult(leftResult.GetString() >= rightResult.GetString()); 
			case TokenIdentifier::OP_LESS_EQUAL:	return EvaluationResult(leftResult.GetString() <= rightResult.GetString());  
		}
	}
	
	unit->FatalError("Invalid constant operation.", Token);
	return EvaluationResult(false);
}
int main(int argc, char** argv)
{
  // init node
  std::string manager_name = "ias_classifier_manager";
  ros::init(argc, argv, manager_name);
  ros::NodeHandle n("~");

  // init manager in a different thread (or start it up separately)
  ClassifierManager g_manager(n);
  startManagerThread();
  sleep(1);

  try
  {
    // load datasets "read only" style into matrices with elements of type double (could use DS as a type)
    // NOTE: will use data from the same file using some tricks as an example
    DS ds("data/test.h5", false, true);

    // in case the features and labels are not named /x and /y in the DS as required by ICF, they can be
    // renamed, aliased, or uploaded by specifying correct name (see examples for all three cases below)
    if (!ds.contains("/x"))
      ds.renameFeatureMatrix("/train", "/x"); // rename, permanent if flushed to disk (see constructor parameters)
    if (!ds.contains("/y"))
      ds.alias("/train_labels", "/y"); // alias; "/train_labels" can now also be accessed as "/y"

    // print out data
    bool print_training_data = false;
    if (print_training_data)
    {
      DS::MatrixPtr features = ds.getFeatureMatrix("/x");
      std::cout << "train features: " << features->rows() << "x" << features->cols() << std::endl;
      std::cout << (*features) << std::endl;
      DS::MatrixPtr labels = ds.getFeatureMatrix("/y");
      std::cout << "train labels: " << labels->rows() << "x" << labels->cols() << std::endl;
      std::cout << (*labels) << std::endl;
    }
    bool print_testing_data = false;
    if (print_testing_data)
    {
      DS::MatrixPtr features = ds.getFeatureMatrix("/test");
      std::cout << "test features: " << features->rows() << "x" << features->cols() << std::endl;
      std::cout << (*features) << std::endl;
      DS::MatrixPtr labels = ds.getFeatureMatrix("/test_labels");
      std::cout << "test labels: " << labels->rows() << "x" << labels->cols() << std::endl;
      std::cout << (*labels) << std::endl;
    }

    // upload training data and ground truth with a string as identifier
    ServerSideRepo data_store(n, manager_name);
    data_store.uploadData(ds, "train");
    data_store.uploadData(ds, "test", "/test", "/test_labels"); // uploading different features and labels for testing than /x and /y

    // start client for a classifier
    ClassifierClient client(n, manager_name, "knn", "-m L2 -k 1");
    //ClassifierClient client(n, manager_name, "svm", "-a 2 -t 2 -v 5 -5:2:15 -15:2:3");
    //ClassifierClient client(n, manager_name, "boost", "--weak_count 2000 --max_depth 1 -t 1");

    // assign uploaded data to the classifier and train it
    client.assignData("train", icf::Train); // training data
    client.assignData("test", icf::Eval); // OPTIONAL: evaluation data, must have labels!
    client.assignData("test", icf::Classify); // testing data (here same as the evaluation data)
    client.train();
    // it is possible to save the trained classifier to a file and load later or with a different client
    // if saving is done after evaluation, the confusion matrix is saved/loaded as well:
    // client.save("path/to.file");
    // client.load("path/to.file");

    // evaluate classifier
    EvaluationResult result = client.evaluate();
    bool print_complete_response = false;
    if (print_complete_response)
    {
      std::cout << "Evaluation result" << std::endl;
      std::cout << result << std::endl;
    }
    std::cout << "Confusion Matrix:" << std::endl;
    std::cout << result.getConfusionMatrix()->getCM() << std::endl;
    std::cout << "Error rate = " << result.getErrorRate() << std::endl;

    // classify data and check result for each feature point
    ClassificationResult classificationResult = client.classify();
    DataSet<double>::MatrixPtr features = ds.getX();
    DataSet<double>::MatrixPtr labels = ds.getY(); // OPTIONAL: compare to expected results manually
    for (int i = 0; i < features->rows(); i++)
      std::cout << "Expected " << (*labels)(i, 0) << " and got " << classificationResult.results->at(i)
          << " for the data point " << features->row(i) << std::endl;
  }
  catch (ICFException& e)
  {
    std::cerr << boost::diagnostic_information(e);
    std::vector<service_unavailable_error>* err = boost::get_error_info<service_unavailable_collection>(e);
    if (err != NULL)
    {
      for (std::vector<service_unavailable_error>::iterator iter = err->begin(); iter != err->end(); iter++)
        std::cerr << "Error: " << iter->value() << std::endl;
    }
    else
      std::cerr << "No service availability related errors" << std::endl;
  }

  // stop manager
  g_stop = true;
  ros::Rate r(10);
  while (!g_hasStopped)
  {
    r.sleep();
  }
  return 0;
}