Spark! Fuzzy Logic Editor Help


Variables | Sets | Rules | API | Integration | Tutorial


Complete Sample Program

SparkDemo1.h - the .h file exported from Spark!
// 
// File: C:\WORK\Source\Spark\Integration\Sample\sparkdemo1.h
// Generated: Tuesday, January 16, 2001 12:03 PM by Spark! V1.2b
// 
// Copyright (C) 2000 Louder Than A Bomb! Software
// 
// To use the namespace define USE_SPARK_NAMESPACE.  Using the namespace
// prevents duplicate constants. 
//


#ifdef USE_SPARK_NAMESPACE
namespace SPARKDEMO1
{
#endif // USE_SPARK_NAMESPACE

enum 
	{
	ENEMY_DISTANCE = 0,		// id: Enemy Distance 
	ENEMY_HEALTH = 1,		// id: Enemy Health 
	OUR_HEALTH = 2,		// id: Our Health 
	OUTPUT = 3,		// id: Output 
	 // Define output terms for MOM defuzzification
	OUTPUT_RUN_AWAY = 0,		// Output Set: Run Away 
	OUTPUT_FIGHT_DEFENSIVELY = 1,		// Output Set: Fight Defensively 
	OUTPUT_ALL_OUT_ATTACK = 2		// Output Set: All Out Attack 
	}; // end enum

#ifdef USE_SPARK_NAMESPACE
} // end namespace SPARKDEMO1
#endif // USE_SPARK_NAMESPACE

 

 

 

SparkDemo1.cpp - main file for the application integrating the SparkDemo1 model
 
//
// File:	SparkDemo1.cpp
//
// Purpose:	Quick-and-dirty integration example for Spark!
//
// Copyright © 2000 Louder Than A Bomb! Software
//
// Include files
#include <iostream.h>	// for i/o functions
// include .h for Spark! integration.  

//#include "../include/SparkModel.h"

// include the .h for the model we're opening
#include "SparkDemo1.h"
// define the class we want to use...
// the version of the class that we use is dependents on if the compiler we're
// using can import a class from a dll created with MSVC.
// either way the model object will be used EXACTLY the same.
// SparkModelWrapper uses LoadLibrary() and GetProcAddress() to call the
// SparkModel dll functions, SparkModel imports the whole class directly.
// Examine SparkModel.h for the details.
#ifdef _USING_MSVC_
#define	SPARKMODEL	SparkModel
#else
#define	SPARKMODEL	SparkModelWrapper
#endif // _USING_MSVC_
// prototypes
void get_values(SPARKMODEL* model);
void set_values(SPARKMODEL* model);
void print_output_vals(SPARKMODEL* model);
int main(int argc, char* argv[]);
int main(int argc, char* argv[])
{
	int	return_val;	// return value from funcs
	char	option;		// var for selection of what user wants to do
	SPARKMODEL	model;
	// load the SparkDemo model

	return_val = model.load("../SparkDemo1.spk", true);

	if (return_val)
		{
		// there was an error loading the model...
		// if the error is -100 or -200 we had an error in a system call,
		// otherwize we can get the error text
		if (return_val == -100 || return_val == -200)
			{
			DWORD err = GetLastError();
			cout << "Error calling Load().  GetLastError() returned: " << err << endl;
			}
		else
			{
			const char* err = model.get_msg_textA();
			cout << "Error calling Load(): " << err << endl;
			}
		return 1;
		} // end if return_val
	// Set the values
 	set_values(&model);

	// get the values...
	get_values(&model);
	
	// just for fun let's do some event stuff...
 	HANDLE enemy_dist_event = model.get_var_notification(ENEMY_DISTANCE);

	// we'll wait 100 milliseconds for a change...	 
	if (model.wait(enemy_dist_event, 100))
		{
		cout << "Enemy distance value changed.  ";
		cout << "Current value is: " << model.get_value(ENEMY_DISTANCE) << endl;
		}
	else
		cout << "Value didn't change!" << endl;

	return 0;

} // end main()
void set_values(SPARKMODEL* model)
{
	float value;
 	cout << "Enter value for Enemy Distance: ";
 	cin >> value;
	// set the value...
  	model->set_value(ENEMY_DISTANCE, value);
 	cout << "Enter value for Enemy Health: ";
	cin >> value;
	model->set_value(ENEMY_HEALTH, value);
	cout << "Enter value for Our Health: ";
	cin >> value;
	// set the value...
	model->set_value(OUR_HEALTH, value);
	print_output_vals(model);
} // end set_values()
void get_values(SPARKMODEL* model)
{
	cout << "Enemy Distance: " << model->get_value(ENEMY_DISTANCE) << endl;
	cout << "Enemy Health: " << model->get_value(ENEMY_HEALTH) << endl;
	cout << "Our Health: " << model->get_value(OUR_HEALTH) << endl;
	print_output_vals(model);
} // end get_values()
void print_output_vals(SPARKMODEL* model)
{
	int mom_output;
	cout << "Output value is: ";
	cout << model->get_cog_output() << endl;
	mom_output = model->get_mom_output();
	cout << "Output result is: ";
	if (mom_output == OUTPUT_RUN_AWAY)
		cout << "Run Away" << endl;
	if (mom_output == OUTPUT_FIGHT_DEFENSIVELY)
		cout << "Fight Defensively" << endl;
	if (mom_output == OUTPUT_ALL_OUT_ATTACK)
		cout << "All Out Attack" << endl;
} // end get_values()