Skip to content

Commit

Permalink
BZ-1051463 - Saving a rule file should not cause a Data Model Oracle …
Browse files Browse the repository at this point in the history
…rebuild
  • Loading branch information
Rikkola committed Apr 11, 2014
1 parent a1c96fd commit 1d2d55b
Show file tree
Hide file tree
Showing 15 changed files with 785 additions and 1 deletion.
5 changes: 5 additions & 0 deletions kie-wb-common-services/kie-wb-common-services-api/pom.xml
Expand Up @@ -36,6 +36,11 @@
<artifactId>uberfire-client-api</artifactId>
</dependency>

<dependency>
<groupId>org.uberfire</groupId>
<artifactId>uberfire-io</artifactId>
</dependency>

</dependencies>

</project>
Expand Up @@ -16,15 +16,27 @@

package org.kie.workbench.common.services.backend.source;

import javax.inject.Inject;
import javax.inject.Named;

import org.uberfire.io.IOService;
import org.uberfire.java.nio.file.Path;

public abstract class DRLBaseSourceService
extends BaseSourceService<String> {

@Inject
@Named("ioStrategy")
private IOService ioService;

@Override
public String getSource( final Path path,
final String drl ) {
return drl;
}

@Override
public String getSource(Path path) {
return ioService.readAllString(path);
}
}
Expand Up @@ -29,6 +29,11 @@ public interface SourceService<T> {
*/
String getSource( final Path path,
final T model );
/**
* @param path path to the file
* @return Source generated from the model, that the path points to.
*/
String getSource(Path path);

String getPattern();

Expand Down
@@ -0,0 +1,25 @@
/*
* Copyright 2014 JBoss Inc
*
* 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.shared.rulename;

import java.util.List;

public interface RuleNameService {

public List<String> getRuleNames(String packageName);

}
6 changes: 6 additions & 0 deletions kie-wb-common-services/kie-wb-common-services-backend/pom.xml
Expand Up @@ -52,6 +52,12 @@
<artifactId>commons-fileupload</artifactId>
</dependency>

<dependency>
<groupId>org.uberfire</groupId>
<artifactId>uberfire-nio2-fs</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
@@ -0,0 +1,150 @@
/*
* Copyright 2014 JBoss Inc
*
* 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.backend.rulename;

import java.io.File;
import java.net.URI;
import java.util.Iterator;

import org.uberfire.java.nio.IOException;
import org.uberfire.java.nio.file.ClosedWatchServiceException;
import org.uberfire.java.nio.file.FileSystem;
import org.uberfire.java.nio.file.InvalidPathException;
import org.uberfire.java.nio.file.LinkOption;
import org.uberfire.java.nio.file.Path;
import org.uberfire.java.nio.file.WatchEvent;
import org.uberfire.java.nio.file.WatchKey;
import org.uberfire.java.nio.file.WatchService;

public class MockPath implements Path {

private final String fileName;
private final FileSystem fileSystem;

public MockPath(String fileName, FileSystem fileSystem) {
this.fileName = fileName;
this.fileSystem = fileSystem;
}

@Override public FileSystem getFileSystem() {
return fileSystem;
}

@Override public boolean isAbsolute() {
return false;
}

@Override public Path getRoot() {
return null;
}

@Override public Path getFileName() {
return this;
}

@Override public Path getParent() {
return null;
}

@Override public int getNameCount() {
return 0;
}

@Override public Path getName(int index) throws IllegalArgumentException {
return null;
}

@Override public Path subpath(int beginIndex, int endIndex) throws IllegalArgumentException {
return null;
}

@Override public boolean startsWith(Path other) {
return false;
}

@Override public boolean startsWith(String other) throws InvalidPathException {
return false;
}

@Override public boolean endsWith(Path other) {
return false;
}

@Override public boolean endsWith(String other) throws InvalidPathException {
return false;
}

@Override public Path normalize() {
return null;
}

@Override public Path resolve(Path other) {
return null;
}

@Override public Path resolve(String other) throws InvalidPathException {
return null;
}

@Override public Path resolveSibling(Path other) {
return null;
}

@Override public Path resolveSibling(String other) throws InvalidPathException {
return null;
}

@Override public Path relativize(Path other) throws IllegalArgumentException {
return null;
}

@Override public URI toUri() throws IOException, SecurityException {
return URI.create("git://" + fileName);
}

@Override public Path toAbsolutePath() throws IOException, SecurityException {
return null;
}

@Override public Path toRealPath(LinkOption... options) throws IOException, SecurityException {
return null;
}

@Override public File toFile() throws UnsupportedOperationException {
return null;
}

@Override public int compareTo(Path path) {
return 0;
}

@Override public Iterator<Path> iterator() {
return null;
}

@Override public WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers) throws UnsupportedOperationException, IllegalArgumentException, ClosedWatchServiceException, IOException, SecurityException {
return null;
}

@Override public WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws UnsupportedOperationException, IllegalArgumentException, ClosedWatchServiceException, IOException, SecurityException {
return null;
}

@Override public String toString() {
return fileName;
}
}
@@ -0,0 +1,152 @@
/*
* Copyright 2014 JBoss Inc
*
* 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.backend.rulename;

import java.util.ArrayList;
import java.util.List;

public class RuleNameResolver {

private ArrayList<String> ruleNames = new ArrayList<String>();

private String drl;

private boolean currentRuleNameHasQuotes = false;

public RuleNameResolver(String drl) {
this.drl = drl;
}

public List<String> resolve() {

// Strip comments
while (drl.contains("//")) {
int commentStart = drl.indexOf("//");
int endOfComment = drl.indexOf("\n", commentStart);
if (endOfComment >= 0) {
drl = drl.substring(0, commentStart) + drl.substring(endOfComment);
} else {
drl = drl.substring(0, commentStart);
}
}

while (hasMultiLineComment()) {
clearMultiLineComment();
}

while (hasRule()) {
String ruleLine = getNextRuleHeaderLine();

stripNameFromLine(ruleNames, ruleLine);

drl = drl.substring(drl.indexOf(ruleLine) + ruleLine.length());
}

return ruleNames;
}

private void clearMultiLineComment() {
int startIndexOfTheComment = drl.indexOf("/*");

drl = drl.substring(0, startIndexOfTheComment) + drl.substring(getEndOfComment());
}

private int getEndOfComment() {
int endOfComment = drl.indexOf("*/") + "*/".length();

while (endOfNextComment(endOfComment) >= 0) {
int endOfNextComment = endOfNextComment(endOfComment) + "*/".length();

String substring = drl.substring(endOfComment, endOfNextComment);
if (endOfNextComment >= 0 && !hasMultiLineComment(substring)) {
endOfComment = endOfNextComment;
} else {
break;
}
}

return endOfComment;
}

private boolean hasMultiLineComment(String substring) {
return substring.contains("*/")
&& substring.contains("/*")
&& substring.indexOf("/*") < substring.indexOf("*/");
}

private int endOfNextComment(int endOfComment) {
return drl.indexOf("*/", endOfComment);
}

private boolean hasMultiLineComment() {
return drl.indexOf("/*") >= 0;
}

private String getNextRuleHeaderLine() {
int beginIndex = getRuleIndex();
return drl.substring(beginIndex, drl.indexOf("\n", beginIndex));
}

private int getRuleIndex() {
int ruleIndex = drl.indexOf("rule");

int beginIndexWithQuotes = drl.indexOf("rule \"");

if (ruleIndex >= beginIndexWithQuotes && beginIndexWithQuotes >= 0) {
currentRuleNameHasQuotes = true;
return beginIndexWithQuotes;
} else {
currentRuleNameHasQuotes = false;
return ruleIndex;
}
}

private boolean hasRule() {
return getRuleIndex() >= 0;
}

private void stripNameFromLine(ArrayList<String> ruleNames, String ruleLine) {
ruleLine = removeTheWorldRuleAndWhiteSpacesFromTheBeginning(ruleLine);

int endIndex = getRuleNameEndIndex(ruleLine);
if (endIndex >= 0) {
ruleNames.add(ruleLine.substring(0, endIndex));
} else {
ruleNames.add(ruleLine);
}
}

private String removeTheWorldRuleAndWhiteSpacesFromTheBeginning(String ruleLine) {
return ruleLine.substring(getRuleFrontBitLength()).trim();
}

private int getRuleNameEndIndex(String ruleLine) {
if (currentRuleNameHasQuotes) {
return ruleLine.indexOf("\"");
} else {
return ruleLine.indexOf(" ");
}
}

private int getRuleFrontBitLength() {
if (currentRuleNameHasQuotes) {
return "rule \"".length();
} else {
return "rule".length();
}
}
}

0 comments on commit 1d2d55b

Please sign in to comment.