4 |
import org.fenfire.*; |
import org.fenfire.*; |
5 |
import org.fenfire.swamp.*; |
import org.fenfire.swamp.*; |
6 |
import org.fenfire.vocab.*; |
import org.fenfire.vocab.*; |
7 |
|
|
8 |
|
import org.nongnu.alph.*; |
9 |
|
|
10 |
import java.util.Iterator; |
import java.util.Iterator; |
11 |
|
|
12 |
public class RDFUtil { |
public class RDFUtil { |
13 |
public static boolean dbg = false; |
public static boolean dbg = false; |
14 |
static private void pa(String s) { System.out.println("RDFUtil: "+s); } |
static private void p(String s) { System.out.println("RDFUtil: "+s); } |
15 |
|
|
16 |
/** Create a new resource with given type. |
/** Create a new resource with given type. |
17 |
*/ |
*/ |
19 |
Object obj = Nodes.N(); |
Object obj = Nodes.N(); |
20 |
fen.graph.add(obj, RDF.type, type); |
fen.graph.add(obj, RDF.type, type); |
21 |
|
|
22 |
if(dbg) pa("N: "+fen+" "+fen.graph+" "+fen.constgraph+ |
if(dbg) p("N: "+fen+" "+fen.graph+" "+fen.constgraph+ |
23 |
" "+obj+" "+type); |
" "+obj+" "+type); |
24 |
|
|
25 |
if (!isNodeType(fen, obj, type)) throw new Error("Impossible!"); |
if (!isNodeType(fen, obj, type)) throw new Error("Impossible!"); |
31 |
/** Check if node is the type which is asked. |
/** Check if node is the type which is asked. |
32 |
*/ |
*/ |
33 |
static public boolean isNodeType(Fen fen, Object node, Object type) { |
static public boolean isNodeType(Fen fen, Object node, Object type) { |
34 |
if(dbg) pa("Isnodetype: "+node+" "+type); |
if(dbg) p("Isnodetype: "+node+" "+type); |
35 |
Iterator it = fen.constgraph.findN_11X_Iter(node, RDF.type); |
Iterator it = fen.constgraph.findN_11X_Iter(node, RDF.type); |
36 |
while(it.hasNext()) { |
while(it.hasNext()) { |
37 |
Object obj = it.next(); |
Object obj = it.next(); |
38 |
if(dbg) pa(" entry: "+ obj); |
if(dbg) p(" entry: "+ obj); |
39 |
if ( obj.equals(type) ) return true; |
if ( obj.equals(type) ) return true; |
40 |
} |
} |
41 |
if(dbg) pa("None matched"); |
if(dbg) p("None matched"); |
42 |
return false; |
return false; |
43 |
} |
} |
44 |
|
|
49 |
return Integer.parseInt(lit.getTextString() ); |
return Integer.parseInt(lit.getTextString() ); |
50 |
} |
} |
51 |
|
|
52 |
|
|
53 |
|
/** Split node to two nodes. Enfilade is splitted in ind, |
54 |
|
* i.e. (node, [foobar]) will be with ind 2 be splitted to |
55 |
|
* (node, [foo]) and (split, [bar]). All properties will |
56 |
|
* be copied to split. |
57 |
|
* |
58 |
|
* @return the new node |
59 |
|
*/ |
60 |
|
static public Object splitNode(Fen fen, Object node, int ind) { |
61 |
|
Object split = Nodes.N(); |
62 |
|
|
63 |
|
Enfilade1D enf = (Enfilade1D)fen.txtfunc.f(fen.constgraph, node); |
64 |
|
if (ind < 0 || ind >= enf.length()) |
65 |
|
throw new Error("Ind is too short or long! "+ind); |
66 |
|
|
67 |
|
Enfilade1D toEnf = enf.sub(ind, enf.length()); |
68 |
|
Enfilade1D fromEnf = enf.sub(0, ind); |
69 |
|
fen.txt.set(node, fromEnf); |
70 |
|
fen.txt.set(split, toEnf); |
71 |
|
|
72 |
|
Iterator iter = fen.graph.findN_1XA_Iter(node); |
73 |
|
while (iter.hasNext()) { |
74 |
|
Object predicate = iter.next(); |
75 |
|
if (predicate != FF.content) { |
76 |
|
Iterator it = fen.graph.findN_11X_Iter(node, predicate); |
77 |
|
while (it.hasNext()) { |
78 |
|
Object object = it.next(); |
79 |
|
if (dbg) p("Pred: " + predicate); |
80 |
|
if (dbg) p(" Obj: "+object); |
81 |
|
fen.graph.add(split, predicate, object); |
82 |
|
} |
83 |
|
} |
84 |
|
} |
85 |
|
return split; |
86 |
|
} |
87 |
} |
} |
88 |
|
|