franz inc logo  
  download learning center franz inc search franz inc resources franz inc          

allegrograph
racer
tbc
services
support
  Learning Center
  Documentation
  Updates
     Info
  FAQ
about

RSS Feeds

AllegroServe at opensource.franz.com

AGPrologRules.java

package com.franz.ag.examples;

import com.franz.ag.*;


public class AGPrologRules {

    /**
     * Demonstrates some basics of using Horn rules in Prolog.
     * 
     * @param unused
     * @throws AllegroGraphException 
     */
    public static void main(String[] args) throws AllegroGraphException {
        
        // Connect to server, which must already be running.
        AllegroGraphConnection ags = new AllegroGraphConnection();
        try {
            ags.enable();
        } catch (Exception e) {
            throw new AllegroGraphException("Server connection problem", e);
        }

        // Create a fresh triple store for this example. 
        AllegroGraph ts = ags.renew("prologrules", AGPaths.TRIPLE_STORES);

        // Load the Kennedy data
        AGLoadNtriples.loadNTriplesWithTiming(ts, AGPaths.dataSources("kennedy.ntriples"));

        // Index the store for faster querying
        AGIndexAllTriples.indexAllTriplesWithTiming(ts);
        
        // Register any namespaces
        ts.registerNamespace("ex", "http://example.org/kennedy/");

        // Add some horn rules for use in queries.  The <-- operator 
        // overwrites any previous definitions for the clause head.
        addPrologRule("(<-- (male ?x) (q ?x !ex:sex !ex:male))", ags);
        addPrologRule("(<-- (female ?x) (q ?x !ex:sex !ex:female))", ags);
        addPrologRule("(<-- (father ?x ?y) (male ?x) (q ?x !ex:has-child ?y))", ags);
        addPrologRule("(<-- (mother ?x ?y) (female ?x) (q ?x !ex:has-child ?y))", ags);
        
        // Query the store for all sons of person1. 
        String pquery = "(?x)" + "(q !ex:person1 !ex:has-child ?x)" + "(male ?x)";
        AGPrologSelect.doPrologSelect(ts, pquery);
        
        // Query the store for mothers and sons
        pquery = "(?m ?s)" + "(mother ?m ?s) (male ?s)";
        AGPrologSelect.doPrologSelect(ts, pquery);
        
        // Now for fathers and daughters
        pquery = "(?f ?d)" + "(father ?f ?d) (female ?d)";
        AGPrologSelect.doPrologSelect(ts, pquery);
        
        // Close the triple store and disconnect from the server.
        ts.closeTripleStore();
        ags.disable();
    }

    public static void addPrologRule(String string, AllegroGraphConnection ags) throws AllegroGraphException {
        // Add rule by calling evalInServer.
        // Note that adding rules affects the entire server.  I.e.,
        // you may overwrite a rule or redefine a functor used by another
        // AllegroGraph client.
        AGEvalInServer.evalInServer("(enable-!-reader)",ags);
        AGEvalInServer.evalInServer(string,ags);
    }

}

Up | Next

 

© 2008 Franz Inc - Privacy Statement
[ Consulting Services | Franz | TopQuadrant | Racer Systems ]