Author: [email protected]
Date: Mon Oct 31 12:35:25 2011
New Revision: 1687
Log:
Merge semantic-typed with service as maven doesn't allow us to have this kind
of cycle in projects :-(AMDATUSEMANTICWEB-1).
Added:
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Entity.java
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/EntityConstraint.java
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Query.java
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Row.java
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Util.java
Removed:
trunk/amdatu-semanticweb/semantic-query-typed/
Modified:
trunk/amdatu-semanticweb/semanticservice-rdf2go/pom.xml
Modified: trunk/amdatu-semanticweb/semanticservice-rdf2go/pom.xml
==============================================================================
--- trunk/amdatu-semanticweb/semanticservice-rdf2go/pom.xml (original)
+++ trunk/amdatu-semanticweb/semanticservice-rdf2go/pom.xml Mon Oct 31
12:35:25 2011
@@ -14,12 +14,13 @@
<name>Amdatu Semantic Web Service - RDF 2 GO</name>
<description>This bundle provides the semanticservice based on RDF 2 GO
Sesame</description>
<dependencies>
- <dependency>
- <groupId>org.amdatu.semanticweb</groupId>
- <artifactId>org.amdatu.semanticweb.semantic.query.typed</artifactId>
- <version>0.2.0-SNAPSHOT</version>
+<dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.3</version>
+ <scope>compile</scope>
</dependency>
- <dependency>
+ <dependency>
<groupId>org.amdatu.semanticweb</groupId>
<artifactId>org.amdatu.semanticweb.rdf2go.sesame</artifactId>
<version>0.2.0-SNAPSHOT</version>
@@ -38,13 +39,15 @@
<Private-Package>
org.amdatu.semantic.impl
</Private-Package>
+<Embed-Dependency>commons-codec;scope=compile;inline=true</Embed-Dependency>
<Import-Package>
org.apache.felix.dm;provide:=true,*
</Import-Package>
<Export-Package>
org.amdatu.semantic;provide:=true,
org.amdatu.semantic.query;provide:=true,
- org.amdatu.semantic.namespaces;provide:=true
+ org.amdatu.semantic.namespaces;provide:=true,
+ org.amdatu.semantic.query.typed
</Export-Package>
</instructions>
</configuration>
Added:
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Entity.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Entity.java
Mon Oct 31 12:35:25 2011
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.semantic.query.typed;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.amdatu.semantic.Model;
+import org.amdatu.semantic.query.typed.Row;
+import org.amdatu.semantic.query.Constraint;
+import org.amdatu.semantic.query.typed.Query;
+import org.amdatu.semantic.query.Selector;
+
+/**
+ * The Entity class is the basis for creating new entities based on RDF nodes.
It provides helper methods
+ * for getting and setting typed data.
+ *
+ * To build your own Entity class,
+ * <ul>
+ * <li>extend this class</li>
+ * <li>add a (String, Model) constructor</li>
+ * <li>add a static <code>create</code> method to set up the model with a new
instance of your entity</li>
+ * <li>add getters and setters for what you deem necessary</li>
+ * <li>add EntityConstraint generating methods, which help in building
queries</li>
+ * </ul>
+ */
+public abstract class Entity {
+ protected final Constraint.Uri m_uri;
+ protected final Model m_model;
+
+ public Entity(String uri, Model model) {
+ m_uri = Constraint.uri(uri);
+ m_model = model;
+ }
+
+ public Constraint.Uri getUri() {
+ return m_uri;
+ }
+
+ protected <T> T get(Constraint.Predicate predicate, Class<T> resultClass) {
+ Query q = new Query()
+ .select(Query.selector("anything"))
+ .where(new Constraint(m_uri, predicate,
(Constraint.variable("anything"))));
+
+ // TODO what do we get back? Do we get too many?
+ for (Row r : q.select(m_model)) {
+ String value = r.get("anything");
+ return Util.makeNew(resultClass, value, m_model);
+ }
+ return null;
+ }
+
+ protected void remove(Constraint.Predicate predicate, Object value) {
+ if (value == null) {
+ return;
+ }
+ m_model.remove(m_uri.toString(), predicate.toString(),
value.toString());
+ }
+
+ protected void set(Constraint.Predicate predicate, Object value) {
+ m_model.add(m_uri.toString(), predicate.toString(), value.toString());
+ }
+
+ protected <T> Collection<T> getCollection(Constraint.Predicate predicate,
Class<T> resultClass) {
+ return getCollection(new Query()
+ .select(Query.selector("anything"))
+ .where(new Constraint(m_uri, predicate,
(Constraint.variable("anything")))),
+ Query.selector("anything", resultClass));
+ }
+
+ /**
+ * Creates a collection based on the query, using the selector to find the
variable
+ * and type to use.
+ */
+ protected <T> Collection<T> getCollection(Query query, Selector<T>
selector) {
+ List<T> result = new ArrayList<T>();
+
+ for (Row r : query.select(m_model)) {
+ result.add(r.get(selector));
+ }
+
+ return result;
+ }
+}
\ No newline at end of file
Added:
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/EntityConstraint.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/EntityConstraint.java
Mon Oct 31 12:35:25 2011
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.semantic.query.typed;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.commons.codec.binary.Hex;
+
+import org.amdatu.semantic.query.Constraint;
+import org.amdatu.semantic.query.Selector;
+
+/**
+ * An {@link EntityConstraint} works as both a typed {@link Constraint} to be
used in {@link Query}s, and as a
+ * wrapper for a {@link Selector}, which allows you to get typed values out of
a {@link Row}.
+ */
+public class EntityConstraint<T> extends Constraint implements Selector<T> {
+ private volatile Constraint.Variable m_variable =
Constraint.variable(getIdentifier());
+ private Class<T> m_selectorClass;
+
+ public EntityConstraint(Value value, Class<T> selectorClass) {
+ super(value);
+ m_selectorClass = selectorClass;
+ }
+
+
+ protected EntityConstraint<T> withVariable(Predicate predicate, Variable
object, Class<T> selectorClass) {
+ m_variable = object;
+ return with(predicate, object, selectorClass);
+ }
+
+ protected String getIdentifier() {
+ try {
+ return (getSubject() instanceof Constraint.Uri) ?
+ new
String(Hex.encodeHex(getSubject().getValue().getBytes("UTF-8"))) :
getSubject().getValue();
+ }
+ catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("Im not a real java - i just think i
am.");
+ }
+ }
+
+ public <TYPE> EntityConstraint<TYPE> with(Predicate predicate, Value
object, Class<TYPE> selectorClass) {
+ if (object instanceof Variable) {
+ m_variable = (Variable) object;
+ m_selectorClass = (Class<T>) selectorClass;
+ }
+ super.with(predicate, object);
+ return (EntityConstraint<TYPE>) this;
+ }
+
+ public <TYPE> EntityConstraint<TYPE> as(Class<TYPE> selectorClass) {
+ m_selectorClass = (Class<T>) selectorClass;
+ return (EntityConstraint<TYPE>) this;
+ }
+
+ @Override
+ public Constraint.Variable getVariable() {
+ return m_variable;
+ }
+
+ @Override
+ public Class<T> getSelectorClass() {
+ return m_selectorClass;
+ }
+}
\ No newline at end of file
Added:
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Query.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Query.java
Mon Oct 31 12:35:25 2011
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.semantic.query.typed;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.amdatu.semantic.Model;
+import org.amdatu.semantic.RowSet;
+import org.amdatu.semantic.query.Constraint;
+import org.amdatu.semantic.query.Selector;
+
+// TODO Also, I don't like the way we replicate all methods here; we could
replace this by self-referential typing,
+// but that's at least as ugly.
+public class Query extends org.amdatu.semantic.query.Query {
+
+ @Override
+ public Query select(String modifier, Selector<?>... selectors) {
+ super.select(modifier, selectors);
+ return this;
+ }
+
+ @Override
+ public Query select(Selector<?>... selectors) {
+ super.select(selectors);
+ return this;
+ };
+
+ @Override
+ public Query where(Constraint... constraints) {
+ super.where(constraints);
+ return this;
+ }
+
+ @Override
+ public RowSet<Row> select(Model m) {
+ return new TypedRowSet(super.select(m), m);
+ }
+
+ private static class TypedRowSet implements RowSet<Row> {
+
+ private final RowSet<? extends org.amdatu.semantic.Row> m_set;
+ private final Model m_model;
+
+ public TypedRowSet(RowSet<? extends org.amdatu.semantic.Row> set,
Model model) {
+ m_set = set;
+ m_model = model;
+ }
+
+ @Override
+ public Iterator<Row> iterator() {
+ return new TypedRowSetIterator(m_set.iterator(), m_model);
+ }
+
+ @Override
+ public List<String> getVariables() {
+ return m_set.getVariables();
+ }
+ }
+
+ private static class TypedRowSetIterator implements Iterator<Row> {
+
+ private final Iterator<? extends org.amdatu.semantic.Row> m_iterator;
+ private final Model m_model;
+
+ public TypedRowSetIterator(Iterator<? extends org.amdatu.semantic.Row>
iterator, Model model) {
+ m_iterator = iterator;
+ m_model = model;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return m_iterator.hasNext();
+ }
+
+ @Override
+ public Row next() {
+ return new TypedRow(m_iterator.next(), m_model);
+ }
+
+ @Override
+ public void remove() {
+ m_iterator.remove();
+ }
+ }
+
+ private static class TypedRow implements Row {
+
+ private final org.amdatu.semantic.Row m_row;
+ private final Model m_model;
+
+ public TypedRow(org.amdatu.semantic.Row row, Model model) {
+ m_row = row;
+ m_model = model;
+ }
+
+ @Override
+ public String get(String variableName) {
+ return m_row.get(variableName);
+ }
+
+ @Override
+ public <T> T get(Selector<T> selector) {
+ return (T) Util.makeNew(selector.getSelectorClass(),
get(selector.getVariable().getValue()), m_model);
+ }
+ }
+
+
+}
Added:
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Row.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Row.java
Mon Oct 31 12:35:25 2011
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.semantic.query.typed;
+
+import org.amdatu.semantic.query.Selector;
+
+public interface Row extends org.amdatu.semantic.Row {
+ public <T> T get(Selector<T> constraint);
+}
Added:
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Util.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-semanticweb/semanticservice-rdf2go/src/main/java/org/amdatu/semantic/query/typed/Util.java
Mon Oct 31 12:35:25 2011
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.semantic.query.typed;
+
+import java.lang.reflect.Constructor;
+import java.util.Collection;
+
+import org.amdatu.semantic.Model;
+
+public class Util {
+ public static StringBuilder join(Collection<?> parts, String separator) {
+ StringBuilder result = new StringBuilder();
+
+ for (Object part : parts) {
+ if (result.length() > 0) {
+ result.append(separator);
+ }
+ result.append(part);
+ }
+
+ return result;
+ }
+
+ public static <T> T makeNew(Class<T> resultClass, String value, Model
model) {
+ // TODO exception handling
+ if (resultClass.equals(String.class)) {
+ return (T) value;
+ }
+ try {
+ try {
+ Constructor<T> constructor =
resultClass.getConstructor(String.class, Model.class);
+ return constructor.newInstance(value, model);
+ }
+ catch (NoSuchMethodException nsme) {
+ // no problem, we'll just try one with only a string
constructor.
+ }
+ Constructor<T> constructor =
resultClass.getConstructor(String.class);
+ return constructor.newInstance(value);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits