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.jpl;
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 jpl.Compound;
30 import jpl.Term;
31
32
33
34
35
36
37 class JplStructure extends JplTerm implements PrologStructure {
38
39 JplStructure(PrologProvider provider, String functor, PrologTerm... arguments) {
40 super(STRUCTURE_TYPE, provider);
41 Term[] terms = new Term[arguments.length];
42 for (int i = 0; i < arguments.length; i++) {
43 terms[i] = ((JplTerm) arguments[i]).value;
44 }
45 value = new Compound(removeQuoted(functor), terms);
46 }
47
48 JplStructure(PrologProvider provider, String functor, Term... arguments) {
49 super(STRUCTURE_TYPE, provider);
50 value = new Compound(removeQuoted(functor), arguments);
51 }
52
53 JplStructure(PrologProvider provider, PrologTerm left, String operator, PrologTerm right) {
54 super(STRUCTURE_TYPE, provider);
55 Term leftOperand = ((JplTerm) left).value;
56 Term rightOperand = ((JplTerm) right).value;
57 value = new Compound(operator, new Term[] { leftOperand, rightOperand });
58 }
59
60 JplStructure(PrologProvider provider, Term left, String functor, Term right) {
61 super(STRUCTURE_TYPE, provider, new Compound(functor, new Term[] { left, right }));
62 }
63
64 JplStructure(int objectType, PrologProvider provider, Term newJRef) {
65 super(objectType, provider, newJRef);
66 }
67
68 public PrologTerm getArgument(int index) {
69 checkIndex(index, getArity());
70 return getArguments()[index];
71 }
72
73 public PrologTerm[] getArguments() {
74 Compound structure = (Compound) value;
75 int arity = structure.arity();
76 PrologTerm[] arguments = new PrologTerm[arity];
77 for (int i = 0; i < arity; i++) {
78 arguments[i] = toTerm(structure.arg(i + 1), PrologTerm.class);
79 }
80 return arguments;
81 }
82
83 public int getArity() {
84 Compound structure = (Compound) value;
85 return structure.arity();
86 }
87
88 public String getFunctor() {
89 Compound structure = (Compound) value;
90 return structure.name();
91 }
92
93 public final PrologTerm getRight() {
94 return getArgument(1);
95 }
96
97 public final PrologTerm getLeft() {
98 return getArgument(0);
99 }
100
101 public final String getOperator() {
102 return getFunctor();
103 }
104
105 }