fix(builder): fix the construction of concept tree (#142)

This commit is contained in:
baifuyu 2024-03-07 14:42:14 +08:00 committed by GitHub
parent f8f8206b0c
commit df86073bdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 53 additions and 13 deletions

View File

@ -23,15 +23,13 @@ import com.antgroup.openspg.builder.core.strategy.predicting.RecordPredicting;
import com.antgroup.openspg.builder.core.strategy.predicting.RecordPredictingImpl;
import com.antgroup.openspg.builder.model.exception.BuilderRecordException;
import com.antgroup.openspg.builder.model.pipeline.config.SPGTypeMappingNodeConfig;
import com.antgroup.openspg.builder.model.record.BaseAdvancedRecord;
import com.antgroup.openspg.builder.model.record.BaseSPGRecord;
import com.antgroup.openspg.builder.model.record.BuilderRecord;
import com.antgroup.openspg.builder.model.record.RelationRecord;
import com.antgroup.openspg.builder.model.record.*;
import com.antgroup.openspg.cloudext.interfaces.graphstore.adapter.util.EdgeRecordConvertor;
import com.antgroup.openspg.cloudext.interfaces.graphstore.adapter.util.VertexRecordConvertor;
import com.antgroup.openspg.common.util.StringUtils;
import com.antgroup.openspg.core.schema.model.BaseOntology;
import com.antgroup.openspg.core.schema.model.identifier.BaseSPGIdentifier;
import com.antgroup.openspg.core.schema.model.identifier.ConceptIdentifier;
import com.antgroup.openspg.core.schema.model.identifier.SPGIdentifierTypeEnum;
import com.antgroup.openspg.core.schema.model.identifier.SPGTypeIdentifier;
import com.antgroup.openspg.core.schema.model.type.BaseSPGType;
@ -113,11 +111,19 @@ public class SPGTypeMappingHelper {
return true;
}
public List<BaseSPGRecord> toSPGRecords(BuilderRecord record) {
public List<BaseSPGRecord> toSPGRecords(BuilderRecord record, boolean first) {
Map<String, String> propertyValues = propertyMapping(record);
Map<String, String> relationValues = relationMapping(record);
List<BaseSPGRecord> results = new ArrayList<>();
if (first && spgType.isConceptType()) {
List<BuilderRecord> fatherConcepts = hypernymPredicate(record, propertyValues);
for (BuilderRecord fatherConcept : fatherConcepts) {
results.addAll(toSPGRecords(fatherConcept, false));
}
}
Map<String, String> relationValues = relationMapping(record);
List<BaseAdvancedRecord> advancedRecords = toAdvancedRecord(propertyValues, relationValues);
for (BaseAdvancedRecord advancedRecord : advancedRecords) {
if (CollectionUtils.isNotEmpty(advancedRecord.getRelationRecords())) {
@ -155,6 +161,25 @@ public class SPGTypeMappingHelper {
return propertyValues;
}
private List<BuilderRecord> hypernymPredicate(
BuilderRecord record, Map<String, String> propertyValues) {
String bizId = propertyValues.get("id");
if (StringUtils.isBlank(bizId)) {
throw new BuilderRecordException("id is not in propertyValues");
}
List<BuilderRecord> fatherBuilderRecord = new ArrayList<>();
String fatherId = new ConceptIdentifier(bizId).getFatherId();
while (StringUtils.isNotBlank(fatherId)) {
Map<String, String> fatherProps = new HashMap<>(1);
fatherProps.put(config.getSourceFromTarget("id"), fatherId);
fatherBuilderRecord.add(record.withNewProps(fatherProps));
fatherId = new ConceptIdentifier(fatherId).getFatherId();
}
return fatherBuilderRecord;
}
public Map<String, String> relationMapping(BuilderRecord record) {
List<SPGTypeMappingNodeConfig.MappingConfig> relationMappingConfigs =
config.getRelationLinkingConfigs();
@ -194,7 +219,7 @@ public class SPGTypeMappingHelper {
Map<String, String> propertyValues, Map<String, String> relationValues) {
String bizId = propertyValues.get("id");
if (StringUtils.isBlank(bizId)) {
throw new BuilderRecordException("");
throw new BuilderRecordException("id is not in propertyValues");
}
BaseAdvancedRecord advancedRecord =

View File

@ -89,6 +89,6 @@ public class SPGTypeMappingProcessor extends BaseProcessor<SPGTypeMappingNodeCon
return Collections.emptyList();
}
return mappingHelper.toSPGRecords(record);
return mappingHelper.toSPGRecords(record, true);
}
}

View File

@ -127,4 +127,13 @@ public class SPGTypeMappingNodeConfig extends BaseNodeConfig {
.filter(MappingConfig::isSubRelationLinking)
.collect(Collectors.toList());
}
public String getSourceFromTarget(String target) {
for (MappingConfig config : mappingConfigs) {
if (config.getTarget().equals(target)) {
return config.getSource();
}
}
throw new RuntimeException("no source for target=" + target);
}
}

View File

@ -47,7 +47,6 @@ public class ConceptRecord extends BaseAdvancedRecord {
private void addNameProperty() {
Property property = conceptType.getPropertyMap().get(NAME);
if (property != null) {
// 把原来的属性中的name删除补上从id里面抽取出来的name
properties.removeIf(record -> NAME.equals(record.getName()));
SPGPropertyValue spgPropertyValue = new SPGPropertyValue(conceptName.getName());
this.properties.add(new SPGPropertyRecord(property, spgPropertyValue));

View File

@ -24,7 +24,6 @@ import com.antgroup.openspg.cloudext.interfaces.graphstore.model.lpg.schema.Edge
import com.antgroup.openspg.core.schema.model.identifier.ConceptIdentifier;
import com.google.common.collect.Lists;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
/**
* Strategy for translation from alter item of {@link ConceptRecord} to {@link LPGRecordAlterItem}s.
@ -74,6 +73,6 @@ public class ConceptRecord2LPGStrategy extends SPGTypeRecord2LPGStrategy {
private boolean isRootConcept(ConceptRecord conceptRecord) {
ConceptIdentifier name = conceptRecord.getConceptName();
return StringUtils.isBlank(name.getFatherId());
return name.isRootConcept();
}
}

View File

@ -11,7 +11,6 @@
~ is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
~ or implied.
-->
<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>

View File

@ -24,6 +24,8 @@ public class ConceptIdentifier extends BaseSPGIdentifier {
/** The seperator symbol between parent and child concept names. */
public static final String SEPARATOR = "-";
private static final String ROOT = "__ROOT__";
/** Unique id of concept node. */
private final String id;
@ -51,8 +53,11 @@ public class ConceptIdentifier extends BaseSPGIdentifier {
public String getFatherId() {
String[] splits = id.split(SEPARATOR);
if (splits.length <= 1) {
if (ROOT.equals(id)) {
return StringUtils.EMPTY;
}
if (splits.length <= 1) {
return ROOT;
} else {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < splits.length - 2; i++) {
@ -63,6 +68,10 @@ public class ConceptIdentifier extends BaseSPGIdentifier {
}
}
public boolean isRootConcept() {
return ROOT.equals(id);
}
@Override
public String toString() {
return id;