Author: mir
Date: Tue Mar 23 15:28:18 2010
New Revision: 926618

URL: http://svn.apache.org/viewvc?rev=926618&view=rev
Log:
CLEREZZA-171: added RandGraph

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/TripleCollectionWrapper.java
Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/pom.xml

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/pom.xml?rev=926618&r1=926617&r2=926618&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/pom.xml
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/pom.xml
 Tue Mar 23 15:28:18 2010
@@ -20,5 +20,9 @@
             <groupId>org.apache.clerezza</groupId>
             <artifactId>org.apache.clerezza.rdf.core</artifactId>
         </dependency>
+               <dependency>
+                       <groupId>commons-lang</groupId>
+                       <artifactId>commons-lang</artifactId>
+               </dependency>
     </dependencies>
 </project>
\ No newline at end of file

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java?rev=926618&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
 Tue Mar 23 15:28:18 2010
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.clerezza.rdf.core.test;
+
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.MGraph;
+
+/**
+ *
+ * @author mir
+ */
+class MGraphWrapper extends TripleCollectionWrapper implements MGraph{
+
+       public MGraphWrapper(MGraph mGraph) {
+               super(mGraph);
+       }
+
+       @Override
+       public Graph getGraph() {
+               throw new UnsupportedOperationException("Not supported yet.");
+       }
+
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java?rev=926618&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
 Tue Mar 23 15:28:18 2010
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.clerezza.rdf.core.test;
+
+import java.util.Iterator;
+import java.util.UUID;
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.commons.lang.RandomStringUtils;
+
+/**
+ * A <code>MGraph</code> wrapper that allows randomly growing and shrinking of
+ * the wrapped mgraph.
+ *
+ * @author mir
+ */
+public class RandomGraph extends MGraphWrapper {
+       private int growthSpeed = 3;
+       private int interconnectivity = 2;
+
+       public RandomGraph(MGraph mGraph) {
+               super(mGraph);
+       }
+
+       public RandomGraph(int growthSpeed, int interconnectivity, MGraph 
mGraph) {
+               super(mGraph);
+               if (growthSpeed <= 0 || interconnectivity <= 0) {
+                       throw new IllegalArgumentException("growth speed and 
the interconnectivity "
+                                       + "value have to be equals or highter 
one");
+               }
+               this.growthSpeed = growthSpeed;
+               this.interconnectivity = interconnectivity;
+       }
+
+       /**
+        * Add or removes randomly a triple.
+        *
+        * @return the triple that was added or removed.
+        */
+       public Triple evolve() {
+               Triple triple;
+               int random = rollDice(growthSpeed);
+               if (random == 0 && size() != 0) {
+                       triple = getRandomTriple();
+                       remove(triple);
+               } else {
+                       triple = createRandomTriple();
+                       add(triple);
+               }
+               return triple;
+       }
+               
+       private Triple createRandomTriple() {
+               return new TripleImpl(getSubject(), getPredicate(), 
getObject());
+       }
+
+       private NonLiteral getSubject() {
+               int random = rollDice(interconnectivity);
+               System.out.println("---->" + random);
+               if (size() == 0) {
+                       random = 0;
+               }
+               switch (random) {
+                       case 0: // create new NonLiteral
+                               Resource newResource;
+                               do {
+                                       newResource = createRandomResource();
+                               } while (!(newResource instanceof NonLiteral));
+                               return (NonLiteral) newResource;
+                       default: // get existing NonLiteral
+                               Resource existingResource;
+                               do {
+                                       existingResource = 
getExistingResource();
+                                       if (existingResource == null) {
+                                               random = 0;
+                                       }
+                               } while (!(existingResource instanceof 
NonLiteral));
+
+                               return (NonLiteral) existingResource;
+               }
+       }
+
+       private UriRef getPredicate() {
+               int random = rollDice(interconnectivity);
+               if (size() == 0) {
+                       random = 0;
+               }
+               switch (random) {
+                       case 0: // create new UriRef
+                               return createRandomUriRef();
+                       default: // get existing UriRef
+                               Resource existingResource;
+                               do {
+                                       existingResource = 
getExistingResource();
+                                       if (existingResource == null) {
+                                               random = 0;
+                                       }
+                               } while (!(existingResource instanceof UriRef));
+                               return (UriRef) existingResource;
+               }
+       }
+
+       private Resource getObject() {
+               int random = rollDice(interconnectivity);
+               if (size() == 0) {
+                       random = 0;
+               }               
+               switch (random) {
+                       case 0: // create new resource
+                               return createRandomResource();
+                       default: // get existing resource
+                               Resource existingResource = 
getExistingResource();
+                               if (existingResource == null) {
+                                       random = 0;
+                               }
+                               return existingResource;
+               }
+       }
+
+       private static int rollDice(int faces) {
+               return Double.valueOf(Math.random() * faces).intValue();
+       }
+
+       private Resource createRandomResource() {
+               switch (rollDice(3)) {
+                       case 0:
+                               return new BNode();
+                       case 1:
+                               return createRandomUriRef();
+                       case 2:
+                               return new 
PlainLiteralImpl(RandomStringUtils.random(rollDice(100) + 1));
+               }
+               throw new RuntimeException("in createRandomResource()");
+       }
+
+       private Resource getExistingResource() {
+               Triple triple = getRandomTriple();
+               if (triple == null) {
+                       return null;
+               }
+               switch (rollDice(3)) {
+                       case 0:
+                               return triple.getSubject();
+                       case 1:
+                               return triple.getPredicate();
+                       case 2:
+                               return triple.getObject();
+               }
+               return null;
+       }
+
+       private UriRef createRandomUriRef() {
+               return new UriRef("http://"; + UUID.randomUUID().toString());
+       }
+
+       /**
+        * Returns a random triple.
+        */
+       public Triple getRandomTriple() {
+               int size = this.size();
+               if (size == 0) {
+                       return null;
+               }
+               Iterator<Triple> triples = iterator();
+               while (triples.hasNext()) {
+                       Triple triple = triples.next();
+                       if (rollDice(this.size()) == 0) {
+                               return triple;
+                       }
+               }
+               return getRandomTriple();
+       }
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/TripleCollectionWrapper.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/TripleCollectionWrapper.java?rev=926618&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/TripleCollectionWrapper.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/TripleCollectionWrapper.java
 Tue Mar 23 15:28:18 2010
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.clerezza.rdf.core.test;
+
+import java.util.Collection;
+import java.util.Iterator;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.event.FilterTriple;
+import org.apache.clerezza.rdf.core.event.GraphListener;
+
+/**
+ *
+ * @author mir
+ */
+class TripleCollectionWrapper implements TripleCollection {
+
+       protected TripleCollection wrapped;
+
+       public TripleCollectionWrapper(TripleCollection tc) {
+               this.wrapped = tc;
+       }
+
+       @Override
+       public Iterator<Triple> filter(NonLiteral subject, UriRef predicate, 
Resource object) {
+               return wrapped.filter(subject, predicate, object);
+       }
+
+       @Override
+       public void addGraphListener(GraphListener listener, FilterTriple 
filter, long delay) {
+               wrapped.addGraphListener(listener, filter, delay);
+       }
+
+       @Override
+       public void addGraphListener(GraphListener listener, FilterTriple 
filter) {
+               wrapped.addGraphListener(listener, filter);
+       }
+
+       @Override
+       public void removeGraphListener(GraphListener listener) {
+               wrapped.removeGraphListener(listener);
+       }
+
+       @Override
+       public int size() {
+               return wrapped.size();
+       }
+
+       @Override
+       public boolean isEmpty() {
+               return wrapped.isEmpty();
+       }
+
+       @Override
+       public boolean contains(Object o) {
+               return wrapped.contains(o);
+       }
+
+       @Override
+       public Iterator<Triple> iterator() {
+               return wrapped.iterator();
+       }
+
+       @Override
+       public Object[] toArray() {
+               return wrapped.toArray();
+       }
+
+       @Override
+       public <T> T[] toArray(T[] a) {
+               return wrapped.toArray(a);
+       }
+
+       @Override
+       public boolean add(Triple e) {
+               return wrapped.add(e);
+       }
+
+       @Override
+       public boolean remove(Object o) {
+               return wrapped.remove(o);
+       }
+
+       @Override
+       public boolean containsAll(Collection<?> c) {
+               return wrapped.containsAll(c);
+       }
+
+       @Override
+       public boolean addAll(Collection<? extends Triple> c) {
+               return wrapped.addAll(c);
+       }
+
+       @Override
+       public boolean removeAll(Collection<?> c) {
+               return wrapped.removeAll(c);
+       }
+
+       @Override
+       public boolean retainAll(Collection<?> c) {
+               return wrapped.retainAll(c);
+       }
+
+       @Override
+       public void clear() {
+               wrapped.clear();
+       }
+
+}


Reply via email to