Example

Here we discuss some conventions regarding file structure and setup of imports, namespace, etc.

We use separate files for

  1. ontologies (classes with properties and their cardinalities and ranges, subclass relationships, and rules)
  2. knowledge bases (facts/individuals/instances)
  3. queries

Some boilerplate items go in the beginning of each file.

  1. If we want OWL/SWRL compatibility, we put this directive at the beginning of the file (as discussed in Directives).
:-setsemantics{inheritance(none)}.
  1. Import statements. The “KB” files import the necessary “ontology” files that define the vocabulary they use. The “queries” files import the necessary “KB” files containing the KBs that are queried. These statements have the form shown in Imports. For OWL/SWRL compatibility, we also import the file core/owl_swrl in every file.
  2. A “loaded” statement, as also discussed in Imports.
  3. Namespace prefix declarations (see Directives) for all prefixes used in the file. It is safest to cut-and-paste them to avoid typos, or put them all in a file to be imported. Mistakes due to erroneous prefix declarations can be very tricky to find and correct.

Other conventions:

  • if we don’t want to add any properties for a class, we put an empty frame - required for some UI features. Example: Man [||].
  • we indent with two spaces (both for multiple lines containing multiple properties in a frame, and for lines in rule bodies)
  • we use “,” instead of \and between sub-goals in a rule or query
  • we use CamelCase for the names of individuals, classes, rules, etc. Individuals and classes start with an uppercase letter, properties with a lowercase letter.
  • we name all rules - required for some UI features and regression testing

Below, we show a small, complete example of these conventions. These files are also distributed with Sunflower Studio.

Example ontology file finance.flr

:- setsemantics{inheritance=none}.
loaded('flr/finance/finance').

?- \unless loaded('core/owl_swrl') \do \add('core/owl_swrl'>>main).

:- iriprefix{finance =
 'http://www.sri.com/ror/financial/ontologies/finance/finance.flr#'}.

finance#InvestmentAccount [||].

finance#InvestmentPurchase [|
  finance#account {1..1} => finance#InvestmentAccount,
  finance#executionTime {1..1} => \dateTime,
  finance#principal {1..1} => \double,
  finance#asset {1..1} => finance#Asset |].

@!{TotalPurchaseAmount}
finance#purchaseTotal(?total, ?account) :-
  ?account : finance#InvestmentAccount,
  ?total = sum{?principal | ?purchase :
   finance#InvestmentPurchase [ finance#account ->
   ?account, finance#principal-> ?principal ] }.

Example knowledge base file finance_kb.flr

:- setsemantics{inheritance=none}.
loaded('flr/finance/finance_kb').

?- \unless loaded('core/owl_swrl') \do \add('core/owl_swrl'>>main).
?- \unless loaded('flr/finance/finance') \do \add('flr/finance/finance'>>main).

:- iriprefix{finance =
 'http://www.sri.com/ror/financial/ontologies/finance/finance.flr#'}.
:- iriprefix{finance_kb =
 'http://www.sri.com/ror/financial/ontologies/finance/finance_kb.flr#'}.

finance_kb#AuntieValsInvestmentAccount : finance#InvestmentAccount.

finance_kb#'B5634-0117-1' : finance#InvestmentPurchase [
  finance#account -> finance_kb#AuntieValsInvestmentAccount,
  finance#executionTime -> "2013-01-16T12:30:00"^^\dateTime,
  finance#principal -> 2000000,
  finance#asset -> finance#'Apple' ].

finance_kb#'B5634-0117-2' : finance#InvestmentPurchase [
  finance#account -> finance_kb#AuntieValsInvestmentAccount,
  finance#executionTime -> "2013-02-16T12:31:00"^^\dateTime,
  finance#principal -> 1000000,
  finance#asset -> finance#'CVS'].

Example query file finance_demo.flr

:- setsemantics{inheritance=none}.
loaded('flr/finance/finance_demo').

?- \unless loaded('core/owl_swrl') \do \add('core/owl_swrl'>>main).
?- \unless loaded('flr/finance/finance_kb') \do \add('flr/finance/finance_kb'>>main).

:- iriprefix{finance =
 'http://www.sri.com/ror/financial/ontologies/finance/finance.flr#'}.

@!{'What is the total of all the purchases?'(?total, ?account)} !-
 finance#purchaseTotal(?total, ?account).