Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Variables and Facts

What are Variables?

In CrewAI, variables are used to store data that can be referenced and manipulated in a program. They act as containers for data values. When you create a variable, you reserve a space in the system's memory where you can store a value and change it later.

Declaring Variables

In CrewAI, you declare a variable using specific syntax. Here's an example:

var myVariable = 10;

In this example, myVariable is the name of the variable, and 10 is the value stored in the variable. The var keyword is used to declare the variable.

Types of Variables

Variables can store different types of data. The common types are:

  • Integer: Whole numbers, e.g., 1, 2, 3
  • Float: Decimal numbers, e.g., 1.5, 2.75
  • String: Text, e.g., "Hello, World!"
  • Boolean: True or false values

What are Facts?

In CrewAI, facts represent pieces of information that are always true. They are used to define properties and relationships in the system. Facts are typically used in conjunction with rules to infer new information.

Declaring Facts

Facts are declared in a way similar to variables but are used differently. Here's an example:

fact person(name, age);

In this example, person is a fact that takes name and age as parameters. This fact can be used to store information about people.

Using Facts

Once you have declared a fact, you can use it to store specific data. For instance:

person("Alice", 30);

This statement asserts that there is a person named Alice who is 30 years old.

Combining Variables and Facts

Variables and facts can be combined to create dynamic and flexible programs. For example, you can use variables to represent changing data and facts to represent constant truths. Here is a simple example combining both:

var age = 25;
fact person("Bob", age);

In this case, the variable age is set to 25, and the fact person uses this variable to assert that Bob is 25 years old.

Conclusion

Understanding variables and facts is crucial for working with CrewAI. Variables allow you to store and manipulate data, while facts help you define constant truths about the system. By mastering these concepts, you can create complex and dynamic programs.