Making your own logical language
However, there was one particular comment that spoke about making it functional and usable.While I was working on this *TOP SECRET* project which will make development even easier for beginners using CoronaSDK (How much *more* simpler can it become)? To clear the writers block, I started to play with this code and I came to a workable code.
so here's what I have,
lists = newList() lists.set_father("Vader","Luke") lists.set_father("Vader","Leia") print(lists.is_mother("Vader","Leia")) print(lists.is_father("Vader","Leia")) print(lists.is_father("Vader","Luke")) lists.on_father("Vader","Luke",function() print("Hey, that's his dad") end) lists.on_father("Vader","Leia",function() print("Hey, that's her dad") end) lists.on_father("Vader","Vader",function() print("Hey, that's messed up") end) print(lists.who_father("Luke")) print(lists.who_father("Leia")) print(lists.who_father("Vader")) local res = (lists.list_father("Vader")) for k,v in ipairs(res) do print(v) end
When I run this, this is what the console prints for me
false true true Hey, that's his dad Hey, that's her dad Vader Vader Undefined Luke Leia
So let us see what it happening here,
Line 1 creates a special list object called list, this is special as it allows for these commands without having to define them.
Line 3 and Line 4 set the relationship father for Vader --> Luke, then for Vader --> Leia
Line 6 we try to query if the relationship between Vader and Leia if Vader is Leia's mother to which we get the first false
Line 7 queries if Vader is Leia's father, to which we get a true
Line 8 queries if Vader is Luke's father, to which we get a true
Line 10 we set up a special event, on_ specifies that on matching the relationship of father between Vader and Luke, perform this function which is specified inline. Since we are printing the output, we see that message
Line 11 we match the relationship of father between Vader and Leia, performing the function that prints the message
Line 12 we match the relationship of father between Vader and Vader, since it does not match, it does not perform the function to prints the message
Line 14 we query the other way around as to who_ the father of Luke is, to which we get the response Vader
Line 15 we query for the father of Leia, to which we get the response Vader
Line 16 we query for the father of Vader, which is undefined so it return that it is undefined
Line 18 we get the list in the variable res, we request for the list where the relationship of Vader is father, since this is a table, we need to iterate through to print the results as in lines 19-21
You can see that there are no fixed functions defined, we can more or less define any relationship that we want, This can be even used in our apps, but can be limited as you have to know the relationships that you want to create at runtime, so this is fun for those sitting with the console and trying stuff out, I doubt that this can be used in a distributed program in its current form.
Hope this gives you a little breather from the complex post earlier.
ApplicationI was responding to one of the comments and I did make note that this can be very useful in Ontologies, Ontologies work on relationships and rules. So, I will when I get some time try to re-write an ontology app (for pure academical purposes only) that can set up these relationship and return results based on the relationships.
So, we can define that all animals have four legs. Then we can define that a dog is an animal. It is then inferred that a dog has four legs as it is an animal. So, you can see that it can be very powerful and useful too. This was the buzz word around 2007-2008 haven't heard about it much since then. I was in touch with a colleague that was using ontologies to mine datasets and perform analysis. Interesting application.
For our sample, we cannot have inferences at this point of time, to support that kind of syntax, this code that I have will have to be modified on a totally different level. Till then, let us try some more
list.set_artist("Herge", "Tintin") list.set_employee("Mad","Don Martin") list.set_employee("Mad","Sergio Argones") list.set_product("Ansca", "Corona") list.set_product("Apple", "iPad") list.set_product("Apple", "iPhone") list.set_product("Apple", "Mac OSX") print(list.is_product("Apple","Windows")) print(list.is_product("Ansca","Corona")) print(list.who_artist("Tintin")) print(list.who_employee("Don Martin")) local res = lists.list_product("Apple") for k,v in ipairs(res) do print"Products by Apple -> " .. v) end
The codesThis is not the source code but the code that does the magic. You can see that we could set the second set of relationships and we do not need to define anything special other than the list = newLists() which does all this cool stuff for us.
To create a relationship, we use the set_relationship between two entities, these have to be strings due to the way it is handled. I could do away with using the string literals, but then for a demo did not want to spend time on that.
To query a relationship, we use the is_relationship and the two entities.
To query the other party of a particular relationship, we use the who_relationship that returns the corresponding entity for the one passed
and To get a list of entities that are related to the one passed, we use the list_relationship, this returns an array of entities that match the relationship for the passed entitiy.
once again I will iterate that we have not created any functions but they all seem to work, the magic of metatables.
NOTE : Since I am planning on using this in some of my other projects, I shall not be able to provide the source code at this point of time.