Hi,
I modified the class
http://code.google.com/a/eclipselabs.org/p/emftriple/source/browse/trunk/examples/org.social.network/src/org/social/Main.java
to store and retrive some element.
public class Main {
private static int connectionMore = 0;
private static int connectionMoreValue = 2;
final static int NB_PERSON = 30; // MAX 10000
public static void main(String[] args) throws Exception {
// Initialisation of EmfTriple with Jena TDB as Back end
ETriple.init(new JenaModule());
// Register the metamodel
EPackage.Registry.INSTANCE.put(NetworkPackage.eNS_URI,
NetworkPackage.eINSTANCE);
EPackage.Registry.INSTANCE.put(PersistencePackage.eNS_URI,
PersistencePackage.eINSTANCE);
// Read persistence.xml
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("network", null);
EntityManager em = emf.createEntityManager();
System.out.println("Start of EmfTriple Example");
long startTime = System.currentTimeMillis();
// Create the social network
SocialNetwork social =
NetworkFactory.eINSTANCE.createSocialNetwork();
social.setName("My Social");
List<Person> all = createPersons();
createConnections(all);
social.getMembers().addAll(all);
// Persist the Social Network
em.getTransaction().begin();
em.persist(social);
em.flush();
em.getTransaction().commit();
long endTime = System.currentTimeMillis();
System.out.println("Time to persist " + NB_PERSON + " persons: " +
((endTime - startTime) / 1000.0) + " sec");
startTime = System.currentTimeMillis();
// Query for all members with more than 45 friends.
em.getTransaction().begin();
@SuppressWarnings("unchecked")
List<Person> moreThanFortyFiveFriends = em.createNativeQuery(
"select ?e where { " +
" ?e a <
http://xmlns.com/foaf/0.1/Person> ." +
" ?e <
http://xmlns.com/foaf/0.1/knows> ?k " +
"} " +
"group by ?e " +
"having (count(?k) > "+ connectionMoreValue +")", Person.class)
.getResultList();
em.getTransaction().commit();
endTime = System.currentTimeMillis();
System.out.println("Time to query: " + ((endTime - startTime) /
1000.0) + " sec");
System.out.println("There is " + moreThanFortyFiveFriends.size() + "
with more than "+connectionMoreValue+" friends in your network.");
System.out.println("People:");
for (Person person : moreThanFortyFiveFriends){
System.out.println(person.getId()+ ": "+person.getFirstName()+"
"+person.getLastName());
}
System.out.println("End of EmfTriple Example");
em.getTransaction().begin();
Person emp = em.find(Person.class,
NetworkPackage.eINSTANCE.getPerson_Id().getEAnnotation("Id").getDetails().get("base")
+1);
System.out.println("Founded "+emp.getId()+ ": "+emp.getFirstName()+"
"+emp.getLastName());
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria =
criteriaBuilder.createQuery(Person.class);
criteria.from(Person.class);
List<Person> persons = em.createQuery(criteria).getResultList();
if (persons!=null)
System.out.println("Persons Founded: "+persons.size());
else
System.out.println("Null!!!");
List<Person> persons2 = em.createQuery("SELECT e FROM Person e",
Person.class).getResultList();
if (persons2!=null)
System.out.println("Persons Founded: "+persons2.size());
else
System.out.println("Null!!!");
em.flush();
em.getTransaction().commit();
}
private static List<Person> createPersons() {
final List<Person> list = new ArrayList<Person>();
int persons = 0;
FileInputStream stream;
try {
stream = new FileInputStream("src/randomNames.csv");
final BufferedReader buffer = new BufferedReader(new
InputStreamReader(stream));
String strLine = buffer.readLine();
while (persons != NB_PERSON && strLine != null) {
final String[] names = strLine.split(",");
persons+=1;
final Person person = NetworkFactory.eINSTANCE.createPerson();
person.setId(persons);
person.setFirstName(names[0]);
person.setLastName(names[1]);
list.add(person);
System.out.println("Added "+person.getId()+ ":
"+person.getFirstName()+" "+person.getLastName());
strLine = buffer.readLine();
}
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Created " + persons + " persons");
return list;
}
private static List<Person> createConnections(List<Person> persons) {
final Random r = new Random();
int connect = 0;
for (Person p: persons) {
int nbConnections = r.nextInt(5);
for (int i = 0; i < nbConnections; i++) {
Person c = persons.get(r.nextInt(persons.size()));
if (c != p && !p.getKnows().contains(c)) {
p.getKnows().add(c);
connect+=1;
}
}
if (p.getKnows().size()>connectionMoreValue){
connectionMore++;
}
}
System.out.println("Created " + connect + " connections");
System.out.println("Added " + connectionMore + " people with more
than "+connectionMoreValue+" friends in your network.");
return persons;
}
}
and on console the result is:
WARN [main] (SetupTDB.java:755) - No BGP optimizer
Start of EmfTriple Example
Added 1: Kristina Chung
Added 2: Paige Chen
Added 3: Sherri Melton
Added 4: Gretchen Hill
Added 5: Karen Puckett
Added 6: Patrick Song
Added 7: Elsie Hamilton
Added 8: Hazel Bender
Added 9: Malcolm Wagner
Added 10: Dolores McLaughlin
Added 11: Francis McNamara
Added 12: Sandy Raynor
Added 13: Marion Moon
Added 14: Beth Woodard
Added 15: Julia Desai
Added 16: Jerome Wallace
Added 17: Neal Lawrence
Added 18: Jean Griffin
Added 19: Kristine Dougherty
Added 20: Crystal Powers
Added 21: Alex May
Added 22: Eric Steele
Added 23: Wesley Teague
Added 24: Franklin Vick
Added 25: Claire Gallagher
Added 26: Marian Solomon
Added 27: Marcia Walsh
Added 28: Dwight Monroe
Added 29: Wayne Connolly
Added 30: Stephanie Hawkins
Created 30 persons
Created 46 connections
Added 6 people with more than 2 friends in your network.
Time to persist 30 persons: 0.984 sec
Time to query: 0.739 sec
There is 6 with more than 2 friends in your network.
People:
11: Francis McNamara
24: Franklin Vick
19: Kristine Dougherty
23: Wesley Teague
0: null null
0: null null
End of EmfTriple Example
Founded 1: Kristina Chung
Null!!!
Exception in thread "main" java.lang.UnsupportedOperationException
at
com.emftriple.query.NativeQueryFactoryImpl.createTypedQuery(NativeQueryFactoryImpl.java:
47)
at
com.emftriple.impl.AbstractEntityManager.createQuery(AbstractEntityManager.java:
210)
at org.social.Main.main(Main.java:120)
And i have 2 problems
- some persons seem are stored whitout values ("0: null null" on the
console)
- when i use Criteria, result is always null
- when i use JPQL query (i read guide on
http://code.google.com/a/eclipselabs.org/p/emftriple/wiki/EMFTripleJPA),
i have java.lang.UnsupportedOperationException
Can you help me to understand if i wrong something or if there is a
bug?
Thanks
Domenico