Data_Tables and Queries

Accessing Internal Data

samples/basic/tutorial/products.kb and products_1.mdb

Some knowledgebases need to reason over data that can be stored in an internal data table. To do this, KnowledgeWright provides data_table objects and the find() function. Data_Tables are generally more flexible than external database tables because each column can contain different types of data, i.e. strings and numbers. Data_Tables are also more powerful because entries can consist of formulas and object names. In this case, the KnowledgeWright reasoning engine is called to determine a value before the find() function returns a value.

Create the Data_Table and Find() Query

To illustrate this, we will query a data_table to determine the upgrade price for a product. First we need to build a knowledgebase with a data_table to contain the pricing information and a fact whose value is the result of a find() function that queries the data_table:

  1. Create a new knowledgebase named products.
  2. Set the goal in the main knowledgebase object to upgrade_price.
  3. Create a new data_table named upgrades.
  4. In upgrades, select Column/Add to create 4 columns: product, version, new_product and price. You can drag the column headers left and right to arrange them in the order you would like.
  5. Use Row/Add to enter the following data:
    old_product
    version
    new_product
    price
    "ap1pc"
    "4.0"
    "ap1pc"
    0
    "ap1pc"
    "4.1"
    "ap1pc"
    0
    "ap1pc"
    "5.0"
    "ap1pc"
    0
    "ap1pc"
    "4.0"
    "apxpc"
    2500
    "ap1pc"
    "4.1"
    "apxpc"
    2500
    "ap1pc"
    "5.0"
    "apxpc"
    2500
    "apxpc"
    "4.0"
    "apxpc"
    1500
    "apxpc"
    "4.1"
    "apxpc"
    1000
    "apxpc"
    "5.0"
    "apxpc"
    0
  6. Create new fact named price. For it's value enter:
    find(price from upgrades where old_product = old_product and version = old_version and new_product = new_product)

Ask the Questions and Display the Answer

Our knowledgebase asks the three questions, old_product, old_version and new_product used in the find() function above. Using the answers to those questions, the fact price gets the price. The price is displayed in a short document.

  1. Create a new knowledgebase named products.
  2. Set the goal in the main knowledgebase object to upgrade_price.
  3. Create the three questions:
  4. Create a text object named upgrade_price with text that reads:
    The price for this upgrade is %price%.
  5. Run the knowledgebase.

Note, you can use the find() function anywhere any object name is allowed. You do not have to create a fact object to issue a query In this case we needed a separate fact object because the result of query is embedded in a text object.




Copyright ©1995-2006 Amzi! inc. All Rights Reserved. Privacy Policy.
Amzi!, KnowledgeWright and WebLS are registered trademarks and Adventure in Prolog, Logic Server, Logic Explorer, Adventure in Prolog and Subscription Plus are trademarks of Amzi! inc.