mirror of
https://github.com/OpenSPG/openspg.git
synced 2025-11-15 18:08:30 +00:00
fix(builder): fix the construction of concept tree (#142)
This commit is contained in:
parent
f8f8206b0c
commit
df86073bdb
@ -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.core.strategy.predicting.RecordPredictingImpl;
|
||||||
import com.antgroup.openspg.builder.model.exception.BuilderRecordException;
|
import com.antgroup.openspg.builder.model.exception.BuilderRecordException;
|
||||||
import com.antgroup.openspg.builder.model.pipeline.config.SPGTypeMappingNodeConfig;
|
import com.antgroup.openspg.builder.model.pipeline.config.SPGTypeMappingNodeConfig;
|
||||||
import com.antgroup.openspg.builder.model.record.BaseAdvancedRecord;
|
import com.antgroup.openspg.builder.model.record.*;
|
||||||
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.cloudext.interfaces.graphstore.adapter.util.EdgeRecordConvertor;
|
import com.antgroup.openspg.cloudext.interfaces.graphstore.adapter.util.EdgeRecordConvertor;
|
||||||
import com.antgroup.openspg.cloudext.interfaces.graphstore.adapter.util.VertexRecordConvertor;
|
import com.antgroup.openspg.cloudext.interfaces.graphstore.adapter.util.VertexRecordConvertor;
|
||||||
import com.antgroup.openspg.common.util.StringUtils;
|
import com.antgroup.openspg.common.util.StringUtils;
|
||||||
import com.antgroup.openspg.core.schema.model.BaseOntology;
|
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.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.SPGIdentifierTypeEnum;
|
||||||
import com.antgroup.openspg.core.schema.model.identifier.SPGTypeIdentifier;
|
import com.antgroup.openspg.core.schema.model.identifier.SPGTypeIdentifier;
|
||||||
import com.antgroup.openspg.core.schema.model.type.BaseSPGType;
|
import com.antgroup.openspg.core.schema.model.type.BaseSPGType;
|
||||||
@ -113,11 +111,19 @@ public class SPGTypeMappingHelper {
|
|||||||
return true;
|
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> propertyValues = propertyMapping(record);
|
||||||
Map<String, String> relationValues = relationMapping(record);
|
|
||||||
|
|
||||||
List<BaseSPGRecord> results = new ArrayList<>();
|
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);
|
List<BaseAdvancedRecord> advancedRecords = toAdvancedRecord(propertyValues, relationValues);
|
||||||
for (BaseAdvancedRecord advancedRecord : advancedRecords) {
|
for (BaseAdvancedRecord advancedRecord : advancedRecords) {
|
||||||
if (CollectionUtils.isNotEmpty(advancedRecord.getRelationRecords())) {
|
if (CollectionUtils.isNotEmpty(advancedRecord.getRelationRecords())) {
|
||||||
@ -155,6 +161,25 @@ public class SPGTypeMappingHelper {
|
|||||||
return propertyValues;
|
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) {
|
public Map<String, String> relationMapping(BuilderRecord record) {
|
||||||
List<SPGTypeMappingNodeConfig.MappingConfig> relationMappingConfigs =
|
List<SPGTypeMappingNodeConfig.MappingConfig> relationMappingConfigs =
|
||||||
config.getRelationLinkingConfigs();
|
config.getRelationLinkingConfigs();
|
||||||
@ -194,7 +219,7 @@ public class SPGTypeMappingHelper {
|
|||||||
Map<String, String> propertyValues, Map<String, String> relationValues) {
|
Map<String, String> propertyValues, Map<String, String> relationValues) {
|
||||||
String bizId = propertyValues.get("id");
|
String bizId = propertyValues.get("id");
|
||||||
if (StringUtils.isBlank(bizId)) {
|
if (StringUtils.isBlank(bizId)) {
|
||||||
throw new BuilderRecordException("");
|
throw new BuilderRecordException("id is not in propertyValues");
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseAdvancedRecord advancedRecord =
|
BaseAdvancedRecord advancedRecord =
|
||||||
|
|||||||
@ -89,6 +89,6 @@ public class SPGTypeMappingProcessor extends BaseProcessor<SPGTypeMappingNodeCon
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return mappingHelper.toSPGRecords(record);
|
return mappingHelper.toSPGRecords(record, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -127,4 +127,13 @@ public class SPGTypeMappingNodeConfig extends BaseNodeConfig {
|
|||||||
.filter(MappingConfig::isSubRelationLinking)
|
.filter(MappingConfig::isSubRelationLinking)
|
||||||
.collect(Collectors.toList());
|
.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,7 +47,6 @@ public class ConceptRecord extends BaseAdvancedRecord {
|
|||||||
private void addNameProperty() {
|
private void addNameProperty() {
|
||||||
Property property = conceptType.getPropertyMap().get(NAME);
|
Property property = conceptType.getPropertyMap().get(NAME);
|
||||||
if (property != null) {
|
if (property != null) {
|
||||||
// 把原来的属性中的name删除,补上从id里面抽取出来的name
|
|
||||||
properties.removeIf(record -> NAME.equals(record.getName()));
|
properties.removeIf(record -> NAME.equals(record.getName()));
|
||||||
SPGPropertyValue spgPropertyValue = new SPGPropertyValue(conceptName.getName());
|
SPGPropertyValue spgPropertyValue = new SPGPropertyValue(conceptName.getName());
|
||||||
this.properties.add(new SPGPropertyRecord(property, spgPropertyValue));
|
this.properties.add(new SPGPropertyRecord(property, spgPropertyValue));
|
||||||
|
|||||||
@ -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.antgroup.openspg.core.schema.model.identifier.ConceptIdentifier;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strategy for translation from alter item of {@link ConceptRecord} to {@link LPGRecordAlterItem}s.
|
* 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) {
|
private boolean isRootConcept(ConceptRecord conceptRecord) {
|
||||||
ConceptIdentifier name = conceptRecord.getConceptName();
|
ConceptIdentifier name = conceptRecord.getConceptName();
|
||||||
return StringUtils.isBlank(name.getFatherId());
|
return name.isRootConcept();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,6 @@
|
|||||||
~ is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
~ is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||||
~ or implied.
|
~ or implied.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@ -24,6 +24,8 @@ public class ConceptIdentifier extends BaseSPGIdentifier {
|
|||||||
/** The seperator symbol between parent and child concept names. */
|
/** The seperator symbol between parent and child concept names. */
|
||||||
public static final String SEPARATOR = "-";
|
public static final String SEPARATOR = "-";
|
||||||
|
|
||||||
|
private static final String ROOT = "__ROOT__";
|
||||||
|
|
||||||
/** Unique id of concept node. */
|
/** Unique id of concept node. */
|
||||||
private final String id;
|
private final String id;
|
||||||
|
|
||||||
@ -51,8 +53,11 @@ public class ConceptIdentifier extends BaseSPGIdentifier {
|
|||||||
|
|
||||||
public String getFatherId() {
|
public String getFatherId() {
|
||||||
String[] splits = id.split(SEPARATOR);
|
String[] splits = id.split(SEPARATOR);
|
||||||
if (splits.length <= 1) {
|
if (ROOT.equals(id)) {
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
if (splits.length <= 1) {
|
||||||
|
return ROOT;
|
||||||
} else {
|
} else {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
for (int i = 0; i < splits.length - 2; i++) {
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return id;
|
return id;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user