1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package io.github.prolobjectlink.prolog.jlog;
23
24 import static io.github.prolobjectlink.prolog.PrologTermType.STRUCTURE_TYPE;
25
26 import io.github.prolobjectlink.prolog.PrologProvider;
27 import io.github.prolobjectlink.prolog.PrologStructure;
28 import io.github.prolobjectlink.prolog.PrologTerm;
29 import ubc.cs.JLog.Terms.jCompoundTerm;
30 import ubc.cs.JLog.Terms.jPredicate;
31 import ubc.cs.JLog.Terms.jTerm;
32
33
34
35
36
37
38 class JLogStructure extends JLogTerm implements PrologStructure {
39
40 protected JLogStructure(int type, PrologProvider provider, jTerm value) {
41 super(type, provider, value);
42 }
43
44 protected JLogStructure(PrologProvider provider, String functor, jTerm term) {
45 super(STRUCTURE_TYPE, provider);
46 jCompoundTerm compound = new jCompoundTerm(1);
47 compound.addTerm(term);
48 value = new jPredicate(functor, compound);
49 }
50
51 protected JLogStructure(PrologProvider provider, String functor, PrologTerm... arguments) {
52 super(STRUCTURE_TYPE, provider);
53 value = new jPredicate(functor, adaptCompound(arguments));
54 }
55
56 protected JLogStructure(PrologProvider provider, String functor, jCompoundTerm arguments) {
57 super(STRUCTURE_TYPE, provider);
58 value = new jPredicate(functor, arguments);
59 }
60
61 protected JLogStructure(PrologProvider provider, PrologTerm left, String operator, PrologTerm right) {
62 super(STRUCTURE_TYPE, provider);
63 PrologTerm[] operands = { left, right };
64 value = new jPredicate(operator, adaptCompound(operands));
65 }
66
67 protected JLogStructure(PrologProvider provider, jTerm left, String functor, jTerm right) {
68 super(STRUCTURE_TYPE, provider);
69 jCompoundTerm compound = new jCompoundTerm(2);
70 compound.addTerm(left);
71 compound.addTerm(right);
72 value = new jPredicate(functor, compound);
73 }
74
75 public PrologTerm getArgument(int index) {
76 checkIndex(index, getArity());
77 jPredicate structure = (jPredicate) value;
78 jCompoundTerm compound = structure.getArguments();
79 return toTerm(compound.elementAt(index), PrologTerm.class);
80 }
81
82 public PrologTerm[] getArguments() {
83 jPredicate structure = (jPredicate) value;
84 int arity = structure.getArity();
85 PrologTerm[] arguments = new PrologTerm[arity];
86 jCompoundTerm compound = structure.getArguments();
87 for (int i = 0; i < arity; i++) {
88 arguments[i] = toTerm(compound.elementAt(i), PrologTerm.class);
89 }
90 return arguments;
91 }
92
93 public int getArity() {
94 jPredicate structure = (jPredicate) value;
95 return structure.getArity();
96 }
97
98 public String getFunctor() {
99 jPredicate structure = (jPredicate) value;
100 return structure.getName();
101 }
102
103 public final PrologTerm getRight() {
104 return getArgument(1);
105 }
106
107 public final PrologTerm getLeft() {
108 return getArgument(0);
109 }
110
111 public final String getOperator() {
112 return getFunctor();
113 }
114
115 }