Skip to content

Commit

Permalink
Resolved #32
Browse files Browse the repository at this point in the history
  • Loading branch information
Quenio dos Santos committed Mar 24, 2017
1 parent 9e60e0a commit 41552fd
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cml-acceptance/cases/livir-books/client-output.txt
Expand Up @@ -4,4 +4,4 @@ Classes:
- livir.books.BookStore
- livir.books.Book

Book(isbn="1234", title="Programming Adventures")
Book(quantity="10", isbn="1234", title="Programming Adventures")
3 changes: 3 additions & 0 deletions cml-acceptance/cases/livir-books/compiler-output.txt
@@ -1,6 +1,9 @@
model files:
- pom.xml

Product files:
- src/main/java/livir/books/Product.java

Book files:
- src/main/java/livir/books/Book.java

Expand Down
7 changes: 6 additions & 1 deletion cml-acceptance/cases/livir-books/main.cml
@@ -1,4 +1,9 @@
concept Book
concept Product
{
quantity: Integer;
}

concept Book: Product
{
isbn: String;
title: String;
Expand Down
7 changes: 2 additions & 5 deletions cml-acceptance/cases/mini-cml-language/client-output.txt
@@ -1,7 +1,4 @@
Mini-CML Compiler

Classes:
- mcml.language.ModelElement
- mcml.language.Scope
- mcml.language.PropertyList

Model(parentScope=not present, elements=[])
Concept(parentScope=not present, elements=[], name="SomeConcept", abstracted="true")
9 changes: 9 additions & 0 deletions cml-acceptance/cases/mini-cml-language/compiler-output.txt
Expand Up @@ -4,8 +4,17 @@ model files:
ModelElement files:
- src/main/java/mcml/language/ModelElement.java

NamedElement files:
- src/main/java/mcml/language/NamedElement.java

Scope files:
- src/main/java/mcml/language/Scope.java

PropertyList files:
- src/main/java/mcml/language/PropertyList.java

Concept files:
- src/main/java/mcml/language/Concept.java

Model files:
- src/main/java/mcml/language/Model.java
14 changes: 14 additions & 0 deletions cml-acceptance/cases/mini-cml-language/main.cml
Expand Up @@ -4,6 +4,11 @@ abstract concept ModelElement
parentScope: Scope?;
}

abstract concept NamedElement: ModelElement
{
name: String;
}

abstract concept Scope: ModelElement
{
elements: ModelElement*;
Expand All @@ -13,6 +18,15 @@ abstract concept PropertyList: Scope
{
}

concept Concept: NamedElement, PropertyList
{
abstracted: Boolean;
}

concept Model: Scope
{
}

target cmlc
{
groupId = "mcml";
Expand Down
Expand Up @@ -7,6 +7,10 @@ newLineIf2(cond1, cond2) ::= <<
<if(cond1 && cond2)><\n><endif>
>>

emptyLineIf2(cond1, cond2) ::= <<
<if(cond1 && cond2)><\n><\n><endif>
>>

newLineIfEither(cond1, cond2) ::= <<
<if(cond1 || cond2)><\n><endif>
>>
Expand Down
Expand Up @@ -83,20 +83,18 @@ delegatedGetters(concept, delegatedProperties) ::= <<

constructorForClients(concept, classNameSuffix, superProperties) ::= <<
<if(!concept.abstract && concept.allAncestors && !superProperties)>
<newLineIf(concept.properties)><\\>
<newLineIfEither(concept.allAncestors, concept.properties)><\\>
public <typeName(concept)><classNameSuffix>(<constructorParamList([], [concept.allProperties])>)
{
this(
<constructorArgsForClients(concept)>
);
<constructorArgsForClients(concept)>
}<\\>
<endif>
>>

constructorArgsForClients(concept) ::= <<
<concept.allAncestors:createInvocation();separator=",\n"><\\>
<if(concept.allAncestors)>,<endif>
<concept.properties:fieldName();separator=",\n">
<concept.allAncestors:createInvocation();separator="\n"><\\>
<emptyLineIf2(concept.allAncestors, concept.properties)><\\>
<concept.properties:fieldInit();separator="\n">
>>

primaryConstructor(concept, classNameSuffix, superProperties, paramAncestors) ::= <<
Expand Down Expand Up @@ -133,11 +131,12 @@ this.<fieldName(namedElement)> = <fieldName(namedElement)>;
>>

createInvocation(concept) ::= <<
<fieldName(concept)> = <\\>
<typeName(concept)>.create(<\\>
<concept.allAncestors:fieldName();separator=", "><\\>
<commaIf(concept.allAncestors)><\\>
<commaIf2(concept.allAncestors, concept.properties)><\\>
<concept.properties:fieldName();separator=", "><\\>
)
);
>>

superConstructor(inheritedProperties) ::= <<
Expand Down
Expand Up @@ -25,7 +25,7 @@ static <typeName(concept)> create(<createParamList([], concept.allProperties)>)
>>

createMethodForDescendants(concept, classNameSuffix) ::= <<
<if(concept.directAncestors && classNameSuffix)>
<if((concept.abstract || concept.directAncestors) && classNameSuffix)>
<newLineIf(concept.properties)><\\>
static <typeName(concept)> create(<createParamList(concept.allAncestors, concept.properties)>)
{
Expand Down
15 changes: 15 additions & 0 deletions cml-compiler/cml-generator/src/test/java/java_lang/ClassTest.java
Expand Up @@ -27,6 +27,21 @@ public void class2__concept_abstract_ancestor_multiple() throws IOException
testClassTemplateWithSuffix(concept, "class2__concept_abstract_ancestor_multiple.txt");
}

@Test
public void class2__concept_concrete_ancestor_empty() throws IOException
{
final Concept productConcept = Concept.create("Product");
productConcept.addElement(Property.create("description", null, Type.create("String", null)));

final Concept intermediateConcept = Concept.create("Intermediate");
intermediateConcept.addDirectAncestor(productConcept);

final Concept bookConcept = Concept.create("Book");
bookConcept.addDirectAncestor(intermediateConcept);

testClassTemplateWithSuffix(bookConcept, "class2__concept_concrete_ancestor_empty.txt");
}

@Test
public void class2__concept_concrete_ancestor_multiple() throws IOException
{
Expand Down
Expand Up @@ -17,6 +17,16 @@ protected String getTemplateFileName()
return "/java_lang/interface.stg";
}

@Test
public void concept_abstract() throws IOException
{
final Concept concept = Concept.create("Book", true);

concept.addElement(Property.create("title", null, Type.create("String", null)));

testInterfaceTemplateWithCreateMethod(concept, "concept_abstract.txt");
}

@Test
public void concept_abstract_ancestor() throws IOException
{
Expand Down
@@ -0,0 +1,31 @@
class BookImpl implements Book
{
private final Product product;
private final Intermediate intermediate;

public BookImpl(String description)
{
product = Product.create(description);
intermediate = Intermediate.create(product);
}

public BookImpl(Product product, Intermediate intermediate)
{
this.product = product;
this.intermediate = intermediate;
}

public String getDescription()
{
return product.getDescription();
}

public String toString()
{
return new StringBuilder(Book.class.getSimpleName())
.append('(')
.append("description=").append(String.format("\"%s\"", getDescription()))
.append(')')
.toString();
}
}
Expand Up @@ -8,12 +8,11 @@ class BookImpl implements Book

public BookImpl(String baseProperty, String description, Integer quantity, String title)
{
this(
Base.create(baseProperty),
Product.create(base, description),
StockItem.create(base, quantity),
title
);
base = Base.create(baseProperty);
product = Product.create(base, description);
stockItem = StockItem.create(base, quantity);

this.title = title;
}

public BookImpl(Base base, Product product, StockItem stockItem, String title)
Expand Down
@@ -0,0 +1,9 @@
interface Book
{
String getTitle();

static Book create(String title)
{
return new BookImpl(title);
}
}
2 changes: 1 addition & 1 deletion java-clients/livir-console/pom.xml
Expand Up @@ -40,7 +40,7 @@
<configuration>
<archive>
<manifest>
<mainClass>mcml.compiler.Launcher</mainClass>
<mainClass>livir.console.Launcher</mainClass>
</manifest>
</archive>
<descriptorRefs>
Expand Down
@@ -1,4 +1,4 @@
package mcml.compiler;
package livir.console;

import livir.books.Book;
import livir.books.BookStore;
Expand All @@ -13,6 +13,6 @@ public static void main(final String[] args)
System.out.println("- " + BookStore.class.getName());
System.out.println("- " + Book.class.getName());
System.out.println();
System.out.println(new Book("1234", "Programming Adventures"));
System.out.println(new Book(10, "1234", "Programming Adventures"));
}
}
@@ -1,19 +1,20 @@
package mcml.compiler;

import mcml.language.ModelElement;
import mcml.language.PropertyList;
import mcml.language.Scope;
import mcml.language.Concept;
import mcml.language.Model;

import static java.util.Collections.emptySet;

public class Launcher
{
public static void main(final String[] args)
{
final Model model = Model.create(null, emptySet());
final Concept concept = Concept.create(null, emptySet(), "SomeConcept", true);

System.out.println("Mini-CML Compiler");
System.out.println();
System.out.println("Classes:");
System.out.println("- " + ModelElement.class.getName());
System.out.println("- " + Scope.class.getName());
System.out.println("- " + PropertyList.class.getName());
System.out.println();
System.out.println(model);
System.out.println(concept);
}
}

0 comments on commit 41552fd

Please sign in to comment.