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

AGPrologFunctorQminus.java

package com.franz.ag.examples;

import com.franz.ag.*;

public class AGPrologFunctorQminus {

    /**
     * Demonstrates some basics of using the Prolog functor q- to query 
     * triple stores.
     * 
     * @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("functorqminus", AGPaths.TRIPLE_STORES);

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

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

        // Find all of the children of person1
        String pquery = "(?x)(q !ex:person1 !ex:has-child ?x)";
        AGUtils.doPrologQuery(ts, pquery);

        // Find all of the grand children of person1
        pquery = "(?y)(q !ex:person1 !ex:has-child ?x)(q ?x !ex:has-child ?y)";
        AGUtils.doPrologQuery(ts, pquery);

        // Find all of the grand children of person1 that have spouses
        pquery = "(?y ?z)" + "(q !ex:person1 !ex:has-child ?x)"
                + "(q ?x !ex:has-child ?y)" + "(q ?y !ex:spouse ?z)";
        AGUtils.doPrologQuery(ts, pquery);

        // Find parents and children with the same first name 
        pquery = "(?x ?y)" +
                 "(q ?x !ex:first-name ?n1)" +
                 "(q ?x !ex:has-child ?y)" +
                 "(q ?y !ex:first-name ?n2)" +
                 "(= ?n1 ?n2)";
        AGUtils.doPrologQuery(ts, pquery);

        // Find parents and children that went to ivy league schools
        pquery = "(?x ?y)" +
                 "(q ?x !ex:alma-mater ?am)" +
                 "(q ?am !ex:ivy-league !ex:true)" +
                 "(q ?x !ex:has-child ?y)" +
                 "(q ?y !ex:alma-mater ?am2)" +
                 "(q ?am2 !ex:ivy-league !ex:true)";
        AGUtils.doPrologQuery(ts, pquery);
        
        // Find those that attended the same ivy league school
        pquery = "(?x ?y)" +     
                 "(q ?x !ex:alma-mater ?am)" +
                 "(q ?am !ex:ivy-league !ex:true)" +
                 "(q ?x !ex:has-child ?y)" +
                 "(q ?y !ex:alma-mater ?am)";
        AGUtils.doPrologQuery(ts, pquery);
        
        // Close the triple store and disconnect from the server.
        ts.closeTripleStore();
        ags.disable();
    }
}

Up | Next

 

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