Skip to content

Commit

Permalink
AF-593: Decouple DMO from Drools (apache#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarkley authored and porcelli committed Oct 16, 2017
1 parent e769cd1 commit b5eea24
Show file tree
Hide file tree
Showing 371 changed files with 13,832 additions and 13,589 deletions.
1 change: 1 addition & 0 deletions drools-wb-extensions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
1 change: 1 addition & 0 deletions drools-wb-extensions/drools-wb-drl-extension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.drools</groupId>
<artifactId>drools-wb-drl-extension</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>

<artifactId>drools-wb-drl-extension-server</artifactId>
<packaging>jar</packaging>

<name>Drools Workbench - Extensions - DRL - Server</name>
<description>Drools Workbench - Extensions - DRL - Server</description>

<dependencies>
<dependency>
<groupId>org.kie.workbench.services</groupId>
<artifactId>kie-wb-common-datamodel-api</artifactId>
</dependency>
<dependency>
<groupId>org.kie.workbench.services</groupId>
<artifactId>kie-wb-common-services-backend</artifactId>
</dependency>
<dependency>
<groupId>org.kie.workbench.services</groupId>
<artifactId>kie-wb-common-datamodel-backend</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-workbench-models-datamodel-api</artifactId>
</dependency>
<dependency>
<groupId>org.kie.soup</groupId>
<artifactId>kie-soup-project-datamodel-api</artifactId>
</dependency>
<dependency>
<groupId>org.uberfire</groupId>
<artifactId>uberfire-nio2-model</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.workbench.common.services.datamodel.backend.server.cache;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.enterprise.context.Dependent;

import org.drools.compiler.lang.dsl.DSLMappingEntry;
import org.drools.compiler.lang.dsl.DSLTokenizedMappingFile;
import org.drools.workbench.models.datamodel.oracle.DSLActionSentence;
import org.drools.workbench.models.datamodel.oracle.DSLConditionSentence;
import org.drools.workbench.models.datamodel.rule.DSLSentence;
import org.kie.soup.project.datamodel.oracle.ExtensionKind;
import org.kie.workbench.common.services.backend.file.DSLFileFilter;
import org.kie.workbench.common.services.datamodel.spi.DataModelExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.uberfire.java.nio.file.DirectoryStream.Filter;
import org.uberfire.java.nio.file.Path;

@Dependent
public class DSLExtension implements DataModelExtension {

public static final DSLFileFilter DSL_FILE_FILTER = new DSLFileFilter();
private static final Logger log = LoggerFactory.getLogger(DSLExtension.class);

private static class DSLMapping implements ExtensionMapping<DSLSentence> {

private ExtensionKind<DSLSentence> kind;
private List<DSLSentence> sentences;

DSLMapping(ExtensionKind<DSLSentence> kind, List<DSLSentence> sentences) {
this.kind = kind;
this.sentences = sentences;
}

@Override
public ExtensionKind<DSLSentence> getKind() {
return kind;
}

@Override
public List<DSLSentence> getValues() {
return sentences;
}
}

@Override
public Filter<Path> getFilter() {
return DSL_FILE_FILTER;
}

@Override
public List<ExtensionMapping<?>> getExtensions(Path path, String content) {
final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile();
List<DSLSentence> actionSentences = new ArrayList<>();
List<DSLSentence> conditionSentences = new ArrayList<>();
try {
if (dslLoader.parseAndLoad(new StringReader(content))) {
for (DSLMappingEntry entry : dslLoader.getMapping().getEntries()) {
if (entry.getSection() == DSLMappingEntry.CONDITION) {
final DSLMappingEntry definition = entry;
final DSLSentence sentence = new DSLSentence();
sentence.setDrl(definition.getMappingValue());
sentence.setDefinition(definition.getMappingKey());
conditionSentences.add(sentence);
} else if (entry.getSection() == DSLMappingEntry.CONSEQUENCE) {
final DSLMappingEntry definition = entry;
final DSLSentence sentence = new DSLSentence();
sentence.setDrl(definition.getMappingValue());
sentence.setDefinition(definition.getMappingKey());
actionSentences.add(sentence);
}
}
}
} catch (IOException e) {
log.error(e.getMessage());
}

return Arrays.asList(new DSLMapping(DSLActionSentence.INSTANCE, actionSentences),
new DSLMapping(DSLConditionSentence.INSTANCE, conditionSentences));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kie.workbench.common.services.datamodel.backend.server;

import java.util.List;

import org.drools.workbench.models.datamodel.oracle.DSLActionSentence;
import org.drools.workbench.models.datamodel.oracle.DSLConditionSentence;
import org.junit.Test;
import org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator;
import org.kie.soup.project.datamodel.oracle.ExtensionKind;
import org.kie.soup.project.datamodel.oracle.PackageDataModelOracle;
import org.kie.workbench.common.services.datamodel.backend.server.builder.packages.PackageDataModelOracleBuilder;
import org.kie.workbench.common.services.datamodel.backend.server.cache.DSLExtension;
import org.kie.workbench.common.services.datamodel.spi.DataModelExtension.ExtensionMapping;

import static junit.framework.Assert.*;

public class DSLExtensionTest {

private DSLExtension extension = new DSLExtension();

@Test
public void testAddConditionDSLSentence() {
List<ExtensionMapping<?>> mappings = extension.getExtensions(null,
"[when]There is a Smurf=Smurf()");

assertEquals(1,
totalValues(mappings));
assertEquals(1,
valuesOfKind(mappings, DSLConditionSentence.INSTANCE));
}

@Test
public void testAddActionDSLSentence() {
List<ExtensionMapping<?>> mappings = extension.getExtensions(null,
"[then]Greet Smurf=System.out.println(\"Hello Smurf\");");

assertEquals(1,
totalValues(mappings));
assertEquals(1,
valuesOfKind(mappings, DSLActionSentence.INSTANCE));
}

@Test
public void testAddMultipleConditionDSLSentence() {
List<ExtensionMapping<?>> mappings = extension.getExtensions(null,
"[when]There is a Smurf=Smurf()\n"
+ "[when]There is Happy Smurf=Smurf( nature = HAPPY )");

assertEquals(2,
totalValues(mappings));
assertEquals(2,
valuesOfKind(mappings, DSLConditionSentence.INSTANCE));
}

@Test
public void testAddMultipleActionDSLSentence() {
List<ExtensionMapping<?>> mappings = extension.getExtensions(null,
"[then]Report Smurfs=System.out.println(\"There is a Smurf\");\n"
+ "[then]Greet Happy Smurf=System.out.println(\"Hello Happy Smurf\");");

assertEquals(2,
totalValues(mappings));
assertEquals(2,
valuesOfKind(mappings, DSLActionSentence.INSTANCE));
}

private static int totalValues(final List<ExtensionMapping<?>> mappings) {
return (int) mappings
.stream()
.flatMap(em -> em.getValues().stream())
.count();
}

private static int valuesOfKind(final List<ExtensionMapping<?>> mappings, final ExtensionKind kind) {
return (int) mappings
.stream()
.filter(em -> kind.equals(em.getKind()))
.flatMap(em -> em.getValues().stream())
.count();
}
}
19 changes: 19 additions & 0 deletions drools-wb-extensions/drools-wb-drl-extension/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.drools</groupId>
<artifactId>drools-wb-extensions</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>

<artifactId>drools-wb-drl-extension</artifactId>
<packaging>pom</packaging>

<name>Drools Workbench - Extensions - DRL</name>
<description>Drools Workbench - DRL</description>

<modules>
<module>drools-wb-drl-extension-server</module>
</modules>

</project>
19 changes: 19 additions & 0 deletions drools-wb-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.drools</groupId>
<artifactId>drools-wb</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>

<artifactId>drools-wb-extensions</artifactId>
<packaging>pom</packaging>

<name>Drools Workbench - Extensions</name>
<description>Drools Workbench - Extensions</description>

<modules>
<module>drools-wb-drl-extension</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>org.uberfire</groupId>
<artifactId>uberfire-commons</artifactId>
</dependency>
<dependency>
<groupId>org.kie.soup</groupId>
<artifactId>kie-soup-commons</artifactId>
</dependency>

<dependency>
<groupId>org.guvnor</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.drools.workbench.models.datamodel.rule.DSLSentence;
import org.guvnor.common.services.shared.metadata.model.Overview;
import org.jboss.errai.common.client.api.annotations.Portable;
import org.uberfire.commons.validation.PortablePreconditions;
import org.kie.soup.commons.validation.PortablePreconditions;

@Portable
public class DrlModelContent {
Expand All @@ -34,21 +34,21 @@ public class DrlModelContent {
public DrlModelContent() {
}

public DrlModelContent( final String drl,
final Overview overview,
final List<String> fullyQualifiedClassNames,
final List<DSLSentence> dslConditions,
final List<DSLSentence> dslActions ) {
this.overview = PortablePreconditions.checkNotNull( "overview",
overview);
this.drl = PortablePreconditions.checkNotNull( "drl",
drl );
this.fullyQualifiedClassNames = PortablePreconditions.checkNotNull( "fullyQualifiedClassNames",
fullyQualifiedClassNames );
this.dslConditions = PortablePreconditions.checkNotNull( "dslConditions",
dslConditions );
this.dslActions = PortablePreconditions.checkNotNull( "dslActions",
dslActions );
public DrlModelContent(final String drl,
final Overview overview,
final List<String> fullyQualifiedClassNames,
final List<DSLSentence> dslConditions,
final List<DSLSentence> dslActions) {
this.overview = PortablePreconditions.checkNotNull("overview",
overview);
this.drl = PortablePreconditions.checkNotNull("drl",
drl);
this.fullyQualifiedClassNames = PortablePreconditions.checkNotNull("fullyQualifiedClassNames",
fullyQualifiedClassNames);
this.dslConditions = PortablePreconditions.checkNotNull("dslConditions",
dslConditions);
this.dslActions = PortablePreconditions.checkNotNull("dslActions",
dslActions);
}

public String getDrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<artifactId>drools-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.uberfire</groupId>
<artifactId>uberfire-project-datamodel-api</artifactId>
<groupId>org.kie.soup</groupId>
<artifactId>kie-soup-project-datamodel-api</artifactId>
</dependency>
<dependency>
<groupId>org.uberfire</groupId>
<artifactId>uberfire-project-datamodel-commons</artifactId>
<groupId>org.kie.soup</groupId>
<artifactId>kie-soup-project-datamodel-commons</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
Expand Down
Loading

0 comments on commit b5eea24

Please sign in to comment.