Skip to content

Commit 31a7520

Browse files
committed
Added library example
1 parent b0fe876 commit 31a7520

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Get all the books contained in this library
2+
var library = LibraryType.all().first();
3+
var allBooks = library.book;
4+
5+
// Print the current number of books
6+
allBooks.size().println();
7+
8+
// Create a new book
9+
var newBook: new BookType;
10+
newBook.title = "MDE in Practice";
11+
12+
// Add the book to the library
13+
library.book.add(newBook);
14+
15+
// Get all books and print the new size
16+
BookType.all().size().println();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Get all the books contained in this library
2+
var allBooks = BookType.all();
3+
4+
// Print the total number of pages of all books
5+
var total = 0;
6+
for (aBook in allBooks) {
7+
total = total + aBook.pages;
8+
}
9+
("Total pages: " + total).println();
10+
11+
// ... the same using collect() and sum()
12+
// instead of a for loop
13+
BookType.all().collect(b|b.pages).sum().println();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<library xsi:noNamespaceSchemaLocation="library.xsd">
3+
<book title="EMF Eclipse Modeling Framework" pages="744">
4+
<author>Dave Steinberg</author>
5+
<author>Frank Budinsky</author>
6+
<author>Marcelo Paternostro</author>
7+
<author>Ed Merks</author>
8+
<published>2009</published>
9+
</book>
10+
<book title="Eclipse Modeling Project: A Domain-Specific Language (DSL) Toolkit" pages="736">
11+
<author>Richard Gronback</author>
12+
<published>2009</published>
13+
</book>
14+
<book title="Official Eclipse 3.0 FAQs" pages="432">
15+
<author>John Arthorne</author>
16+
<author>Chris Laffra</author>
17+
<published>2004</published>
18+
</book>
19+
</library>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
3+
<xs:element name="library">
4+
<xs:complexType>
5+
<xs:sequence>
6+
<xs:element name="book" maxOccurs="unbounded">
7+
<xs:complexType>
8+
<xs:sequence>
9+
<xs:element name="author" maxOccurs="unbounded" type="xs:string"></xs:element>
10+
<xs:element name="published" type="xs:int"></xs:element>
11+
</xs:sequence>
12+
<xs:attribute name="title" type="xs:string"></xs:attribute>
13+
<xs:attribute name="pages" type="xs:int"></xs:attribute>
14+
</xs:complexType>
15+
</xs:element>
16+
</xs:sequence>
17+
</xs:complexType>
18+
</xs:element>
19+
</xs:schema>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Get the first library element in the document
2+
var library = LibraryType.all().first();
3+
4+
// Get all the books contained in this library
5+
var allBooks = library.book;
6+
7+
// We can get all the books in the document by querying directly the book type
8+
var allBooksAlternative = BookType.all();
9+
10+
// Iterate through the collection of books, navigate the pages attribute and
11+
// return the title of the book if it has more than 700 pages
12+
for (aBook in allBooks) {
13+
if (aBook.pages > 700) {
14+
aBook.isTypeOf(BookType).println();
15+
("The " + aBook.title + " is a large book!").println();
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Get the first book contained in the library
2+
var emfBook = BookType.all().first();
3+
4+
// Set the title to a new one
5+
emfBook.title = "EMF Book";
6+
7+
// Print the changed title (NB.: You need to have selected the "Store on disposal"
8+
// option in the run configuration to save changes to the XML file.)
9+
var changedEmfBook = BookType.all().first();
10+
changedEmfBook.title.println();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.eclipse.epsilon.examples.xsdxml;
2+
3+
import java.io.File;
4+
5+
import org.eclipse.epsilon.common.util.StringProperties;
6+
import org.eclipse.epsilon.emc.emf.xml.XmlModel;
7+
import org.eclipse.epsilon.eol.EolModule;
8+
9+
public class LibraryExample {
10+
11+
public static void main(String[] args) throws Exception {
12+
13+
// Parse the EOL program
14+
EolModule module = new EolModule();
15+
module.parse(new File("library/query-by-type.eol"));
16+
17+
// Load the model
18+
XmlModel model = new XmlModel();
19+
StringProperties properties = new StringProperties();
20+
properties.put(XmlModel.PROPERTY_NAME, "M");
21+
// No need to specify XSD URI due to xsi:noNamespaceSchemaLocation="library.xsd" in library.xml
22+
properties.put(XmlModel.PROPERTY_MODEL_URI, new File("library/library.xml").toURI());
23+
properties.put(XmlModel.PROPERTY_READONLOAD, "true");
24+
properties.put(XmlModel.PROPERTY_STOREONDISPOSAL, "false");
25+
model.load(properties);
26+
27+
// Make the model available to the program
28+
module.getContext().getModelRepository().addModel(model);
29+
30+
// Execute the EOL program
31+
module.execute();
32+
33+
// Dispose of the model
34+
module.getContext().getModelRepository().dispose();
35+
}
36+
}

0 commit comments

Comments
 (0)