Skip to content

Commit e42b636

Browse files
committed
update for properties import
1 parent 77e20e5 commit e42b636

1 file changed

Lines changed: 162 additions & 40 deletions

File tree

import-generic/scripts/json-importer.js

Lines changed: 162 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -461,28 +461,44 @@ const getClassId = iri => {
461461
* @param {*} data_cnt data triple count obtained
462462
* @returns fixed counts as { cnt, object_cnt, data_cnt }
463463
*/
464-
function fix_cnt_values(cnt, object_cnt, data_cnt) {
465-
if (cnt) {
466-
if (object_cnt && object_cnt > cnt) {
467-
object_cnt = cnt
468-
}
469-
if (data_cnt && data_cnt > cnt) {
470-
data_cnt = cnt
471-
}
472-
if (!object_cnt && !data_cnt) {
473-
object_cnt = cnt;
474-
data_cnt = 0;
475-
} else if (object_cnt) {
476-
data_cnt = cnt - object_cnt;
477-
} else if (data_cnt) {
478-
object_cnt = cnt - data_cnt;
479-
}
464+
function fix_cnt_values(p_cnt, p_object_cnt, p_data_cnt, maxTripleCountRounded) {
465+
let cnt = p_cnt;
466+
let object_cnt = p_object_cnt;
467+
let data_cnt = p_data_cnt;
468+
let pData;
469+
470+
if (!cnt && maxTripleCountRounded) {
471+
cnt = maxTripleCountRounded;
472+
pData = { triple_count_inserted: true }
473+
}
474+
475+
if (object_cnt && object_cnt > cnt) {
476+
if (!pData) pData = {}
477+
pData.object_cnt_orig = object_cnt
478+
object_cnt = cnt
479+
}
480+
if (data_cnt && data_cnt > cnt) {
481+
if (!pData) pData = {}
482+
pData.data_cnt_orig = data_cnt
483+
data_cnt = cnt
480484
}
481-
return { cnt, object_cnt, data_cnt }
485+
486+
if (!object_cnt && !data_cnt) {
487+
if (!pData) pData = {}
488+
pData.object_cnt_assumed = true
489+
object_cnt = cnt;
490+
data_cnt = 0;
491+
} else if (object_cnt) {
492+
data_cnt = cnt - object_cnt;
493+
} else if (data_cnt) {
494+
object_cnt = cnt - data_cnt;
495+
}
496+
497+
return { cnt, object_cnt, data_cnt, pData }
482498
}
483499

484500
const PROPS = new Map(); // iri -> property_id
485-
const addProperty = async p => {
501+
const addProperty = async (p, { maxTripleCountRounded }) => {
486502
// ?localName: "julPrecipitationDays"
487503
// ?namespace: "http://dbpedia.org/property/"
488504
// fullName: "http://dbpedia.org/property/julPrecipitationDays"
@@ -493,6 +509,11 @@ const addProperty = async p => {
493509
// objectTripleCount: 0 -> object_cnt
494510
// closedDomain: true
495511
// closedRange: true
512+
513+
// hasFollowersOK: 5
514+
// hasIncomingPropertiesOK: 5
515+
// hasOutgoingPropertiesOK: 5
516+
496517
// SourceClasses[]:
497518
// ...
498519
// TargetClasses[]:
@@ -502,48 +523,140 @@ const addProperty = async p => {
502523

503524
let ns_id = await resolveNsPrefix(p.namespace);
504525

505-
let domain_class_id = null;
506-
if (p.closedDomain && p.SourceClasses) {
507-
const candidates = p.SourceClasses.filter(x => x.importanceIndex > 0);
508-
if (candidates.length === 1) {
509-
domain_class_id = getClassId(candidates[0].classFullName);
510-
}
511-
}
512-
let range_class_id = null;
513-
if (p.closedRange && p.dataTripleCount === 0 && p.TargetClasses) {
514-
const candidates = p.TargetClasses.filter(x => x.importanceIndex > 0);
515-
if (candidates.length === 1) {
516-
range_class_id = getClassId(candidates[0].classFullName);
517-
}
518-
}
519-
520526
let property_id;
527+
521528
try {
522-
let { cnt, object_cnt, data_cnt } = fix_cnt_values(
529+
let { cnt, object_cnt, data_cnt, pData } = fix_cnt_values(
523530
p.tripleCount,
524531
p.objectTripleCount,
525-
p.dataTripleCount
532+
p.dataTripleCount,
533+
maxTripleCountRounded,
526534
);
527535

536+
let domain_class_id = null;
537+
// if (p.closedDomain && p.SourceClasses) {
538+
// const candidates = p.SourceClasses.filter(x => x.importanceIndex > 0);
539+
// if (candidates.length === 1) {
540+
// domain_class_id = getClassId(candidates[0].classFullName);
541+
// }
542+
// }
543+
if (p.SourceClasses) { //MMM vai te nevajag veco nosacījumu?
544+
const candidates = p.SourceClasses.filter(x => x.isPrincipal === true);
545+
if (candidates.length === 1) {
546+
domain_class_id = getClassId(candidates[0].classFullName);
547+
if (candidates[0].principalAssertedSize) {
548+
if (!pData) pData = {}
549+
pData.domain_asserted_size = candidates[0].principalAssertedSize
550+
pData.domain_is_indirect = true
551+
}
552+
}
553+
}
554+
let range_class_id = null;
555+
// if (p.closedRange && p.dataTripleCount === 0 && p.TargetClasses) {
556+
// const candidates = p.TargetClasses.filter(x => x.importanceIndex > 0);
557+
// if (candidates.length === 1) {
558+
// range_class_id = getClassId(candidates[0].classFullName);
559+
// }
560+
// }
561+
if (p.TargetClasses) { //MMM vai te nevajag veco nosacījumu?
562+
const candidates = p.TargetClasses.filter(x => x.isPrincipal === true);
563+
if (candidates.length === 1) {
564+
range_class_id = getClassId(candidates[0].classFullName);
565+
if (candidates[0].principalAssertedSize) {
566+
if (!pData) pData = {}
567+
pData.range_asserted_size = candidates[0].principalAssertedSize
568+
pData.range_is_indirect = true
569+
}
570+
}
571+
}
572+
573+
let has_followers_ok = false
574+
let has_incoming_props_ok = false
575+
let has_outgoing_props_ok = false
576+
if (p.hasFollowersOK) {
577+
if (!pData) pData = {}
578+
pData.has_followers_ok = p.hasFollowersOK
579+
has_followers_ok = true
580+
}
581+
if (p.hasIncomingPropertiesOK) {
582+
if (!pData) pData = {}
583+
pData.has_common_objects_ok = p.hasIncomingPropertiesOK
584+
has_incoming_props_ok = true
585+
}
586+
if (p.hasOutgoingPropertiesOK) {
587+
if (!pData) pData = {}
588+
pData.has_common_subjects_ok = p.hasOutgoingPropertiesOK
589+
has_outgoing_props_ok = true
590+
}
591+
592+
if (p.sourceClassesOK) {
593+
if (!pData) pData = {}
594+
pData.source_classes_ok = p.sourceClassesOK
595+
}
596+
if (p.targetClassesOK) {
597+
if (!pData) pData = {}
598+
pData.target_classes_ok = p.targetClassesOK
599+
}
600+
601+
let source_cover_complete = p.closedDomain || false
602+
if (p.closedSourceAssertedSize) {
603+
source_cover_complete = true
604+
if (!pData) pData = {}
605+
pData.closed_source_asserted_size = p.closedSourceAssertedSize
606+
pData.closed_source_is_indirect = true
607+
}
608+
609+
let max_cardinality = p.maxCardinality
610+
if (p.maxCardinality1AssertionSize) {
611+
max_cardinality = 1
612+
if (!pData) pData = {}
613+
pData.max_cardinality_1_asserted_size = p.maxCardinality1AssertionSize
614+
pData.max_cardinality_1_is_indirect = true
615+
}
616+
let inverse_max_cardinality = p.maxInverseCardinality
617+
if (p.maxInverseCardinality1AssertionSize) {
618+
inverse_max_cardinality = 1
619+
if (!pData) pData = {}
620+
pData.inverse_max_cardinality_1_asserted_size = p.maxInverseCardinality1AssertionSize
621+
pData.inverse_max_cardinality_1_is_indirect = true
622+
}
623+
624+
let target_cover_complete = p.closedRange || false
625+
if (p.closedTargetAssertedSize) {
626+
target_cover_complete = true
627+
if (!pData) pData = {}
628+
pData.closed_target_asserted_size = p.closedTargetAssertedSize
629+
pData.closed_target_is_indirect = true
630+
}
631+
528632
property_id = (await db.one(`INSERT INTO ${dbSchema}.properties
529633
(iri, ns_id, local_name, display_name,
530634
cnt, object_cnt, data_cnt,
531635
max_cardinality, inverse_max_cardinality,
532636
source_cover_complete, target_cover_complete,
533-
domain_class_id, range_class_id)
637+
domain_class_id, range_class_id,
638+
data,
639+
has_followers_ok, has_outgoing_props_ok, has_incoming_props_ok,
640+
distinct_subjects, distinct_objects, distinct_triples, blank_node_subjects, blank_node_objects)
534641
VALUES ($1, $2, $3, $3,
535642
$4, $5, $6,
536643
$7, $8,
537644
$9, $10,
538-
$11, $12)
645+
$11, $12,
646+
$13,
647+
$14, $15, $16,
648+
$17, $18, $19, $20, $21)
539649
RETURNING id`,
540650
[
541651
p.fullName, ns_id, p.localName,
542652
// p.tripleCount, p.objectTripleCount, p.dataTripleCount,
543653
cnt, object_cnt, data_cnt,
544-
p.maxCardinality, p.maxInverseCardinality,
545-
p.closedDomain || false, p.closedRange || false,
654+
max_cardinality, inverse_max_cardinality,
655+
source_cover_complete, target_cover_complete,
546656
domain_class_id, range_class_id,
657+
pData,
658+
has_followers_ok, has_outgoing_props_ok, has_incoming_props_ok,
659+
p.distinctSubjectsCount, p.distinctObjectsCount, p.distinctTriples, p.blankNodeSubjects, p.blankNodeObjects,
547660
])).id;
548661
PROPS.set(p.fullName, property_id);
549662

@@ -1334,8 +1447,17 @@ const importFromJSON = async data => {
13341447
// properties
13351448
if (data.Properties) {
13361449
let propsBar = new ProgressBar(`[:bar] ( :current props of :total, :percent)`, { total: data.Properties.length, width: 100, incomplete: '.' });
1450+
1451+
// calculate max of tripleCount
1452+
let maxTripleCount = 0
1453+
for (const p of data.Properties) {
1454+
if (p.tripleCount) maxTripleCount = Math.max(maxTripleCount, p.tripleCount)
1455+
}
1456+
const maxTripleCountRounded = roundUpToSingleDigitPower(maxTripleCount)
1457+
// end calculate max of tripleCount
1458+
13371459
for (const p of data.Properties) {
1338-
await addProperty(p);
1460+
await addProperty(p, { maxTripleCountRounded });
13391461
await addPropertyLabels(p);
13401462
propsBar.tick();
13411463
}

0 commit comments

Comments
 (0)