rdfref-user.sty 3.53 KB
Newer Older
Andreas Zwinkau's avatar
initial  
Andreas Zwinkau committed
1
\ProvidesPackage{rdfref-user}
Andreas Zwinkau's avatar
Andreas Zwinkau committed
2
\RequirePackage{rdfref-core,nameref}
Andreas Zwinkau's avatar
initial  
Andreas Zwinkau committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
\newwrite\exportfile
\def\RootObject{_:doc}

% blank nodes support
% useful for adding unnamed objects
\newcount\rdf@blank@cnt
% shouldn't this be document dependent?
\def\rdf@blk{blank}
\def\BlankNode{%
	\global\advance\rdf@blank@cnt by 1\relax%
	\expandafter\def\expandafter\CurrentObject\expandafter{_:\rdf@blk\the\rdf@blank@cnt}%
}

\def\CurrentObject{\RootObject}

% shouldn't this be in rdfref-core?
% Save property definitions, they are saved with \rdflabel
\newcommand\AddRdfType[2]{%
\def\do##1{\csgdef{rdf:type@##1}{\noexpand\unexpanded{#2}}}
%\csgdef{rdf:type@#1}{\unexpanded{#2}}
\docsvlist{#1}
}
\newcommand\AddProperty[2]{%
\AddTriple{\CurrentObject}{#1}{#2}%
}

% add expanded property
% usefull for adding page numbers etc
\newcommand\AddPropertyEx[2]{%
\AddTripleEx{\CurrentObject}{#1}{#2}%
}

\newcommand\WithObject[2]{%
\begingroup%
\def\CurrentObject{#1}%
\def\run{#2}
\run%
\endgroup%
}

\newcommand\UseRdfType[1]{%
  \ifcsdef{rdf:type@\GuessRdfType{#1}}%
  {\csuse{rdf:type@\GuessRdfType{#1}}}%
  {\typeout{Package rdfref error: unknown type of reference #1 :\GuessRdfType{#1}:}}%
}
% we save these commands in order to enable letting \label etc. to \rdf.. 
% equivalents
% this also means, that rdfref should be loaded after all packages that modify
% ref mechanism (like all others such packages)
% if something goes wrong, redefine these macros to new definitions again
\let\rdfref@oldlabel\label
\let\rdfref@oldref\ref
\let\rdfref@oldpageref\pageref

% replacement for \label
\newcommand\rdflabel[1]{%
  \rdfref@oldlabel{#1}
  \AddTriple{#1}{doc:hasParent}{\CurrentObject}%
  \AddTriple{#1}{doc:envir}{\@currenvir}%
  \def\CurrentObject{#1}%
  \UseRdfType{#1}
}

% replacement for \ref
\newcommand\rdfref@core[2]{%
	\bgroup%
% save \CurrentObject
	\edef\ParentObject{\CurrentObject}%
	\BlankNode% \CurrentObject is blank node
	\AddPropertyEx{doc:hasParent}{\ParentObject}%
	\AddPropertyEx{doc:pageNo}{\thepage}%
	\AddPropertyEx{doc:refersTo}{#1}%
	\AddTripleEx{#1}{doc:referedBy}{\CurrentObject}%
	\AddPropertyEx{rdfs:label}{Blank node: \rdf@blk\the\rdf@blank@cnt}%
  #2%
	\egroup%
}
	
\newcommand\rdfref[1]{%
	\rdfref@core{#1}{%
    \AddPropertyEx{rdf:type}{doc:reference}%
  }%
	\rdfref@oldref{#1}%
}

\newcommand\rdfpageref[1]{%
	\rdfref@core{#1}{%
	  \AddPropertyEx{rdf:type}{doc:pagereference}%
  }%
	\rdfref@oldpageref{#1}%
}


\newcommand\GuessRdfType[1]{%
% We first try if current environment is defined as type, if not, then guess type from the parameter
\ifcsdef{rdf:type@\@currenvir}{\@currenvir}{%
\ifx\RdfPrefix{#1}\Nil\Nil\else%
\RdfPrefix{#1}%
\fi%
}%
}

% import ontology
\newcount\undercat
\newcommand\inputontology[1]{%
\makeatletter
\undercat=\the\catcode`\_
\catcode`\_=12
\input{#1}
\catcode`\_=\the\undercat
\makeatother
}

% This is horrible
\newcommand\ExportGraphviz[2]{%
digraph G\{
\forcsvlist{\listadd\typelist}{#1}
%\forcsvlist{\listadd\propertylist}{#2}
\def\worker##1##2{%
%"\ObjectLabel" -> \GetProperty{\First##1}
\edef\ppp{\GetProperty{##1}{##2}}
\typeout{Worker: ##1: ##1 : \ppp}
\IfSubStr{\ppp}{???}{}{%
"\ObjectLabel" -> %
\IfSubStr{\ppp}{:}{"\GetProperty{\GetProperty{\ppp}{rdf:type}}{rdfs:label}: \GetProperty{\ppp}{rdfs:label}"}{"\ppp"} %
%\GetProperty{##1}{##2} 
 [label="##2"];\\%
}
}
\MapPropertyList{rdf:type}{%
\xifinlist{\Second##1}{\typelist}{%
\edef\ObjectLabel{\GetProperty{\Second##1}{rdfs:label}: \GetProperty{\First##1}{rdfs:label}}%
%\ObjectLabel \& \Second##1\\}
\bgroup%
%\MapObjectList{\First##1}{}
\forcsvlist{\worker{\First##1}}{#2}%
\egroup
}%
{}%
}
\}
}
\endinput