Skip to content

Commit fb4a6a5

Browse files
ajouter ContextManager dédié au contexte d'exécution
1 parent 34faba4 commit fb4a6a5

11 files changed

Lines changed: 240 additions & 123 deletions

File tree

src/main/java/fr/inria/corese/core/load/LoadException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public LoadException(Exception ee) {
1313
this.set(ee);
1414
}
1515

16+
public LoadException(String message) {
17+
super(message);
18+
}
19+
1620
public LoadException(String message, Throwable cause) {
1721
super(message, cause);
1822
}

src/main/java/fr/inria/corese/core/print/HTMLFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ String process(Mappings map, Graph g, String trans){
115115
Transformer t = Transformer.create(g, trans);
116116
context.setTransform(trans);
117117
complete(context, graph);
118-
t.setContext(context);
118+
t.getContextManager().setContext(context);
119119

120120
// if (map != null && map.getQuery() != null){
121121
// // Transformer inherit Query Transformer Visitor if any

src/main/java/fr/inria/corese/core/print/ResultFormat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ public String toString() {
376376
String transformer() {
377377
Transformer t = Transformer.create(theGraph(), getMappings(), getTransformation());
378378
if (getContext() != null) {
379-
t.setContext(getContext());
379+
t.getContextManager().setContext(getContext());
380380
}
381381
if (getBind() != null) {
382-
t.setBinding(getBind());
382+
t.getContextManager().setBinding(getBind());
383383
}
384384
return t.toString();
385385
}

src/main/java/fr/inria/corese/core/query/PluginTransform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Context getQueryContext(Binding b, Environment env, Producer p) {
134134
*/
135135
Context getTransformerContext(Binding b, Environment env, Producer p) throws EngineException {
136136
Transformer t = getTransformerCurrent(b, env, p);
137-
return t.getContext();
137+
return t.getContextManager().getContext();
138138
}
139139

140140
/**
@@ -214,7 +214,7 @@ boolean isWith(Expr exp) {
214214
void complete(Query q, Transformer t, IDatatype uri) {
215215
t.complete(q, (Transformer) q.getTransformer());
216216
if (uri != null) {
217-
t.getContext().set(TransformerUtils.STL_TRANSFORM, uri);
217+
t.getContextManager().getContext().set(TransformerUtils.STL_TRANSFORM, uri);
218218
}
219219
}
220220

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package fr.inria.corese.core.transform;
2+
3+
import fr.inria.corese.core.kgram.api.query.Environment;
4+
import fr.inria.corese.core.kgram.core.Mapping;
5+
import fr.inria.corese.core.kgram.core.Mappings;
6+
import fr.inria.corese.core.kgram.core.Query;
7+
import fr.inria.corese.core.sparql.api.IDatatype;
8+
import fr.inria.corese.core.sparql.triple.function.term.Binding;
9+
import fr.inria.corese.core.sparql.triple.parser.Context;
10+
import fr.inria.corese.core.sparql.triple.parser.Dataset;
11+
import fr.inria.corese.core.sparql.triple.parser.NSManager;
12+
13+
public class ContextManager {
14+
private Context context;
15+
private Binding binding;
16+
private Mappings mappings;
17+
private Dataset dataset;
18+
private final TransformerMapping transformerMapping;
19+
private final NSManager namespaceManager;
20+
21+
22+
public ContextManager(TransformerMapping mapping, NSManager nsm) {
23+
this.transformerMapping = mapping;
24+
this.context = new Context();
25+
this.namespaceManager = nsm;
26+
}
27+
28+
/**
29+
* Create mapping for template execution
30+
*/
31+
public Mapping createMapping(Query template, IDatatype[] args, IDatatype focus) {
32+
return transformerMapping.getMapping(template, args, focus);
33+
}
34+
35+
/**
36+
* Share context between mappings and environment
37+
*/
38+
public Mapping shareContext(Mapping mapping, Environment env) {
39+
if (env != null && env.getBind() != null) {
40+
mapping.setBind(env.getBind());
41+
}
42+
43+
if (mappings != null) {
44+
if (mapping.getBind() == null) {
45+
mapping.setBind(Binding.create());
46+
}
47+
mapping.getBind().setMappings(mappings);
48+
}
49+
50+
return mapping;
51+
}
52+
53+
/**
54+
* Set context value
55+
*/
56+
public void setContextValue(String key, IDatatype value) {
57+
context.set(key, value);
58+
}
59+
60+
/**
61+
* Get context value
62+
*/
63+
public IDatatype getContextValue(String key) {
64+
return context.get(key);
65+
}
66+
67+
/**
68+
* Check if context has value
69+
*/
70+
public boolean hasContextValue(String key) {
71+
return context.hasValue(key);
72+
}
73+
74+
/**
75+
* Complete context with another context
76+
*/
77+
public void completeContext(Context otherContext) {
78+
if (otherContext != null) {
79+
context.complete(otherContext);
80+
}
81+
}
82+
83+
// Getters and setters
84+
public Context getContext() {
85+
return context;
86+
}
87+
88+
public void setContext(Context context) {
89+
this.context = context;
90+
initContext();
91+
}
92+
93+
/**
94+
* Define prefix from Context slot st:prefix = ((ns uri))
95+
*/
96+
void initContext() {
97+
if (getContext() != null) {
98+
if (getContext().hasValue(TransformerUtils.STL_PREFIX)) {
99+
definePrefix();
100+
}
101+
}
102+
}
103+
104+
void definePrefix() {
105+
for (IDatatype def : context.get(TransformerUtils.STL_PREFIX).getValueList()) {
106+
if (def.isList() && def.size() >= 2) {
107+
getNSM().definePrefix(def.get(0).getLabel(), def.get(1).getLabel());
108+
}
109+
}
110+
}
111+
112+
public Binding getBinding() {
113+
return binding;
114+
}
115+
116+
public void setBinding(Binding binding) {
117+
this.binding = binding;
118+
}
119+
120+
public Mappings getMappings() {
121+
return mappings;
122+
}
123+
124+
public void setMappings(Mappings mappings) {
125+
this.mappings = mappings;
126+
}
127+
128+
public Dataset getDataset() {
129+
return dataset;
130+
}
131+
132+
public void setDataset(Dataset dataset) {
133+
this.dataset = dataset;
134+
}
135+
136+
public NSManager getNSM() {
137+
return namespaceManager;
138+
}
139+
140+
}

src/main/java/fr/inria/corese/core/transform/Loader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void load(Load ld, QueryEngine qe, String pp) throws LoadException {
152152

153153
// remove #
154154
String clean(String uri){
155-
return Transformer.getURI(uri);
155+
return TransformerUtils.getURI(uri);
156156
}
157157

158158

0 commit comments

Comments
 (0)