Giter Club home page Giter Club logo

isis-script's Introduction

Isis Script

Isis Script is an Xtext-based DSL for generating Java code for the Apache Isis framework from a textual representation.

The DSL

The Isis Script DSL is inspired by concepts from Domain-Driven Design. It provides keywords for creating entities (with their annotations, injections, properties, events, actions and UI hints) and services (with their annotations, injections and actions). To map the Isis Script DSL to the corresponding Java code the notion of packages and imports are supported as well.

The following example shows an entity with a property, an action publishing an event and a title UI hint:

package domainapp.dom.modules.simple

import org.apache.isis.applib.annotation.Action
import org.apache.isis.applib.annotation.DomainObject

@DomainObject(objectType = "SIMPLE")
@Queries(#[
	@Query(name = "findByName", language = "JDOQL",
		value = "SELECT FROM domainapp.dom.modules.simple.SimpleObject WHERE name.indexOf(:name) >= 0")
])
entity SimpleObject {
	property String name

	@Action(domainEvent = UpdateNameDomainEvent)
	action void updateName {
		parameter String newName
		body {
			setName(newName)
		}
		event UpdateNameDomainEvent
	}

	title {
		TranslatableString.tr("Object: {name}", "name", name)
	}
}

The following example (package and imports are omitted for brevity) shows a repository service for the aforementioned entity:

@DomainService(repositoryFor = SimpleObject)
service SimpleObjects {
	action List<SimpleObject> listAll {
		body {
			container.allInstances(SimpleObject)
		}
	}

	action List<SimpleObject> findByName {
		@ParameterLayout(named="Name") 
		parameter String name
		body {
			container.allMatches(new QueryDefault(SimpleObject, "findByName", "name", name))
		}
	}
}

The following example (package and imports are omitted for brevity) shows a service with an action using an injected repository:

service SimpleObjectProvider {
	inject SimpleObjects repo

	action List<SimpleObject> allObjects {
		body {
			repo.listAll
		}
	}
}

The following examples shows the complete Isis Script for the entity and repository service of the domain object SimpleObject from the Isis Script simpleapp example project created from the Apache Isis Maven archetype SimpleApp:

package domainapp.dom.modules.simple

import javax.jdo.annotations.Column
import javax.jdo.annotations.DatastoreIdentity
import javax.jdo.annotations.IdGeneratorStrategy
import javax.jdo.annotations.IdentityType
import javax.jdo.annotations.PersistenceCapable
import javax.jdo.annotations.Queries
import javax.jdo.annotations.Query
import javax.jdo.annotations.Unique
import javax.jdo.annotations.Version
import javax.jdo.annotations.VersionStrategy
import org.apache.isis.applib.annotation.Action
import org.apache.isis.applib.annotation.BookmarkPolicy
import org.apache.isis.applib.annotation.DomainObject
import org.apache.isis.applib.annotation.DomainObjectLayout
import org.apache.isis.applib.annotation.Editing
import org.apache.isis.applib.annotation.Parameter
import org.apache.isis.applib.annotation.ParameterLayout
import org.apache.isis.applib.annotation.Property
import org.apache.isis.applib.annotation.Title
import org.apache.isis.applib.services.i18n.TranslatableString

@PersistenceCapable(identityType=IdentityType.DATASTORE)
@DatastoreIdentity(strategy=IdGeneratorStrategy.IDENTITY, column="id")
@Version(strategy=VersionStrategy.VERSION_NUMBER, column="version")
@Queries(#[
	@Query(name = "find", language = "JDOQL",
		value = "SELECT FROM domainapp.dom.modules.simple.SimpleObject"),
	@Query(name = "findByName", language = "JDOQL",
		value = "SELECT FROM domainapp.dom.modules.simple.SimpleObject WHERE name.indexOf(:name) >= 0")
])
@Unique(name="SimpleObject_name_UNQ", members = #["name"])
@DomainObject(objectType = "SIMPLE")
@DomainObjectLayout(bookmarking = BookmarkPolicy.AS_ROOT)
entity SimpleObject {

	@Column(allowsNull="false", length = 40)
	@Title(sequence="1")
	@Property(editing = Editing.DISABLED)
	property String name

	@Action(domainEvent = UpdateNameDomainEvent)
	action SimpleObject updateName {
		@Parameter(maxLength = 40)
		@ParameterLayout(named = "New name")
		parameter String newName {
			default {
				getName
			}
		}
		body {
			setName(newName)
			this
		}
		validate {
			if (newName.contains("!"))
				TranslatableString.tr("Exclamation mark is not allowed")
			else null
		}
		event UpdateNameDomainEvent
	}

	title {
		TranslatableString.tr("Object: {name}", "name", name)
	}
}
package domainapp.dom.modules.simple

import org.apache.isis.applib.annotation.DomainServiceLayout
import org.apache.isis.applib.annotation.DomainService
import org.apache.isis.applib.annotation.Action
import org.apache.isis.applib.annotation.SemanticsOf
import org.apache.isis.applib.annotation.BookmarkPolicy
import org.apache.isis.applib.annotation.ActionLayout
import org.apache.isis.applib.annotation.MemberOrder
import org.apache.isis.applib.annotation.ParameterLayout
import org.apache.isis.applib.query.QueryDefault
import java.util.List

@DomainService(repositoryFor = SimpleObject)
@DomainServiceLayout(menuOrder = "10")
service SimpleObjects {

	@Action(semantics = SemanticsOf.SAFE)
	@ActionLayout(bookmarking = BookmarkPolicy.AS_ROOT)
	@MemberOrder(sequence = "1")
	action List<SimpleObject> listAll {
		body {
			container.allInstances(SimpleObject)
		}
	}

	@Action(semantics = SemanticsOf.SAFE)
	@ActionLayout(bookmarking = BookmarkPolicy.AS_ROOT)
	@MemberOrder(sequence = "2")
	action List<SimpleObject> findByName {
		@ParameterLayout(named="Name") 
		parameter String name
		body {
			container.allMatches(new QueryDefault(SimpleObject, "findByName", "name", name))
		}
	}

	@MemberOrder(sequence = "3")
	action SimpleObject create {
		@ParameterLayout(named="Name")
		parameter String name
		body {
			val obj = container.newTransientInstance(SimpleObject)
			obj.name = name
			container.persistIfNotAlready(obj)
			obj
		}
	}
}

A formal description of the Isis Script DSL can be found here.

The Implementation

Isis Script uses Xtexts support for accessing JVM types, e.g. for Java annotations, super types, return types or parameters.

@Action(domainEvent = UpdateNameDomainEvent)
action SimpleObject updateName {
	@Parameter(maxLength = 40)
	@ParameterLayout(named = "New name")
	parameter String newName {

Xbase expressions are used to implement the dynamic parts of the DSL, e.g. property features, actions or UI hints.

validate {
    if (newName.contains("!"))
    	TranslatableString.tr("Exclamation mark is not allowed")
    else null
}

Isis Script files (which are using the file name extension .isis) are representing a single Java type. Depending on the declaration type keyword (entity or service) the corresponding source code is generated in the package defined via the package keyword within the folder target/generated-sources/isis/. This source code is plain Java which is compiled by the Java compiler.

The Tools

The Eclipse DSL Editor

Isis Script comes with an Xtext-generated Eclipse editor which supports the IDE features known from the Java editor, e.g

  • Syntax Coloring
  • Outline View
  • Content Assist
  • Organize Imports
  • Validator
  • Quick Fixes
  • Hyper Linking

DSL Editor

The source code in the editor is evaluated while typing. So the outline and the problems markers are updated automatically. If enabled then selecting a node in the outline then the corresponding code block is selected in the editor view.

DSL Editor with selection in outline

When a modified Isis Script is saved in the editor then the corresponding Java source file is generated within the folder target/generated-sources/isis/.

Generated Java Source Code

To switch between the generated Java source file and the Isis Script file the option "Open Generated File" / "Open Source File" from the editors context menu can be used.

The Maven Support

Isis Script files are compiled to Java source code via the Xtext Maven Plugin. To add the Isis Script output folder to Mavens list of Java source folder the Build Helper Maven Plugin is used. Finally the generated Java source code is compiled by the Maven Compiler Plugin.

A corresponding Maven POM is available in the simpleapp example project.

System Requirements

To use Isis Script you need local installations of the following tools:

Installation

The Isis Script Editor can be installed with the Eclipse Update Manager Help > Install New Software... from the update site https://raw.githubusercontent.com/vaulttec/isis-script/updatesite/.

License

Isis Script is released under the Eclipse Public License, Version 1.0.

isis-script's People

Contributors

mwhesse avatar tjuerge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mwhesse jifffffy

isis-script's Issues

no viable alternative at input 'repository'

Hi

I'm trying the example given in the readme, and there seems to be an issue with the respository keyword.

In Ecliipse I am getting: "no viable alternative at input 'repository'".

I'm not sure if this is related, but I also see:

eclipse.buildId=4.5.0.I20150603-2000
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.java.product
Command-line arguments:  -os linux -ws gtk -arch x86_64 -product org.eclipse.epp.package.java.product

org.apache.log4j
Error
Sun Jul 19 00:31:06 CEST 2015
org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator  - Error calling inferrer


java.lang.NullPointerException
    at org.vaulttec.isis.script.jvmmodel.IsisJvmModelInferrer._infer(IsisJvmModelInferrer.java:81)
    at org.vaulttec.isis.script.jvmmodel.IsisJvmModelInferrer.infer(IsisJvmModelInferrer.java:577)
    at org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator.installDerivedState(JvmModelAssociator.java:382)
    at org.eclipse.xtext.resource.DerivedStateAwareResource.installDerivedState(DerivedStateAwareResource.java:240)
    at org.eclipse.xtext.xbase.resource.BatchLinkableResource.getContents(BatchLinkableResource.java:148)
    at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareBatchTypeResolver.getEntryPoints(LogicalContainerAwareBatchTypeResolver.java:44)
    at org.eclipse.xtext.xbase.typesystem.internal.DefaultBatchTypeResolver.getTypeResolver(DefaultBatchTypeResolver.java:63)
    at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$1.get(CachingBatchTypeResolver.java:49)
    at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$1.get(CachingBatchTypeResolver.java:1)
    at org.eclipse.xtext.util.OnChangeEvictingCache.get(OnChangeEvictingCache.java:77)
    at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver.doResolveTypes(CachingBatchTypeResolver.java:46)
    at org.eclipse.xtext.xbase.typesystem.internal.AbstractBatchTypeResolver.resolveTypes(AbstractBatchTypeResolver.java:69)
    at org.eclipse.xtext.xbase.resource.BatchLinkingService.resolveBatched(BatchLinkingService.java:71)
    at org.eclipse.xtext.xbase.resource.BatchLinkableResource.resolveLazyCrossReferences(BatchLinkableResource.java:165)
    at org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences(EcoreUtil2.java:528)
    at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.postParse(XtextDocumentReconcileStrategy.java:174)
    at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.doReconcile(XtextDocumentReconcileStrategy.java:152)
    at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.reconcile(XtextDocumentReconcileStrategy.java:66)
    at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.doRun(XtextReconciler.java:445)
    at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.access$3(XtextReconciler.java:425)
    at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler$DocumentListener$1.exec(XtextReconciler.java:136)
    at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler$DocumentListener$1.exec(XtextReconciler.java:1)
    at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:121)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.modify(XtextDocument.java:428)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.process(XtextDocument.java:337)
    at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler$DocumentListener.performNecessaryUpdates(XtextReconciler.java:133)
    at org.eclipse.xtext.ui.editor.model.XtextDocument.updateContentBeforeRead(XtextDocument.java:249)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.internalReadOnly(XtextDocument.java:518)
    at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.readOnly(XtextDocument.java:492)
    at org.eclipse.xtext.ui.editor.model.XtextDocument.readOnly(XtextDocument.java:133)
    at org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingReconciler$2.run(HighlightingReconciler.java:320)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

Suggestion to give isis-script a snappier name...

We chose "Isis" as the name of the framework because the original authors lived (still live) in Oxfordshire, and the River Thames is called Isis when it flows through Oxford.

So, in thinking about the name of a language for Isis, I hit upon a bad pun that "source" is a "source programming language" but also the "source of a river". The source of the River Thames is officially a place called Thames_Head, which isn't much of a name, but it's very close to a village called Kemble, which is (I think) a great name.

https://en.wikipedia.org/wiki/Thames_Head

Thoughts?

Is the 'event' keyword necessary?

At first I was confused as to its purpose, but then I figured out you must be using it to generate the event class? But couldn't you infer this class just from the annotation, eg @action(domainEvent=UpdateNameDomainEvent) ?

Error trying to use eclipse plugin

Hi

This looks like a really excellent plugin. I was just thinking that it would be interesting to do a sculptor cartridge for Apache Isis, then found this.

Anyway, when trying to run it, I am not getting eclipe recognize the .isis extension, and get the below error:

eclipse.buildId=4.5.0.I20150603-2000
java.version=1.7.0_79
java.vendor=Oracle Corporation
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=en_US
Framework arguments: -product org.eclipse.epp.package.java.product
Command-line arguments: -data file:/home/anton/workspaces/tradeworks-modeling/ -os linux -ws gtk -arch x86_64 -product org.eclipse.epp.package.java.product

org.vaulttec.isis.script.ui
Error
Sat Jul 18 00:18:32 CEST 2015
FrameworkEvent ERROR

org.osgi.framework.BundleException: Could not resolve module: org.vaulttec.isis.script.ui [1821]
Unresolved requirement: Require-Capability: osgi.ee; filter:="(&(osgi.ee=JavaSE)(version=1.8))"

at org.eclipse.osgi.container.Module.start(Module.java:434)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1561)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

eclipse.buildId=4.5.0.I20150603-2000
java.version=1.7.0_79
java.vendor=Oracle Corporation
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=en_US
Framework arguments: -product org.eclipse.epp.package.java.product
Command-line arguments: -data file:/home/anton/workspacesmodeling/ -os linux -ws gtk -arch x86_64 -product org.eclipse.epp.package.java.product

compilation errors

I have replaced the example SimpleObject class with the example in the readme, and the code generates fine - looks good.

However, when trying to run, I get the following:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project strategybuilder-dom: Compilation failure: Compilation failure:
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[21,36] package domainapp.dom.modules.simple does not exist
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[22,36] package domainapp.dom.modules.simple does not exist
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[38,17] cannot find symbol
[ERROR] symbol:   class SimpleObject
[ERROR] location: class domainapp.dom.app.homepage.HomePageViewModel
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[46,5] cannot find symbol
[ERROR] symbol:   class SimpleObjects
[ERROR] location: class domainapp.dom.app.homepage.HomePageViewModel
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[21,36] package domainapp.dom.modules.simple does not exist
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[22,36] package domainapp.dom.modules.simple does not exist
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[38,17] cannot find symbol
[ERROR] symbol:   class SimpleObject
[ERROR] location: class domainapp.dom.app.homepage.HomePageViewModel
[ERROR] /home/anton/Documents/software/isis/dom/src/main/java/domainapp/dom/app/homepage/HomePageViewModel.java:[46,5] cannot find symbol
[ERROR] symbol:   class SimpleObjects
[ERROR] location: class domainapp.dom.app.homepage.HomePageViewModel
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :strategybuilder-dom

Do I need to add the path to the generated source in the pom? If yes, where/how should I do that? I have tried adding:

 <build>
     <resources>
                    <resource>
                        <directory>target/generated-sources/isis/</directory>
                    </resource>
                </resources>

but this did not help.

Thanks

The generated compareTo should use varargs version rather than CSV.

rather than:

@Override
 public int compareTo(final SimpleObject other) {
    return org.apache.isis.applib.util.ObjectContracts.compare(this, other, "name,count");
 }

should be:

@Override
public int compareTo(final SimpleObject other) {
  return org.apache.isis.applib.util.ObjectContracts.compare(this, other, "name","count");
}

outline view doesn't show child nodes for actions (though it does for properties)

I added a property:

@Property(optionality=OPTIONAL)
property Integer count {
    choices {
        #[1,2,3]
    }
}

and its "choices" node appears in the outline view, but the same is not true for the children of the "updateName" action:

@Action(domainEvent = UpdateNameDomainEvent)
action SimpleObject updateName {
    @Parameter(maxLength = 40)
    @ParameterLayout(named = "New name")
    parameter String newName {
        default {
            ...
        }
    }
    body {
        ...
    }
    validate {
                    ...
    }
    event UpdateNameDomainEvent
}

see:

image

Further features for IDE

Many years ago I wrote some Eclipse plugins of my own for the Naked Objects framework (precursor to Apache Isis). None of that code is salvageable, but perhaps some of the ideas I implemented could be brought into isis-script.

Screenshot below:
no-ide

Generated fields for injected services should have default visibility...

... so that unit tests can easily mock them out.

Currently I'm having to use reflection:

public class SimpleObjectsTest {

    @Mock
    DomainObjectContainer mockContainer;
    SimpleObjects simpleObjects;

    @Before
    public void setUp() throws Exception {
        simpleObjects = new SimpleObjects();
        
        //simpleObjects.container = mockContainer;
        Field field = simpleObjects.getClass().getField("container");
        field.set(simpleObjects, mockContainer);
    }

Update to Xtext 2.11

Version 2.11 of Xtext / Xtend is released (bug list).

Starting with version 2.11 Xtext requires Java 8 as execution environment. The minimal supported target platform is Eclipse Luna.

Automatically create a domain event hierarchy

If you look at the isis-app-todoapp or the isis-module-security addons, say, you'll see that I create a hierarchy of events... for the member, for the class, for the module. This is so that any subscribers on these events can choose the scope/granularity. (A subscriber at the module level could in essence veto all that module's functionality from the UI).

Perhaps the DSL could automatically generate this event hierarchy?

Add support for mixins

Mixins were introduced in 1.10.0 (10-nov-2015), so I think that's after the time when you did most of the work on this repo.

In terms of a keyword, I was thinking that another name for them could be "behaviour" (or "behavior", I suppose, for the US).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.