Skip to content

Commit

Permalink
adding example source files and configuration for the semanticunivers…
Browse files Browse the repository at this point in the history
…e article
  • Loading branch information
Michael Grove authored and Michael Grove committed Apr 23, 2010
1 parent b54f71d commit 9c244d6
Show file tree
Hide file tree
Showing 5 changed files with 495 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/examples.empire.annotation.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

com.clarkparsia.empire.annotation.RdfsClass=com.clarkparsia.empire.examples.Book, com.clarkparsia.empire.examples.Manifestation
javax.persistence.NamedQuery=
20 changes: 20 additions & 0 deletions examples/examples.empire.config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright (c) 2009-2010 Clark & Parsia, LLC. <http://www.clarkparsia.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

annotation.index = examples.empire.annotation.config

0.name = oreilly
0.factory = sesame
0.files = catalog.rdf
160 changes: 160 additions & 0 deletions examples/src/com/clarkparsia/empire/examples/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright (c) 2009-2010 Clark & Parsia, LLC. <http://www.clarkparsia.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.clarkparsia.empire.examples;

import com.clarkparsia.empire.annotation.Namespaces;
import com.clarkparsia.empire.annotation.RdfsClass;
import com.clarkparsia.empire.annotation.SupportsRdfIdImpl;
import com.clarkparsia.empire.annotation.RdfProperty;
import com.clarkparsia.empire.SupportsRdfId;

import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.FetchType;
import javax.persistence.CascadeType;

import java.util.Date;
import java.util.Collection;
import java.util.HashSet;
import java.net.URI;

/**
* <p>Example class from the Empire Semantic Universe Article</p>
*
* @author Michael Grove
*/
@Namespaces({"frbr", "http://vocab.org/frbr/core#",
"dc", "http://purl.org/dc/terms/",
"foaf", "http://xmlns.com/foaf/0.1/"})
@RdfsClass("frbr:Expression")
@Entity
public class Book implements SupportsRdfId {
/**
* Default support for the ID of an RDF concept
*/
private SupportsRdfId mIdSupport = new SupportsRdfIdImpl();

@RdfProperty("dc:title")
private String mTitle;

@RdfProperty("dc:publisher")
private String mPublisher;

@RdfProperty("dc:issued")
private Date mIssued;

@RdfProperty("foaf:primarySubjectOf")
private URI mPrimarySubjectOf;

@OneToMany(fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@RdfProperty("frbr:embodiment")
private Collection<Manifestation> mEmbodiments = new HashSet<Manifestation>();

public String getTitle() {
return mTitle;
}

public void setTitle(final String theTitle) {
mTitle = theTitle;
}

public String getPublisher() {
return mPublisher;
}

public void setPublisher(final String thePublisher) {
mPublisher = thePublisher;
}

public Date getIssued() {
return mIssued;
}

public void setIssued(final Date theIssued) {
mIssued = theIssued;
}

public URI getPrimarySubjectOf() {
return mPrimarySubjectOf;
}

public void setPrimarySubjectOf(final URI thePrimarySubjectOf) {
mPrimarySubjectOf = thePrimarySubjectOf;
}

public Collection<Manifestation> getEmbodiments() {
return mEmbodiments;
}

public void setEmbodiments(final Collection<Manifestation> theEmbodiments) {
mEmbodiments = theEmbodiments;
}

/**
* @inheritDoc
*/
public RdfKey getRdfId() {
return mIdSupport.getRdfId();
}

/**
* @inheritDoc
*/
public void setRdfId(final RdfKey theId) {
mIdSupport.setRdfId(theId);
}

@Override
public boolean equals(final Object theObj) {
if (this == theObj) {
return true;
}
if (theObj == null || getClass() != theObj.getClass()) {
return false;
}

final Book aBook = (Book) theObj;

if (getRdfId() != null) {
return getRdfId().equals( aBook.getRdfId() );
}
else {
if (mEmbodiments != null ? !mEmbodiments.equals(aBook.mEmbodiments) : aBook.mEmbodiments != null) {
return false;
}
if (mIssued != null ? !mIssued.equals(aBook.mIssued) : aBook.mIssued != null) {
return false;
}
if (mPrimarySubjectOf != null ? !mPrimarySubjectOf.equals(aBook.mPrimarySubjectOf) : aBook.mPrimarySubjectOf != null) {
return false;
}
if (mPublisher != null ? !mPublisher.equals(aBook.mPublisher) : aBook.mPublisher != null) {
return false;
}
if (mTitle != null ? !mTitle.equals(aBook.mTitle) : aBook.mTitle != null) {
return false;
}
}

return true;
}

@Override
public int hashCode() {
return getRdfId() == null ? 0 : getRdfId().value().hashCode();
}
}
168 changes: 168 additions & 0 deletions examples/src/com/clarkparsia/empire/examples/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2009-2010 Clark & Parsia, LLC. <http://www.clarkparsia.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.clarkparsia.empire.examples;

import com.clarkparsia.empire.Empire;
import com.clarkparsia.empire.impl.RdfQuery;
import com.clarkparsia.empire.sesametwo.OpenRdfEmpireModule;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import java.net.URI;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

/**
* <p></p>
*
* @author Michael Grove
*/
public class Main {
public static void main(String[] args) {
System.setProperty("empire.configuration.file", "examples.empire.config.properties");

// loads Sesame bindings for Empire
Empire.init(new OpenRdfEmpireModule());

// create an EntityManager for the specified persistence context
EntityManager aManager = Persistence.createEntityManagerFactory("oreilly")
.createEntityManager();

// this retrieves a particular book from the database
Book aBook = aManager.find(Book.class, URI.create("urn:x-domain:oreilly.com:product:9780596514129.IP"));

// prints: Switching to the Mac: The Missing Manual, Leopard Edition
System.err.println(aBook.getTitle());

// prints: O'Reilly Media / Pogue Press
System.err.println(aBook.getPublisher());

// creating a new book:
Book aNewBook = new Book();
aNewBook.setIssued(new Date());
aNewBook.setTitle("How to use Empire");
aNewBook.setPublisher("Clark & Parsia");
aNewBook.setPrimarySubjectOf(URI.create("http://github.com/clarkparsia/Empire"));

// grab the ebook manifestation
Manifestation aEBook = aManager.find(Manifestation.class, URI.create("urn:x-domain:oreilly.com:product:9780596104306.EBOOK"));

// and we'll use it as the embodiment of our new book.
aNewBook.setEmbodiments(Arrays.asList(aEBook));

// save the new book to the database
aManager.persist(aNewBook);

Book aNewBookCopy = aManager.find(Book.class, aNewBook.getRdfId());

// true!
System.err.println(aNewBook.equals(aNewBookCopy));

// lets edit our book...
// maybe we changed the title and published as a PDF
aNewBook.setTitle("Return of the Empire");

// create a new manifestation
Manifestation aPDFManifestation = new Manifestation();
aPDFManifestation.setIssued(new Date());
// set the dc:type attribute
aPDFManifestation.setType(URI.create("http://purl.oreilly.com/product-types/PDF"));

aNewBook.setEmbodiments(Arrays.asList(aPDFManifestation));

// now save our edits
aManager.merge(aNewBook);

// print the new information we just saved
System.err.println(aNewBook.getTitle());
System.err.println(aNewBook.getEmbodiments());

// and importantly, verify that the new manifestation was also saved due to the cascaded merge operation
// specified in the Book class via the @OneToMany annotation

// true!
System.err.println(aManager.contains(aPDFManifestation));

// the copy of the book contains the old information
System.err.println(aNewBookCopy.getTitle());
System.err.println(aNewBookCopy.getEmbodiments());

// but can be refreshed...
aManager.refresh(aNewBookCopy);

// and now contains the correct, up-to-date information
System.err.println(aNewBookCopy.getTitle());
System.err.println(aNewBookCopy.getEmbodiments());

// now we can delete our new book
aManager.remove(aNewBook);

// false!
System.err.println(aManager.contains(aNewBook));

// but the new manifestation still exists, since we did not specify that deletes should cascade...

// true!
System.err.println(aManager.contains(aPDFManifestation));

// Lastly, we can use the query API to run arbitrary sparql queries
// create a jpql-style partial SPARQL query (JPQL is currently unsupported)
Query aQuery = aManager.createQuery("where { ?result frbr:embodiment ?manifest." +
" ?foo <http://purl.org/goodrelations/v1#typeOfGood> ?manifest . " +
" ?foo <http://purl.org/goodrelations/v1#hasPriceSpecification> ?price. " +
" ?price <http://purl.org/goodrelations/v1#hasCurrencyValue> ?value. " +
" ?price <http://purl.org/goodrelations/v1#hasCurrency> \"USD\"@en." +
" filter(?value > ??min). }");

// this query should return instances of type Book
aQuery.setHint(RdfQuery.HINT_ENTITY_CLASS, Book.class);

// set the parameter in the query to the value for the min price
// parameters are prefixed with ??
aQuery.setParameter("min", 30);

// now execute the query to get the list of all books which are $30 USD
List aResults = aQuery.getResultList();

// 233 results
System.err.println("Num Results: " + aResults.size());

// print the titles of the first five results
for (int i = 0; i < 5; i++) {
Book aBookResult = (Book) aResults.get(i);
System.err.println(aBookResult.getTitle());
}

/*
* Switching to the Mac: The Missing Manual, Leopard Edition
* O'Reilly Media / Pogue Press
* true
* Return of the Empire
* [http://purl.oreilly.com/product-types/PDF]
* true
* How to use Empire
* [http://purl.oreilly.com/product-types/EBOOK]
* Return of the Empire
* [http://purl.oreilly.com/product-types/PDF]
* false
* true
*
*/
}
}
Loading

0 comments on commit 9c244d6

Please sign in to comment.