mirror of
https://github.com/OpenSPG/openspg.git
synced 2025-08-16 21:00:59 +00:00
feat(reasoner): support re match udf (#203)
This commit is contained in:
parent
2ac8fe2261
commit
57f6abdd9c
@ -943,7 +943,7 @@ class PatternParser extends Serializable {
|
||||
|
||||
def parseLabelName(ctx: Label_nameContext): LabelType = {
|
||||
ctx.getChild(0) match {
|
||||
case c: Entity_typeContext => EntityLabelType(c.getText)
|
||||
case c: Entity_typeContext => parseEntityType(c)
|
||||
case c: Concept_nameContext =>
|
||||
ConceptLabelType(
|
||||
parseIdentifier(c.meta_concept_type().identifier()),
|
||||
@ -951,6 +951,17 @@ class PatternParser extends Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
def parseEntityType(ctx: Entity_typeContext): LabelType = {
|
||||
val name = ctx.getChild(0) match {
|
||||
case c: IdentifierContext => parseIdentifier(c)
|
||||
case c: Prefix_nameContext =>
|
||||
parseIdentifier(c.identifier(0)) +
|
||||
c.period().getText +
|
||||
parseIdentifier(c.identifier(1))
|
||||
}
|
||||
EntityLabelType(name)
|
||||
}
|
||||
|
||||
def parseIdentifier(ctx: IdentifierContext): String = {
|
||||
ctx.oC_SymbolicName().EscapedSymbolicName() match {
|
||||
case null => ctx.oC_SymbolicName().getText
|
||||
|
@ -19,6 +19,36 @@ import org.scalatest.funspec.AnyFunSpec
|
||||
import org.scalatest.matchers.should.Matchers.{convertToAnyShouldWrapper, equal}
|
||||
|
||||
class PatternParserTest extends AnyFunSpec {
|
||||
it("test key word") {
|
||||
val s =
|
||||
"""
|
||||
|GraphStructure {
|
||||
| A [`Action`]
|
||||
| B [Finish]
|
||||
| A->B [endAction]
|
||||
|}
|
||||
|Rule {
|
||||
|}
|
||||
|Action {
|
||||
| get(A.name,B.name)
|
||||
|}
|
||||
|""".stripMargin
|
||||
val parser = new LexerInit().initKGReasonerParser(s)
|
||||
val patternParser = new PatternParser()
|
||||
val block = patternParser.parseGraphStructureDefine(
|
||||
parser
|
||||
.kg_dsl()
|
||||
.base_job()
|
||||
.kgdsl_old_define()
|
||||
.the_graph_structure()
|
||||
.graph_structure_define())
|
||||
print(block.pretty)
|
||||
block.isInstanceOf[MatchBlock] should equal(true)
|
||||
val aNode = block.asInstanceOf[MatchBlock]
|
||||
.patterns("unresolved_default_path").graphPattern.getNode("A")
|
||||
aNode == null should equal(false)
|
||||
aNode.typeNames.contains("Action") should equal(true)
|
||||
}
|
||||
it("old") {
|
||||
val s =
|
||||
"""
|
||||
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2023 OpenSPG Authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.antgroup.openspg.reasoner.udf.builtin.udf;
|
||||
|
||||
import com.antgroup.openspg.reasoner.udf.model.UdfDefine;
|
||||
import com.antgroup.openspg.reasoner.udf.model.UdfOperatorTypeEnum;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ReMatch {
|
||||
|
||||
/**
|
||||
* regex like match
|
||||
*
|
||||
* @param inputStr
|
||||
* @param regex
|
||||
* @return
|
||||
*/
|
||||
@UdfDefine(name = "regex_match", udfType = UdfOperatorTypeEnum.OPERATOR)
|
||||
public String regexMatch(String inputStr, String regex) {
|
||||
if (inputStr == null) {
|
||||
return null;
|
||||
}
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(inputStr);
|
||||
if (matcher.find()) {
|
||||
return matcher.group();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -54,6 +54,16 @@ public class UdfTest {
|
||||
DateUtils.timeZone = TimeZone.getTimeZone("Asia/Shanghai");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReMatch() {
|
||||
UdfMng mng = UdfMngFactory.getUdfMng();
|
||||
IUdfMeta udfMeta =
|
||||
mng.getUdfMeta("regex_match", Lists.newArrayList(KTString$.MODULE$, KTString$.MODULE$));
|
||||
Object rst =
|
||||
udfMeta.invoke("Hello, my email address is example@example.com", "\\b\\w+@\\w+\\.\\w+\\b");
|
||||
Assert.assertEquals(rst, "example@example.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonGet() {
|
||||
UdfMng mng = UdfMngFactory.getUdfMng();
|
||||
|
Loading…
x
Reference in New Issue
Block a user