Building a Mediator

Step 5: Using the mediator

To use the mediator you've defined, you need to get the object definitions into your environment. First, save the K2MDL code in a file that has a .mdl extention. If you want to break it into smaller files, you can include external .mdl (and .oql) files in a K2MDL file. To do so, place an include section at the top of your .mdl file, for example:

include {
    readscript "functions.oql";
    readscript "classes.mdl";
}

When you make a client connection to K2, you can execute a readscript command that will import your object definitions into your client's query environment. This will not affect the environment of any other clients, and will not persist once your client closes its connection.

When a K2 server is started, one of its properties, called K2.system.Server.SCRIPT_SEARCH_PATH, controls where the server is allowed to look for included files. The .mdl file you include (and all files it includes, etc.) must be in one of these directories. If the server was started with its K2.system.Server.ALLOW_ABSOLUTE_READSCRIPTS property set to true, your readscript command may contain an absolute path to a file that is not in one of these directories.

Once the object definitions are in your environment, you can issue queries against them. For example, if one wanted to find all people whose salary is larger than half of the budget of a project on which they work, one would issue the query:

select distinct struct(person: person.name, project: project.name)
from People person, person.member_of_projects project
where person.salary > (project.budget / 2);

This query will be translated by K2 into a query against the data sources and optimized and executed like any other query. As you can see this query is very simple, especially when compared to the equivalent query expressed in terms of the data sources (which is left as an exercise for the reader).


[Tech Docs Index]