From 9c244d6bda3866e5a9ac8b72a6d658730001415b Mon Sep 17 00:00:00 2001 From: Michael Grove Date: Fri, 23 Apr 2010 15:30:15 -0400 Subject: [PATCH] adding example source files and configuration for the semanticuniverse article --- examples/examples.empire.annotation.config | 3 + examples/examples.empire.config.properties | 20 +++ .../com/clarkparsia/empire/examples/Book.java | 160 +++++++++++++++++ .../com/clarkparsia/empire/examples/Main.java | 168 ++++++++++++++++++ .../empire/examples/Manifestation.java | 144 +++++++++++++++ 5 files changed, 495 insertions(+) create mode 100644 examples/examples.empire.annotation.config create mode 100644 examples/examples.empire.config.properties create mode 100644 examples/src/com/clarkparsia/empire/examples/Book.java create mode 100644 examples/src/com/clarkparsia/empire/examples/Main.java create mode 100644 examples/src/com/clarkparsia/empire/examples/Manifestation.java diff --git a/examples/examples.empire.annotation.config b/examples/examples.empire.annotation.config new file mode 100644 index 0000000..c9005ee --- /dev/null +++ b/examples/examples.empire.annotation.config @@ -0,0 +1,3 @@ + +com.clarkparsia.empire.annotation.RdfsClass=com.clarkparsia.empire.examples.Book, com.clarkparsia.empire.examples.Manifestation +javax.persistence.NamedQuery= diff --git a/examples/examples.empire.config.properties b/examples/examples.empire.config.properties new file mode 100644 index 0000000..eb38312 --- /dev/null +++ b/examples/examples.empire.config.properties @@ -0,0 +1,20 @@ +# +# Copyright (c) 2009-2010 Clark & Parsia, LLC. +# +# 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 \ No newline at end of file diff --git a/examples/src/com/clarkparsia/empire/examples/Book.java b/examples/src/com/clarkparsia/empire/examples/Book.java new file mode 100644 index 0000000..661fa8d --- /dev/null +++ b/examples/src/com/clarkparsia/empire/examples/Book.java @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2009-2010 Clark & Parsia, LLC. + * + * 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; + +/** + *

Example class from the Empire Semantic Universe Article

+ * + * @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 mEmbodiments = new HashSet(); + + 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 getEmbodiments() { + return mEmbodiments; + } + + public void setEmbodiments(final Collection 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(); + } +} diff --git a/examples/src/com/clarkparsia/empire/examples/Main.java b/examples/src/com/clarkparsia/empire/examples/Main.java new file mode 100644 index 0000000..17e5077 --- /dev/null +++ b/examples/src/com/clarkparsia/empire/examples/Main.java @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2009-2010 Clark & Parsia, LLC. + * + * 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; + +/** + *

+ * + * @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 ?manifest . " + + " ?foo ?price. " + + " ?price ?value. " + + " ?price \"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 + * + */ + } +} diff --git a/examples/src/com/clarkparsia/empire/examples/Manifestation.java b/examples/src/com/clarkparsia/empire/examples/Manifestation.java new file mode 100644 index 0000000..1904457 --- /dev/null +++ b/examples/src/com/clarkparsia/empire/examples/Manifestation.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2009-2010 Clark & Parsia, LLC. + * + * 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.RdfProperty; +import com.clarkparsia.empire.annotation.SupportsRdfIdImpl; +import com.clarkparsia.empire.SupportsRdfId; + +import javax.persistence.Entity; + +import java.util.Date; +import java.net.URI; + +/** + *

+* +* @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:Manifestation") +@Entity +public class Manifestation implements SupportsRdfId { + /** + * Default support for the ID of an RDF concept + */ + private SupportsRdfId mIdSupport = new SupportsRdfIdImpl(); + + @RdfProperty("dc:issued") + private Date mIssued; + + @RdfProperty("dc:type") + private URI mType; + + @RdfProperty("dc:identifier") + private URI mDcId; + + @RdfProperty("dc:extent") + private String mExtent; + + public Date getIssued() { + return mIssued; + } + + public void setIssued(final Date theIssued) { + mIssued = theIssued; + } + + public URI getType() { + return mType; + } + + public void setType(final URI theType) { + mType = theType; + } + + public URI getDcId() { + return mDcId; + } + + public void setDcId(final URI theDcId) { + mDcId = theDcId; + } + + public String getExtent() { + return mExtent; + } + + @Override + public String toString() { + return mType != null ? mType.toString() : getRdfId().toString(); + } + + public void setExtent(final String theExtent) { + mExtent = theExtent; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + final Manifestation aObj = (Manifestation) o; + + if (getRdfId() != null) { + return getRdfId().equals( aObj.getRdfId() ); + } + else { + if (mDcId != null ? !mDcId.equals(aObj.mDcId) : aObj.mDcId != null) { + return false; + } + if (mExtent != null ? !mExtent.equals(aObj.mExtent) : aObj.mExtent != null) { + return false; + } + if (mIssued != null ? !mIssued.equals(aObj.mIssued) : aObj.mIssued != null) { + return false; + } + if (mType != null ? !mType.equals(aObj.mType) : aObj.mType != null) { + return false; + } + } + + return true; + } + + @Override + public int hashCode() { + return getRdfId() == null ? 0 : getRdfId().value().hashCode(); + } + + /** + * @inheritDoc + */ + public RdfKey getRdfId() { + return mIdSupport.getRdfId(); + } + + /** + * @inheritDoc + */ + public void setRdfId(final RdfKey theId) { + mIdSupport.setRdfId(theId); + } +}