0
this is xml converted in prolog file which i wnated to be get access for each child using parent group.
here is file :-

:- style_check(-singleton).
better('SWI-Prolog', AnyOtherProlog).

group('Running Conditions',
     item('No Cylinders are Cut Out
or Chief Limited',
       outvar('ECUA', '02012008'),
       operator(eq),
       constant('false')
     ),
     item('No FWE Request',
       outvar('ECUA', '010110'),
       operator(eq),
       constant('false')
     ),     

    item('No Slowdown',
      outvar('ECUA', '010124'),
      operator(eq),
      constant('false')
    ),  
    item('Cylinder Monitoring Ok',
      outvar('SPSU', '0240'),
      operator(eq),
      constant('true')
    ),  
    item('Second Fuel Supply
System Ready',
      outvar('SPCU', '600202'),
      operator(eq),
      constant('true')
       )               
  ).

how to write the predicates for tis tree ??

these are my predicates:-

isgroup(G,X):-  group(G,X).
isgroup(X,Gname):-  group(G,X),
                    item(X,gname).                      
isitem(_,item(_)).
isitem(item(_),outvar(_,_)).
isitem(outvar(_,_),operator(_)).
isitem(operator(_),constant(_)).
CapelliC
  • 59,646
  • 5
  • 47
  • 90
krishn
  • 11
  • 2

1 Answers1

2

IMO, you're a bit off-track.

What I mean, SWI-Prolog has first class support for SGML (from which XML is based), with powerful builtins to read/write/analyze compliant resources.

So, you're better to revert to proper XML representation, based on element/3. This can be done (approximately) with this snippet

term_xml(Struct, element(Tag, [], Elems)) :-
    compound(Struct),
    Struct =.. [Tag|Args],
    maplist(term_xml, Args, Elems).
term_xml(Struct, element(Tag, [], Elems)) :-
    var(Struct),
    maplist(term_xml, Args, Elems),
    Struct =.. [Tag|Args].
term_xml(Term, Term).

for instance ($X it's SWI-Prolog way to recall previously answered X)

?- term_xml(a(b,c(d,e,f(g,h,i),j)),X).
X = element(a, [], [b, element(c, [], [d, e, element(f, [], [g|...]), j])])

?- term_xml(T,$X).
T = a(b, c(d, e, f(g, h, i), j)) 

note that attributes (second arg of element/3) are not handled, since are lost from your previous conversion.

Once you have an XML representation:

?- [library(xpath)].

?- xpath($X,//f,C).
C = element(f, [], [g, h, i]) ;
false.

edit

Anyway, some hints as required to handle your current data structure: since group has variable arity (depending on number of items), you cannot 'fetch' it directly from Prolog DB. clause/3 will help. Let's say we want to count how many item/4 we have in all groups:

count_items(C) :- aggregate_all(sum(N), (
    current_predicate(group/Arity),
    length(Args, Arity),
    Group =.. [group|Args],
    clause(Group, true),
    aggregate_all(count, member(item(_,_,_,_), Args), N)
), C).

As you can see, in Prolog variable arity terms as data structures are a questionable choice...

CapelliC
  • 59,646
  • 5
  • 47
  • 90