




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Sarah McGee John Yue May 10, 2006
Why does SkyNet have a database? Typical simulations store everything on the local disk and then copy it to a database. However, the database is an active and integral part of SkyNet. The database stores the state of the world in real time. A database is capable of handle a lot of data. It can also act as a file system and can facilitate in searching for data. What is stored in the database? The database stored the four basic components for any simulation: entities, attributes, behaviors, and rules. The database reflects the model’s notion of these objects. As a result, the database is divided into two sets of tables: the relationship between entities, attributes, and attribute types and the relationship between rules, behaviors, assembles, and query parameters. In case you were wondering, “Sn” stands for SkyNet. When designing the tables, we noticed that a class called “Attributes” already existed in MSDN, so as a convention we added the letters “Sn” before all of the database classes.
Rules and Behaviors Rules and behaviors are also stored in the stored in the database. They are keyed by their name and mapped to the assembly table. The assembly table maps a guid to a .dll converted into a byte array. A .dll can store rules, behaviors, or both. The location table is used to store search parameters such as area of influence for a rule in the database. A SnLocation must be mapped to a rule before it can be searched for in the database. The area of influence is represented by a rectangle. The UpperLeftX and UpperLeftY refer to the upper left corner of the rectangle. The LowerRightX and LowerRightY refer to the lower right corner of the rectangle. With these two coordinates, one can calculate the enclosed area. The other two search parameters, Arg and Arg2, are just random strings that you can map to a rule. For example, if a rule affects mud, then may you want to set the Arg1 to “mud”, so that you can search for the rule by that field. This is the order of operations for rules and behaviors. First, an assembly must be loaded into the database. This will automatically sort through the .dll and store the rules and behaviors in the correct tables. Then, you must map the rule to the behavior in the database it affects. In order to search for rules by their parameters, you have to map the parameters to a rule. A behavior can be affected by multiple rules. This is due to the fact that a rule relating to mud can affect multiple areas, but still be the same rule. That way, you would have one entry in the rule table, but multiple entries in the location table.
Querying By using the database adapter and different parameters, the model is able to query the database for lists of attribute types, entities, behaviors, rules. Attribute types must be added to the database first before an entity and its attributes can be added. One can query an attribute type be its name or guid. You can also ask for a list of all of the attribute types currently in the database. You can query for a list of entities that have attributes of the same type. The type can be given as a string or a guid. You can also search for entities that have attribute values that fall within a specific range. Behaviors can be queried simply by their names. While entities are just a collection of data, rules are defined in code. Also the querying parameters are independent of the rule itself. For example, you can query rules by their area of influence. However, a rule that alters a behavior while traversing mud may be affect more than one entity. Therefore, we decoupled the search parameters from the rules. Once a rule has been mapped to its parameters, you can query for rules by the name of the rule, the name of the behavior associated by the rule, the rule’s general area of influence, and the two other search parameters. You can also search rules by points in case you want to know which rules affect an entity at a particular location.
In our design we used the IDatabaseAdapter interface to allow the server model to communicate with the database. As a result, the database appears to the model as a single object instead of individual tables. There are several major functionalities inside the adapter, such as storing entities, storing rules and behaviors, searching for rules based on their area of influence and the behaviors they affect. Complete database administration tasks such as dropping and creating the database, running SQL scripts are contained within the adapter so that all of the other server administration tasks can be built on top of this. Furthermore, our IDatabaseAdapter is designed in such a way that it is flexible and extensible. Flexible being that we can change the actual database we are using. For example, our implementation uses Microsoft SQL server, but we can also use MySQL database without changing the existing code at all. It will be one line of change in a configuration file. It is also extensible in the sense that the functionality can be easily extended by adding more functions in the interface and implementing them. To initialize, the DatabaseAdapter requires a string to the path of the database script. It also currently requires an ISimulation, but I think that this can be removed now.
It is possible to store .dlls as byte arrays in the database. However, because of integration problems and as a compromise between programmers to find the true bug (which was not the assemblies), the assemblies have to be stored on the local disk. To change it back, in the SkyNet.sql script, change this: [AssemblyName] nvarchar NOT NULL, To this: [AssemblyName] varbinary NOT NULL, Then in SnAssembly, if anything refers to assemblyName or AssemblyName, change “string” to “byte[]”. In the LoadAssembly method of DatabaseAdapter, change this: SnAssembly sa = new SnAssembly(assemblyguid, filename); To this: SnAssembly sa = new SnAssembly(assemblyguid, File.ReadAllBytes(filename)); In RetrieceBehavior, change this: if (loadedAssembly == null) { loadedAssembly = Assembly.LoadFile(sa.AssemblyName); } To this: Assembly loadedAssembly; if (loadedAssembly == null) { loadedAssembly = Assembly.Load(sa.AssemblyName); } The RetrieveRuleByName already has the correct load assembly call. Hopefully, this will work for you.