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