Friday, 5 July 2013

Automatic user interface generation with CNL

There are many ways to automatically build and manage user interfaces. In this article we want to present a web application for automatic form generation that uses semantic knowledge description.
We believe that this solution is not only flexible but also highly understandable for non-technical users.



Fluent Editor  allows you to write rules for semantic forms with Controlled Natural Language (e.g. English CNL). User form in this example is built automatically based on knowledge base in CNL. Requirements for the form are written using modal expressions such as 'must', 'should' or 'can' e.g. 'Every patient must have-first-name ( some-string-value ).'



Ontology used to build user form in this example looks like below. This ontology is used by the web application to build a user form.

Every form is a thing.
Every
patient-form is a form.
Every
patient-form must concern a visit.

Comment: Visit.
Every visit must have-date (some-datetime-value ).
Every
visit must have-health-center health-center.
Something is a health-center if-and-only-if-it is either Outpatients-Clinics-Of-Coi-Warsaw or Outpatients-Clinics-Of-Coi-Wroclaw.
Every visit must have-number ( some-integer-value ).
Every patient-form must concern patient.

 Comment: Patient.
Every patient must have-first-name  ( some-string-value ).
Every
patient must have-last-name  ( some-string-value ).
Every
patient must have-gender gender-type.
Every
patient must have-age (some-integer-value ).
Every
patient must have-birth-date (some-datetime-value).
Something
is a gender-type if-and-only-if-it is either Male or Female.
Every
patient that have-gender Female is a female-patient.
Every
female-patient must have-maiden-name ( some-string-value ).
Every patient-form must concern an examination.

 Comment:Examination.
Every examination can have-observation observation.
Every radiological-assessment is an observation.
Every
invasive-assessment is an observation.
Every ct is radiological-assessment. 
Every xray is radiological-assessment.
Every et is radiological-assessment.
Every biopsy is a invasive-assessment.
Every observation must have-date ( some-datetime-value ).
Every radiological-assessment must have-description nothing-but ( some-string-value).
Every ct must have-contrast-state yes-or-now-answer.
Something
is a yes-or-now-answer if-and-only-if-it is either Yes-Answer or No-Answer.
Every biopsy must have-result pathological-assessment.
Something
is a pathological-assessment if-and-only-if-it is either Gist-Confirmed or Retroperitoneal-Sarcoma or Other-Cancer.
Every examination that has-observation a biopsy that has-result Gist-Confirmed is a examination-with-sarcoma.
Every examination-with-sarcoma must concern a sarcoma.

 Comment: Sarcoma.
Every sarcoma must have-mass-localization a mass-localization-type .
Every
sarcoma must have-diameter ( some-string-value ).
Every sarcoma must have-localization a localization.
Every
sarcoma must have-surgical-state a surgical-state.
Every
sarcoma must have-contradictions-state a contradictions-state.
Every
sarcoma must have-patient-consent a yes-or-now-answer.

Something is a separation-state if-and-only-if-it is either Ruptured or Non-Ruptured.
Something
is a surgical-state if-and-only-if-it is either Operable or Operable-With-Significant-Surgical-Morbidity or Non-Operable.
Something
is a contradictions-state if-and-only-if-it is either No-Contradictions-For-Surgery or Contradictions-For-Surgery.
Something
is a pathological-assessment if-and-only-if-it is either Gist-Confirmed or Retroperitoneal-Sarcoma or Other-Cancer.
Something
is a mass-localization-type if-and-only-if-it is either Localized or Locally-Advanced or Metastatic.
Something
is a localization if-and-only-if-it is either Stomach or Brain.

User form is represented by the concept form Patient-form is an instance of a form class. It requires some other information to be specified, such as patient details, examination details etc. Every dependent concept can also require some other data. Furthermore, we can build a hierarchy of concepts in CNL and it must be included in generated form. In this example observation, invasive assessment and radiological-assessment make a hierarchy of concepts.


Modifying  theform

We can modify the form at any time without making any changes to the source code of application. The ontology loaded by the application is the only place, where changes must be done. If new rules become valid, user form is refreshed and widgets are placed in the right place. For example, we can add two new rules to ontology.

Every patient must have-age (some-integer-value ).
Every patient must have-birth-date (some-datetime-value).

 After refreshing the form new widgets appear in the patient section.


MicroOntorion is used to perform queries and retrieve all valid requirements. When the user fills out the form and presses 'Submit' button, application verifies if all the rules are satisfied. Based on user input, new rules can be activated e.g. requirement 'Every female-patient must have-maiden-name (some-string-value).' is valid only for female patients. Application presents generated CNL knowledge, which can be used to infer new information about patient health and any further medical decisions.



If you want to learn more about Fluent Editor CNL-EN grammar, visit this link.


*) FluentEditor 2, ontology editor, is a comprehensive tool for editing and manipulating complex ontologies that uses Controlled Natural Language. Fluent editor provides one with a more suitable for human users alternative to XML-based OWL editors. It's main feature is the usage of Controlled English as a knowledge modeling language. Supported via Predictive Editor, it prohibits one from entering any sentence that is grammatically or morphologically incorrect and actively helps the user during sentence writing. The Controlled English is a subset of Standard English with restricted grammar and vocabulary in order to reduce the ambiguity and complexity inherent in full English.


Tuesday, 4 June 2013

Does your application understand you? Hello World with microOntorion SDK.

Nature likes symmetry. If you interact with someone, usually you expect the interaction from the other side. The same situation is in the case of application. A huge effort is put on developing programmes, that will not only process the input, but also understand it (or at least try to understand). That is the point of ontology matters and semantic technology.
Now, as a programmer, you can participate in the semantic world of applications. This post will show how to use ontology in your application with help of microOntorion.

microOntorion SDK

microOntorion is an end point to Ontorion environment. It makes all reasoning locally, on your computer. It is provided as a .dll library.

Before you start working on your semantic application, you need to add reference to microOntorion library.

Using directives may be helpful.
using Ontorion.MicroOntorion;
using Ontorion;

Initialization


Lets create in Main function microOntorion object, that allows to import ontology and query against it:

MicroOntorion oep = new MicroOntorion();

Now, we can load ontology. The source ontology should be prepared as CNL sentences. The easiest way to do this is to use Fluent Editor 2. It has auto-complete feature that ensures you that your ontology and queries are valid grammar sentences. Fluent Editor 2 writes ontology as *.encnl files, which can you stream directly to microOntorion.
There is also possibility to construct sentences as string and load in that form to microOntorion library.
We will utilize the first method.
Lets construct simple ontology (in Fluent Editor 2) and save as myOntology.encnl file.


Comment: 'Sample IT ontology'.

Server-1 is a server and hosts Application-1.
Server-2 is a server and hosts Application-2.

Server-1 has-ip-address equal-to '173.194.70.102'.
Server-1 has-ip-address equal-to '173.194.70.103'.
Server-1 has-ip-address equal-to '173.194.70.104'.

Server-2 has-ip-address equal-to '206.190.36.45'.

Application-1 is an application that serves Customer-1 and serves Customer-2.
Application-3 is an application that serves Customer-3.

Application-1 has-name equal-to 'Fluent Editor'.
Application-1 has-name equal-to 'Fluent Editor 2'.
Application-3 has-name equal-to 'Ontorion'.

Customer-1 is a customer and has-severity critical.
Customer-2 is a customer and has-severity medium.
Customer-3 is a customer and has-severity low.

X is-hosted-on Y if-and-only-if Y hosts X.
Every application must be-hosted-on server.

Part-2: 'Incidents'.
Incident-1 has-reported-date equal-to 2012-01-01 and was-reported-by Operator-1.
Incident-1 has-affected Server-1.

Incident-2 has-reported-date equal-to 2012-01-02 and was-reported-by Operator-1.
Incident-2 has-affected Application-2.


Now we can import our ontology to microOntorion:

try
{
    using (FileStream ontologyFileStream = new FileStream("../../../myOntology.encnl", FileMode.Open, FileAccess.Read))
    {
        // load ontology from file
        microOntorion.Load(ontologyFileStream);
    }
}
catch (Ontorion.ConsistencyException e)
{
    // when exception has been thrown check if knowledge has been incosistent
    foreach (var expl in microOntorion.GetExplanations())
    {
        PrintResults(expl);
    }
    return;
}
catch (Exception e)
{
    return;
}

Function Load makes also some preprocessing and your application processes ontology only once.
Function GetExplanations provides some information about sources of errors, that occurred due to ontology preprocessing (e.g. ontology is inconsistent).
Function PrintResults just prints results:

private static void PrintResults(List<string> result)
{
    string res = "";
    foreach (var item in result)
    {
        res += item + " ";
    }
    Console.WriteLine(res);
}

Asking query

At this moment we have create MicroOntorion object and ontology is loaded. It is time to ask some question. Lets prepare it:

// build your query
string query = String.Format("Who-Or-What is-hosted-on server that has-ip-address equal-to '173.194.70.102' ?");

Get results of this query:

// get superconcepts returned by the query
List<string> superconcepts = microOntorion.GetSuperconceptsOf(query);
Console.Write("Superconcepts: ");
PrintResults(superconcepts);

// get superconcepts returned by the query
List<string> subconcepts = microOntorion.GetSubconceptsOf(query);
Console.Write("Subconcepts: ");
PrintResults(subconcepts);

// get all instances returned by the query
List<string> instances = microOntorion.GetInstances(query, int.MaxValue);
Console.Write("Instances: ");
PrintResults(instances);

GetSubconceptsOf, GetSuperconceptsOf and GetInstances returns subconcepts, superconcepts and instances respectively that satisfy query.

Attributes

We can also ask for attributes of specified instances, e.g. Application-1, that were returned from the previous function. Application-1 has name attribute. Lets get this attribute:

// get names of the applications (as attributes)
foreach (var item in instances)
{
    Console.WriteLine("{0} has name(s): {1}", item, string.Join(", ", microOntorion.GetAttributeValues(item, "have-name").ToArray()));
}

OWLAPI has some problems with extracting attributes from complex sentences. It is recommended to attach attributes to instances in separate sentence.

Requirements

There is also possibility to ask for requirements. Lets get all requirements for application concept.

// get all modalities for application concept.
var res = microOntorion.GetRequirements("application");
Console.WriteLine("Requirements for application concept:");
foreach (var item in res)
{
    Console.WriteLine("- {0}", item.Key);
    foreach (var req in item.Value)
    {
        Console.WriteLine("--- {0}", req);
    }
}

MicroOntorion.GetRequirements supports now only simple requirements:
Every <C> <modality><R><D>
where:
<C> is concept, e.g. application,
<modality> is modality such as must, should, ... etc.,
<R> is role, e.g. be-hosted-on,
<D> is any ending of sentence, can be quite complex

The result of querying our ontology:

Superconcepts:
Subconcepts:
Instances: Application-1
Application-1 has name(s): Fluent Editor 2, Fluent Editor
Requirements for application concept:
- MUST
--- is-hosted-on a server
Press any key to continue . . .

Whole source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ontorion.MicroOntorion;
using System.IO;
using Ontorion;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            MicroOntorion microOntorion = new MicroOntorion();
            try
            {
                using (FileStream ontologyFileStream = new FileStream("../../../myOntology.encnl", FileMode.Open, FileAccess.Read))
                {
                    // load ontology from file
                    microOntorion.Load(ontologyFileStream);

                    // build your query
                    string query = String.Format("Who-Or-What is-hosted-on server that has-ip-address equal-to '173.194.70.102' ?");

                    // get superconcepts returned by the query
                    List<string> superconcepts = microOntorion.GetSuperconceptsOf(query);
                    Console.Write("Superconcepts: ");
                    PrintResults(superconcepts);

                    // get superconcepts returned by the query
                    List<string> subconcepts = microOntorion.GetSubconceptsOf(query);
                    Console.Write("Subconcepts: ");
                    PrintResults(subconcepts);

                    // get all instances returned by the query
                    List<string> instances = microOntorion.GetInstances(query, int.MaxValue);
                    Console.Write("Instances: ");
                    PrintResults(instances);


                    // get names of the applications (as attributes)
                    foreach (var item in instances)
                    {
                        Console.WriteLine("{0} has name(s): {1}", item, string.Join(", ", microOntorion.GetAttributeValues(item, "have-name").ToArray()));
                    }

                    // get all modalities for application concept.
                    var res = microOntorion.GetRequirements("application");
                    Console.WriteLine("Requirements for application concept:");
                    foreach (var item in res)
                    {
                        Console.WriteLine("- {0}", item.Key);
                        foreach (var req in item.Value)
                        {
                            Console.WriteLine("--- {0}", req);
                        }
                    }

                }
            }
            catch (Ontorion.ConsistencyException e)
            {
                // when exception has been thrown check if knowledge has been incosistent
                foreach (var expl in microOntorion.GetExplanations())
                {
                    PrintResults(expl);
                }
                return;
            }
            catch (Exception e)
            {
                return;
            }
        }

        private static void PrintResults(List<string> result)
        {
            string res = "";
            foreach (var item in result)
            {
                res += item + " ";
            }
            Console.WriteLine(res);
        }
    }
}

---

You can download microOntorion SDK here.



*) FluentEditor 2, ontology editor, is a comprehensive tool for editing and manipulating complex ontologies that uses Controlled Natural Language. Fluent editor provides one with a more suitable for human users alternative to XML-based OWL editors. It's main feature is the usage of Controlled English as a knowledge modeling language. Supported via Predictive Editor, it prohibits one from entering any sentence that is grammatically or morphologically incorrect and actively helps the user during sentence writing. The Controlled English is a subset of Standard English with restricted grammar and vocabulary in order to reduce the ambiguity and complexity inherent in full English.

Friday, 3 May 2013

Querying against your ontology – an IT infrastructure scenario


Fluent Editor 2 allows you to edit ontologies that are compatible with OWL2 and SWRL and are expressed with Controlled Natural Language (e.g. English CNL).


Simple model for IT infrastructure

Let’s consider a scenario where the knowledge engineer wants to model simple IT infrastructure:
  • there are servers that hosts applications (in other words applications are hosted on the servers) 
  • applications serves different customers 
  • our customers have different severity (critical, medium, low) 
  • moreover, we want to make sure that every application will be assigned to some server in our ontology

His CNL ontology may look like below:

Ontology:
Comment: 'Sample IT ontology'.

Server-1 is a server and hosts Application-1.
Server-2 is a server and hosts Application-2.

Application-1 is an application that serves Customer-1 and serves Customer-2.
Application-3 is an application that serves Customer-3.

Customer-1 is a customer and has-severity critical.
Customer-2 is a customer and has-severity medium.
Customer-3 is a customer and has-severity low.

X is-hosted-on Y if-and-only-if Y hosts X.
Every application must be-hosted-on server.

In Fluent Editor grammar each instance name begins with capital letter (“Server-1”), concept name begins with lowercase (“server”) as well as relation name (“hosts”).

There is no need to explicitly “define” a relation beforehand – you simply use it for instance definition. It will automatically appear on the Taxonomy Tree while editing.

Last two lines introduces a symmetric relation (“is-hosted-on”) and a requirement with modal expression (“must”) to enforce every application must be-hosted-on a server. Symmetric relation makes this expression more convenient (not every server must host an application).

Asking questions

OK. Now we can start asking queries against our ontology. E.g. what hosts applications that serves customers that has-severity critical?

CNL question shall be entered to the reasoner question-box:
Query:
Who-Or-What hosts application that serves customer that has-severity critical?

The answer is (as expected):
Answer:
<?>: Server-1

What’s interesting, the above example can be expressed in much shorten way – by abandon concept designation (assuming we only want ask similar questions as above):
Ontology:
Comment: 'Sample IT ontology'.

Server-1 hosts Application-1.
Server-2 hosts Application-2.

Application-1 serves Customer-1 and serves Customer-2.
Application-3 serves Customer-3.

Customer-1 has-severity critical.
Customer-2 has-severity medium.
Customer-3 has-severity low.

With this shorten ontology we can still ask the same question, but regarding “something” rather than particular concepts (servers and applications):
Query:
Who-Or-What hosts something that serves something that has-severity critical?

And the answer again:
Answer:
<?>: Server-1


Enforcing requirements with modal expressions

Modal expressions are nice way to express requirements for the knowledge. In the above example we express a requirement that every application must be-hosted-on a server:
CNL:
X is-hosted-on Y if-and-only-if Y hosts X.
Every application must be-hosted-on server.

How does work? When the ontology (or its part) is validated, e.g. before committing changes to the knowledge server. Fluent Editor has built-in validator for the ontology being edited (will be available with May release). 
Validating the first example will show these results:
Ontology (validated):
Comment: 'Sample IT ontology'.

Server-1 is a server and hosts Application-1.
Server-2 is a server and hosts Application-2.

Application-1 is an application that serves Customer-1 and serves Customer-2.
Application-3 is an application that serves Customer-3.

Customer-1 is a customer and has-severity critical.
Customer-2 is a customer and has-severity medium.
Customer-3 is a customer and has-severity low.

X is-hosted-on Y if-and-only-if Y hosts X.
Every application must be-hosted-on server.



All instances that were subject to validation (there is some expression with “must”, “can” or “shall” – they differ by the interpretation by the end-user application. Fluent Editor differentiate them by color of the warning) are highlighted. Green means all requirements are fulfilled. Red means there is some requirement not met. Requirements expressed with “shall” will be warned with yellow, when not met. End-user application may forbid committing changes to the database, when some requirements are not met.

In the above example:
·         Application-1 is-hosted-on the Server-1 (validated),
·         Application-2 is-hosted-on the Server-2 (but nowhere denoted as an application concept, thus not being subject to validation) and
·         Application-3 is not assigned to any server – thus marked as red.

To fulfill our requirement we shall designed somehow that Application-3 is hosted on some server, e.g.:
CNL:
Application-3 is-hosted-on Server-2.

or:
CNL:
Server-2 hosts Application-3.

or even indirectly (with SWRL rules in this particular example):
CNL:
If X hosts something that contains Y then X hosts Y.

Group-A contains Application-3.
Server-2 hosts Group-A.


The above example is as simple, as possible, but gives a quick insight how this kind of semantic technology can be used for a very practical problem.

If want to learn more about Fluent Editor CNL-EN grammar, visit this link.


*) FluentEditor 2, ontology editor, is a comprehensive tool for editing and manipulating complex ontologies that uses Controlled Natural Language. Fluent editor provides one with a more suitable for human users alternative to XML-based OWL editors. It's main feature is the usage of Controlled English as a knowledge modeling language. Supported via Predictive Editor, it prohibits one from entering any sentence that is grammatically or morphologically incorrect and actively helps the user during sentence writing. The Controlled English is a subset of Standard English with restricted grammar and vocabulary in order to reduce the ambiguity and complexity inherent in full English.