.. _section-example: Example ======= Here we discuss some conventions regarding file structure and setup of imports, namespace, etc. We use separate files for #. ontologies (classes with properties and their cardinalities and ranges, subclass relationships, and rules) #. knowledge bases (facts/individuals/instances) #. 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 :ref:`section-Directives`). .. code-block:: flora :- setsemantics{inheritance(none)}. 2) 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 :ref:`section-Imports`. For OWL/SWRL compatibility, we also import the file core/owl\_swrl in every file. 3) A “loaded” statement, as also discussed in :ref:`section-Imports`. 4) Namespace prefix declarations (see :ref:`section-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. .. _section-Example ontology file finance.flr: Example ontology file finance.flr --------------------------------- .. code-block:: flora :- 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 ] }. .. _section-Example knowledge base file finance\_kb.flr: Example knowledge base file finance\_kb.flr ------------------------------------------- .. code-block:: flora :- 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']. .. _section-Example query file finance\_demo.flr: Example query file finance\_demo.flr ------------------------------------ .. code-block:: flora :- 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).