Repository navigation
Expand file tree
/
Copy pathgenerator.pl
More file actions
1733 lines (1518 loc) · 73.4 KB
/
Copy pathgenerator.pl
File metadata and controls
1733 lines (1518 loc) · 73.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=============================================================================
Diesel Generator Expert Configurator
SWI-Prolog — Single-file version
ARCHITECTURE — four strictly isolated layers
=============================================
┌──────────────────────────────────────────────────────────────┐
│ LAYER 4 — PRESENTATION │
│ print_bom_tree/2, print_service_tree/2, help_menu/0 │
│ Receives opaque Config and bom_node/4 terms. │
│ Calls ONLY Layer 2 interfaces. Knows no parameter names, │
│ no component names, no ontology predicates. │
├──────────────────────────────────────────────────────────────┤
│ LAYER 3 — QUERY / CLI │
│ list_*, count_*, print_config_summary/2 │
│ Calls ONLY Layer 2 interfaces. Formats results for display. │
├──────────────────────────────────────────────────────────────┤
│ LAYER 2 — REASONING │
│ product_variant/2, config_bom/3, config_price/3 │
│ service_variant/4, service_scope_price/3 │
│ Pure logic. Calls ONLY Layer 1 predicates. │
│ Single access point to Config values: config_get/3. │
├──────────────────────────────────────────────────────────────┤
│ LAYER 1 — ONTOLOGY │
│ Facts only. No procedural logic. │
│ product/1 product_parameters/2 parameter_values/2 │
│ compatibility_rule/2 compatibility_holds/2 │
│ product_structure/2 component/3 │
│ service_parameter_values/3 service_item/3 │
└──────────────────────────────────────────────────────────────┘
CONFIGURATION REPRESENTATION
============================
A product configuration is an opaque pair-list:
Config = [apparent_power-kva_400, voltage-v_400, frequency-hz_50,
emission-eu_stage_5, certification-ce]
THE ONLY WAY to read a value from Config outside Layer 1 is:
config_get(+Config, +ParameterName, -Value)
This means:
- The reasoner never mentions 'emission', 'voltage', etc. by name.
- The printer never mentions parameter names.
- Adding/removing a parameter touches ONLY Layer 1 facts.
- To swap pair-list for library(assoc) when parameters grow past ~15:
change config_get/3 only. Zero other changes required.
EXTENDING THE SYSTEM
====================
Add a parameter:
1. Append atom to product_parameters/2 list [Layer 1]
2. Add parameter_values/2 fact [Layer 1]
3. Add compatibility_rule/2 + compatibility_holds/2 [Layer 1, if needed]
→ Zero changes in Layers 2, 3, 4.
Add a component:
1. Add comp{} entry to product_structure/2 [Layer 1]
2. Add component/3 fact(s) [Layer 1]
→ Zero changes in Layers 2, 3, 4.
Add a service item:
1. Add service_parameter_values/3 facts [Layer 1]
2. Add service_item/3 fact(s) [Layer 1]
→ Zero changes in Layers 2, 3, 4.
CLI usage:
swipl generator.pl
?- help_menu.
?- list_variants_with_count(diesel_generator).
?- print_bom_tree(diesel_generator,
[apparent_power-kva_400, voltage-v_400, frequency-hz_50,
emission-eu_stage_5, certification-ce]).
?- print_service_tree(
[genset_service-standard, scr_service-extended,
commissioning-full, warranty-warranty_5yr], eu_stage_5).
?- validate_ontology.
chcp 65001 — Windows can't display the ASCII diagrams properly.
Run that in your terminal before anything else.
It switches the Windows console to UTF-8. Then rebuild.
=============================================================================*/
:- use_module(library(lists)).
:- use_module(library(apply)).
:- use_module(library(pairs)).
:- use_module(library(ansi_term)).
:- set_prolog_flag(color_term, true).
:- use_module(generator_graphics).
/*=============================================================================
LAYER 1 — ONTOLOGY
Facts only. The only calls allowed here are to other Layer 1 facts,
specifically in compatibility_holds/2 which calls the compatible_vf/2
and compatible_ec/2 facts, and config_get/3. No iteration, no findall.
=============================================================================*/
/*-----------------------------------------------------------------------------
1.1 Product catalogue
-----------------------------------------------------------------------------*/
product(diesel_generator).
% product_parameters(+Product, -Params)
% Ordered list — determines the order of pairs in every Config list.
product_parameters(diesel_generator, [
placement,
application_type,
apparent_power,
lv_connection,
voltage,
frequency,
emission,
routine_test,
certification,
daytank_init_fill,
def_daytank_init_fill
]).
/*-----------------------------------------------------------------------------
1.2 Parameter domains
To add a value: extend the list. Nothing else changes anywhere.
-----------------------------------------------------------------------------*/
parameter_values(placement, [indoor, outdoor]).
parameter_values(application_type, [industrial, marine]).
parameter_values(apparent_power, [kva_250, kva_400, kva_560]).
parameter_values(lv_connection, [single, double]).
parameter_values(voltage, [v_400, v_480]).
parameter_values(frequency, [hz_50, hz_60]).
parameter_values(emission, [eu_stage_5, eu_stage_3, epa_tier_4, imo_tier_3]).
parameter_values(routine_test, [grid, load_bank]).
parameter_values(certification, [dnv, ce, ul, none]).
parameter_values(daytank_init_fill, [empty, full]).
parameter_values(def_daytank_init_fill, [empty, full]).
/*-----------------------------------------------------------------------------
1.3 Compatibility rules
Two-part declaration keeps domain knowledge in Layer 1:
Part A: compatibility_rule(Product, RuleName)
Registers which rules apply to a product.
Add a new rule: add a fact here + a compatibility_holds/2 clause.
Part B: compatibility_holds(RuleName, Config)
What "satisfied" means for each named rule.
Uses config_get/3 — the ONLY sanctioned way to read Config.
This is Layer 1 because it encodes domain knowledge (which values
may coexist), not algorithmic iteration.
Raw compatible_vf/2 and compatible_ec/2 facts are private to this section.
-----------------------------------------------------------------------------*/
compatibility_rule(diesel_generator, voltage_frequency).
compatibility_rule(diesel_generator, emission_certification).
compatibility_rule(diesel_generator, emission_application_type).
compatibility_holds(voltage_frequency, Config) :-
config_get(Config, voltage, V),
config_get(Config, frequency, F),
compatible_voltage_frequency(V, F).
compatibility_holds(emission_certification, Config) :-
config_get(Config, emission, E),
config_get(Config, certification, C),
compatible_emission_certification(E, C).
compatibility_holds(emission_application_type, Config) :-
config_get(Config, emission, E),
config_get(Config, application_type, AT),
compatible_emission_application_type(E, AT).
% 480 V is only valid at 60 Hz; 400 V allows both.
compatible_voltage_frequency(v_400, hz_50).
compatible_voltage_frequency(v_400, hz_60).
compatible_voltage_frequency(v_480, hz_60).
compatible_emission_certification(eu_stage_5, dnv).
compatible_emission_certification(eu_stage_5, ce).
compatible_emission_certification(eu_stage_5, none).
compatible_emission_certification(eu_stage_3, dnv).
compatible_emission_certification(eu_stage_3, ce).
compatible_emission_certification(eu_stage_3, none).
compatible_emission_certification(epa_tier_4, ul).
compatible_emission_certification(epa_tier_4, none).
compatible_emission_certification(imo_tier_3, dnv).
compatible_emission_certification(imo_tier_3, ce).
compatible_emission_certification(imo_tier_3, none).
compatible_emission_certification(imo_tier_3, ul).
compatible_emission_application_type(eu_stage_5, industrial).
compatible_emission_application_type(eu_stage_3, industrial).
compatible_emission_application_type(epa_tier_4, industrial).
compatible_emission_application_type(imo_tier_3, marine).
/*-----------------------------------------------------------------------------
1.4 Product structure
comp{} dict fields:
name — atom; must match a component/3 entry
mandatory — true | false
requires — none
param(ParameterName, [AllowedValues])
include this component only when the named parameter's
value is in the allowed list
To extend: add a comp{} entry here + component/3 facts below.
-----------------------------------------------------------------------------*/
product_structure(diesel_generator, [
comp{name: genset, mandatory: true, requires: none},
comp{name: electrical_cabinet, mandatory: true, requires: none},
comp{name: daytank, mandatory: true, requires: none},
comp{name: battery, mandatory: true, requires: none},
comp{name: battery_charger, mandatory: true, requires: none},
comp{name: air_intake, mandatory: true, requires: none},
comp{name: air_outlet, mandatory: true, requires: none},
comp{name: inner_exhaust_piping, mandatory: true, requires: none},
comp{name: outer_exhaust_piping, mandatory: true, requires: none},
comp{name: muffler, mandatory: true, requires: none},
comp{name: scr_unit, mandatory: false,
requires: param(emission, [eu_stage_5, epa_tier_4, imo_tier_3])},
comp{name: def_main_tank, mandatory: false,
requires: param(emission, [eu_stage_5, epa_tier_4, imo_tier_3])},
comp{name: def_day_tank, mandatory: false,
requires: param(emission, [eu_stage_5, epa_tier_4, imo_tier_3])},
comp{name: def_pump, mandatory: false,
requires: param(emission, [eu_stage_5, epa_tier_4, imo_tier_3])},
comp{name: containerized_enclosure, mandatory: false, requires: none}
]).
/*-----------------------------------------------------------------------------
1.5 Component definitions
component(Product, ComponentName, component{...})
used_in field — a list of match specs, each one of:
any matches every config
v(Power,Freq) matches specific apparent_power + frequency values
EmissionAtom matches a specific emission parameter value
To add a variant: add a component/3 fact. Nothing else changes.
-----------------------------------------------------------------------------*/
% --- GENSET ------------------------------------------------------------------
component(diesel_generator, genset, component{
used_in: [match([apparent_power-kva_250, frequency-hz_50])],
type: rotating_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'TAD1-250-50',
rating_ISO_8528: 'PRP', cosphi: 0.8,
active_power: kw_200, rotating_speed: rpm_1500,
price_dkk: 400000, service_interval_hours: 500,
sub_components: [engine, alternator, base_frame, speed_governor,
avr, excitation_system, cooling_system, lubrication_system]
}).
component(diesel_generator, genset, component{
used_in: [match([apparent_power-kva_250, frequency-hz_60])],
type: rotating_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'TAD1-250-60',
rating_ISO_8528: 'PRP', cosphi: 0.8,
active_power: kw_200,rotating_speed: rpm_1800,
price_dkk: 420000, service_interval_hours: 500,
sub_components: [engine, alternator, base_frame, speed_governor,
avr, excitation_system, cooling_system, lubrication_system]
}).
component(diesel_generator, genset, component{
used_in: [match([apparent_power-kva_400, frequency-hz_50])],
type: rotating_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'TAD2-400-50',
rating_ISO_8528: 'PRP', cosphi: 0.8,
active_power: kw_320, rotating_speed: rpm_1500,
price_dkk: 550000, service_interval_hours: 500,
sub_components: [engine, alternator, base_frame, speed_governor,
avr, excitation_system, cooling_system, lubrication_system]
}).
component(diesel_generator, genset, component{
used_in: [match([apparent_power-kva_400, frequency-hz_60])],
type: rotating_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'TAD2-400-60',
rating_ISO_8528: 'PRP', cosphi: 0.8,
active_power: kw_320, rotating_speed: rpm_1800,
price_dkk: 550000, service_interval_hours: 500,
sub_components: [engine, alternator, base_frame, speed_governor,
avr, excitation_system, cooling_system, lubrication_system]
}).
component(diesel_generator, genset, component{
used_in: [match([apparent_power-kva_560, frequency-hz_50])],
type: rotating_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'TAD2-560-50',
rating_ISO_8528: 'PRP', cosphi: 0.8,
active_power: kw_448, rotating_speed: rpm_1500,
price_dkk: 600000, service_interval_hours: 500,
sub_components: [engine, alternator, base_frame, speed_governor,
avr, excitation_system, cooling_system, lubrication_system]
}).
component(diesel_generator, genset, component{
used_in: [match([apparent_power-kva_560, frequency-hz_60])],
type: rotating_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'TAD2-560-60',
rating_ISO_8528: 'PRP', cosphi: 0.8,
active_power: kw_448, rotating_speed: rpm_1800,
price_dkk: 600000, service_interval_hours: 500,
sub_components: [engine, alternator, base_frame, speed_governor,
avr, excitation_system, cooling_system, lubrication_system]
}).
% --- ELECTRICAL CABINET ------------------------------------------------------
component(diesel_generator, electrical_cabinet, component{
used_in: [any], type: cabinet,
quantity: 1, replaceable: true, vendor: 'HPS', article: 'HPA-01',
price_dkk: 320000, service_interval_hours: 10000,
sub_components: [plc, circuit_breakers, bus_bars,
control_panel, relay_protection]
}).
% --- DAYTANK -----------------------------------------------------------------
component(diesel_generator, daytank, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'ABZ', article: 'DT-500',
autonomy_hours: 18,
price_dkk: 45000, service_interval_hours: 8000,
sub_components: [fuel_level_sensor, overflow_valve, fill_pump]
}).
% --- BATTERY (quantity = 2) --------------------------------------------------
component(diesel_generator, battery, component{
used_in: [any], type: minor,
quantity: 2, replaceable: true, vendor: 'Varta', article: 'VARTA-AGM-12V',
price_dkk: 4500, service_interval_hours: 8000
}).
% --- BATTERY CHARGER (quantity = 2) ------------------------------------------
component(diesel_generator, battery_charger, component{
used_in: [any], type: minor,
quantity: 2, replaceable: true, vendor: 'Victron', article: 'VICTRON-BC-12V',
price_dkk: 3500, service_interval_hours: 8000
}).
% --- AIR HANDLING ------------------------------------------------------------
component(diesel_generator, air_intake, component{
used_in: [any], type: air_flow,
quantity: 1, replaceable: true, vendor: 'Lowara', article: 'LOWARA-AIR-INTAKE',
price_dkk: 45000, service_interval_hours: 8000,
sub_components: [intake_filter, intake_damper, intake_louvre]
}).
component(diesel_generator, air_outlet, component{
used_in: [any], type: air_flow,
quantity: 1, replaceable: true, vendor: 'Lowara', article: 'LOWARA-AIR-OUTLET',
price_dkk: 45000, service_interval_hours: 8000,
sub_components: [outlet_damper, outlet_louvre]
}).
% --- EXHAUST -----------------------------------------------------------------
component(diesel_generator, inner_exhaust_piping, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Burgess', article: 'BMI-440',
price_dkk: 8000, service_interval_hours: 10000
}).
component(diesel_generator, outer_exhaust_piping, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Burgess', article: 'BME-440',
price_dkk: 8000, service_interval_hours: 10000
}).
component(diesel_generator, muffler, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Burgess', article: 'BM-440',
price_dkk: 8000, service_interval_hours: 10000
}).
% --- DEF SYSTEM --------------------------------------------------------------
component(diesel_generator, scr_unit, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Dinex', article: 'SCR-220',
price_dkk: 45000, service_interval_hours: 8000,
sub_components: [catalyst, temperature_sensor, nox_sensor]
}).
component(diesel_generator, def_main_tank, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Dinex', article: 'DEF-220',
price_dkk: 45000, service_interval_hours: 8000,
sub_components: [level_sensor, heating_element]
}).
component(diesel_generator, def_day_tank, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Dinex', article: 'DEF-230',
price_dkk: 12000, service_interval_hours: 8000
}).
component(diesel_generator, def_pump, component{
used_in: [any], type: static_equipment,
quantity: 1, replaceable: true, vendor: 'Dinex', article: 'DEF-240',
price_dkk: 18000, service_interval_hours: 8000,
sub_components: [pump_motor, dosing_valve, strainer]
}).
% --- CONTAINERIZED ENCLOSURE -------------------------------------------------
component(diesel_generator, containerized_enclosure, component{
used_in: [match([emission-eu_stage_5]),
match([emission-epa_tier_4]),
match([emission-imo_tier_3])],
type: structural,
quantity: 1, replaceable: false, vendor: 'ABZ', article: 'ENCL-01',
price_dkk: 2800000, service_interval_hours: 30000,
sub_components: [ventilation_inlet, ventilation_outlet,
cable_penetration, access_door]
}).
component(diesel_generator, containerized_enclosure, component{
used_in: [match([emission-eu_stage_3])],
type: structural,
quantity: 1, replaceable: false, vendor: 'ABZ', article: 'ENCL-02',
price_dkk: 2200000, service_interval_hours: 30000,
sub_components: [ventilation_inlet, ventilation_outlet,
cable_penetration, access_door]
}).
/*-----------------------------------------------------------------------------
1.6 Service parameter domains
service_parameter_values(ItemName, variant, [Keys])
Closed set of variant/tier keys for each service item.
service_parameter_values(emission, [Values])
Emission contexts recognised in the service domain.
Mirrors the product emission domain.
-----------------------------------------------------------------------------*/
service_parameter_values(emission, [eu_stage_5, eu_stage_3, epa_tier_4, any]).
service_parameter_values(genset_service, variant, [standard, extended, premium]).
service_parameter_values(cabinet_service, variant, [standard, extended]).
service_parameter_values(daytank_service, variant, [standard]).
service_parameter_values(battery_service, variant, [standard]).
service_parameter_values(exhaust_service, variant, [standard]).
service_parameter_values(scr_service, variant, [standard, extended]).
service_parameter_values(def_service, variant, [standard]).
service_parameter_values(commissioning, variant, [basic, full]).
service_parameter_values(warranty, variant, [warranty_2yr, warranty_5yr]).
service_parameter_values(remote_monitoring, variant, [basic, advanced]).
/*-----------------------------------------------------------------------------
1.7 Service item definitions
service_item(ItemName, VariantKey, service{...})
Dict fields:
description — human-readable label
applies_to — [any] | [EmissionAtom, ...]
price_dkk — fixed price
interval — hours (0 = one-time)
sub_items — [atom, ...] task list shown in tree
-----------------------------------------------------------------------------*/
service_item(genset_service, standard, service{
description: 'Genset standard service (500 h)',
applies_to: [any], price_dkk: 18000, interval: 500,
sub_items: [oil_change, oil_filter, fuel_filter,
air_filter, belt_inspection, coolant_check]
}).
service_item(genset_service, extended, service{
description: 'Genset extended service (incl. injector test)',
applies_to: [any], price_dkk: 28000, interval: 500,
sub_items: [oil_change, oil_filter, fuel_filter, air_filter,
belt_inspection, coolant_check,
injector_test, compression_test]
}).
service_item(genset_service, premium, service{
description: 'Genset premium service (full overhaul)',
applies_to: [any], price_dkk: 48000, interval: 1000,
sub_items: [oil_change, oil_filter, fuel_filter, air_filter,
belt_replacement, coolant_flush,
injector_test, compression_test,
thermostat_replacement, hose_inspection]
}).
service_item(cabinet_service, standard, service{
description: 'Cabinet standard service',
applies_to: [any], price_dkk: 8000, interval: 8000,
sub_items: [thermal_scan, relay_test, busbar_torque_check]
}).
service_item(cabinet_service, extended, service{
description: 'Cabinet extended service (incl. PLC firmware)',
applies_to: [any], price_dkk: 14000, interval: 8000,
sub_items: [thermal_scan, relay_test, busbar_torque_check,
plc_firmware_update, insulation_resistance_test]
}).
service_item(daytank_service, standard, service{
description: 'Day-tank standard service',
applies_to: [any], price_dkk: 4500, interval: 8000,
sub_items: [level_sensor_check, valve_inspection, tank_cleaning]
}).
service_item(battery_service, standard, service{
description: 'Battery & charger service',
applies_to: [any], price_dkk: 3500, interval: 8000,
sub_items: [battery_load_test, terminal_cleaning, charger_output_check]
}).
service_item(exhaust_service, standard, service{
description: 'Exhaust system inspection',
applies_to: [any], price_dkk: 5000, interval: 8000,
sub_items: [flex_joint_check, muffler_inspection, piping_leak_test]
}).
service_item(scr_service, standard, service{
description: 'SCR standard service',
applies_to: [eu_stage_5, epa_tier_4], price_dkk: 12000, interval: 4000,
sub_items: [catalyst_pressure_drop, nox_sensor_calibration,
temp_sensor_check, dosing_valve_test]
}).
service_item(scr_service, extended, service{
description: 'SCR extended service (catalyst replacement)',
applies_to: [eu_stage_5, epa_tier_4], price_dkk: 38000, interval: 8000,
sub_items: [catalyst_replacement, nox_sensor_replacement,
temp_sensor_check, dosing_valve_test, system_regeneration]
}).
service_item(def_service, standard, service{
description: 'DEF system service',
applies_to: [eu_stage_5, epa_tier_4], price_dkk: 6500, interval: 4000,
sub_items: [def_pump_inspection, strainer_cleaning,
level_sensor_check, heating_element_check]
}).
service_item(commissioning, basic, service{
description: 'Basic commissioning (functional test)',
applies_to: [any], price_dkk: 22000, interval: 0,
sub_items: [visual_inspection, load_bank_test_50pct,
protection_relay_test, handover_documentation]
}).
service_item(commissioning, full, service{
description: 'Full commissioning (incl. emission & noise tests)',
applies_to: [eu_stage_5, eu_stage_3, epa_tier_4],
price_dkk: 45000, interval: 0,
sub_items: [visual_inspection, load_bank_test_100pct,
protection_relay_test, emission_measurement,
noise_measurement, handover_documentation, operator_training]
}).
service_item(warranty, warranty_2yr, service{
description: '2-year extended warranty',
applies_to: [any], price_dkk: 35000, interval: 0,
sub_items: [parts_coverage, labour_coverage]
}).
service_item(warranty, warranty_5yr, service{
description: '5-year extended warranty',
applies_to: [any], price_dkk: 75000, interval: 0,
sub_items: [parts_coverage, labour_coverage, annual_preventive_visit]
}).
service_item(remote_monitoring, basic, service{
description: 'Remote monitoring — basic (alarms only)',
applies_to: [any], price_dkk: 12000, interval: 0,
sub_items: [sim_card_subscription, alarm_forwarding, portal_access]
}).
service_item(remote_monitoring, advanced, service{
description: 'Remote monitoring — advanced (full telemetry)',
applies_to: [any], price_dkk: 24000, interval: 0,
sub_items: [sim_card_subscription, alarm_forwarding, portal_access,
trend_logging, predictive_maintenance_alerts, annual_report]
}).
/*=============================================================================
LAYER 2 — REASONING
Pure logic. Calls ONLY Layer 1 predicates.
No format/write. No direct calls to component/3, parameter_values/2,
or product_structure/2 from Layers 3 or 4.
=============================================================================*/
/*-----------------------------------------------------------------------------
2.0 Config access interface — THE SINGLE SEAM
config_get(+Config, +Key, -Value)
This is the ONLY way to read a value from a Config anywhere in the system.
Config is currently a pair-list: [param1-val1, param2-val2, ...]
To upgrade to library(assoc) for 20+ parameters:
Replace the body of this predicate with get_assoc(Key, Config, Value).
Also update pick_value/2 to build an assoc instead of a pair.
Zero other changes required anywhere.
-----------------------------------------------------------------------------*/
config_get(Config, Key, Value) :-
member(Key-Value, Config).
/*-----------------------------------------------------------------------------
2.1 Variant generator
product_variant(+Product, -Config)
Generates all valid configurations for Product via backtracking.
Config = [Param1-Value1, Param2-Value2, ...] (product_parameters order).
No parameter names appear in this predicate body.
It is driven entirely by product_parameters/2, parameter_values/2,
and compatibility_rule/2 + compatibility_holds/2 from Layer 1.
-----------------------------------------------------------------------------*/
product_variant(Product, Config) :-
product_parameters(Product, Params),
maplist(pick_value, Params, Config),
forall(
compatibility_rule(Product, Rule),
compatibility_holds(Rule, Config)
).
% pick_value/2 — assigns one value to a parameter (non-deterministic)
pick_value(Param, Param-Value) :-
parameter_values(Param, Values),
member(Value, Values).
/*-----------------------------------------------------------------------------
2.2 BoM resolver
PUBLIC interface (called by Layers 3 & 4):
config_bom(+Product, +Config, -BoMNodes)
BoMNodes = list of bom_node(Name, Article, Qty, Subs)
Layers 3 & 4 never touch component/3 or product_structure/2 directly.
PRIVATE helpers (Layer 2 internal):
active_comps/3 — filter product_structure to active comp{} entries
comp_active/2 — single comp{} activation check (generic, param-blind)
resolve_comp/4 — find the matching component dict for this Config
used_in_matches/2 — match one used_in spec atom/term against Config
-----------------------------------------------------------------------------*/
config_bom(Product, Config, BoMNodes) :-
product_structure(Product, Structure),
active_comps(Structure, Config, ActiveComps),
maplist(comp_to_node(Product, Config), ActiveComps, BoMNodes).
% active_comps/3 — keep only entries whose activation condition holds
active_comps([], _, []).
active_comps([C|Rest], Config, [C|Out]) :-
comp_active(C, Config), !,
active_comps(Rest, Config, Out).
active_comps([_|Rest], Config, Out) :-
active_comps(Rest, Config, Out).
% comp_active/2 — three cases; the param-based case is fully generic:
% it never names 'emission' — it reads whatever parameter is declared
% in the requires field of the comp{} dict.
comp_active(Comp, _) :-
Comp.mandatory = true.
comp_active(Comp, Config) :-
Comp.mandatory = false,
Comp.requires = param(Param, TriggerValues),
config_get(Config, Param, Value),
member(Value, TriggerValues).
comp_active(Comp, _) :-
Comp.mandatory = false,
Comp.requires = none.
% comp_to_node/4 — resolve dict and build public bom_node/4 term
comp_to_node(Product, Config, Comp,
bom_node(Name, Article, Qty, Subs)) :-
Name = Comp.name,
resolve_comp(Product, Name, Config, D),
Article = D.article,
Qty = D.quantity,
( get_dict(sub_components, D, Subs) -> true ; Subs = [] ).
% resolve_comp/4 — find the component dict whose used_in spec matches Config
resolve_comp(Product, Name, Config, D) :-
component(Product, Name, D),
member(Spec, D.used_in),
used_in_matches(Spec, Config).
% used_in_matches/2
used_in_matches(any, _).
used_in_matches(match(Pairs), Config) :-
forall(
member(Param-Value, Pairs),
config_get(Config, Param, Value)
).
/*-----------------------------------------------------------------------------
2.3 Pricing
config_price(+Product, +Config, -Total)
Sums price_dkk × quantity for all active BoM components.
Does NOT use bom_node/4 (which omits price) — resolves component
dicts directly for pricing, keeping the public BoM interface clean.
-----------------------------------------------------------------------------*/
config_price(Product, Config, Total) :-
product_structure(Product, Structure),
active_comps(Structure, Config, ActiveComps),
findall(LinePrice,
( member(Comp, ActiveComps),
resolve_comp(Product, Comp.name, Config, D),
LinePrice is D.price_dkk * D.quantity
),
Prices),
sumlist(Prices, Total).
/*-----------------------------------------------------------------------------
2.4 Price extremes and sorted list
cheapest_configs(+Product, -Configs, -MinPrice)
most_expensive_configs(+Product, -Configs, -MaxPrice)
all_configs_by_price(+Product, -SortedPairs)
SortedPairs = [Price-Config, ...] ascending
-----------------------------------------------------------------------------*/
cheapest_configs(Product, Configs, MinPrice) :-
findall(P-C,
(product_variant(Product, C), config_price(Product, C, P)),
Pairs),
min_member(MinPrice-_, Pairs),
findall(C, member(MinPrice-C, Pairs), Configs).
most_expensive_configs(Product, Configs, MaxPrice) :-
findall(P-C,
(product_variant(Product, C), config_price(Product, C, P)),
Pairs),
max_member(MaxPrice-_, Pairs),
findall(C, member(MaxPrice-C, Pairs), Configs).
all_configs_by_price(Product, Sorted) :-
findall(P-C,
(product_variant(Product, C), config_price(Product, C, P)),
Pairs),
msort(Pairs, Sorted).
/*-----------------------------------------------------------------------------
2.5 Service configurator — independent of product Layer 1
service_item_available(+Item, +VK, +Emission)
service_variant(+Item, +VK, +Emission, -D)
all_available_service_variants(+Emission, -Pairs)
service_cheapest_variant(+Item, +Emission, -VK, -Price)
service_most_expensive_variant(+Item, +Emission, -VK, -Price)
-----------------------------------------------------------------------------*/
service_item_available(Item, VK, _) :-
service_item(Item, VK, D), member(any, D.applies_to).
service_item_available(Item, VK, Emission) :-
service_item(Item, VK, D),
\+ member(any, D.applies_to),
member(Emission, D.applies_to).
service_variant(Item, VK, Emission, D) :-
service_item(Item, VK, D),
service_item_available(Item, VK, Emission).
all_available_service_variants(Emission, Pairs) :-
findall(Item-VK,
(service_item(Item, VK, _), service_item_available(Item, VK, Emission)),
Pairs).
service_cheapest_variant(Item, Emission, VK, Price) :-
findall(P-K,
( service_parameter_values(Item, variant, Vs), member(K, Vs),
service_variant(Item, K, Emission, D), P = D.price_dkk ),
Pairs),
Pairs \= [], min_member(Price-VK, Pairs).
service_most_expensive_variant(Item, Emission, VK, Price) :-
findall(P-K,
( service_parameter_values(Item, variant, Vs), member(K, Vs),
service_variant(Item, K, Emission, D), P = D.price_dkk ),
Pairs),
Pairs \= [], max_member(Price-VK, Pairs).
/*-----------------------------------------------------------------------------
2.6 Service pricing
service_scope_price(+Scope, +Emission, -Total)
Scope = [ItemName-VariantKey, ...]
Items unavailable for Emission are silently skipped.
-----------------------------------------------------------------------------*/
service_scope_price(Scope, Emission, Total) :-
findall(P,
(member(Item-VK, Scope), service_variant(Item, VK, Emission, D), P = D.price_dkk),
Prices),
sumlist(Prices, Total).
/*-----------------------------------------------------------------------------
2.7 Q&A query helpers
These are the only Layer 2 predicates that name a specific parameter by
atom — because they ARE the answer to "what depends on parameter X?"
That question is inherently parameter-specific.
components_requiring_param(+Param, +Value, -Names)
variants_for_param_value(+Product, +Param, +Value, -Configs)
-----------------------------------------------------------------------------*/
components_requiring_param(Param, Value, Names) :-
product(Product),
product_structure(Product, Structure),
findall(Name,
( member(Comp, Structure),
Comp.requires = param(Param, TriggerVals),
member(Value, TriggerVals),
Name = Comp.name
),
Names).
variants_for_param_value(Product, Param, Value, Configs) :-
findall(C,
(product_variant(Product, C), config_get(C, Param, Value)),
Configs).
/*=============================================================================
LAYER 3 — QUERY / CLI HELPERS
Calls ONLY Layer 2 interfaces (and product_parameters/2 / parameter_values/2
for the two catalogue-inspection helpers, which are display-only).
No component/3, no product_structure/2, no service_item/3 directly.
=============================================================================*/
/*-----------------------------------------------------------------------------
3.0 Parameters and variants — catalogue inspection
-----------------------------------------------------------------------------*/
% list_parameters/1 — catalogue inspection (Layer 1 exception: read-only display)
% CLI: list_parameters(diesel_generator).
list_parameters(Product) :-
product_parameters(Product, Params),
format("Parameters of ~w:~n", [Product]),
forall(member(P, Params), format(" ~w~n", [P])).
% list_values/1 — catalogue inspection
% CLI: list_values(emission).
list_values(Param) :-
parameter_values(Param, Values),
format("Values for ~w:~n", [Param]),
forall(member(V, Values), format(" ~w~n", [V])).
% list_variants/1
% CLI: list_variants(diesel_generator).
list_variants(Product) :-
product_variant(Product, Config),
format(" ~w~n", [Config]),
fail.
list_variants(_).
% count_variants/2
% CLI: count_variants(diesel_generator, N).
count_variants(Product, N) :-
findall(C, product_variant(Product, C), Cs),
length(Cs, N).
% list_variants_with_count/1
% CLI: list_variants_with_count(diesel_generator).
list_variants_with_count(Product) :-
findall(C, product_variant(Product, C), Configs),
length(Configs, Total),
forall(member(C, Configs), format(" ~w~n", [C])),
format("~nTotal variants: ~w~n", [Total]).
% print_configs_flat(+Product, +Filters)
% Filters = list of param-value pairs to match against
% CLI: print_configs_flat(diesel_generator,
% [apparent_power-kva_400, emission-eu_stage_5]).
print_configs_flat(Product, Filters) :-
findall(Config,
( product_variant(Product, Config),
forall(
member(Param-Value, Filters),
config_get(Config, Param, Value)
)
),
Configs),
length(Configs, N),
format("~nFound ~w configuration(s):~n~n", [N]),
forall(
member(Config, Configs),
( write(' '),
write_term(Config, [quoted(true), max_depth(0)]),
nl
)
).
% print_config/1
% Prints a single Config with each pair on its own line.
% CLI: print_config(Config).
print_config(Config) :-
format("Config = [~n", []),
print_config_pairs(Config).
print_config_pairs([Last]) :-
!,
write(' '),
write_term(Last, [quoted(true), max_depth(0)]),
nl,
format("].~n", []).
print_config_pairs([H|T]) :-
write(' '),
write_term(H, [quoted(true), max_depth(0)]),
write(','),
nl,
print_config_pairs(T).
% print_all_configs(+Product, +Filters)
% Same filter convention, verbose one-per-line output.
print_all_configs(Product, Filters) :-
findall(Config,
( product_variant(Product, Config),
forall(
member(Param-Value, Filters),
config_get(Config, Param, Value)
)
),
Configs),
length(Configs, N),
format("~nFound ~w configuration(s):~n", [N]),
forall(
member(Config, Configs),
( format("~`-t~40|~n", []),
print_config(Config)
)
).
% list_configs_by_price/1
% CLI: list_configs_by_price(diesel_generator).
list_configs_by_price(Product) :-
all_configs_by_price(Product, Sorted),
forall(
member(Price-Config, Sorted),
format(" ~w DKK → ~w~n", [Price, Config])
).
% print_config_summary/2
% CLI: product_variant(diesel_generator, Config),
% config_get(Config, apparent_power, kva_400), !,
% print_config_summary(diesel_generator, Config).
print_config_summary(Product, Config) :-
config_price(Product, Config, Price),
config_bom(Product, Config, BoMNodes),
format("~n--- Configuration Summary ---~n", []),
format(" Config : ~w~n", [Config]),
format(" Price : ~w DKK~n", [Price]),
format(" BoM entries:~n", []),
forall(
member(bom_node(Name, Article, Qty, _), BoMNodes),
format(" ~w x~w ~w~n", [Name, Qty, Article])
).
/*-----------------------------------------------------------------------------
3.1 BoM inspection
-----------------------------------------------------------------------------*/
%-------------------------------------------------------------
% list_component_types/0
% List all unique component types defined in the ontology.
% CLI: list_component_types.
%-------------------------------------------------------------
list_component_types :-
findall(Type,
( product(Product),
component(Product, _, D),
Type = D.type
),
Types),
sort(Types, Unique),
format("~nComponent types:~n", []),
forall(member(T, Unique), format(" ~w~n", [T])).
%-------------------------------------------------------------
% list_components_of_type/1
% List all components of a given type across all products.
% CLI: list_components_of_type(static_equipment).
% list_components_of_type(minor).
%-------------------------------------------------------------
list_components_of_type(Type) :-
format("~nComponents of type '~w':~n", [Type]),
findall(Name-Article,
( product(Product),
component(Product, Name, D),
D.type = Type,
Article = D.article
),
Pairs),
sort(Pairs, Unique),
( Unique = []
-> format(" (none found)~n", [])
; forall(member(Name-Article, Unique),
format(" ~w (~w)~n", [Name, Article]))
).