Fixed Javadoc issues for the mixed Scala/Java project.
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroJavaParser.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroJavaParser.java
new file mode 100644
index 0000000..d10aa2d
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroJavaParser.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.nlpcraft.common.makro;
+
+import java.util.Set;
+
+/**
+ * Java adapter for macro parser (so that Java reflection could work).
+ */
+public class NCMacroJavaParser implements NCMacroJavaParserTrait {
+ private final NCMacroParser impl = new NCMacroParser();
+
+ /**
+ * Expands given macro DSL string.
+ *
+ * @param s Macro DSL string to expand.
+ * @return Set of macro expansions for a given macro DSL string.
+ */
+ public Set<String> expand(String s) {
+ return impl.expandJava(s);
+ }
+
+ /**
+ * Adds or overrides given macro.
+ *
+ * @param name Macro name (typically an upper case string).
+ * It must start with '<' and end with '>' symbol.
+ * @param macro Value of the macro (any arbitrary string).
+ * @return {@code true} if an existing macro was overridden, {@code false} otherwise.
+ */
+ public boolean addMacro(String name, String macro) {
+ boolean f = impl.hasMacro(name);
+
+ impl.addMacro(name, macro);
+
+ return f;
+ }
+
+ /**
+ * Removes macro.
+ *
+ * @param name Name of the macro to remove.
+ * @return {@code true} if given macro was indeed found and removed, {@code false} otherwise.
+ */
+ public boolean removeMacro(String name) {
+ boolean f = impl.hasMacro(name);
+
+ impl.removeMacro(name);
+
+ return f;
+ }
+
+ /**
+ * Tests whether this processor has given macro.
+ *
+ * @param name Name of the macro to test.
+ * @return {@code true} if macro was found, {@code false} otherwise.
+ */
+ public boolean hasMacro(String name) {
+ return impl.hasMacro(name);
+ }
+}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroJavaParserTrait.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroJavaParserTrait.java
new file mode 100644
index 0000000..58d5452
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroJavaParserTrait.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.nlpcraft.common.makro;
+
+import java.util.Set;
+
+/**
+ * Necessary plug for Javadoc to work on mixed Java/Scala project.
+ */
+public interface NCMacroJavaParserTrait {
+ /**
+ * Expands given macro DSL string.
+ *
+ * @param s Macro DSL string to expand.
+ * @return Set of macro expansions for a given macro DSL string.
+ */
+ Set<String> expand(String s);
+
+ /**
+ * Adds or overrides given macro.
+ *
+ * @param name Macro name (typically an upper case string).
+ * It must start with '<' and end with '>' symbol.
+ * @param macro Value of the macro (any arbitrary string).
+ * @return {@code true} if an existing macro was overridden, {@code false} otherwise.
+ */
+ boolean addMacro(String name, String macro);
+
+ /**
+ * Removes macro.
+ *
+ * @param name Name of the macro to remove.
+ * @return {@code true} if given macro was indeed found and removed, {@code false} otherwise.
+ */
+ boolean removeMacro(String name);
+
+ /**
+ * Tests whether this processor has given macro.
+ *
+ * @param name Name of the macro to test.
+ * @return {@code true} if macro was found, {@code false} otherwise.
+ */
+ boolean hasMacro(String name);
+}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java
index 8a1460e..5c87a43 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCMacroProcessor.java
@@ -17,31 +17,46 @@
package org.apache.nlpcraft.model;
-import org.apache.nlpcraft.common.makro.NCMacroParser;
+import org.apache.nlpcraft.common.NCException;
+import org.apache.nlpcraft.common.makro.NCMacroJavaParserTrait;
import java.util.Set;
/**
* Standalone synonym macro DSL processor.
* <p>
- * This processor provides the same macro support as the built-in macro support in YAML/JSON models. It is
- * requires when the model is generated programmatically rather than using YAML/JSON, and synonyms need to be
- * processed in the same way. In such cases, this class can be used to manually process synonyms macro DSL.
+ * This processor provides the same macro support as the built-in macro support in data models.
+ * It is a general purpose macro-processor and it can be used standalone when testing synonyms,
+ * developing NERs, visualizing synonyms in toolchains, etc.
* <p>
- * Read full documentation in <a target=_ href="https://nlpcraft.apache.org/data-model.html">Data Model</a> section and review
+ * Read full documentation on synonym macro DSL in <a target=_ href="https://nlpcraft.apache.org/data-model.html">Data Model</a> section and review
* <a target=_ href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/">examples</a>.
*/
public class NCMacroProcessor {
- private final NCMacroParser impl = new NCMacroParser();
+ private final NCMacroJavaParserTrait impl = mkImpl();
+
+ // Need to do that in order to avoid Javadoc failures due to mixed Scala/Java project.
+ private static NCMacroJavaParserTrait mkImpl() {
+ try {
+ return (NCMacroJavaParserTrait)Class.forName("org.apache.nlpcraft.common.makro.NCMacroJavaParser")
+ .getDeclaredConstructor().newInstance();
+ }
+ catch (Exception e) {
+ throw new NCException("Error initializing object of type: org.apache.nlpcraft.common.makro.NCMacroJavaParser", e);
+ }
+ }
/**
* Expands given macro DSL string.
+ * <p>
+ * Read full documentation on synonym macro DSL in <a target=_ href="https://nlpcraft.apache.org/data-model.html">Data Model</a> section and review
+ * <a target=_ href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/">examples</a>.
*
* @param s Macro DSL string to expand.
* @return Set of macro expansions for a given macro DSL string.
*/
public Set<String> expand(String s) {
- return impl.expandJava(s);
+ return impl.expand(s);
}
/**
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroParserSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroParserSpec.scala
index 497f8aa..9d7dcf1 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroParserSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroParserSpec.scala
@@ -18,21 +18,23 @@
package org.apache.nlpcraft.common.makro
import org.apache.nlpcraft.common._
+import org.apache.nlpcraft.model.NCMacroProcessor
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import scala.compat.Platform._
-import scala.util.control.Exception._
+import scala.collection.JavaConverters._
/**
* Tests for text parser.
*/
class NCMacroParserSpec {
- private val parser = NCMacroParser(
- "<A>" → "aaa",
- "<B>" → "<A> bbb",
- "<C>" → "<A> bbb {z|w}"
- )
+ private val parser = new NCMacroProcessor()
+
+ parser.addMacro("<A>", "aaa")
+ parser.addMacro("<B>", "<A> bbb")
+ parser.addMacro("<C>", "<A> bbb {z|w}")
+
// Add macros for testing...
parser.addMacro("<OF>", "{of|for|per}")
@@ -52,16 +54,14 @@
parser.addMacro("<METRICS_A>", "{analytics|statistics|measurements|analysis|report|efficiency|performance}")
parser.addMacro("<METRICS_B>", "{metrics|data|info|information|facts}")
parser.addMacro("<METRICS>","{<METRICS_A>|<METRICS_B>|<METRICS_A> <METRICS_B>|<METRICS_B> <METRICS_A>}")
-
- private val ignoreNCE = ignoring(classOf[NCE])
-
+
/**
*
* @param txt Text to expand.
* @param exp Expected expansion strings.
*/
def checkEq(txt: String, exp: Seq[String]): Unit = {
- val z = parser.expand(txt).sorted
+ val z = parser.expand(txt).asScala.toSeq.sorted
val w = exp.sorted
if (z != w)