AWS Database Blog

Create a Knowledge Graph application with metaphactory and Amazon Neptune

metaphactory is a highly configurable, low-code user interface system for searching, browsing and exploring RDF Knowledge Graphs.

Amazon Neptune fully managed graph database service built for the cloud, that can store RDF Knowledge Graphs of 100’s of billions of triples.

In a previous post, we described how to connect Amazon Neptune to metaphactory, securely, and then how to explore and search the Neptune graph data using metaphactory.

Visit the metaphactory website to understand security best practices for user access to metaphactory, and connecting metaphactory to Neptune.

In this post, we show how you can use metaphactory to build an end user application using its dynamic model driven components, driven by SPARQL queries.

Prerequisites

You should have access to metaphactory, connected to an Amazon Neptune cluster in your AWS account, and an understating of how to construct HTML pages.

You can follow this guide without any previous experience of the RDF data model and SPARQL query language, but a general understanding of these technologies is recommended for context.

Solution overview

The solution in this post allows you to perform the following actions:

  • Build end-user search interfaces
  • Use templates to create rich data visualizations
  • Make SPARQL queries to Neptune’s SPARQL endpoint
  • Use SPARQL queries to define model driven user interface components

Setting up a Custom Search Page with metaphactory

Customers are responsible for the costs of services used. For help with estimating costs of AWS services, visit the AWS Pricing Calculator.

For help estimating costs of metaphactory, visit the metaphactory product page on AWS marketplace.

In addition to its out-of-the-box capabilities for searching and exploring data, metaphactory also offers an extensive set of Web components for building domain-specific end-user applications on top of knowledge graphs.

The first step in this model-driven approach is to define an Ontology that describes the concepts and relations in a specific domain. The Ontology helps define the model that application engineers use to build end-user interfaces. For this post we use a public ontology describing the model of Nobel prize winners.

We perform the following steps to demonstrate how to construct a search interface:

  • Replace the existing start page content
  • Make the search input appear on the metaphactory start page
  • Set the search input to be the main entry point into the Nobel prize dataset

metaphactory offers a set of components or building blocks that can be put together to form the user interface, such as tables, charts, and search features. Pages can be populated with components by inserting special HTML tags. For example, if you type in <semantic-table></semantic-table> into the Template Page Editor, this will instruct metaphactory to load the table component; the table will display the data specified in the query in the component’s configuration.

metaphactory comes with wizards that simplify the process of configuring components so that you can visually set up and configure components by referencing specific parts of the model.

To replace the existing start page content, choose the “metaphacts” logo situated in the top left corner of the application header. Then, to start editing the page content, click “Edit Page”.

Edit metaphactory page screenshot

  1. Now in Edit mode, choose the drop-down menu located at the top of the Template Page Editor and select the Search Wizard.
  2. In the popup, on the Search Wizard’s first step, keep the default options and choose “Next: Select classes”.
    Search wizard - select features
  3. On the next step, choose the “Nobel Prize” and “Laureate” classes and then choose “Next: Generate Configuration”.
    Search wizard - select Classes
  4. Preview how the user interface will look like and approve the Wizard results by choosing “Next: Insert configuration”.

Now the configuration is inserted in the Template Page Editor, we can save the template. Choose “Save & View” and view the result on the page.

Enter the name of a laureate, or a year to see results from this search.

Search wizard - complete

The wizard used the underlying ontology to help create a new interface from scratch that delivers a powerful search for end users. For more information refer to the dedicated metaphactory blog post about wizards.

Metaphactory Templates

In this guide, we use pre-created metaphactory templates that already contain lots of components, which we will edit. For more information about creating templates from scratch, visit the application building section of the metaphactory documentation.

Using metaphactory templates, you can assign a template to any resource in the knowledge graph. Usually, the templates are created for classes of an ontology, allowing all instances of that class to automatically inherit the template and be visualized in a uniform manner.

For example, you could have one template for the Laureate class, and one for the NobelPrize class. This means that every time you load a page for a Laureate, or a NobelPrize, the relevant HTML template will be used, rendering different default layouts for different class types.

With the help of templates, application engineers create custom knowledge views that match their end users’ interaction patterns and needs.

For example, if you enter the term “Curie” into the new search box, from the results, select “Marie Curie, née Sklodowska”.

On the “Marie Curie, née Sklodowska” page, we see the graph navigation component.

Graph navigation component

Edit the templates for the “Laureate” and “Nobel Prize” classes

Switch to the “Page” view, highlighted as “1” in the following screenshot, and then choose “Edit Page”, highlighted as “2” in the following screenshot.

how to open page editor

The Template Page Editor displays the list of classes to which this particular instance belongs to. We select the “Template:http://data.nobelprize.org/terms/Laureate” class.

Template page editor - laureate class

In the Template Page Editor for this class, insert the Laureate Template code snippet from the metaphactory GitHub repository, then choose “Save and View”.

The template is automatically applied to all instances of the “Laureate” class. The page of Marie Curie now shows in-page navigation, maps, lists, and timelines.

Published template page for Marie Curie

Apply the Nobel Prize Template code snippet from the GitHub repository for the Nobel Prize class the same way we did for “Laureate”, then choose “Save and View”.

Marie Curie page now including Nobel prize

Now we have set templates for Laureates and Nobel Prizes, we can navigate from Marie Curie to the page of the Nobel prize in Chemistry 1911 and then from there onwards to other resources, such as a prize awarded in the same year but in literature, to the famous playwright Maurice Maeterlinck.

Querying the data with metaphactory

metaphactory uses the SPARQL query language to dynamically configure almost every component in the platform. To access the SPARQL query editor, choose the “SPARQL” button located in the application header.

To learn more about the SPARQL query language, refer to the open source Amazon Neptune graph-notebook project, specifically the SPARQL tutorial section. You can run and access a graph notebook in many ways including via the AWS console.

Open the SPARQL editor and execute the following query to display all laureates in all categories:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX nbp: <http://data.nobelprize.org/terms/> 

SELECT DISTINCT ?nobelPrize ?category ?laureate 

WHERE { 

    ?nobelPrize nbp:category ?Category; 
                nbp:year     ?year; 
                nbp:laureate ?laureate. 
    
    ?Category   rdfs:label   ?category. 

}
SPARQL editor

The results to a SPARQL query are first returned in JSON format, which describes a list of ‘bindings’ for each ‘head’ returned. The following snippet is an example.

{
  "head": { "vars": [ "book" , "title" ]
  } ,
  "results": { 
    "bindings": [
      {
        "book": { "type": "uri" , "value": "http://example.org/book/book6" } ,
        "title": { "type": "literal" , "value": "Harry Potter and the Half-Blood Prince" }
      } ,
      {
        "book": { "type": "uri" , "value": "http://example.org/book/book7" } ,
        "title": { "type": "literal" , "value": "Harry Potter and the Deathly Hallows" }
      }
   ]
  }
}

metaphcatory converts this JSON response into an HTML table, before rendering it back to the user in an easy-to-read format.

The query result is displayed in a table with three columns: Nobel Prize, Category, and Laureate.

The SELECT clause of the query defines the variables that will be the response columns, while the WHERE clause provides the bindings between the variables and the resources of the knowledge graph.

Filtering the query for a specific category and year

The query above returns all the Nobel Prizes, their corresponding categories, and their laureates. But the result set might be too big, so we introduce additional FILTER clauses to our query to limit the number of rows.

Execute the following query to return only the Nobel Prizes in Physics and that were awarded in 2018:

PREFIX rdfs: 		<http://www.w3.org/2000/01/rdf-schema#> 
PREFIX nbp: 		<http://data.nobelprize.org/terms/> 
PREFIX dcterms: 	<http://purl.org/dc/terms/>

SELECT DISTINCT ?nobelPrize ?category ?laureate WHERE { 
	
		?nobelPrize 	nbp:year 	?year. 
		?s 		?p 		?year. 
		
		?s		^dcterms:hasPart/dcterms:category 	?Category; 
				nbp:laureate					?laureate. 
		
		?s		^dcterms:hasPart	?nobelPrize. 

		?Category 	rdfs:label		?category. 
		
		FILTER(?category ="Physics") 
		FILTER(?year =2018) 

}

Add a new table to the Nobel Prize template page

By entering a new “semantic-table” with a SPARQL query into the Nobel Prize template page, we can show all the Nobel Prizes that were awarded in the same category as the current instance.

(For example, it will show all other Nobel Prizes in physics if the currently viewed Nobel Prize is in the Physics category).

Open the Nobel Prize template page, and place the cursor at the location shown here:

Template page editor, highlighted insert location

Insert the following <semantic-table> HTML element at the location of the arrow:

<semantic-table query=
' 
	PREFIX rdfs: 		<http://www.w3.org/2000/01/rdf-schema#> 
	PREFIX nbp: 		<http://data.nobelprize.org/terms/> 
	PREFIX dcterms: 		<http://purl.org/dc/terms/>
		
	SELECT DISTINCT ?nobelPrize ?category ?laureate ?motivation ?year ?share WHERE { 
	
		<{{page-resource}}> 	nbp:category 	?category . 
		?nobelPrize 	nbp:category 	?category . 
		?nobelPrize	nbp:laureate 	?laureate . 
	
		?laureate		nbp:laureateAward 	?award . 
		?nobelPrize 	dcterms:hasPart 	?award .
	
		?award 		nbp:motivation 	?motivation ; 
				nbp:share 		?share ; 
				nbp:year 		?year . 

		# ?s 		^dcterms:hasPart/nbp:year 	?year; 
		# 		nbp:laureate		?laureate;
		# 		nbp:motivation		?motivation. 
		# ?s 		^dcterms:hasPart		?nobelPrize. 
		# ?laureate 	^nbp:laureate/nbp:share 	?share. 

		FILTER(LANG(?motivation)="en") 
		FILTER(?nobelPrize != <{{page-resource}}>)
	 
	} 
	ORDER BY DESC (?year)	 
' 
> 
</semantic-table>

Now all the Nobel Prize pages will show this additional table, and the table will automatically render links to all the other Nobel Prize and Laureate pages that are in the table.

Nobel prize page with new table visible

Cleanup

AWS resources that are no longer required should be deleted to avoid unnecessary costs.

metaphactory is available in the AWS marketplace, and it runs on Amazon EC2.

If you no longer need metaphactory, navigate to the Amazon EC2 console, find the instance that metaphactory is running on, and terminate the instance.

Summary

In this post, we used metaphactory on top of Amazon Neptune to create a small knowledge graph application that visualizes the data about laureates and Nobel Prizes.

You can explore some of the Nobel Prize dataset further in the metaphactory Nobel Prize dataset live demo system. To reproduce the examples described in the post or extend the app further, first see the related post on how to connect Amazon Neptune to metaphactory, and then refer to the metaphactory documentation for more information.

For further features of metaphactory, refer to the tutorials on Ontology Modeling and Application Building or dive into the blog posts on individual features and use cases.

For further information about the Amazon Neptune managed graph database, visit the Amazon Neptune home page.


About the Authors

Charles IvieCharles Ivie is a Senior Graph Architect with the Amazon Neptune team at AWS. As a highly respected expert within the knowledge graph community, he has been designing, leading, and implementing graph solutions for over 15 years.

Dmitry PavlovDmitry Pavlov is Director of Customer Success at metaphacts. He is responsible for making sure that our clients have a positive and rewarding experience with metaphactory and he drives many customer-oriented product improvement initiatives to make this experience even smoother.