emfpath

40
EMFPath Even more cool than what I do with straws Mikaël Barbero OBEO

Upload: mikaelbarbero

Post on 13-Jun-2015

1.699 views

Category:

Documents


2 download

DESCRIPTION

These are the slides I presented during Eclipse Con 2011http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2164

TRANSCRIPT

Page 1: EMFPath

EMFPath

Even more cool than what I

do with strawsMikaël Barbero

OBEO

Page 2: EMFPath

public void demo(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = new HashSet<Borrower>(); List<Writer> allWriters = new ArrayList<Writer>(); for (Book book : books) { List<Borrower> borrowers = book.getBorrowers(); allBorrowers.addAll(borrowers); allWriters.add(book.getAuthor()); } Set<Borrower> filteredBorowers = new HashSet<Borrower>(); for (Borrower borrower : allBorrowers) { if (borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook)) { filteredBorowers.add(borrower); } } }

Page 3: EMFPath

Java library used internally @ Google for years

Superset of Google Collections

About Guava

2007 2008 2009 2010 2011

Google Collec

tions

v0.5

Google Collec

tions

v1Guava

r03

Guava r0

8

Page 4: EMFPath
Page 5: EMFPath
Page 6: EMFPath

About Guava

Page 7: EMFPath

IO

About Guava

Page 8: EMFPath

IO Networking

About Guava

Page 9: EMFPath

IO Networking Concurrency

About Guava

Page 10: EMFPath

IO Networking Concurrency

Primitive types

About Guava

Page 11: EMFPath

IO Networking Concurrency

Primitive types Collections

About Guava

Page 12: EMFPath

IO Networking Concurrency

Primitive types Collections

About Guava

Page 13: EMFPath

in com.google.common.base package

Function<F, T> to transform a collection

Predicate<T> to filter out a collection

public interface Function<F, T> {

T apply(F from);

}

public interface Predicate<T> {boolean apply(T from);

}

Page 14: EMFPath

in com.google.common.collect package

Iterables.filter()

Iterators.filter()

Collections2.filter()

Sets.filter()

Page 15: EMFPath

4224 7 12813

Page 16: EMFPath

4224 7 12813

public static void demo(Collection<Integer> c) { Predicate<Integer> isEven = new Predicate<Integer>() { @Override public boolean apply(Integer input) { return (input.intValue() % 2 == 0); } }; Collections2.filter(c, isEven); }

Page 17: EMFPath

4224 128

4224 7 12813

public static void demo(Collection<Integer> c) { Predicate<Integer> isEven = new Predicate<Integer>() { @Override public boolean apply(Integer input) { return (input.intValue() % 2 == 0); } }; Collections2.filter(c, isEven); }

Page 18: EMFPath

in com.google.common.collect package

Iterables.transform()

Iterators.transform()

Collections2.transform()

Lists.transform()

Page 19: EMFPath

OrangeApple Kiwi PearBanana

Page 20: EMFPath

OrangeApple Kiwi PearBanana

public void demo(Collection<String> c) { Function<String, String> toUpperCase = new Function<String, String>() { @Override public String apply(String input) { return input.toUpperCase(); } }; Collections2.transform(c, toUpperCase); }

Page 21: EMFPath

ORANGEAPPLE KIWI PEARBANANA

OrangeApple Kiwi PearBanana

public void demo(Collection<String> c) { Function<String, String> toUpperCase = new Function<String, String>() { @Override public String apply(String input) { return input.toUpperCase(); } }; Collections2.transform(c, toUpperCase); }

Page 22: EMFPath

Predicates

Functions

composeforPredicate

andornotcompose

Compose and combine

Page 23: EMFPath

Beware of

Page 24: EMFPath

Beware of

lazyness

Page 25: EMFPath

Copy!

Lists.newArrayList() Lists.newLinkedList() Sets.newHashSet() Sets.newLinkedHashSet() Sets.newTreeSet()

ImmutableList.copyOf() ImmutableSet.copyOf()

and make it immutable...

Page 26: EMFPath

Path

Page 27: EMFPath

Set of functions and predicates for EMF objects

Code generators for your own Ecore models

and few other utility classes

public interface Function<F, T> {

T apply(F from);

}public interface Predicate<T> {

boolean apply(T from);}

Page 28: EMFPath

‣ parent (eContainer)

‣ ancestor‣ ancestorOrSelf‣ child (eContents)

‣ descendant (eAllContents)

‣ descendantOrSelf‣ following‣ followingSibling‣ preceding‣ precedingSibling

Lazy EObjects containment tree walking

EObject myObject;Collection<EObject> fs = followingSibling.of(myObject);

Page 29: EMFPath

HavingIsKind/IsTypeIsAncestor/IsChild

Common predicates

public static Collection<EObject> demoHaving(Collection<EObject> c) {

return Collections2.filter(c, Having.feature(EcorePackage.Literals.ENAMED_ELEMENT__NAME,

StringPredicates.firstUpperCase) ); }

Page 30: EMFPath

non-EMF functions & predicates

length : Function<String, Integer>toL1Case : Function<String, String>toLowerCase : Function<String, String>toU1Case : Function<String, String>toUpperCase : Function<String, String>trim : Function<String, String>replaceAll(Pattern, String)replaceAll(String, String)replaceFirst(Pattern, String)replaceFirst(String, String)substring(int)substring(int, int)

Strings

Predicates to test ordering:equalless thangreater thanless or equalgreater or equal

Comparable

Page 31: EMFPath

Ecore API has been Guava-ified

‣ EObject.eResource() is wrapped as a Function in EObjectPath.eResource

‣ EObject.eIsProxy() is wrapped as a Predicate in EObjectPath.eIsProxy

‣ EClass.getESuperTypes() is wrapped as a Function in EClass.eSuperTypes

‣ EReference.isContainment() is wrapped as a Predicate in ERefrencePath.isContainment

Page 32: EMFPath

Ecore has been Guava-ified through a generator

that is available for your own Ecore model

+

Page 33: EMFPath

time to play

Page 34: EMFPath

public void demo(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = new HashSet<Borrower>(); List<Writer> allWriters = new ArrayList<Writer>(); for (Book book : books) { List<Borrower> borrowers = book.getBorrowers(); allBorrowers.addAll(borrowers); allWriters.add(book.getAuthor()); } Set<Borrower> filteredBorowers = new HashSet<Borrower>(); for (Borrower borrower : allBorrowers) { if (borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook)) { filteredBorowers.add(borrower); } } }

Page 35: EMFPath

public void demo2(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = ImmutableSet.copyOf( Iterables.concat( Collections2.transform(books, BookPath.borrowers)));

List<Writer> allWriters = Lists.transform(books, BookPath.author); Predicate<Borrower> predicate = new Predicate<Borrower>() { @Override public boolean apply(Borrower borrower) { return borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook); } }; Set<Borrower> filteredBorrowers = Sets.filter(allBorrowers, predicate); }

Page 36: EMFPath

public void demo2(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = ImmutableSet.copyOf( Iterables.concat( Collections2.transform(books, BookPath.borrowers)));

List<Writer> allWriters = Lists.transform(books, BookPath.author); Predicate<Borrower> predicate = new Predicate<Borrower>() { @Override public boolean apply(Borrower borrower) { return borrower.getLastName().startsWith("B") && borrower.getBorrowed().contains(aSpecificBook); } }; Set<Borrower> filteredBorrowers = Sets.filter(allBorrowers, predicate); }

Page 37: EMFPath

public void demo2(Library l) { List<Book> books = l.getBooks(); Set<Borrower> allBorrowers = ImmutableSet.copyOf( Iterables.concat( Collections2.transform(books, BookPath.borrowers)));

List<Writer> allWriters = Lists.transform(books, BookPath.author); Set<Borrower> filteredBorrowers = Sets.filter(allBorrowers, predicate); }

Page 38: EMFPath

1. Function and Predicate2. Lazyness3. Walking EObjects containment tree along XPath-like axes4. Code generators for your own Ecore model

What you should remember

about this presentation

Page 39: EMFPath

1. Guava is cool! 2. EMF is cool too!3. EMFPath bridges the gap between Guava and EMF4. EMFPath is cool - Q.E.D

What you should REALLY remember

about this presentation

http://code.google.com/a/eclipselabs.org/p/emfpath/

EMFPath v0.4.0 is available NOW!

Page 40: EMFPath

Q&A